1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "ash/wallpaper/wallpaper_controller_impl.h"
6 
7 #include <cmath>
8 #include <cstdlib>
9 
10 #include "ash/public/cpp/ash_pref_names.h"
11 #include "ash/public/cpp/ash_switches.h"
12 #include "ash/public/cpp/shell_window_ids.h"
13 #include "ash/public/cpp/test/shell_test_api.h"
14 #include "ash/public/cpp/wallpaper_controller_client.h"
15 #include "ash/public/cpp/wallpaper_controller_observer.h"
16 #include "ash/root_window_controller.h"
17 #include "ash/session/session_controller_impl.h"
18 #include "ash/session/test_session_controller_client.h"
19 #include "ash/shell.h"
20 #include "ash/test/ash_test_base.h"
21 #include "ash/wallpaper/wallpaper_utils/wallpaper_resizer.h"
22 #include "ash/wallpaper/wallpaper_view.h"
23 #include "ash/wallpaper/wallpaper_widget_controller.h"
24 #include "ash/wm/overview/overview_controller.h"
25 #include "ash/wm/window_cycle_controller.h"
26 #include "ash/wm/window_state.h"
27 #include "base/command_line.h"
28 #include "base/files/file_util.h"
29 #include "base/files/scoped_temp_dir.h"
30 #include "base/run_loop.h"
31 #include "base/strings/stringprintf.h"
32 #include "base/task/current_thread.h"
33 #include "base/task/post_task.h"
34 #include "base/task/task_observer.h"
35 #include "base/task/thread_pool/thread_pool_instance.h"
36 #include "base/test/bind.h"
37 #include "base/time/time_override.h"
38 #include "chromeos/constants/chromeos_switches.h"
39 #include "components/prefs/scoped_user_pref_update.h"
40 #include "components/user_manager/fake_user_manager.h"
41 #include "components/user_manager/scoped_user_manager.h"
42 #include "components/user_manager/user_names.h"
43 #include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
44 #include "third_party/skia/include/core/SkBitmap.h"
45 #include "third_party/skia/include/core/SkColor.h"
46 #include "ui/aura/window.h"
47 #include "ui/compositor/layer_tree_owner.h"
48 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
49 #include "ui/compositor/test/layer_animator_test_controller.h"
50 #include "ui/display/display.h"
51 #include "ui/display/screen.h"
52 #include "ui/gfx/canvas.h"
53 #include "ui/gfx/codec/jpeg_codec.h"
54 #include "ui/views/widget/widget.h"
55 
56 using session_manager::SessionState;
57 
58 namespace ash {
59 namespace {
60 
61 // Containers IDs used for tests.
62 constexpr int kWallpaperId = kShellWindowId_WallpaperContainer;
63 constexpr int kLockScreenWallpaperId =
64     kShellWindowId_LockScreenWallpaperContainer;
65 constexpr int kAlwaysOnTopWallpaperId =
66     kShellWindowId_AlwaysOnTopWallpaperContainer;
67 
68 constexpr char kDefaultSmallWallpaperName[] = "small.jpg";
69 constexpr char kDefaultLargeWallpaperName[] = "large.jpg";
70 constexpr char kGuestSmallWallpaperName[] = "guest_small.jpg";
71 constexpr char kGuestLargeWallpaperName[] = "guest_large.jpg";
72 constexpr char kChildSmallWallpaperName[] = "child_small.jpg";
73 constexpr char kChildLargeWallpaperName[] = "child_large.jpg";
74 
75 // Colors used to distinguish between wallpapers with large and small
76 // resolution.
77 constexpr SkColor kLargeCustomWallpaperColor = SK_ColorDKGRAY;
78 constexpr SkColor kSmallCustomWallpaperColor = SK_ColorLTGRAY;
79 
80 // A color that can be passed to |CreateImage|. Specifically chosen to not
81 // conflict with any of the custom wallpaper colors.
82 constexpr SkColor kWallpaperColor = SK_ColorMAGENTA;
83 
GetDummyFileId(const AccountId & account_id)84 std::string GetDummyFileId(const AccountId& account_id) {
85   return account_id.GetUserEmail() + "-hash";
86 }
87 
GetDummyFileName(const AccountId & account_id)88 std::string GetDummyFileName(const AccountId& account_id) {
89   return account_id.GetUserEmail() + "-file";
90 }
91 
92 constexpr char kUser1[] = "user1@test.com";
93 const AccountId account_id_1 = AccountId::FromUserEmail(kUser1);
94 const std::string wallpaper_files_id_1 = GetDummyFileId(account_id_1);
95 const std::string file_name_1 = GetDummyFileName(account_id_1);
96 
97 constexpr char kUser2[] = "user2@test.com";
98 const AccountId account_id_2 = AccountId::FromUserEmail(kUser2);
99 const std::string wallpaper_files_id_2 = GetDummyFileId(account_id_2);
100 const std::string file_name_2 = GetDummyFileName(account_id_2);
101 
102 const std::string kDummyUrl = "https://best_wallpaper/1";
103 const std::string kDummyUrl2 = "https://best_wallpaper/2";
104 
105 // Creates an image of size |size|.
CreateImage(int width,int height,SkColor color)106 gfx::ImageSkia CreateImage(int width, int height, SkColor color) {
107   SkBitmap bitmap;
108   bitmap.allocN32Pixels(width, height);
109   bitmap.eraseColor(color);
110   gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
111   return image;
112 }
113 
114 // Returns number of child windows in a shell window container.
ChildCountForContainer(int container_id)115 int ChildCountForContainer(int container_id) {
116   aura::Window* root = Shell::Get()->GetPrimaryRootWindow();
117   aura::Window* container = root->GetChildById(container_id);
118   return static_cast<int>(container->children().size());
119 }
120 
121 // Steps a layer animation until it is completed. Animations must be enabled.
RunAnimationForLayer(ui::Layer * layer)122 void RunAnimationForLayer(ui::Layer* layer) {
123   // Animations must be enabled for stepping to work.
124   ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_multiplier(),
125             ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
126 
127   ui::LayerAnimatorTestController controller(layer->GetAnimator());
128   // Multiple steps are required to complete complex animations.
129   // TODO(vollick): This should not be necessary. crbug.com/154017
130   while (controller.animator()->is_animating()) {
131     controller.StartThreadedAnimationsIfNeeded();
132     base::TimeTicks step_time = controller.animator()->last_step_time();
133     layer->GetAnimator()->Step(step_time +
134                                base::TimeDelta::FromMilliseconds(1000));
135   }
136 }
137 
138 // Writes a JPEG image of the specified size and color to |path|. Returns true
139 // on success.
WriteJPEGFile(const base::FilePath & path,int width,int height,SkColor color)140 bool WriteJPEGFile(const base::FilePath& path,
141                    int width,
142                    int height,
143                    SkColor color) {
144   base::ScopedAllowBlockingForTesting allow_blocking;
145   SkBitmap bitmap;
146   bitmap.allocN32Pixels(width, height);
147   bitmap.eraseColor(color);
148   std::vector<unsigned char> output;
149   if (!gfx::JPEGCodec::Encode(bitmap, 80 /*quality*/, &output)) {
150     LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
151     return false;
152   }
153 
154   size_t bytes_written = base::WriteFile(
155       path, reinterpret_cast<const char*>(&output[0]), output.size());
156   if (bytes_written != output.size()) {
157     LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of "
158                << output.size() << " to " << path.value();
159     return false;
160   }
161   return true;
162 }
163 
164 // Returns custom wallpaper path. Creates the directory if it doesn't exist.
GetCustomWallpaperPath(const char * sub_dir,const std::string & wallpaper_files_id,const std::string & file_name)165 base::FilePath GetCustomWallpaperPath(const char* sub_dir,
166                                       const std::string& wallpaper_files_id,
167                                       const std::string& file_name) {
168   base::ScopedAllowBlockingForTesting allow_blocking;
169   base::FilePath wallpaper_path =
170       WallpaperControllerImpl::GetCustomWallpaperPath(
171           sub_dir, wallpaper_files_id, file_name);
172   if (!base::DirectoryExists(wallpaper_path.DirName()))
173     base::CreateDirectory(wallpaper_path.DirName());
174 
175   return wallpaper_path;
176 }
177 
WaitUntilCustomWallpapersDeleted(const AccountId & account_id)178 void WaitUntilCustomWallpapersDeleted(const AccountId& account_id) {
179   const std::string wallpaper_file_id = GetDummyFileId(account_id);
180 
181   base::FilePath small_wallpaper_dir =
182       WallpaperControllerImpl::GetCustomWallpaperDir(
183           WallpaperControllerImpl::kSmallWallpaperSubDir)
184           .Append(wallpaper_file_id);
185   base::FilePath large_wallpaper_dir =
186       WallpaperControllerImpl::GetCustomWallpaperDir(
187           WallpaperControllerImpl::kLargeWallpaperSubDir)
188           .Append(wallpaper_file_id);
189   base::FilePath original_wallpaper_dir =
190       WallpaperControllerImpl::GetCustomWallpaperDir(
191           WallpaperControllerImpl::kOriginalWallpaperSubDir)
192           .Append(wallpaper_file_id);
193 
194   while (base::PathExists(small_wallpaper_dir) ||
195          base::PathExists(large_wallpaper_dir) ||
196          base::PathExists(original_wallpaper_dir)) {
197   }
198 }
199 
200 // Monitors if any task is processed by the message loop.
201 class TaskObserver : public base::TaskObserver {
202  public:
TaskObserver()203   TaskObserver() : processed_(false) {}
204   ~TaskObserver() override = default;
205 
206   // TaskObserver:
WillProcessTask(const base::PendingTask &,bool)207   void WillProcessTask(const base::PendingTask& /* pending_task */,
208                        bool /* was_blocked_or_low_priority */) override {}
DidProcessTask(const base::PendingTask & pending_task)209   void DidProcessTask(const base::PendingTask& pending_task) override {
210     processed_ = true;
211   }
212 
213   // Returns true if any task was processed.
processed() const214   bool processed() const { return processed_; }
215 
216  private:
217   bool processed_;
218   DISALLOW_COPY_AND_ASSIGN(TaskObserver);
219 };
220 
221 // See content::RunAllTasksUntilIdle().
RunAllTasksUntilIdle()222 void RunAllTasksUntilIdle() {
223   while (true) {
224     TaskObserver task_observer;
225     base::CurrentThread::Get()->AddTaskObserver(&task_observer);
226     // May spin message loop.
227     base::ThreadPoolInstance::Get()->FlushForTesting();
228 
229     base::RunLoop().RunUntilIdle();
230     base::CurrentThread::Get()->RemoveTaskObserver(&task_observer);
231 
232     if (!task_observer.processed())
233       break;
234   }
235 }
236 
237 // A test wallpaper controller client class.
238 class TestWallpaperControllerClient : public WallpaperControllerClient {
239  public:
240   TestWallpaperControllerClient() = default;
241   virtual ~TestWallpaperControllerClient() = default;
242 
open_count() const243   size_t open_count() const { return open_count_; }
close_preview_count() const244   size_t close_preview_count() const { return close_preview_count_; }
245 
246   // WallpaperControllerClient:
OpenWallpaperPicker()247   void OpenWallpaperPicker() override { open_count_++; }
MaybeClosePreviewWallpaper()248   void MaybeClosePreviewWallpaper() override { close_preview_count_++; }
249 
250  private:
251   size_t open_count_ = 0;
252   size_t close_preview_count_ = 0;
253 
254   DISALLOW_COPY_AND_ASSIGN(TestWallpaperControllerClient);
255 };
256 
257 // A test implementation of the WallpaperControllerObserver interface.
258 class TestWallpaperControllerObserver : public WallpaperControllerObserver {
259  public:
TestWallpaperControllerObserver(WallpaperController * controller)260   explicit TestWallpaperControllerObserver(WallpaperController* controller)
261       : controller_(controller) {
262     controller_->AddObserver(this);
263   }
264 
~TestWallpaperControllerObserver()265   ~TestWallpaperControllerObserver() override {
266     controller_->RemoveObserver(this);
267   }
268 
269   // WallpaperControllerObserver
OnWallpaperColorsChanged()270   void OnWallpaperColorsChanged() override { ++colors_changed_count_; }
OnWallpaperBlurChanged()271   void OnWallpaperBlurChanged() override { ++blur_changed_count_; }
OnFirstWallpaperShown()272   void OnFirstWallpaperShown() override { ++first_shown_count_; }
273 
colors_changed_count() const274   int colors_changed_count() const { return colors_changed_count_; }
blur_changed_count() const275   int blur_changed_count() const { return blur_changed_count_; }
first_shown_count() const276   int first_shown_count() const { return first_shown_count_; }
277 
278  private:
279   WallpaperController* controller_;
280   int colors_changed_count_ = 0;
281   int blur_changed_count_ = 0;
282   int first_shown_count_ = 0;
283 
284   DISALLOW_COPY_AND_ASSIGN(TestWallpaperControllerObserver);
285 };
286 
287 }  // namespace
288 
289 class WallpaperControllerTest : public AshTestBase {
290  public:
291   WallpaperControllerTest() = default;
292   ~WallpaperControllerTest() override = default;
293 
SetUp()294   void SetUp() override {
295     AshTestBase::SetUp();
296     auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
297     fake_user_manager_ = fake_user_manager.get();
298     scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
299         std::move(fake_user_manager));
300 
301     // This is almost certainly not what was originally intended for these
302     // tests, but they have never actually exercised properly decoded
303     // wallpapers, as they've never actually been connected to a Data Decoder.
304     // We simulate a "crashing" ImageDcoder to get the behavior the tests were
305     // written around, but at some point they should probably be fixed.
306     in_process_data_decoder_.service().SimulateImageDecoderCrashForTesting(
307         true);
308 
309     controller_ = Shell::Get()->wallpaper_controller();
310     controller_->set_wallpaper_reload_no_delay_for_test();
311 
312     ASSERT_TRUE(user_data_dir_.CreateUniqueTempDir());
313     ASSERT_TRUE(online_wallpaper_dir_.CreateUniqueTempDir());
314     ASSERT_TRUE(custom_wallpaper_dir_.CreateUniqueTempDir());
315     base::FilePath policy_wallpaper;
316     controller_->Init(user_data_dir_.GetPath(), online_wallpaper_dir_.GetPath(),
317                       custom_wallpaper_dir_.GetPath(), policy_wallpaper);
318   }
319 
wallpaper_view()320   WallpaperView* wallpaper_view() {
321     return Shell::Get()
322         ->GetPrimaryRootWindowController()
323         ->wallpaper_widget_controller()
324         ->wallpaper_view();
325   }
326 
327  protected:
328   // Helper function that tests the wallpaper is always fitted to the native
329   // display resolution when the layout is WALLPAPER_LAYOUT_CENTER.
WallpaperFitToNativeResolution(WallpaperView * view,float device_scale_factor,int image_width,int image_height,SkColor color)330   void WallpaperFitToNativeResolution(WallpaperView* view,
331                                       float device_scale_factor,
332                                       int image_width,
333                                       int image_height,
334                                       SkColor color) {
335     gfx::Size size = view->bounds().size();
336     gfx::Canvas canvas(size, device_scale_factor, true);
337     view->OnPaint(&canvas);
338 
339     SkBitmap bitmap = canvas.GetBitmap();
340     int bitmap_width = bitmap.width();
341     int bitmap_height = bitmap.height();
342     for (int i = 0; i < bitmap_width; i++) {
343       for (int j = 0; j < bitmap_height; j++) {
344         if (i >= (bitmap_width - image_width) / 2 &&
345             i < (bitmap_width + image_width) / 2 &&
346             j >= (bitmap_height - image_height) / 2 &&
347             j < (bitmap_height + image_height) / 2) {
348           EXPECT_EQ(color, bitmap.getColor(i, j));
349         } else {
350           EXPECT_EQ(SK_ColorBLACK, bitmap.getColor(i, j));
351         }
352       }
353     }
354   }
355 
356   // Runs AnimatingWallpaperWidgetController's animation to completion.
RunDesktopControllerAnimation()357   void RunDesktopControllerAnimation() {
358     WallpaperWidgetController* controller =
359         Shell::Get()
360             ->GetPrimaryRootWindowController()
361             ->wallpaper_widget_controller();
362     ASSERT_TRUE(controller);
363 
364     ui::LayerTreeOwner* owner = controller->old_layer_tree_owner_for_testing();
365     if (!owner)
366       return;
367 
368     ASSERT_NO_FATAL_FAILURE(RunAnimationForLayer(owner->root()));
369   }
370 
371   // Convenience function to ensure ShouldCalculateColors() returns true.
EnableShelfColoring()372   void EnableShelfColoring() {
373     const gfx::ImageSkia kImage = CreateImage(10, 10, kWallpaperColor);
374     controller_->ShowWallpaperImage(
375         kImage, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
376         /*preview_mode=*/false, /*always_on_top=*/false);
377     SetSessionState(SessionState::ACTIVE);
378 
379     EXPECT_TRUE(ShouldCalculateColors());
380   }
381 
382   // Convenience function to set the SessionState.
SetSessionState(SessionState session_state)383   void SetSessionState(SessionState session_state) {
384     GetSessionControllerClient()->SetSessionState(session_state);
385   }
386 
387   // Helper function to create a |WallpaperInfo| struct with dummy values
388   // given the desired layout.
CreateWallpaperInfo(WallpaperLayout layout)389   WallpaperInfo CreateWallpaperInfo(WallpaperLayout layout) {
390     return WallpaperInfo("", layout, DEFAULT,
391                          base::Time::Now().LocalMidnight());
392   }
393 
394   // Saves images with different resolution to corresponding paths and saves
395   // wallpaper info to local state, so that subsequent calls of |ShowWallpaper|
396   // can retrieve the images and info.
CreateAndSaveWallpapers(const AccountId & account_id)397   void CreateAndSaveWallpapers(const AccountId& account_id) {
398     std::string wallpaper_files_id = GetDummyFileId(account_id);
399     std::string file_name = GetDummyFileName(account_id);
400     base::FilePath small_wallpaper_path =
401         GetCustomWallpaperPath(WallpaperControllerImpl::kSmallWallpaperSubDir,
402                                wallpaper_files_id, file_name);
403     base::FilePath large_wallpaper_path =
404         GetCustomWallpaperPath(WallpaperControllerImpl::kLargeWallpaperSubDir,
405                                wallpaper_files_id, file_name);
406 
407     // Saves the small/large resolution wallpapers to small/large custom
408     // wallpaper paths.
409     ASSERT_TRUE(WriteJPEGFile(small_wallpaper_path, kSmallWallpaperMaxWidth,
410                               kSmallWallpaperMaxHeight,
411                               kSmallCustomWallpaperColor));
412     ASSERT_TRUE(WriteJPEGFile(large_wallpaper_path, kLargeWallpaperMaxWidth,
413                               kLargeWallpaperMaxHeight,
414                               kLargeCustomWallpaperColor));
415 
416     std::string relative_path =
417         base::FilePath(wallpaper_files_id).Append(file_name).value();
418     // Saves wallpaper info to local state for user.
419     WallpaperInfo info = {relative_path, WALLPAPER_LAYOUT_CENTER_CROPPED,
420                           CUSTOMIZED, base::Time::Now().LocalMidnight()};
421     ASSERT_TRUE(controller_->SetUserWallpaperInfo(account_id, info));
422   }
423 
424   // Simulates setting a custom wallpaper by directly setting the wallpaper
425   // info.
SimulateSettingCustomWallpaper(const AccountId & account_id)426   void SimulateSettingCustomWallpaper(const AccountId& account_id) {
427     ASSERT_TRUE(controller_->SetUserWallpaperInfo(
428         account_id,
429         WallpaperInfo("dummy_file_location", WALLPAPER_LAYOUT_CENTER,
430                       CUSTOMIZED, base::Time::Now().LocalMidnight())));
431   }
432 
433   // Initializes default wallpaper paths "*default_*file" and writes JPEG
434   // wallpaper images to them. Only needs to be called (once) by tests that
435   // want to test loading of default wallpapers.
CreateDefaultWallpapers()436   void CreateDefaultWallpapers() {
437     base::ScopedAllowBlockingForTesting allow_blocking;
438     ASSERT_TRUE(default_wallpaper_dir_.CreateUniqueTempDir());
439     const base::FilePath default_wallpaper_path =
440         default_wallpaper_dir_.GetPath();
441 
442     base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
443     const base::FilePath small_file =
444         default_wallpaper_path.Append(kDefaultSmallWallpaperName);
445     command_line->AppendSwitchASCII(chromeos::switches::kDefaultWallpaperSmall,
446                                     small_file.value());
447     const base::FilePath large_file =
448         default_wallpaper_path.Append(kDefaultLargeWallpaperName);
449     command_line->AppendSwitchASCII(chromeos::switches::kDefaultWallpaperLarge,
450                                     large_file.value());
451 
452     const base::FilePath guest_small_file =
453         default_wallpaper_path.Append(kGuestSmallWallpaperName);
454     command_line->AppendSwitchASCII(chromeos::switches::kGuestWallpaperSmall,
455                                     guest_small_file.value());
456     const base::FilePath guest_large_file =
457         default_wallpaper_path.Append(kGuestLargeWallpaperName);
458     command_line->AppendSwitchASCII(chromeos::switches::kGuestWallpaperLarge,
459                                     guest_large_file.value());
460 
461     const base::FilePath child_small_file =
462         default_wallpaper_path.Append(kChildSmallWallpaperName);
463     command_line->AppendSwitchASCII(chromeos::switches::kChildWallpaperSmall,
464                                     child_small_file.value());
465     const base::FilePath child_large_file =
466         default_wallpaper_path.Append(kChildLargeWallpaperName);
467     command_line->AppendSwitchASCII(chromeos::switches::kChildWallpaperLarge,
468                                     child_large_file.value());
469 
470     const int kWallpaperSize = 2;
471     ASSERT_TRUE(WriteJPEGFile(small_file, kWallpaperSize, kWallpaperSize,
472                               kWallpaperColor));
473     ASSERT_TRUE(WriteJPEGFile(large_file, kWallpaperSize, kWallpaperSize,
474                               kWallpaperColor));
475 
476     ASSERT_TRUE(WriteJPEGFile(guest_small_file, kWallpaperSize, kWallpaperSize,
477                               kWallpaperColor));
478     ASSERT_TRUE(WriteJPEGFile(guest_large_file, kWallpaperSize, kWallpaperSize,
479                               kWallpaperColor));
480 
481     ASSERT_TRUE(WriteJPEGFile(child_small_file, kWallpaperSize, kWallpaperSize,
482                               kWallpaperColor));
483     ASSERT_TRUE(WriteJPEGFile(child_large_file, kWallpaperSize, kWallpaperSize,
484                               kWallpaperColor));
485   }
486 
487   // A helper to test the behavior of setting online wallpaper after the image
488   // is decoded. This is needed because image decoding is not supported in unit
489   // tests.
SetOnlineWallpaperFromImage(const AccountId & account_id,const gfx::ImageSkia & image,const std::string & url,WallpaperLayout layout,bool save_file,bool preview_mode,WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback callback)490   void SetOnlineWallpaperFromImage(
491       const AccountId& account_id,
492       const gfx::ImageSkia& image,
493       const std::string& url,
494       WallpaperLayout layout,
495       bool save_file,
496       bool preview_mode,
497       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback callback) {
498     const WallpaperControllerImpl::OnlineWallpaperParams params = {
499         account_id, url, layout, preview_mode};
500     controller_->OnOnlineWallpaperDecoded(params, save_file,
501                                           std::move(callback), image);
502   }
503 
504   // Returns color of the current wallpaper. Note: this function assumes the
505   // wallpaper has a solid color.
GetWallpaperColor()506   SkColor GetWallpaperColor() {
507     const gfx::ImageSkiaRep& representation =
508         controller_->GetWallpaper().GetRepresentation(1.0f);
509     return representation.GetBitmap().getColor(0, 0);
510   }
511 
512   // Wrapper for private ShouldCalculateColors().
ShouldCalculateColors()513   bool ShouldCalculateColors() { return controller_->ShouldCalculateColors(); }
514 
515   // Wrapper for private IsDevicePolicyWallpaper().
IsDevicePolicyWallpaper()516   bool IsDevicePolicyWallpaper() {
517     return controller_->IsDevicePolicyWallpaper();
518   }
519 
GetWallpaperCount()520   int GetWallpaperCount() { return controller_->wallpaper_count_for_testing_; }
521 
GetDecodeFilePaths()522   const std::vector<base::FilePath>& GetDecodeFilePaths() {
523     return controller_->decode_requests_for_testing_;
524   }
525 
SetBypassDecode()526   void SetBypassDecode() { controller_->set_bypass_decode_for_testing(); }
527 
ClearWallpaperCount()528   void ClearWallpaperCount() { controller_->wallpaper_count_for_testing_ = 0; }
529 
ClearDecodeFilePaths()530   void ClearDecodeFilePaths() {
531     controller_->decode_requests_for_testing_.clear();
532   }
533 
ClearWallpaper()534   void ClearWallpaper() { controller_->current_wallpaper_.reset(); }
535 
GetWallpaperContainerId()536   int GetWallpaperContainerId() {
537     return controller_->GetWallpaperContainerId(controller_->locked_);
538   }
539 
540   WallpaperControllerImpl* controller_ = nullptr;  // Not owned.
541 
542   base::ScopedTempDir user_data_dir_;
543   base::ScopedTempDir online_wallpaper_dir_;
544   base::ScopedTempDir custom_wallpaper_dir_;
545   base::ScopedTempDir default_wallpaper_dir_;
546 
547   user_manager::FakeUserManager* fake_user_manager_ = nullptr;
548   std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;
549 
550  private:
551   data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
552 
553   DISALLOW_COPY_AND_ASSIGN(WallpaperControllerTest);
554 };
555 
TEST_F(WallpaperControllerTest,Client)556 TEST_F(WallpaperControllerTest, Client) {
557   TestWallpaperControllerClient client;
558   controller_->SetClient(&client);
559 
560   base::FilePath empty_path;
561   controller_->Init(empty_path, empty_path, empty_path, empty_path);
562 
563   EXPECT_EQ(0u, client.open_count());
564   EXPECT_TRUE(controller_->CanOpenWallpaperPicker());
565   controller_->OpenWallpaperPickerIfAllowed();
566   EXPECT_EQ(1u, client.open_count());
567 }
568 
TEST_F(WallpaperControllerTest,BasicReparenting)569 TEST_F(WallpaperControllerTest, BasicReparenting) {
570   WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
571   controller->CreateEmptyWallpaperForTesting();
572 
573   // Wallpaper view/window exists in the wallpaper container and nothing is in
574   // the lock screen wallpaper container.
575   EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
576   EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
577 
578   controller->OnSessionStateChanged(session_manager::SessionState::LOCKED);
579 
580   // One window is moved from desktop to lock container.
581   EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
582   EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId));
583 
584   controller->OnSessionStateChanged(session_manager::SessionState::ACTIVE);
585 
586   // One window is moved from lock to desktop container.
587   EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
588   EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
589 }
590 
TEST_F(WallpaperControllerTest,SwitchWallpapersWhenNewWallpaperAnimationEnds)591 TEST_F(WallpaperControllerTest, SwitchWallpapersWhenNewWallpaperAnimationEnds) {
592   // We cannot short-circuit animations for this test.
593   ui::ScopedAnimationDurationScaleMode test_duration_mode(
594       ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
595 
596   // Create the wallpaper and its view.
597   WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
598   controller->CreateEmptyWallpaperForTesting();
599 
600   // The new wallpaper is ready to animate.
601   WallpaperWidgetController* widget_controller =
602       Shell::Get()
603           ->GetPrimaryRootWindowController()
604           ->wallpaper_widget_controller();
605   EXPECT_TRUE(widget_controller->IsAnimating());
606 
607   // Force the animation to play to completion.
608   RunDesktopControllerAnimation();
609   EXPECT_FALSE(widget_controller->IsAnimating());
610 }
611 
612 // Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
613 // move all wallpaper views if there are more than one.
TEST_F(WallpaperControllerTest,WallpaperMovementDuringUnlock)614 TEST_F(WallpaperControllerTest, WallpaperMovementDuringUnlock) {
615   // We cannot short-circuit animations for this test.
616   ui::ScopedAnimationDurationScaleMode test_duration_mode(
617       ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
618 
619   // Reset wallpaper state, see ControllerOwnership above.
620   WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
621   controller->CreateEmptyWallpaperForTesting();
622 
623   // Run wallpaper show animation to completion.
624   RunDesktopControllerAnimation();
625 
626   // User locks the screen, which moves the wallpaper forward.
627   controller->OnSessionStateChanged(session_manager::SessionState::LOCKED);
628 
629   // Suspend/resume cycle causes wallpaper to refresh, loading a new wallpaper
630   // that will animate in on top of the old one.
631   controller->CreateEmptyWallpaperForTesting();
632 
633   // In this state we have a wallpaper views stored in
634   // LockScreenWallpaperContainer.
635   WallpaperWidgetController* widget_controller =
636       Shell::Get()
637           ->GetPrimaryRootWindowController()
638           ->wallpaper_widget_controller();
639   EXPECT_TRUE(widget_controller->IsAnimating());
640   EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
641   EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId));
642   // There must be three layers, shield, original and old layers.
643   ASSERT_EQ(3u, wallpaper_view()->layer()->parent()->children().size());
644 
645   // Before the wallpaper's animation completes, user unlocks the screen, which
646   // moves the wallpaper to the back.
647   controller->OnSessionStateChanged(session_manager::SessionState::ACTIVE);
648 
649   // Ensure that widget has moved.
650   EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
651   // There must be two layers, original and old layers while animating.
652   ASSERT_EQ(2u, wallpaper_view()->layer()->parent()->children().size());
653   EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
654 
655   // Finish the new wallpaper animation.
656   RunDesktopControllerAnimation();
657 
658   // Now there is one wallpaper and layer.
659   EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
660   ASSERT_EQ(1u, wallpaper_view()->layer()->parent()->children().size());
661   EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
662 }
663 
664 // Test for crbug.com/156542. Animating wallpaper should immediately finish
665 // animation and replace current wallpaper before next animation starts.
TEST_F(WallpaperControllerTest,ChangeWallpaperQuick)666 TEST_F(WallpaperControllerTest, ChangeWallpaperQuick) {
667   // We cannot short-circuit animations for this test.
668   ui::ScopedAnimationDurationScaleMode test_duration_mode(
669       ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
670 
671   // Reset wallpaper state, see ControllerOwnership above.
672   WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
673   controller->CreateEmptyWallpaperForTesting();
674 
675   // Run wallpaper show animation to completion.
676   RunDesktopControllerAnimation();
677 
678   // Change to a new wallpaper.
679   controller->CreateEmptyWallpaperForTesting();
680 
681   WallpaperWidgetController* widget_controller =
682       Shell::Get()
683           ->GetPrimaryRootWindowController()
684           ->wallpaper_widget_controller();
685   EXPECT_TRUE(widget_controller->IsAnimating());
686 
687   // Change to another wallpaper before animation finished.
688   controller->CreateEmptyWallpaperForTesting();
689 
690   // Run wallpaper show animation to completion.
691   RunDesktopControllerAnimation();
692 
693   EXPECT_FALSE(widget_controller->IsAnimating());
694 }
695 
TEST_F(WallpaperControllerTest,ResizeCustomWallpaper)696 TEST_F(WallpaperControllerTest, ResizeCustomWallpaper) {
697   UpdateDisplay("320x200");
698 
699   gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
700 
701   // Set the image as custom wallpaper, wait for the resize to finish, and check
702   // that the resized image is the expected size.
703   controller_->ShowWallpaperImage(
704       image, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
705       /*preview_mode=*/false, /*always_on_top=*/false);
706   EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper()));
707   RunAllTasksUntilIdle();
708   gfx::ImageSkia resized_image = controller_->GetWallpaper();
709   EXPECT_FALSE(image.BackedBySameObjectAs(resized_image));
710   EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString());
711 
712   // Load the original wallpaper again and check that we're still using the
713   // previously-resized image instead of doing another resize
714   // (http://crbug.com/321402).
715   controller_->ShowWallpaperImage(
716       image, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
717       /*preview_mode=*/false, /*always_on_top=*/false);
718   RunAllTasksUntilIdle();
719   EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
720 }
721 
TEST_F(WallpaperControllerTest,GetMaxDisplaySize)722 TEST_F(WallpaperControllerTest, GetMaxDisplaySize) {
723   // Device scale factor shouldn't affect the native size.
724   UpdateDisplay("1000x300*2");
725   EXPECT_EQ("1000x300",
726             WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
727 
728   // Rotated display should return the rotated size.
729   UpdateDisplay("1000x300*2/r");
730   EXPECT_EQ("300x1000",
731             WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
732 
733   // UI Scaling shouldn't affect the native size.
734   UpdateDisplay("1000x300*2@1.5");
735   EXPECT_EQ("1000x300",
736             WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
737 
738   // First display has maximum size.
739   UpdateDisplay("400x300,100x100");
740   EXPECT_EQ("400x300",
741             WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
742 
743   // Second display has maximum size.
744   UpdateDisplay("400x300,500x600");
745   EXPECT_EQ("500x600",
746             WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
747 
748   // Maximum width and height belongs to different displays.
749   UpdateDisplay("400x300,100x500");
750   EXPECT_EQ("400x500",
751             WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
752 }
753 
754 // Test that the wallpaper is always fitted to the native display resolution
755 // when the layout is WALLPAPER_LAYOUT_CENTER to prevent blurry images.
TEST_F(WallpaperControllerTest,DontScaleWallpaperWithCenterLayout)756 TEST_F(WallpaperControllerTest, DontScaleWallpaperWithCenterLayout) {
757   // We cannot short-circuit animations for this test.
758   ui::ScopedAnimationDurationScaleMode test_duration_mode(
759       ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
760 
761   const gfx::Size high_resolution(3600, 2400);
762   const gfx::Size low_resolution(360, 240);
763   const float high_dsf = 2.0f;
764   const float low_dsf = 1.0f;
765 
766   gfx::ImageSkia image_high_res = CreateImage(
767       high_resolution.width(), high_resolution.height(), kWallpaperColor);
768   gfx::ImageSkia image_low_res = CreateImage(
769       low_resolution.width(), low_resolution.height(), kWallpaperColor);
770 
771   UpdateDisplay("1200x600*2");
772   {
773     SCOPED_TRACE(base::StringPrintf("1200x600*2 high resolution"));
774     controller_->ShowWallpaperImage(
775         image_high_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
776         /*preview_mode=*/false, /*always_on_top=*/false);
777     WallpaperFitToNativeResolution(wallpaper_view(), high_dsf,
778                                    high_resolution.width(),
779                                    high_resolution.height(), kWallpaperColor);
780   }
781   {
782     SCOPED_TRACE(base::StringPrintf("1200x600*2 low resolution"));
783     controller_->ShowWallpaperImage(
784         image_low_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
785         /*preview_mode=*/false, /*always_on_top=*/false);
786     WallpaperFitToNativeResolution(wallpaper_view(), high_dsf,
787                                    low_resolution.width(),
788                                    low_resolution.height(), kWallpaperColor);
789   }
790 
791   UpdateDisplay("1200x600");
792   {
793     SCOPED_TRACE(base::StringPrintf("1200x600 high resolution"));
794     controller_->ShowWallpaperImage(
795         image_high_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
796         /*preview_mode=*/false, /*always_on_top=*/false);
797     WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
798                                    high_resolution.width(),
799                                    high_resolution.height(), kWallpaperColor);
800   }
801   {
802     SCOPED_TRACE(base::StringPrintf("1200x600 low resolution"));
803     controller_->ShowWallpaperImage(
804         image_low_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
805         /*preview_mode=*/false, /*always_on_top=*/false);
806     WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
807                                    low_resolution.width(),
808                                    low_resolution.height(), kWallpaperColor);
809   }
810 
811   UpdateDisplay("1200x600/u@1.5");  // 1.5 ui scale
812   {
813     SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 high resolution"));
814     controller_->ShowWallpaperImage(
815         image_high_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
816         /*preview_mode=*/false, /*always_on_top=*/false);
817     WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
818                                    high_resolution.width(),
819                                    high_resolution.height(), kWallpaperColor);
820   }
821   {
822     SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 low resolution"));
823     controller_->ShowWallpaperImage(
824         image_low_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
825         /*preview_mode=*/false, /*always_on_top=*/false);
826     WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
827                                    low_resolution.width(),
828                                    low_resolution.height(), kWallpaperColor);
829   }
830 }
831 
TEST_F(WallpaperControllerTest,ShouldCalculateColorsBasedOnImage)832 TEST_F(WallpaperControllerTest, ShouldCalculateColorsBasedOnImage) {
833   EnableShelfColoring();
834   EXPECT_TRUE(ShouldCalculateColors());
835 
836   controller_->CreateEmptyWallpaperForTesting();
837   EXPECT_FALSE(ShouldCalculateColors());
838 }
839 
TEST_F(WallpaperControllerTest,ShouldCalculateColorsBasedOnSessionState)840 TEST_F(WallpaperControllerTest, ShouldCalculateColorsBasedOnSessionState) {
841   EnableShelfColoring();
842 
843   SetSessionState(SessionState::UNKNOWN);
844   EXPECT_FALSE(ShouldCalculateColors());
845 
846   SetSessionState(SessionState::OOBE);
847   EXPECT_FALSE(ShouldCalculateColors());
848 
849   SetSessionState(SessionState::LOGIN_PRIMARY);
850   EXPECT_FALSE(ShouldCalculateColors());
851 
852   SetSessionState(SessionState::LOGGED_IN_NOT_ACTIVE);
853   EXPECT_FALSE(ShouldCalculateColors());
854 
855   SetSessionState(SessionState::ACTIVE);
856   EXPECT_TRUE(ShouldCalculateColors());
857 
858   SetSessionState(SessionState::LOCKED);
859   EXPECT_FALSE(ShouldCalculateColors());
860 
861   SetSessionState(SessionState::LOGIN_SECONDARY);
862   EXPECT_FALSE(ShouldCalculateColors());
863 }
864 
TEST_F(WallpaperControllerTest,EnableShelfColoringNotifiesObservers)865 TEST_F(WallpaperControllerTest, EnableShelfColoringNotifiesObservers) {
866   TestWallpaperControllerObserver observer(controller_);
867   EXPECT_EQ(0, observer.colors_changed_count());
868 
869   // Enable shelf coloring will set a customized wallpaper image and change
870   // session state to ACTIVE, which will trigger wallpaper colors calculation.
871   EnableShelfColoring();
872   base::RunLoop().RunUntilIdle();
873   EXPECT_EQ(1, observer.colors_changed_count());
874 }
875 
TEST_F(WallpaperControllerTest,SetCustomWallpaper)876 TEST_F(WallpaperControllerTest, SetCustomWallpaper) {
877   gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
878   WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
879 
880   SimulateUserLogin(kUser1);
881 
882   // Set a custom wallpaper for |kUser1|. Verify the wallpaper is set
883   // successfully and wallpaper info is updated.
884   ClearWallpaperCount();
885   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
886                                   file_name_1, layout, image,
887                                   false /*preview_mode=*/);
888   RunAllTasksUntilIdle();
889   EXPECT_EQ(1, GetWallpaperCount());
890   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
891   EXPECT_EQ(controller_->GetWallpaperType(), CUSTOMIZED);
892   WallpaperInfo wallpaper_info;
893   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
894   WallpaperInfo expected_wallpaper_info(
895       base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(), layout,
896       CUSTOMIZED, base::Time::Now().LocalMidnight());
897   EXPECT_EQ(wallpaper_info, expected_wallpaper_info);
898 
899   // Now set another custom wallpaper for |kUser1|. Verify that the on-screen
900   // wallpaper doesn't change since |kUser1| is not active, but wallpaper info
901   // is updated properly.
902   SimulateUserLogin(kUser2);
903   const SkColor custom_wallpaper_color = SK_ColorCYAN;
904   image = CreateImage(640, 480, custom_wallpaper_color);
905   ClearWallpaperCount();
906   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
907                                   file_name_1, layout, image,
908                                   false /*preview_mode=*/);
909   RunAllTasksUntilIdle();
910   EXPECT_EQ(0, GetWallpaperCount());
911   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
912   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
913   EXPECT_EQ(wallpaper_info, expected_wallpaper_info);
914 
915   // Verify the updated wallpaper is shown after |kUser1| becomes active again.
916   SimulateUserLogin(kUser1);
917   ClearWallpaperCount();
918   controller_->ShowUserWallpaper(account_id_1);
919   RunAllTasksUntilIdle();
920   EXPECT_EQ(1, GetWallpaperCount());
921   EXPECT_EQ(custom_wallpaper_color, GetWallpaperColor());
922 }
923 
TEST_F(WallpaperControllerTest,SetOnlineWallpaper)924 TEST_F(WallpaperControllerTest, SetOnlineWallpaper) {
925   SetBypassDecode();
926   gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
927   WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER_CROPPED;
928   SimulateUserLogin(kUser1);
929 
930   // Verify that there's no offline wallpaper available in the beginning.
931   std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
932   controller_->GetOfflineWallpaperList(base::BindLambdaForTesting(
933       [&run_loop](const std::vector<std::string>& url_list) {
934         EXPECT_TRUE(url_list.empty());
935         run_loop->Quit();
936       }));
937   run_loop->Run();
938 
939   // Verify that the attempt to set an online wallpaper without providing image
940   // data fails.
941   run_loop.reset(new base::RunLoop());
942   ClearWallpaperCount();
943   controller_->SetOnlineWallpaperIfExists(
944       account_id_1, kDummyUrl, layout, false /*preview_mode=*/,
945       base::BindLambdaForTesting([&run_loop](bool file_exists) {
946         EXPECT_FALSE(file_exists);
947         run_loop->Quit();
948       }));
949   run_loop->Run();
950   EXPECT_EQ(0, GetWallpaperCount());
951 
952   // Set an online wallpaper with image data. Verify that the wallpaper is set
953   // successfully.
954   ClearWallpaperCount();
955   controller_->SetOnlineWallpaperFromData(
956       account_id_1, std::string() /*image_data=*/, kDummyUrl, layout,
957       false /*preview_mode=*/,
958       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
959   RunAllTasksUntilIdle();
960   EXPECT_EQ(1, GetWallpaperCount());
961   EXPECT_EQ(controller_->GetWallpaperType(), ONLINE);
962   // Verify that the user wallpaper info is updated.
963   WallpaperInfo wallpaper_info;
964   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
965   WallpaperInfo expected_wallpaper_info(kDummyUrl, layout, ONLINE,
966                                         base::Time::Now().LocalMidnight());
967   EXPECT_EQ(wallpaper_info, expected_wallpaper_info);
968 
969   // Change the on-screen wallpaper to a different one. (Otherwise the
970   // subsequent calls will be no-op since we intentionally prevent reloading the
971   // same wallpaper.)
972   ClearWallpaperCount();
973   controller_->SetCustomWallpaper(
974       account_id_1, wallpaper_files_id_1, file_name_1, layout,
975       CreateImage(640, 480, kWallpaperColor), false /*preview_mode=*/);
976   RunAllTasksUntilIdle();
977   EXPECT_EQ(1, GetWallpaperCount());
978   EXPECT_EQ(controller_->GetWallpaperType(), CUSTOMIZED);
979 
980   // Attempt to set an online wallpaper without providing the image data. Verify
981   // it succeeds this time because |SetOnlineWallpaperFromData| has saved the
982   // file.
983   ClearWallpaperCount();
984   run_loop.reset(new base::RunLoop());
985   controller_->SetOnlineWallpaperIfExists(
986       account_id_1, kDummyUrl, layout, false /*preview_mode=*/,
987       base::BindLambdaForTesting([&run_loop](bool file_exists) {
988         EXPECT_TRUE(file_exists);
989         run_loop->Quit();
990       }));
991   run_loop->Run();
992   EXPECT_EQ(1, GetWallpaperCount());
993   EXPECT_EQ(controller_->GetWallpaperType(), ONLINE);
994 
995   // Verify that the wallpaper with |url| is available offline, and the returned
996   // file name should not contain the small wallpaper suffix.
997   run_loop.reset(new base::RunLoop());
998   controller_->GetOfflineWallpaperList(base::BindLambdaForTesting(
999       [&run_loop](const std::vector<std::string>& url_list) {
1000         EXPECT_EQ(1U, url_list.size());
1001         EXPECT_EQ(GURL(kDummyUrl).ExtractFileName(), url_list[0]);
1002         run_loop->Quit();
1003       }));
1004   run_loop->Run();
1005 
1006   // Log in |kUser2|, and set another online wallpaper for |kUser1|. Verify that
1007   // the on-screen wallpaper doesn't change since |kUser1| is not active, but
1008   // wallpaper info is updated properly.
1009   SimulateUserLogin(kUser2);
1010   ClearWallpaperCount();
1011   controller_->SetOnlineWallpaperFromData(
1012       account_id_1, std::string() /*image_data=*/, kDummyUrl2, layout,
1013       false /*preview_mode=*/,
1014       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
1015   RunAllTasksUntilIdle();
1016   EXPECT_EQ(0, GetWallpaperCount());
1017   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1018   WallpaperInfo expected_wallpaper_info_2(kDummyUrl2, layout, ONLINE,
1019                                           base::Time::Now().LocalMidnight());
1020   EXPECT_EQ(wallpaper_info, expected_wallpaper_info_2);
1021 }
1022 
TEST_F(WallpaperControllerTest,SetAndRemovePolicyWallpaper)1023 TEST_F(WallpaperControllerTest, SetAndRemovePolicyWallpaper) {
1024   SetBypassDecode();
1025   // Simulate the login screen.
1026   ClearLogin();
1027 
1028   // The user starts with no wallpaper info and is not controlled by policy.
1029   WallpaperInfo wallpaper_info;
1030   EXPECT_FALSE(
1031       controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1032   EXPECT_FALSE(controller_->IsPolicyControlled(account_id_1));
1033   // A default wallpaper is shown for the user.
1034   ClearWallpaperCount();
1035   controller_->ShowUserWallpaper(account_id_1);
1036   EXPECT_EQ(1, GetWallpaperCount());
1037   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1038 
1039   // Set a policy wallpaper. Verify that the user becomes policy controlled and
1040   // the wallpaper info is updated.
1041   ClearWallpaperCount();
1042   controller_->SetPolicyWallpaper(account_id_1, wallpaper_files_id_1,
1043                                   std::string() /*data=*/);
1044   RunAllTasksUntilIdle();
1045   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1046   WallpaperInfo policy_wallpaper_info(base::FilePath(wallpaper_files_id_1)
1047                                           .Append("policy-controlled.jpeg")
1048                                           .value(),
1049                                       WALLPAPER_LAYOUT_CENTER_CROPPED, POLICY,
1050                                       base::Time::Now().LocalMidnight());
1051   EXPECT_EQ(wallpaper_info, policy_wallpaper_info);
1052   EXPECT_TRUE(controller_->IsPolicyControlled(account_id_1));
1053   // Verify the wallpaper is not updated since the user hasn't logged in.
1054   EXPECT_EQ(0, GetWallpaperCount());
1055 
1056   // Log in the user. Verify the policy wallpaper is now being shown.
1057   SimulateUserLogin(kUser1);
1058   ClearWallpaperCount();
1059   controller_->ShowUserWallpaper(account_id_1);
1060   EXPECT_EQ(1, GetWallpaperCount());
1061   EXPECT_EQ(controller_->GetWallpaperType(), POLICY);
1062 
1063   // Clear the wallpaper and log out the user. Verify the policy wallpaper is
1064   // shown in the login screen.
1065   ClearWallpaper();
1066   ClearLogin();
1067   controller_->ShowUserWallpaper(account_id_1);
1068   RunAllTasksUntilIdle();
1069   EXPECT_EQ(controller_->GetWallpaperType(), POLICY);
1070   EXPECT_TRUE(controller_->IsPolicyControlled(account_id_1));
1071   // Remove the policy wallpaper. Verify the wallpaper info is reset to default
1072   // and the user is no longer policy controlled.
1073   ClearWallpaperCount();
1074   controller_->RemovePolicyWallpaper(account_id_1, wallpaper_files_id_1);
1075   WaitUntilCustomWallpapersDeleted(account_id_1);
1076   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1077   WallpaperInfo default_wallpaper_info(std::string(),
1078                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
1079                                        base::Time::Now().LocalMidnight());
1080   EXPECT_EQ(wallpaper_info, default_wallpaper_info);
1081   EXPECT_FALSE(controller_->IsPolicyControlled(account_id_1));
1082   // Verify the wallpaper is not updated since the user hasn't logged in (to
1083   // avoid abrupt wallpaper change in login screen).
1084   EXPECT_EQ(0, GetWallpaperCount());
1085   EXPECT_EQ(controller_->GetWallpaperType(), POLICY);
1086 
1087   // Log in the user. Verify the default wallpaper is now being shown.
1088   SimulateUserLogin(kUser1);
1089   ClearWallpaperCount();
1090   controller_->ShowUserWallpaper(account_id_1);
1091   EXPECT_EQ(1, GetWallpaperCount());
1092   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1093 }
1094 
TEST_F(WallpaperControllerTest,RemovePolicyWallpaperNoOp)1095 TEST_F(WallpaperControllerTest, RemovePolicyWallpaperNoOp) {
1096   auto verify_custom_wallpaper_info = [&]() {
1097     EXPECT_EQ(CUSTOMIZED, controller_->GetWallpaperType());
1098     EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
1099     WallpaperInfo wallpaper_info;
1100     EXPECT_TRUE(
1101         controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1102     WallpaperInfo expected_wallpaper_info(
1103         base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(),
1104         WALLPAPER_LAYOUT_CENTER, CUSTOMIZED, base::Time::Now().LocalMidnight());
1105     EXPECT_EQ(expected_wallpaper_info, wallpaper_info);
1106   };
1107 
1108   // Set a custom wallpaper. Verify the user is not policy controlled and the
1109   // wallpaper info is correct.
1110   SimulateUserLogin(kUser1);
1111   ClearWallpaperCount();
1112   controller_->SetCustomWallpaper(
1113       account_id_1, wallpaper_files_id_1, file_name_1, WALLPAPER_LAYOUT_CENTER,
1114       CreateImage(640, 480, kWallpaperColor), false /*preview_mode=*/);
1115   RunAllTasksUntilIdle();
1116   EXPECT_EQ(1, GetWallpaperCount());
1117   EXPECT_FALSE(controller_->IsPolicyControlled(account_id_1));
1118   verify_custom_wallpaper_info();
1119 
1120   // Verify RemovePolicyWallpaper() is a no-op when the user doesn't have a
1121   // policy wallpaper.
1122   controller_->RemovePolicyWallpaper(account_id_1, wallpaper_files_id_1);
1123   RunAllTasksUntilIdle();
1124   verify_custom_wallpaper_info();
1125 }
1126 
TEST_F(WallpaperControllerTest,SetThirdPartyWallpaper)1127 TEST_F(WallpaperControllerTest, SetThirdPartyWallpaper) {
1128   SetBypassDecode();
1129   SimulateUserLogin(kUser1);
1130 
1131   // Verify the user starts with no wallpaper info.
1132   WallpaperInfo wallpaper_info;
1133   EXPECT_FALSE(
1134       controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1135 
1136   // Set a third-party wallpaper for |kUser1|.
1137   const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
1138   gfx::ImageSkia third_party_wallpaper = CreateImage(640, 480, kWallpaperColor);
1139   ClearWallpaperCount();
1140   EXPECT_TRUE(controller_->SetThirdPartyWallpaper(
1141       account_id_1, wallpaper_files_id_1, file_name_1, layout,
1142       third_party_wallpaper));
1143   // Verify the wallpaper is shown.
1144   EXPECT_EQ(1, GetWallpaperCount());
1145   // Verify the user wallpaper info is updated.
1146   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1147   WallpaperInfo expected_wallpaper_info(
1148       base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(), layout,
1149       CUSTOMIZED, base::Time::Now().LocalMidnight());
1150   EXPECT_EQ(wallpaper_info, expected_wallpaper_info);
1151 
1152   // Switch active user to |kUser2|, but set another third-party wallpaper for
1153   // |kUser1|; the operation should not be allowed, because |kUser1| is not the
1154   // active user.
1155   SimulateUserLogin(kUser2);
1156   ClearWallpaperCount();
1157   EXPECT_FALSE(controller_->SetThirdPartyWallpaper(
1158       account_id_1, wallpaper_files_id_2, file_name_2, layout,
1159       third_party_wallpaper));
1160   // Verify the wallpaper is not shown.
1161   EXPECT_EQ(0, GetWallpaperCount());
1162   // Verify the wallpaper info for |kUser1| is updated, because setting
1163   // wallpaper is still allowed for non-active users.
1164   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1165   WallpaperInfo expected_wallpaper_info_2(
1166       base::FilePath(wallpaper_files_id_2).Append(file_name_2).value(), layout,
1167       CUSTOMIZED, base::Time::Now().LocalMidnight());
1168   EXPECT_EQ(wallpaper_info, expected_wallpaper_info_2);
1169 
1170   // Set a policy wallpaper for |kUser2|. Verify that |kUser2| becomes policy
1171   // controlled.
1172   controller_->SetPolicyWallpaper(account_id_2, wallpaper_files_id_2,
1173                                   std::string() /*data=*/);
1174   RunAllTasksUntilIdle();
1175   EXPECT_TRUE(controller_->IsPolicyControlled(account_id_2));
1176   EXPECT_TRUE(controller_->IsActiveUserWallpaperControlledByPolicy());
1177 
1178   // Setting a third-party wallpaper for |kUser2| should not be allowed, because
1179   // third-party wallpapers cannot be set for policy controlled users.
1180   ClearWallpaperCount();
1181   EXPECT_FALSE(controller_->SetThirdPartyWallpaper(
1182       account_id_2, wallpaper_files_id_1, file_name_1, layout,
1183       third_party_wallpaper));
1184   // Verify the wallpaper is not shown.
1185   EXPECT_EQ(0, GetWallpaperCount());
1186   // Verify |kUser2| is still policy controlled and has the policy wallpaper
1187   // info.
1188   EXPECT_TRUE(controller_->IsPolicyControlled(account_id_2));
1189   EXPECT_TRUE(controller_->IsActiveUserWallpaperControlledByPolicy());
1190   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_2, &wallpaper_info));
1191   WallpaperInfo policy_wallpaper_info(base::FilePath(wallpaper_files_id_2)
1192                                           .Append("policy-controlled.jpeg")
1193                                           .value(),
1194                                       WALLPAPER_LAYOUT_CENTER_CROPPED, POLICY,
1195                                       base::Time::Now().LocalMidnight());
1196   EXPECT_EQ(wallpaper_info, policy_wallpaper_info);
1197 }
1198 
TEST_F(WallpaperControllerTest,SetDefaultWallpaperForRegularAccount)1199 TEST_F(WallpaperControllerTest, SetDefaultWallpaperForRegularAccount) {
1200   CreateDefaultWallpapers();
1201   SimulateUserLogin(kUser1);
1202 
1203   // First, simulate setting a user custom wallpaper.
1204   SimulateSettingCustomWallpaper(account_id_1);
1205   WallpaperInfo wallpaper_info;
1206   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1207   WallpaperInfo default_wallpaper_info(std::string(),
1208                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
1209                                        base::Time::Now().LocalMidnight());
1210   EXPECT_NE(wallpaper_info.type, default_wallpaper_info.type);
1211 
1212   // Verify |SetDefaultWallpaper| removes the previously set custom wallpaper
1213   // info, and the large default wallpaper is set successfully with the correct
1214   // file path.
1215   UpdateDisplay("1600x1200");
1216   RunAllTasksUntilIdle();
1217   ClearWallpaperCount();
1218   ClearDecodeFilePaths();
1219   controller_->SetDefaultWallpaper(account_id_1, wallpaper_files_id_1,
1220                                    true /*show_wallpaper=*/);
1221   RunAllTasksUntilIdle();
1222   EXPECT_EQ(1, GetWallpaperCount());
1223   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1224   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1225   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultLargeWallpaperName),
1226             GetDecodeFilePaths()[0]);
1227 
1228   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1229   // The user wallpaper info has been reset to the default value.
1230   EXPECT_EQ(wallpaper_info, default_wallpaper_info);
1231 
1232   SimulateSettingCustomWallpaper(account_id_1);
1233   // Verify |SetDefaultWallpaper| removes the previously set custom wallpaper
1234   // info, and the small default wallpaper is set successfully with the correct
1235   // file path.
1236   UpdateDisplay("800x600");
1237   RunAllTasksUntilIdle();
1238   ClearWallpaperCount();
1239   ClearDecodeFilePaths();
1240   controller_->SetDefaultWallpaper(account_id_1, wallpaper_files_id_1,
1241                                    true /*show_wallpaper=*/);
1242   RunAllTasksUntilIdle();
1243   EXPECT_EQ(1, GetWallpaperCount());
1244   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1245   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1246   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultSmallWallpaperName),
1247             GetDecodeFilePaths()[0]);
1248 
1249   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1250   // The user wallpaper info has been reset to the default value.
1251   EXPECT_EQ(wallpaper_info, default_wallpaper_info);
1252 
1253   SimulateSettingCustomWallpaper(account_id_1);
1254   // Verify that when screen is rotated, |SetDefaultWallpaper| removes the
1255   // previously set custom wallpaper info, and the small default wallpaper is
1256   // set successfully with the correct file path.
1257   UpdateDisplay("800x600/r");
1258   RunAllTasksUntilIdle();
1259   ClearWallpaperCount();
1260   ClearDecodeFilePaths();
1261   controller_->SetDefaultWallpaper(account_id_1, wallpaper_files_id_1,
1262                                    true /*show_wallpaper=*/);
1263   RunAllTasksUntilIdle();
1264   EXPECT_EQ(1, GetWallpaperCount());
1265   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1266   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1267   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultSmallWallpaperName),
1268             GetDecodeFilePaths()[0]);
1269 
1270   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1271   // The user wallpaper info has been reset to the default value.
1272   EXPECT_EQ(wallpaper_info, default_wallpaper_info);
1273 }
1274 
TEST_F(WallpaperControllerTest,SetDefaultWallpaperForChildAccount)1275 TEST_F(WallpaperControllerTest, SetDefaultWallpaperForChildAccount) {
1276   CreateDefaultWallpapers();
1277 
1278   const std::string child_email = "child@test.com";
1279   const AccountId child_account_id = AccountId::FromUserEmail(child_email);
1280   const std::string child_wallpaper_files_id = GetDummyFileId(child_account_id);
1281   fake_user_manager_->AddChildUser(child_account_id);
1282 
1283   // Verify the large child wallpaper is set successfully with the correct file
1284   // path.
1285   UpdateDisplay("1600x1200");
1286   RunAllTasksUntilIdle();
1287   ClearWallpaperCount();
1288   ClearDecodeFilePaths();
1289   controller_->SetDefaultWallpaper(child_account_id, child_wallpaper_files_id,
1290                                    true /*show_wallpaper=*/);
1291   RunAllTasksUntilIdle();
1292   EXPECT_EQ(1, GetWallpaperCount());
1293   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1294   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1295   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kChildLargeWallpaperName),
1296             GetDecodeFilePaths()[0]);
1297 
1298   // Verify the small child wallpaper is set successfully with the correct file
1299   // path.
1300   UpdateDisplay("800x600");
1301   RunAllTasksUntilIdle();
1302   ClearWallpaperCount();
1303   ClearDecodeFilePaths();
1304   controller_->SetDefaultWallpaper(child_account_id, child_wallpaper_files_id,
1305                                    true /*show_wallpaper=*/);
1306   RunAllTasksUntilIdle();
1307   EXPECT_EQ(1, GetWallpaperCount());
1308   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1309   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1310   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kChildSmallWallpaperName),
1311             GetDecodeFilePaths()[0]);
1312 }
1313 
1314 // Verify that the |ShowWallpaperImage| will be called with the default image
1315 // for the guest session only even if there's a policy that has been set for
1316 // another user which invokes |SetPolicyWallpaper|.
TEST_F(WallpaperControllerTest,SetDefaultWallpaperForGuestSessionUnaffectedByWallpaperPolicy)1317 TEST_F(WallpaperControllerTest,
1318        SetDefaultWallpaperForGuestSessionUnaffectedByWallpaperPolicy) {
1319   SetBypassDecode();
1320   // Simulate the login screen.
1321   ClearLogin();
1322   CreateDefaultWallpapers();
1323   ClearWallpaperCount();
1324 
1325   // First, simulate settings for a guest user which will show the default
1326   // wallpaper image by invoking |ShowWallpaperImage|.
1327   SimulateGuestLogin();
1328 
1329   UpdateDisplay("1600x1200");
1330   RunAllTasksUntilIdle();
1331   ClearWallpaperCount();
1332   ClearDecodeFilePaths();
1333 
1334   const AccountId guest_id =
1335       AccountId::FromUserEmail(user_manager::kGuestUserName);
1336   fake_user_manager_->AddGuestUser(guest_id);
1337   controller_->SetDefaultWallpaper(guest_id, wallpaper_files_id_1,
1338                                    /*show_wallpaper=*/true);
1339 
1340   WallpaperInfo wallpaper_info;
1341   WallpaperInfo default_wallpaper_info(std::string(),
1342                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
1343                                        base::Time::Now().LocalMidnight());
1344   // Verify that the current displayed wallpaper is the default one inside the
1345   // guest session.
1346   EXPECT_EQ(1, GetWallpaperCount());
1347   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1348   EXPECT_TRUE(controller_->GetUserWallpaperInfo(guest_id, &wallpaper_info));
1349   EXPECT_EQ(wallpaper_info, default_wallpaper_info);
1350   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1351   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestLargeWallpaperName),
1352             GetDecodeFilePaths()[0]);
1353 
1354   // Second, set a user policy for which is being set for another
1355   // user and verifying that the policy has been applied successfully.
1356   WallpaperInfo policy_wallpaper_info;
1357   controller_->SetPolicyWallpaper(account_id_1, wallpaper_files_id_2,
1358                                   /*data=*/std::string());
1359   EXPECT_TRUE(
1360       controller_->GetUserWallpaperInfo(account_id_1, &policy_wallpaper_info));
1361   WallpaperInfo expected_policy_wallpaper_info(
1362       base::FilePath(wallpaper_files_id_2)
1363           .Append("policy-controlled.jpeg")
1364           .value(),
1365       WALLPAPER_LAYOUT_CENTER_CROPPED, POLICY,
1366       base::Time::Now().LocalMidnight());
1367   EXPECT_EQ(policy_wallpaper_info, expected_policy_wallpaper_info);
1368   EXPECT_TRUE(controller_->IsPolicyControlled(account_id_1));
1369 
1370   // Finally, verifying that the guest session hasn't been affected by the new
1371   // policy and |ShowWallpaperImage| hasn't been invoked another time.
1372 
1373   EXPECT_EQ(1, GetWallpaperCount());
1374   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1375   EXPECT_TRUE(controller_->GetUserWallpaperInfo(guest_id, &wallpaper_info));
1376   EXPECT_EQ(wallpaper_info, default_wallpaper_info);
1377   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1378   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestLargeWallpaperName),
1379             GetDecodeFilePaths()[0]);
1380 }
1381 
TEST_F(WallpaperControllerTest,SetDefaultWallpaperForGuestSession)1382 TEST_F(WallpaperControllerTest, SetDefaultWallpaperForGuestSession) {
1383   CreateDefaultWallpapers();
1384 
1385   // First, simulate setting a custom wallpaper for a regular user.
1386   SimulateUserLogin(kUser1);
1387   SimulateSettingCustomWallpaper(account_id_1);
1388   WallpaperInfo wallpaper_info;
1389   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1390   WallpaperInfo default_wallpaper_info(std::string(),
1391                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
1392                                        base::Time::Now().LocalMidnight());
1393   EXPECT_NE(wallpaper_info.type, default_wallpaper_info.type);
1394 
1395   const AccountId guest_id =
1396       AccountId::FromUserEmail(user_manager::kGuestUserName);
1397   fake_user_manager_->AddGuestUser(guest_id);
1398 
1399   // Verify that during a guest session, |SetDefaultWallpaper| removes the user
1400   // custom wallpaper info, but a guest specific wallpaper should be set,
1401   // instead of the regular default wallpaper.
1402   UpdateDisplay("1600x1200");
1403   RunAllTasksUntilIdle();
1404   ClearWallpaperCount();
1405   ClearDecodeFilePaths();
1406   controller_->SetDefaultWallpaper(guest_id, wallpaper_files_id_1,
1407                                    true /*show_wallpaper=*/);
1408   RunAllTasksUntilIdle();
1409   EXPECT_EQ(1, GetWallpaperCount());
1410   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1411   EXPECT_TRUE(controller_->GetUserWallpaperInfo(guest_id, &wallpaper_info));
1412   EXPECT_EQ(wallpaper_info, default_wallpaper_info);
1413   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1414   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestLargeWallpaperName),
1415             GetDecodeFilePaths()[0]);
1416 
1417   UpdateDisplay("800x600");
1418   RunAllTasksUntilIdle();
1419   ClearWallpaperCount();
1420   ClearDecodeFilePaths();
1421   controller_->SetDefaultWallpaper(guest_id, wallpaper_files_id_1,
1422                                    true /*show_wallpaper=*/);
1423   RunAllTasksUntilIdle();
1424   EXPECT_EQ(1, GetWallpaperCount());
1425   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1426   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1427   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestSmallWallpaperName),
1428             GetDecodeFilePaths()[0]);
1429 }
1430 
TEST_F(WallpaperControllerTest,IgnoreWallpaperRequestInKioskMode)1431 TEST_F(WallpaperControllerTest, IgnoreWallpaperRequestInKioskMode) {
1432   gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
1433   SimulateUserLogin("kiosk", user_manager::USER_TYPE_KIOSK_APP);
1434 
1435   // Verify that |SetCustomWallpaper| doesn't set wallpaper in kiosk mode, and
1436   // |account_id_1|'s wallpaper info is not updated.
1437   ClearWallpaperCount();
1438   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
1439                                   file_name_1, WALLPAPER_LAYOUT_CENTER, image,
1440                                   false /*preview_mode=*/);
1441   RunAllTasksUntilIdle();
1442   EXPECT_EQ(0, GetWallpaperCount());
1443   WallpaperInfo wallpaper_info;
1444   EXPECT_FALSE(
1445       controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1446 
1447   // Verify that |SetOnlineWallpaperFromData| doesn't set wallpaper in kiosk
1448   // mode, and |account_id_1|'s wallpaper info is not updated.
1449   std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
1450   ClearWallpaperCount();
1451   controller_->SetOnlineWallpaperFromData(
1452       account_id_1, std::string() /*image_data=*/, kDummyUrl,
1453       WALLPAPER_LAYOUT_CENTER, false /*preview_mode=*/,
1454       base::BindLambdaForTesting([&run_loop](bool success) {
1455         EXPECT_FALSE(success);
1456         run_loop->Quit();
1457       }));
1458   run_loop->Run();
1459   EXPECT_EQ(0, GetWallpaperCount());
1460   EXPECT_FALSE(
1461       controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1462 
1463   // Verify that |SetDefaultWallpaper| doesn't set wallpaper in kiosk mode, and
1464   // |account_id_1|'s wallpaper info is not updated.
1465   ClearWallpaperCount();
1466   controller_->SetDefaultWallpaper(account_id_1, wallpaper_files_id_1,
1467                                    true /*show_wallpaper=*/);
1468   RunAllTasksUntilIdle();
1469   EXPECT_EQ(0, GetWallpaperCount());
1470   EXPECT_FALSE(
1471       controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1472 }
1473 
1474 // Disable the wallpaper setting for public session since it is ephemeral.
TEST_F(WallpaperControllerTest,NotShowWallpaperSettingInPublicSession)1475 TEST_F(WallpaperControllerTest, NotShowWallpaperSettingInPublicSession) {
1476   SimulateUserLogin("public_session", user_manager::USER_TYPE_PUBLIC_ACCOUNT);
1477   EXPECT_FALSE(controller_->ShouldShowWallpaperSetting());
1478 }
1479 
TEST_F(WallpaperControllerTest,IgnoreWallpaperRequestWhenPolicyIsEnforced)1480 TEST_F(WallpaperControllerTest, IgnoreWallpaperRequestWhenPolicyIsEnforced) {
1481   SetBypassDecode();
1482   gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
1483   SimulateUserLogin(kUser1);
1484 
1485   // Set a policy wallpaper for the user. Verify the user is policy controlled.
1486   controller_->SetPolicyWallpaper(account_id_1, wallpaper_files_id_1,
1487                                   std::string() /*data=*/);
1488   RunAllTasksUntilIdle();
1489   EXPECT_TRUE(controller_->IsPolicyControlled(account_id_1));
1490 
1491   // Verify that |SetCustomWallpaper| doesn't set wallpaper when policy is
1492   // enforced, and the user wallpaper info is not updated.
1493   ClearWallpaperCount();
1494   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
1495                                   file_name_1, WALLPAPER_LAYOUT_CENTER, image,
1496                                   false /*preview_mode=*/);
1497   RunAllTasksUntilIdle();
1498   EXPECT_EQ(0, GetWallpaperCount());
1499   WallpaperInfo wallpaper_info;
1500   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1501   WallpaperInfo policy_wallpaper_info(base::FilePath(wallpaper_files_id_1)
1502                                           .Append("policy-controlled.jpeg")
1503                                           .value(),
1504                                       WALLPAPER_LAYOUT_CENTER_CROPPED, POLICY,
1505                                       base::Time::Now().LocalMidnight());
1506   EXPECT_EQ(wallpaper_info, policy_wallpaper_info);
1507 
1508   // Verify that |SetOnlineWallpaperFromData| doesn't set wallpaper when policy
1509   // is enforced, and the user wallpaper info is not updated.
1510   std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
1511   ClearWallpaperCount();
1512   controller_->SetOnlineWallpaperFromData(
1513       account_id_1, std::string() /*image_data=*/, kDummyUrl,
1514       WALLPAPER_LAYOUT_CENTER_CROPPED, false /*preview_mode=*/,
1515       base::BindLambdaForTesting([&run_loop](bool success) {
1516         EXPECT_FALSE(success);
1517         run_loop->Quit();
1518       }));
1519   run_loop->Run();
1520   EXPECT_EQ(0, GetWallpaperCount());
1521   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1522   EXPECT_EQ(wallpaper_info, policy_wallpaper_info);
1523 
1524   // Verify that |SetDefaultWallpaper| doesn't set wallpaper when policy is
1525   // enforced, and the user wallpaper info is not updated.
1526   ClearWallpaperCount();
1527   controller_->SetDefaultWallpaper(account_id_1, wallpaper_files_id_1,
1528                                    true /*show_wallpaper=*/);
1529   RunAllTasksUntilIdle();
1530   EXPECT_EQ(0, GetWallpaperCount());
1531   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1532   EXPECT_EQ(wallpaper_info, policy_wallpaper_info);
1533 }
1534 
TEST_F(WallpaperControllerTest,VerifyWallpaperCache)1535 TEST_F(WallpaperControllerTest, VerifyWallpaperCache) {
1536   SetBypassDecode();
1537   gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
1538   SimulateUserLogin(kUser1);
1539 
1540   // |kUser1| doesn't have wallpaper cache in the beginning.
1541   gfx::ImageSkia cached_wallpaper;
1542   EXPECT_FALSE(
1543       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
1544   base::FilePath path;
1545   EXPECT_FALSE(controller_->GetPathFromCache(account_id_1, &path));
1546 
1547   // Verify |SetOnlineWallpaperFromData| updates wallpaper cache for |user1|.
1548   controller_->SetOnlineWallpaperFromData(
1549       account_id_1, std::string() /*image_data=*/, kDummyUrl,
1550       WALLPAPER_LAYOUT_CENTER, false /*preview_mode=*/,
1551       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
1552   RunAllTasksUntilIdle();
1553   EXPECT_TRUE(
1554       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
1555   EXPECT_TRUE(controller_->GetPathFromCache(account_id_1, &path));
1556 
1557   // After |kUser2| is logged in, |user1|'s wallpaper cache should still be kept
1558   // (crbug.com/339576). Note the active user is still |user1|.
1559   TestSessionControllerClient* session = GetSessionControllerClient();
1560   session->AddUserSession(kUser2);
1561   EXPECT_TRUE(
1562       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
1563   EXPECT_TRUE(controller_->GetPathFromCache(account_id_1, &path));
1564 
1565   // Verify |SetDefaultWallpaper| clears wallpaper cache.
1566   controller_->SetDefaultWallpaper(account_id_1, wallpaper_files_id_1,
1567                                    true /*show_wallpaper=*/);
1568   EXPECT_FALSE(
1569       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
1570   EXPECT_FALSE(controller_->GetPathFromCache(account_id_1, &path));
1571 
1572   // Verify |SetCustomWallpaper| updates wallpaper cache for |user1|.
1573   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
1574                                   file_name_1, WALLPAPER_LAYOUT_CENTER, image,
1575                                   false /*preview_mode=*/);
1576   RunAllTasksUntilIdle();
1577   EXPECT_TRUE(
1578       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
1579   EXPECT_TRUE(controller_->GetPathFromCache(account_id_1, &path));
1580 
1581   // Verify |RemoveUserWallpaper| clears wallpaper cache.
1582   controller_->RemoveUserWallpaper(account_id_1, wallpaper_files_id_1);
1583   EXPECT_FALSE(
1584       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
1585   EXPECT_FALSE(controller_->GetPathFromCache(account_id_1, &path));
1586 }
1587 
1588 // Tests that the appropriate wallpaper (large vs. small) is shown depending
1589 // on the desktop resolution.
TEST_F(WallpaperControllerTest,ShowCustomWallpaperWithCorrectResolution)1590 TEST_F(WallpaperControllerTest, ShowCustomWallpaperWithCorrectResolution) {
1591   CreateDefaultWallpapers();
1592   const base::FilePath small_custom_wallpaper_path =
1593       GetCustomWallpaperPath(WallpaperControllerImpl::kSmallWallpaperSubDir,
1594                              wallpaper_files_id_1, file_name_1);
1595   const base::FilePath large_custom_wallpaper_path =
1596       GetCustomWallpaperPath(WallpaperControllerImpl::kLargeWallpaperSubDir,
1597                              wallpaper_files_id_1, file_name_1);
1598   const base::FilePath small_default_wallpaper_path =
1599       default_wallpaper_dir_.GetPath().Append(kDefaultSmallWallpaperName);
1600   const base::FilePath large_default_wallpaper_path =
1601       default_wallpaper_dir_.GetPath().Append(kDefaultLargeWallpaperName);
1602 
1603   CreateAndSaveWallpapers(account_id_1);
1604   ClearWallpaperCount();
1605   controller_->ShowUserWallpaper(account_id_1);
1606   RunAllTasksUntilIdle();
1607   // Display is initialized to 800x600. The small resolution custom wallpaper is
1608   // expected. A second decode request with small resolution default wallpaper
1609   // is also expected. (Because unit tests don't support actual wallpaper
1610   // decoding, it falls back to the default wallpaper.)
1611   EXPECT_EQ(1, GetWallpaperCount());
1612   ASSERT_EQ(2u, GetDecodeFilePaths().size());
1613   EXPECT_EQ(small_custom_wallpaper_path, GetDecodeFilePaths()[0]);
1614   EXPECT_EQ(small_default_wallpaper_path, GetDecodeFilePaths()[1]);
1615 
1616   // Hook up another 800x600 display. This shouldn't trigger a reload.
1617   ClearWallpaperCount();
1618   ClearDecodeFilePaths();
1619   UpdateDisplay("800x600,800x600");
1620   RunAllTasksUntilIdle();
1621   EXPECT_EQ(0, GetWallpaperCount());
1622   EXPECT_EQ(0u, GetDecodeFilePaths().size());
1623 
1624   // Detach the secondary display.
1625   UpdateDisplay("800x600");
1626   RunAllTasksUntilIdle();
1627   // Hook up a 2000x2000 display. The large resolution custom wallpaper should
1628   // be loaded.
1629   ClearWallpaperCount();
1630   ClearDecodeFilePaths();
1631   UpdateDisplay("800x600,2000x2000");
1632   RunAllTasksUntilIdle();
1633   EXPECT_EQ(1, GetWallpaperCount());
1634   ASSERT_EQ(2u, GetDecodeFilePaths().size());
1635   EXPECT_EQ(large_custom_wallpaper_path, GetDecodeFilePaths()[0]);
1636   EXPECT_EQ(large_default_wallpaper_path, GetDecodeFilePaths()[1]);
1637 
1638   // Detach the secondary display.
1639   UpdateDisplay("800x600");
1640   RunAllTasksUntilIdle();
1641   // Hook up the 2000x2000 display again. The large resolution default wallpaper
1642   // should persist. Test for crbug/165788.
1643   ClearWallpaperCount();
1644   ClearDecodeFilePaths();
1645   UpdateDisplay("800x600,2000x2000");
1646   RunAllTasksUntilIdle();
1647   EXPECT_EQ(1, GetWallpaperCount());
1648   ASSERT_EQ(2u, GetDecodeFilePaths().size());
1649   EXPECT_EQ(large_custom_wallpaper_path, GetDecodeFilePaths()[0]);
1650   EXPECT_EQ(large_default_wallpaper_path, GetDecodeFilePaths()[1]);
1651 }
1652 
1653 // After the display is rotated, the sign in wallpaper should be kept. Test for
1654 // crbug.com/794725.
TEST_F(WallpaperControllerTest,SigninWallpaperIsKeptAfterRotation)1655 TEST_F(WallpaperControllerTest, SigninWallpaperIsKeptAfterRotation) {
1656   CreateDefaultWallpapers();
1657 
1658   UpdateDisplay("800x600");
1659   RunAllTasksUntilIdle();
1660   controller_->ShowSigninWallpaper();
1661   RunAllTasksUntilIdle();
1662   // Display is initialized to 800x600. The small resolution default wallpaper
1663   // is expected.
1664   EXPECT_EQ(1, GetWallpaperCount());
1665   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1666   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1667   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultSmallWallpaperName),
1668             GetDecodeFilePaths()[0]);
1669 
1670   ClearWallpaperCount();
1671   ClearDecodeFilePaths();
1672   // After rotating the display, the small resolution default wallpaper should
1673   // still be expected, instead of a custom wallpaper.
1674   UpdateDisplay("800x600/r");
1675   RunAllTasksUntilIdle();
1676   EXPECT_EQ(1, GetWallpaperCount());
1677   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
1678   ASSERT_EQ(1u, GetDecodeFilePaths().size());
1679   EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultSmallWallpaperName),
1680             GetDecodeFilePaths()[0]);
1681 }
1682 
1683 // Display size change should trigger wallpaper reload.
TEST_F(WallpaperControllerTest,ReloadWallpaper)1684 TEST_F(WallpaperControllerTest, ReloadWallpaper) {
1685   CreateAndSaveWallpapers(account_id_1);
1686 
1687   // Show a user wallpaper.
1688   UpdateDisplay("800x600");
1689   RunAllTasksUntilIdle();
1690   ClearWallpaperCount();
1691   controller_->ShowUserWallpaper(account_id_1);
1692   RunAllTasksUntilIdle();
1693   EXPECT_EQ(1, GetWallpaperCount());
1694   // Rotating the display should trigger a wallpaper reload.
1695   ClearWallpaperCount();
1696   UpdateDisplay("800x600/r");
1697   RunAllTasksUntilIdle();
1698   EXPECT_EQ(1, GetWallpaperCount());
1699   // Calling |ShowUserWallpaper| again with the same account id and display
1700   // size should not trigger wallpaper reload (crbug.com/158383).
1701   ClearWallpaperCount();
1702   controller_->ShowUserWallpaper(account_id_1);
1703   RunAllTasksUntilIdle();
1704   EXPECT_EQ(0, GetWallpaperCount());
1705 
1706   // Start wallpaper preview.
1707   SimulateUserLogin(kUser1);
1708   std::unique_ptr<aura::Window> wallpaper_picker_window(
1709       CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
1710   WindowState::Get(wallpaper_picker_window.get())->Activate();
1711   ClearWallpaperCount();
1712   controller_->SetCustomWallpaper(
1713       account_id_1, wallpaper_files_id_1, file_name_1, WALLPAPER_LAYOUT_CENTER,
1714       CreateImage(640, 480, kWallpaperColor), true /*preview_mode=*/);
1715   RunAllTasksUntilIdle();
1716   EXPECT_EQ(1, GetWallpaperCount());
1717   // Rotating the display should trigger a wallpaper reload.
1718   ClearWallpaperCount();
1719   UpdateDisplay("800x600");
1720   RunAllTasksUntilIdle();
1721   EXPECT_EQ(1, GetWallpaperCount());
1722   ClearWallpaperCount();
1723   controller_->CancelPreviewWallpaper();
1724   RunAllTasksUntilIdle();
1725   EXPECT_EQ(1, GetWallpaperCount());
1726 
1727   // Show an always-on-top wallpaper.
1728   const base::FilePath image_path =
1729       base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
1730           chromeos::switches::kGuestWallpaperLarge);
1731   CreateDefaultWallpapers();
1732   SetBypassDecode();
1733   ClearWallpaperCount();
1734   controller_->ShowAlwaysOnTopWallpaper(image_path);
1735   RunAllTasksUntilIdle();
1736   EXPECT_EQ(1, GetWallpaperCount());
1737   // Rotating the display should trigger a wallpaper reload.
1738   ClearWallpaperCount();
1739   UpdateDisplay("800x600/r");
1740   RunAllTasksUntilIdle();
1741   EXPECT_EQ(1, GetWallpaperCount());
1742 }
1743 
TEST_F(WallpaperControllerTest,UpdateCustomWallpaperLayout)1744 TEST_F(WallpaperControllerTest, UpdateCustomWallpaperLayout) {
1745   SetBypassDecode();
1746   gfx::ImageSkia image = CreateImage(640, 480, kSmallCustomWallpaperColor);
1747   WallpaperLayout layout = WALLPAPER_LAYOUT_STRETCH;
1748   WallpaperLayout new_layout = WALLPAPER_LAYOUT_CENTER;
1749   SimulateUserLogin(kUser1);
1750 
1751   // Set a custom wallpaper for the user. Verify that it's set successfully
1752   // and the wallpaper info is updated.
1753   ClearWallpaperCount();
1754   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
1755                                   file_name_1, layout, image,
1756                                   false /*preview_mode=*/);
1757   RunAllTasksUntilIdle();
1758   EXPECT_EQ(1, GetWallpaperCount());
1759   EXPECT_EQ(controller_->GetWallpaperLayout(), layout);
1760   WallpaperInfo wallpaper_info;
1761   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1762   WallpaperInfo expected_custom_wallpaper_info(
1763       base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(), layout,
1764       CUSTOMIZED, base::Time::Now().LocalMidnight());
1765   EXPECT_EQ(wallpaper_info, expected_custom_wallpaper_info);
1766 
1767   // Now change to a different layout. Verify that the layout is updated for
1768   // both the current wallpaper and the saved wallpaper info.
1769   ClearWallpaperCount();
1770   controller_->UpdateCustomWallpaperLayout(account_id_1, new_layout);
1771   RunAllTasksUntilIdle();
1772   EXPECT_EQ(1, GetWallpaperCount());
1773   EXPECT_EQ(controller_->GetWallpaperLayout(), new_layout);
1774   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1775   expected_custom_wallpaper_info.layout = new_layout;
1776   EXPECT_EQ(wallpaper_info, expected_custom_wallpaper_info);
1777 
1778   // Now set an online wallpaper. Verify that it's set successfully and the
1779   // wallpaper info is updated.
1780   image = CreateImage(640, 480, kWallpaperColor);
1781   ClearWallpaperCount();
1782   controller_->SetOnlineWallpaperFromData(
1783       account_id_1, std::string() /*image_data=*/, kDummyUrl, layout,
1784       false /*preview_mode=*/,
1785       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
1786   RunAllTasksUntilIdle();
1787   EXPECT_EQ(1, GetWallpaperCount());
1788   EXPECT_EQ(controller_->GetWallpaperType(), ONLINE);
1789   EXPECT_EQ(controller_->GetWallpaperLayout(), layout);
1790   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1791   WallpaperInfo expected_online_wallpaper_info(
1792       kDummyUrl, layout, ONLINE, base::Time::Now().LocalMidnight());
1793   EXPECT_EQ(wallpaper_info, expected_online_wallpaper_info);
1794 
1795   // Now change the layout of the online wallpaper. Verify that it's a no-op.
1796   ClearWallpaperCount();
1797   controller_->UpdateCustomWallpaperLayout(account_id_1, new_layout);
1798   RunAllTasksUntilIdle();
1799   // The wallpaper is not updated.
1800   EXPECT_EQ(0, GetWallpaperCount());
1801   EXPECT_EQ(controller_->GetWallpaperLayout(), layout);
1802   // The saved wallpaper info is not updated.
1803   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
1804   EXPECT_EQ(wallpaper_info, expected_online_wallpaper_info);
1805 }
1806 
1807 // Tests that if a user who has a custom wallpaper is removed from the device,
1808 // only the directory that contains the user's custom wallpapers gets removed.
1809 // The other user's custom wallpaper is not affected.
TEST_F(WallpaperControllerTest,RemoveUserWithCustomWallpaper)1810 TEST_F(WallpaperControllerTest, RemoveUserWithCustomWallpaper) {
1811   SimulateUserLogin(kUser1);
1812   base::FilePath small_wallpaper_path_1 =
1813       GetCustomWallpaperPath(WallpaperControllerImpl::kSmallWallpaperSubDir,
1814                              wallpaper_files_id_1, file_name_1);
1815   // Set a custom wallpaper for |kUser1| and verify the wallpaper exists.
1816   CreateAndSaveWallpapers(account_id_1);
1817   EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
1818 
1819   // Now login another user and set a custom wallpaper for the user.
1820   SimulateUserLogin(kUser2);
1821   base::FilePath small_wallpaper_path_2 = GetCustomWallpaperPath(
1822       WallpaperControllerImpl::kSmallWallpaperSubDir, wallpaper_files_id_2,
1823       GetDummyFileName(account_id_2));
1824   CreateAndSaveWallpapers(account_id_2);
1825   EXPECT_TRUE(base::PathExists(small_wallpaper_path_2));
1826 
1827   // Simulate the removal of |kUser2|.
1828   controller_->RemoveUserWallpaper(account_id_2, wallpaper_files_id_2);
1829   // Wait until all files under the user's custom wallpaper directory are
1830   // removed.
1831   WaitUntilCustomWallpapersDeleted(account_id_2);
1832   EXPECT_FALSE(base::PathExists(small_wallpaper_path_2));
1833 
1834   // Verify that the other user's wallpaper is not affected.
1835   EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
1836 }
1837 
1838 // Tests that if a user who has a default wallpaper is removed from the device,
1839 // the other user's custom wallpaper is not affected.
TEST_F(WallpaperControllerTest,RemoveUserWithDefaultWallpaper)1840 TEST_F(WallpaperControllerTest, RemoveUserWithDefaultWallpaper) {
1841   SimulateUserLogin(kUser1);
1842   base::FilePath small_wallpaper_path_1 =
1843       GetCustomWallpaperPath(WallpaperControllerImpl::kSmallWallpaperSubDir,
1844                              wallpaper_files_id_1, file_name_1);
1845   // Set a custom wallpaper for |kUser1| and verify the wallpaper exists.
1846   CreateAndSaveWallpapers(account_id_1);
1847   EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
1848 
1849   // Now login another user and set a default wallpaper for the user.
1850   SimulateUserLogin(kUser2);
1851   controller_->SetDefaultWallpaper(account_id_2, wallpaper_files_id_2,
1852                                    true /*show_wallpaper=*/);
1853 
1854   // Simulate the removal of |kUser2|.
1855   controller_->RemoveUserWallpaper(account_id_2, wallpaper_files_id_2);
1856 
1857   // Verify that the other user's wallpaper is not affected.
1858   EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
1859 }
1860 
TEST_F(WallpaperControllerTest,IsActiveUserWallpaperControlledByPolicy)1861 TEST_F(WallpaperControllerTest, IsActiveUserWallpaperControlledByPolicy) {
1862   SetBypassDecode();
1863   // Simulate the login screen. Verify that it returns false since there's no
1864   // active user.
1865   ClearLogin();
1866   EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
1867 
1868   SimulateUserLogin(kUser1);
1869   EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
1870   // Set a policy wallpaper for the active user. Verify that the active user
1871   // becomes policy controlled.
1872   controller_->SetPolicyWallpaper(account_id_1, wallpaper_files_id_1,
1873                                   std::string() /*data=*/);
1874   RunAllTasksUntilIdle();
1875   EXPECT_TRUE(controller_->IsActiveUserWallpaperControlledByPolicy());
1876 
1877   // Switch the active user. Verify the active user is not policy controlled.
1878   SimulateUserLogin(kUser2);
1879   EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
1880 
1881   // Logs out. Verify that it returns false since there's no active user.
1882   ClearLogin();
1883   EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
1884 }
1885 
TEST_F(WallpaperControllerTest,WallpaperBlur)1886 TEST_F(WallpaperControllerTest, WallpaperBlur) {
1887   TestWallpaperControllerObserver observer(controller_);
1888 
1889   ASSERT_TRUE(controller_->IsBlurAllowedForLockState());
1890   ASSERT_FALSE(controller_->IsWallpaperBlurredForLockState());
1891 
1892   SetSessionState(SessionState::ACTIVE);
1893   EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
1894   EXPECT_EQ(0, observer.blur_changed_count());
1895 
1896   SetSessionState(SessionState::LOCKED);
1897   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1898   EXPECT_EQ(1, observer.blur_changed_count());
1899 
1900   SetSessionState(SessionState::LOGGED_IN_NOT_ACTIVE);
1901   EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
1902   EXPECT_EQ(2, observer.blur_changed_count());
1903 
1904   SetSessionState(SessionState::LOGIN_SECONDARY);
1905   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1906   EXPECT_EQ(3, observer.blur_changed_count());
1907 
1908   // Blur state does not change below.
1909   SetSessionState(SessionState::LOGIN_PRIMARY);
1910   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1911   EXPECT_EQ(3, observer.blur_changed_count());
1912 
1913   SetSessionState(SessionState::OOBE);
1914   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1915   EXPECT_EQ(3, observer.blur_changed_count());
1916 
1917   SetSessionState(SessionState::UNKNOWN);
1918   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1919   EXPECT_EQ(3, observer.blur_changed_count());
1920 }
1921 
TEST_F(WallpaperControllerTest,WallpaperBlurDuringLockScreenTransition)1922 TEST_F(WallpaperControllerTest, WallpaperBlurDuringLockScreenTransition) {
1923   ui::ScopedAnimationDurationScaleMode test_duration_mode(
1924       ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
1925 
1926   gfx::ImageSkia image = CreateImage(600, 400, kWallpaperColor);
1927   controller_->ShowWallpaperImage(
1928       image, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
1929       /*preview_mode=*/false, /*always_on_top=*/false);
1930 
1931   TestWallpaperControllerObserver observer(controller_);
1932 
1933   ASSERT_TRUE(controller_->IsBlurAllowedForLockState());
1934   ASSERT_FALSE(controller_->IsWallpaperBlurredForLockState());
1935 
1936   ASSERT_EQ(2u, wallpaper_view()->layer()->parent()->children().size());
1937   EXPECT_EQ(ui::LAYER_TEXTURED,
1938             wallpaper_view()->layer()->parent()->children()[0]->type());
1939   EXPECT_EQ(ui::LAYER_TEXTURED,
1940             wallpaper_view()->layer()->parent()->children()[1]->type());
1941 
1942   // Simulate lock and unlock sequence.
1943   controller_->UpdateWallpaperBlurForLockState(true);
1944   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1945   EXPECT_EQ(1, observer.blur_changed_count());
1946 
1947   SetSessionState(SessionState::LOCKED);
1948   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1949   ASSERT_EQ(3u, wallpaper_view()->layer()->parent()->children().size());
1950   EXPECT_EQ(ui::LAYER_SOLID_COLOR,
1951             wallpaper_view()->layer()->parent()->children()[0]->type());
1952   EXPECT_EQ(ui::LAYER_TEXTURED,
1953             wallpaper_view()->layer()->parent()->children()[1]->type());
1954   EXPECT_EQ(ui::LAYER_TEXTURED,
1955             wallpaper_view()->layer()->parent()->children()[2]->type());
1956 
1957   // Change of state to ACTIVE triggers post lock animation and
1958   // UpdateWallpaperBlur(false)
1959   SetSessionState(SessionState::ACTIVE);
1960   EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
1961   EXPECT_EQ(2, observer.blur_changed_count());
1962   ASSERT_EQ(2u, wallpaper_view()->layer()->parent()->children().size());
1963   EXPECT_EQ(ui::LAYER_TEXTURED,
1964             wallpaper_view()->layer()->parent()->children()[0]->type());
1965   EXPECT_EQ(ui::LAYER_TEXTURED,
1966             wallpaper_view()->layer()->parent()->children()[1]->type());
1967 }
1968 
TEST_F(WallpaperControllerTest,LockDuringOverview)1969 TEST_F(WallpaperControllerTest, LockDuringOverview) {
1970   gfx::ImageSkia image = CreateImage(600, 400, kWallpaperColor);
1971   controller_->ShowWallpaperImage(
1972       image, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
1973       /*preview_mode=*/false, /*always_on_top=*/false);
1974   TestWallpaperControllerObserver observer(controller_);
1975 
1976   Shell::Get()->overview_controller()->StartOverview();
1977 
1978   EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
1979   EXPECT_EQ(0, observer.blur_changed_count());
1980 
1981   // Simulate lock and unlock sequence.
1982   SetSessionState(SessionState::LOCKED);
1983 
1984   EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
1985 
1986   // Get wallpaper_view directly because it's not animating.
1987   auto* wallpaper_view = Shell::Get()
1988                              ->GetPrimaryRootWindowController()
1989                              ->wallpaper_widget_controller()
1990                              ->wallpaper_view();
1991 
1992   // Make sure that wallpaper still have blur.
1993   ASSERT_EQ(30, wallpaper_view->blur_sigma());
1994 }
1995 
TEST_F(WallpaperControllerTest,OnlyShowDevicePolicyWallpaperOnLoginScreen)1996 TEST_F(WallpaperControllerTest, OnlyShowDevicePolicyWallpaperOnLoginScreen) {
1997   SetBypassDecode();
1998 
1999   // Verify the device policy wallpaper is shown on login screen.
2000   SetSessionState(SessionState::LOGIN_PRIMARY);
2001   controller_->SetDevicePolicyWallpaperPath(
2002       base::FilePath(kDefaultSmallWallpaperName));
2003   RunAllTasksUntilIdle();
2004   EXPECT_EQ(1, GetWallpaperCount());
2005   EXPECT_TRUE(IsDevicePolicyWallpaper());
2006   // Verify the device policy wallpaper shouldn't be blurred.
2007   ASSERT_FALSE(controller_->IsBlurAllowedForLockState());
2008   ASSERT_FALSE(controller_->IsWallpaperBlurredForLockState());
2009 
2010   // Verify the device policy wallpaper is replaced when session state is no
2011   // longer LOGIN_PRIMARY.
2012   SetSessionState(SessionState::LOGGED_IN_NOT_ACTIVE);
2013   RunAllTasksUntilIdle();
2014   EXPECT_EQ(2, GetWallpaperCount());
2015   EXPECT_FALSE(IsDevicePolicyWallpaper());
2016 
2017   // Verify the device policy wallpaper never shows up again when session
2018   // state changes.
2019   SetSessionState(SessionState::ACTIVE);
2020   RunAllTasksUntilIdle();
2021   EXPECT_EQ(2, GetWallpaperCount());
2022   EXPECT_FALSE(IsDevicePolicyWallpaper());
2023 
2024   SetSessionState(SessionState::LOCKED);
2025   RunAllTasksUntilIdle();
2026   EXPECT_EQ(2, GetWallpaperCount());
2027   EXPECT_FALSE(IsDevicePolicyWallpaper());
2028 
2029   SetSessionState(SessionState::LOGIN_SECONDARY);
2030   RunAllTasksUntilIdle();
2031   EXPECT_EQ(2, GetWallpaperCount());
2032   EXPECT_FALSE(IsDevicePolicyWallpaper());
2033 }
2034 
TEST_F(WallpaperControllerTest,ShouldShowInitialAnimationAfterBoot)2035 TEST_F(WallpaperControllerTest, ShouldShowInitialAnimationAfterBoot) {
2036   CreateDefaultWallpapers();
2037 
2038   // Simulate the login screen after system boot.
2039   base::CommandLine::ForCurrentProcess()->AppendSwitch(
2040       chromeos::switches::kFirstExecAfterBoot);
2041   base::CommandLine::ForCurrentProcess()->AppendSwitch(
2042       chromeos::switches::kLoginManager);
2043   ClearLogin();
2044 
2045   // Show the first wallpaper. Verify that the slower animation should be used.
2046   ClearWallpaperCount();
2047   controller_->ShowUserWallpaper(account_id_1);
2048   RunAllTasksUntilIdle();
2049   EXPECT_TRUE(controller_->ShouldShowInitialAnimation());
2050   EXPECT_EQ(1, GetWallpaperCount());
2051 
2052   // Show the second wallpaper. Verify that the slower animation should not be
2053   // used. (Use a different user type to ensure a different wallpaper is shown,
2054   // otherwise requests of loading the same wallpaper are ignored.)
2055   ClearWallpaperCount();
2056   controller_->ShowUserWallpaper(AccountId::FromUserEmail("child@test.com"));
2057   RunAllTasksUntilIdle();
2058   EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
2059   EXPECT_EQ(1, GetWallpaperCount());
2060 
2061   // Log in the user and show the wallpaper. Verify that the slower animation
2062   // should not be used.
2063   SimulateUserLogin(kUser1);
2064   ClearWallpaperCount();
2065   controller_->ShowUserWallpaper(account_id_1);
2066   RunAllTasksUntilIdle();
2067   EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
2068   EXPECT_EQ(1, GetWallpaperCount());
2069 }
2070 
TEST_F(WallpaperControllerTest,ShouldNotShowInitialAnimationAfterSignOut)2071 TEST_F(WallpaperControllerTest, ShouldNotShowInitialAnimationAfterSignOut) {
2072   CreateDefaultWallpapers();
2073 
2074   // Simulate the login screen after user sign-out. Verify that the slower
2075   // animation should never be used.
2076   base::CommandLine::ForCurrentProcess()->AppendSwitch(
2077       chromeos::switches::kLoginManager);
2078   CreateAndSaveWallpapers(account_id_1);
2079   ClearLogin();
2080 
2081   // Show the first wallpaper.
2082   ClearWallpaperCount();
2083   controller_->ShowUserWallpaper(account_id_1);
2084   RunAllTasksUntilIdle();
2085   EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
2086   EXPECT_EQ(1, GetWallpaperCount());
2087 
2088   // Show the second wallpaper.
2089   ClearWallpaperCount();
2090   controller_->ShowUserWallpaper(AccountId::FromUserEmail("child@test.com"));
2091   RunAllTasksUntilIdle();
2092   EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
2093   EXPECT_EQ(1, GetWallpaperCount());
2094 
2095   // Log in the user and show the wallpaper.
2096   SimulateUserLogin(kUser1);
2097   ClearWallpaperCount();
2098   controller_->ShowUserWallpaper(account_id_1);
2099   RunAllTasksUntilIdle();
2100   EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
2101   EXPECT_EQ(1, GetWallpaperCount());
2102 }
2103 
TEST_F(WallpaperControllerTest,ClosePreviewWallpaperOnOverviewStart)2104 TEST_F(WallpaperControllerTest, ClosePreviewWallpaperOnOverviewStart) {
2105   TestWallpaperControllerClient client;
2106   controller_->SetClient(&client);
2107 
2108   // Verify the user starts with a default wallpaper and the user wallpaper info
2109   // is initialized with default values.
2110   SimulateUserLogin(kUser1);
2111   ClearWallpaperCount();
2112   controller_->ShowUserWallpaper(account_id_1);
2113   RunAllTasksUntilIdle();
2114   EXPECT_EQ(1, GetWallpaperCount());
2115   WallpaperInfo user_wallpaper_info;
2116   WallpaperInfo default_wallpaper_info(std::string(),
2117                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
2118                                        base::Time::Now().LocalMidnight());
2119   EXPECT_TRUE(
2120       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2121   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2122 
2123   // Simulate opening the wallpaper picker window.
2124   std::unique_ptr<aura::Window> wallpaper_picker_window(
2125       CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
2126   WindowState::Get(wallpaper_picker_window.get())->Activate();
2127 
2128   // Set a custom wallpaper for the user and enable preview. Verify that the
2129   // wallpaper is changed to the expected color.
2130   const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
2131   gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
2132   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2133   ClearWallpaperCount();
2134   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
2135                                   file_name_1, layout, custom_wallpaper,
2136                                   true /*preview_mode=*/);
2137   RunAllTasksUntilIdle();
2138   EXPECT_EQ(1, GetWallpaperCount());
2139   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2140   // Verify that the user wallpaper info remains unchanged during the preview.
2141   EXPECT_TRUE(
2142       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2143   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2144 
2145   // Now enter overview mode. Verify the wallpaper changes back to the default,
2146   // the user wallpaper info remains unchanged, and enters overview mode
2147   // properly.
2148   ClearWallpaperCount();
2149   Shell::Get()->overview_controller()->StartOverview();
2150   RunAllTasksUntilIdle();
2151   EXPECT_EQ(1, GetWallpaperCount());
2152   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2153   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
2154   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2155   EXPECT_TRUE(Shell::Get()->overview_controller()->InOverviewSession());
2156   EXPECT_EQ(1u, client.close_preview_count());
2157 }
2158 
TEST_F(WallpaperControllerTest,ClosePreviewWallpaperOnWindowCycleStart)2159 TEST_F(WallpaperControllerTest, ClosePreviewWallpaperOnWindowCycleStart) {
2160   TestWallpaperControllerClient client;
2161   controller_->SetClient(&client);
2162 
2163   // Verify the user starts with a default wallpaper and the user wallpaper info
2164   // is initialized with default values.
2165   SimulateUserLogin(kUser1);
2166   ClearWallpaperCount();
2167   controller_->ShowUserWallpaper(account_id_1);
2168   RunAllTasksUntilIdle();
2169   EXPECT_EQ(1, GetWallpaperCount());
2170   WallpaperInfo user_wallpaper_info;
2171   WallpaperInfo default_wallpaper_info(std::string(),
2172                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
2173                                        base::Time::Now().LocalMidnight());
2174   EXPECT_TRUE(
2175       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2176   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2177 
2178   // Simulate opening the wallpaper picker window.
2179   std::unique_ptr<aura::Window> wallpaper_picker_window(
2180       CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
2181   WindowState::Get(wallpaper_picker_window.get())->Activate();
2182 
2183   // Set a custom wallpaper for the user and enable preview. Verify that the
2184   // wallpaper is changed to the expected color.
2185   const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
2186   gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
2187   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2188   ClearWallpaperCount();
2189   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
2190                                   file_name_1, layout, custom_wallpaper,
2191                                   true /*preview_mode=*/);
2192   RunAllTasksUntilIdle();
2193   EXPECT_EQ(1, GetWallpaperCount());
2194   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2195   // Verify that the user wallpaper info remains unchanged during the preview.
2196   EXPECT_TRUE(
2197       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2198   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2199 
2200   // Now start window cycle. Verify the wallpaper changes back to the default,
2201   // the user wallpaper info remains unchanged, and enters window cycle.
2202   ClearWallpaperCount();
2203   Shell::Get()->window_cycle_controller()->HandleCycleWindow(
2204       WindowCycleController::FORWARD);
2205   RunAllTasksUntilIdle();
2206   EXPECT_EQ(1, GetWallpaperCount());
2207   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2208   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
2209   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2210   EXPECT_TRUE(Shell::Get()->window_cycle_controller()->IsCycling());
2211   EXPECT_EQ(1u, client.close_preview_count());
2212 }
2213 
TEST_F(WallpaperControllerTest,ConfirmPreviewWallpaper)2214 TEST_F(WallpaperControllerTest, ConfirmPreviewWallpaper) {
2215   // Verify the user starts with a default wallpaper and the user wallpaper info
2216   // is initialized with default values.
2217   SimulateUserLogin(kUser1);
2218   ClearWallpaperCount();
2219   controller_->ShowUserWallpaper(account_id_1);
2220   RunAllTasksUntilIdle();
2221   EXPECT_EQ(1, GetWallpaperCount());
2222   WallpaperInfo user_wallpaper_info;
2223   WallpaperInfo default_wallpaper_info(std::string(),
2224                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
2225                                        base::Time::Now().LocalMidnight());
2226   EXPECT_TRUE(
2227       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2228   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2229 
2230   // Simulate opening the wallpaper picker window.
2231   std::unique_ptr<aura::Window> wallpaper_picker_window(
2232       CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
2233   WindowState::Get(wallpaper_picker_window.get())->Activate();
2234 
2235   // Set a custom wallpaper for the user and enable preview. Verify that the
2236   // wallpaper is changed to the expected color.
2237   const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
2238   gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
2239   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2240   ClearWallpaperCount();
2241   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
2242                                   file_name_1, layout, custom_wallpaper,
2243                                   true /*preview_mode=*/);
2244   RunAllTasksUntilIdle();
2245   EXPECT_EQ(1, GetWallpaperCount());
2246   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2247   // Verify that the user wallpaper info remains unchanged during the preview.
2248   EXPECT_TRUE(
2249       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2250   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2251 
2252   // Now confirm the preview wallpaper, verify that there's no wallpaper change
2253   // because the wallpaper is already shown.
2254   ClearWallpaperCount();
2255   controller_->ConfirmPreviewWallpaper();
2256   RunAllTasksUntilIdle();
2257   EXPECT_EQ(0, GetWallpaperCount());
2258   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2259   // Verify that the user wallpaper info is now updated to the custom wallpaper
2260   // info.
2261   WallpaperInfo custom_wallpaper_info(
2262       base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(), layout,
2263       CUSTOMIZED, base::Time::Now().LocalMidnight());
2264   EXPECT_TRUE(
2265       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2266   EXPECT_EQ(user_wallpaper_info, custom_wallpaper_info);
2267 
2268   // Set an empty online wallpaper for the user, verify it fails.
2269   ClearWallpaperCount();
2270   std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
2271   SetOnlineWallpaperFromImage(
2272       account_id_1, gfx::ImageSkia(), kDummyUrl, layout, false /*save_file=*/,
2273       true /*preview_mode=*/,
2274       base::BindLambdaForTesting([&run_loop](bool success) {
2275         EXPECT_FALSE(success);
2276         run_loop->Quit();
2277       }));
2278   run_loop->Run();
2279   EXPECT_EQ(0, GetWallpaperCount());
2280 
2281   // Now set a valid online wallpaper for the user and enable preview. Verify
2282   // that the wallpaper is changed to the expected color.
2283   const SkColor online_wallpaper_color = SK_ColorCYAN;
2284   gfx::ImageSkia online_wallpaper =
2285       CreateImage(640, 480, online_wallpaper_color);
2286   EXPECT_NE(online_wallpaper_color, GetWallpaperColor());
2287   run_loop.reset(new base::RunLoop());
2288   SetOnlineWallpaperFromImage(
2289       account_id_1, online_wallpaper, kDummyUrl, layout, false /*save_file=*/,
2290       true /*preview_mode=*/,
2291       base::BindLambdaForTesting([&run_loop](bool success) {
2292         EXPECT_TRUE(success);
2293         run_loop->Quit();
2294       }));
2295   run_loop->Run();
2296   EXPECT_EQ(1, GetWallpaperCount());
2297   EXPECT_EQ(online_wallpaper_color, GetWallpaperColor());
2298   // Verify that the user wallpaper info remains unchanged during the preview.
2299   EXPECT_TRUE(
2300       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2301   EXPECT_EQ(user_wallpaper_info, custom_wallpaper_info);
2302 
2303   // Now confirm the preview wallpaper, verify that there's no wallpaper change
2304   // because the wallpaper is already shown.
2305   ClearWallpaperCount();
2306   controller_->ConfirmPreviewWallpaper();
2307   RunAllTasksUntilIdle();
2308   EXPECT_EQ(0, GetWallpaperCount());
2309   EXPECT_EQ(online_wallpaper_color, GetWallpaperColor());
2310   // Verify that the user wallpaper info is now updated to the online wallpaper
2311   // info.
2312   WallpaperInfo online_wallpaper_info(kDummyUrl, layout, ONLINE,
2313                                       base::Time::Now().LocalMidnight());
2314   EXPECT_TRUE(
2315       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2316   EXPECT_EQ(user_wallpaper_info, online_wallpaper_info);
2317 }
2318 
TEST_F(WallpaperControllerTest,CancelPreviewWallpaper)2319 TEST_F(WallpaperControllerTest, CancelPreviewWallpaper) {
2320   // Verify the user starts with a default wallpaper and the user wallpaper info
2321   // is initialized with default values.
2322   SimulateUserLogin(kUser1);
2323   ClearWallpaperCount();
2324   controller_->ShowUserWallpaper(account_id_1);
2325   RunAllTasksUntilIdle();
2326   EXPECT_EQ(1, GetWallpaperCount());
2327   WallpaperInfo user_wallpaper_info;
2328   WallpaperInfo default_wallpaper_info(std::string(),
2329                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
2330                                        base::Time::Now().LocalMidnight());
2331   EXPECT_TRUE(
2332       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2333   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2334 
2335   // Simulate opening the wallpaper picker window.
2336   std::unique_ptr<aura::Window> wallpaper_picker_window(
2337       CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
2338   WindowState::Get(wallpaper_picker_window.get())->Activate();
2339 
2340   // Set a custom wallpaper for the user and enable preview. Verify that the
2341   // wallpaper is changed to the expected color.
2342   const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
2343   gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
2344   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2345   ClearWallpaperCount();
2346   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
2347                                   file_name_1, layout, custom_wallpaper,
2348                                   true /*preview_mode=*/);
2349   RunAllTasksUntilIdle();
2350   EXPECT_EQ(1, GetWallpaperCount());
2351   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2352   // Verify that the user wallpaper info remains unchanged during the preview.
2353   EXPECT_TRUE(
2354       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2355   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2356 
2357   // Now cancel the preview. Verify the wallpaper changes back to the default
2358   // and the user wallpaper info remains unchanged.
2359   ClearWallpaperCount();
2360   controller_->CancelPreviewWallpaper();
2361   RunAllTasksUntilIdle();
2362   EXPECT_EQ(1, GetWallpaperCount());
2363   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2364   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
2365   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2366 
2367   // Now set an online wallpaper for the user and enable preview. Verify that
2368   // the wallpaper is changed to the expected color.
2369   const SkColor online_wallpaper_color = SK_ColorCYAN;
2370   gfx::ImageSkia online_wallpaper =
2371       CreateImage(640, 480, online_wallpaper_color);
2372   EXPECT_NE(online_wallpaper_color, GetWallpaperColor());
2373   ClearWallpaperCount();
2374   SetOnlineWallpaperFromImage(
2375       account_id_1, online_wallpaper, kDummyUrl, layout, false /*save_file=*/,
2376       true /*preview_mode=*/,
2377       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
2378   RunAllTasksUntilIdle();
2379   EXPECT_EQ(1, GetWallpaperCount());
2380   EXPECT_EQ(online_wallpaper_color, GetWallpaperColor());
2381   // Verify that the user wallpaper info remains unchanged during the preview.
2382   EXPECT_TRUE(
2383       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2384   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2385 
2386   // Now cancel the preview. Verify the wallpaper changes back to the default
2387   // and the user wallpaper info remains unchanged.
2388   ClearWallpaperCount();
2389   controller_->CancelPreviewWallpaper();
2390   RunAllTasksUntilIdle();
2391   EXPECT_EQ(1, GetWallpaperCount());
2392   EXPECT_NE(online_wallpaper_color, GetWallpaperColor());
2393   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
2394   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2395 }
2396 
TEST_F(WallpaperControllerTest,WallpaperSyncedDuringPreview)2397 TEST_F(WallpaperControllerTest, WallpaperSyncedDuringPreview) {
2398   // Verify the user starts with a default wallpaper and the user wallpaper info
2399   // is initialized with default values.
2400   SimulateUserLogin(kUser1);
2401   ClearWallpaperCount();
2402   controller_->ShowUserWallpaper(account_id_1);
2403   RunAllTasksUntilIdle();
2404   EXPECT_EQ(1, GetWallpaperCount());
2405   WallpaperInfo user_wallpaper_info;
2406   WallpaperInfo default_wallpaper_info(std::string(),
2407                                        WALLPAPER_LAYOUT_CENTER_CROPPED, DEFAULT,
2408                                        base::Time::Now().LocalMidnight());
2409   EXPECT_TRUE(
2410       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2411   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2412 
2413   // Simulate opening the wallpaper picker window.
2414   std::unique_ptr<aura::Window> wallpaper_picker_window(
2415       CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
2416   WindowState::Get(wallpaper_picker_window.get())->Activate();
2417 
2418   // Set a custom wallpaper for the user and enable preview. Verify that the
2419   // wallpaper is changed to the expected color.
2420   const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
2421   gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
2422   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2423   ClearWallpaperCount();
2424   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
2425                                   file_name_1, layout, custom_wallpaper,
2426                                   true /*preview_mode=*/);
2427   RunAllTasksUntilIdle();
2428   EXPECT_EQ(1, GetWallpaperCount());
2429   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2430   // Verify that the user wallpaper info remains unchanged during the preview.
2431   EXPECT_TRUE(
2432       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2433   EXPECT_EQ(user_wallpaper_info, default_wallpaper_info);
2434 
2435   // Now set another custom wallpaper for the user and disable preview (this
2436   // happens if a custom wallpaper set on another device is being synced).
2437   // Verify there's no wallpaper change since preview mode shouldn't be
2438   // interrupted.
2439   const SkColor synced_custom_wallpaper_color = SK_ColorBLUE;
2440   gfx::ImageSkia synced_custom_wallpaper =
2441       CreateImage(640, 480, synced_custom_wallpaper_color);
2442   ClearWallpaperCount();
2443   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_2,
2444                                   file_name_2, layout, synced_custom_wallpaper,
2445                                   false /*preview_mode=*/);
2446   RunAllTasksUntilIdle();
2447   EXPECT_EQ(0, GetWallpaperCount());
2448   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2449   // However, the user wallpaper info should already be updated to the new info.
2450   WallpaperInfo synced_custom_wallpaper_info(
2451       base::FilePath(wallpaper_files_id_2).Append(file_name_2).value(), layout,
2452       CUSTOMIZED, base::Time::Now().LocalMidnight());
2453   EXPECT_TRUE(
2454       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2455   EXPECT_EQ(user_wallpaper_info, synced_custom_wallpaper_info);
2456 
2457   // Now cancel the preview. Verify the synced custom wallpaper is shown instead
2458   // of the initial default wallpaper, and the user wallpaper info is still
2459   // correct.
2460   ClearWallpaperCount();
2461   controller_->CancelPreviewWallpaper();
2462   RunAllTasksUntilIdle();
2463   EXPECT_EQ(1, GetWallpaperCount());
2464   EXPECT_EQ(synced_custom_wallpaper_color, GetWallpaperColor());
2465   EXPECT_TRUE(
2466       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2467   EXPECT_EQ(user_wallpaper_info, synced_custom_wallpaper_info);
2468 
2469   // Repeat the above steps for online wallpapers: set a online wallpaper for
2470   // the user and enable preview. Verify that the wallpaper is changed to the
2471   // expected color.
2472   gfx::ImageSkia online_wallpaper = CreateImage(640, 480, kWallpaperColor);
2473   EXPECT_NE(kWallpaperColor, GetWallpaperColor());
2474 
2475   ClearWallpaperCount();
2476   SetOnlineWallpaperFromImage(
2477       account_id_1, online_wallpaper, kDummyUrl, layout, false /*save_file=*/,
2478       true /*preview_mode=*/,
2479       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
2480   RunAllTasksUntilIdle();
2481   EXPECT_EQ(1, GetWallpaperCount());
2482   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2483   // Verify that the user wallpaper info remains unchanged during the preview.
2484   EXPECT_TRUE(
2485       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2486   EXPECT_EQ(user_wallpaper_info, synced_custom_wallpaper_info);
2487 
2488   // Now set another online wallpaper for the user and disable preview. Verify
2489   // there's no wallpaper change since preview mode shouldn't be interrupted.
2490   const SkColor synced_online_wallpaper_color = SK_ColorCYAN;
2491   gfx::ImageSkia synced_online_wallpaper =
2492       CreateImage(640, 480, synced_online_wallpaper_color);
2493   ClearWallpaperCount();
2494   SetOnlineWallpaperFromImage(
2495       account_id_1, synced_online_wallpaper, kDummyUrl2, layout,
2496       false /*save_file=*/, false /*preview_mode=*/,
2497       WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
2498   RunAllTasksUntilIdle();
2499   EXPECT_EQ(0, GetWallpaperCount());
2500   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2501   // However, the user wallpaper info should already be updated to the new info.
2502   WallpaperInfo synced_online_wallpaper_info(kDummyUrl2, layout, ONLINE,
2503                                              base::Time::Now().LocalMidnight());
2504   EXPECT_TRUE(
2505       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2506   EXPECT_EQ(user_wallpaper_info, synced_online_wallpaper_info);
2507 
2508   // Now cancel the preview. Verify the synced online wallpaper is shown instead
2509   // of the previous custom wallpaper, and the user wallpaper info is still
2510   // correct.
2511   ClearWallpaperCount();
2512   controller_->CancelPreviewWallpaper();
2513   RunAllTasksUntilIdle();
2514   EXPECT_EQ(1, GetWallpaperCount());
2515   EXPECT_EQ(synced_online_wallpaper_color, GetWallpaperColor());
2516   EXPECT_TRUE(
2517       controller_->GetUserWallpaperInfo(account_id_1, &user_wallpaper_info));
2518   EXPECT_EQ(user_wallpaper_info, synced_online_wallpaper_info);
2519 }
2520 
TEST_F(WallpaperControllerTest,AddFirstWallpaperAnimationEndCallback)2521 TEST_F(WallpaperControllerTest, AddFirstWallpaperAnimationEndCallback) {
2522   ui::ScopedAnimationDurationScaleMode test_duration_mode(
2523       ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2524   std::unique_ptr<aura::Window> test_window(
2525       CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
2526 
2527   bool is_first_callback_run = false;
2528   controller_->AddFirstWallpaperAnimationEndCallback(
2529       base::BindLambdaForTesting(
2530           [&is_first_callback_run]() { is_first_callback_run = true; }),
2531       test_window.get());
2532   // The callback is not run because the first wallpaper hasn't been set.
2533   RunAllTasksUntilIdle();
2534   EXPECT_FALSE(is_first_callback_run);
2535 
2536   // Set the first wallpaper.
2537   controller_->ShowDefaultWallpaperForTesting();
2538   bool is_second_callback_run = false;
2539   controller_->AddFirstWallpaperAnimationEndCallback(
2540       base::BindLambdaForTesting(
2541           [&is_second_callback_run]() { is_second_callback_run = true; }),
2542       test_window.get());
2543   {
2544     // The animation is quite short (0.01 seconds) which is problematic in
2545     // debug builds if RunAllTasksUntilIdle is a bit slow to execute. That leads
2546     // to test flakes. We work around that by temporarily freezing time, which
2547     // prevents the animation from unexpectedly completing too soon.
2548     // Ideally this test should use MockTime instead, which will become easier
2549     // after https://crrev.com/c/1352260 lands.
2550     base::subtle::ScopedTimeClockOverrides time_override(
2551         nullptr,
2552         []() {
2553           static base::TimeTicks time_ticks =
2554               base::subtle::TimeTicksNowIgnoringOverride();
2555           return time_ticks;
2556         },
2557         nullptr);
2558 
2559     RunAllTasksUntilIdle();
2560   }
2561   // Neither callback is run because the animation of the first wallpaper
2562   // hasn't finished yet.
2563   EXPECT_FALSE(is_first_callback_run);
2564   EXPECT_FALSE(is_second_callback_run);
2565 
2566   // Force the animation to complete. The two callbacks are both run.
2567   RunDesktopControllerAnimation();
2568   EXPECT_TRUE(is_first_callback_run);
2569   EXPECT_TRUE(is_second_callback_run);
2570 
2571   // The callback added after the first wallpaper animation is run right away.
2572   bool is_third_callback_run = false;
2573   controller_->AddFirstWallpaperAnimationEndCallback(
2574       base::BindLambdaForTesting(
2575           [&is_third_callback_run]() { is_third_callback_run = true; }),
2576       test_window.get());
2577   EXPECT_TRUE(is_third_callback_run);
2578 }
2579 
TEST_F(WallpaperControllerTest,ShowOneShotWallpaper)2580 TEST_F(WallpaperControllerTest, ShowOneShotWallpaper) {
2581   gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
2582   WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
2583   SimulateUserLogin(kUser1);
2584   // First, set a custom wallpaper for |kUser1|. Verify the wallpaper is shown
2585   // successfully and the user wallpaper info is updated.
2586   ClearWallpaperCount();
2587   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
2588                                   file_name_1, layout, custom_wallpaper,
2589                                   false /*preview_mode=*/);
2590   RunAllTasksUntilIdle();
2591   EXPECT_EQ(1, GetWallpaperCount());
2592   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2593   EXPECT_EQ(WallpaperType::CUSTOMIZED, controller_->GetWallpaperType());
2594   const WallpaperInfo expected_wallpaper_info(
2595       base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(), layout,
2596       WallpaperType::CUSTOMIZED, base::Time::Now().LocalMidnight());
2597   WallpaperInfo wallpaper_info;
2598   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
2599   EXPECT_EQ(expected_wallpaper_info, wallpaper_info);
2600 
2601   // Show a one-shot wallpaper. Verify it is shown successfully.
2602   ClearWallpaperCount();
2603   constexpr SkColor kOneShotWallpaperColor = SK_ColorWHITE;
2604   gfx::ImageSkia one_shot_wallpaper =
2605       CreateImage(640, 480, kOneShotWallpaperColor);
2606   controller_->ShowOneShotWallpaper(one_shot_wallpaper);
2607   RunAllTasksUntilIdle();
2608   EXPECT_EQ(1, GetWallpaperCount());
2609   EXPECT_EQ(kOneShotWallpaperColor, GetWallpaperColor());
2610   EXPECT_EQ(WallpaperType::ONE_SHOT, controller_->GetWallpaperType());
2611   EXPECT_FALSE(controller_->IsBlurAllowedForLockState());
2612   EXPECT_FALSE(controller_->ShouldApplyColorFilter());
2613 
2614   // Verify that we can reload wallpaer without losing it.
2615   // This is important for screen rotation.
2616   controller_->ReloadWallpaperForTesting(/*clear_cache=*/false);
2617   RunAllTasksUntilIdle();
2618   EXPECT_EQ(2, GetWallpaperCount());  // Reload increments count.
2619   EXPECT_EQ(kOneShotWallpaperColor, GetWallpaperColor());
2620   EXPECT_EQ(WallpaperType::ONE_SHOT, controller_->GetWallpaperType());
2621   EXPECT_FALSE(controller_->IsBlurAllowedForLockState());
2622   EXPECT_FALSE(controller_->ShouldApplyColorFilter());
2623 
2624   // Verify the user wallpaper info is unaffected, and the one-shot wallpaper
2625   // can be replaced by the user wallpaper.
2626   EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
2627   EXPECT_EQ(expected_wallpaper_info, wallpaper_info);
2628   ClearWallpaperCount();
2629   controller_->ShowUserWallpaper(account_id_1);
2630   RunAllTasksUntilIdle();
2631   EXPECT_EQ(1, GetWallpaperCount());
2632   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2633   EXPECT_EQ(WallpaperType::CUSTOMIZED, controller_->GetWallpaperType());
2634 }
2635 
TEST_F(WallpaperControllerTest,OnFirstWallpaperShown)2636 TEST_F(WallpaperControllerTest, OnFirstWallpaperShown) {
2637   TestWallpaperControllerObserver observer(controller_);
2638   EXPECT_EQ(0, GetWallpaperCount());
2639   EXPECT_EQ(0, observer.first_shown_count());
2640   // Show the first wallpaper, verify the observer is notified.
2641   controller_->ShowWallpaperImage(CreateImage(640, 480, SK_ColorBLUE),
2642                                   CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
2643                                   /*preview_mode=*/false,
2644                                   /*always_on_top=*/false);
2645   RunAllTasksUntilIdle();
2646   EXPECT_EQ(SK_ColorBLUE, GetWallpaperColor());
2647   EXPECT_EQ(1, GetWallpaperCount());
2648   EXPECT_EQ(1, observer.first_shown_count());
2649   // Show the second wallpaper, verify the observer is not notified.
2650   controller_->ShowWallpaperImage(CreateImage(640, 480, SK_ColorCYAN),
2651                                   CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
2652                                   /*preview_mode=*/false,
2653                                   /*always_on_top=*/false);
2654   RunAllTasksUntilIdle();
2655   EXPECT_EQ(SK_ColorCYAN, GetWallpaperColor());
2656   EXPECT_EQ(2, GetWallpaperCount());
2657   EXPECT_EQ(1, observer.first_shown_count());
2658 }
2659 
2660 // Although ephemeral users' custom wallpapers are not saved to disk, they
2661 // should be kept within the user session. Test for https://crbug.com/825237.
TEST_F(WallpaperControllerTest,ShowWallpaperForEphemeralUser)2662 TEST_F(WallpaperControllerTest, ShowWallpaperForEphemeralUser) {
2663   // Add an ephemeral user session and simulate login, like SimulateUserLogin.
2664   UserSession session;
2665   session.session_id = 0;
2666   session.user_info.account_id = account_id_1;
2667   session.user_info.is_ephemeral = true;
2668   Shell::Get()->session_controller()->UpdateUserSession(std::move(session));
2669   TestSessionControllerClient* const client = GetSessionControllerClient();
2670   client->ProvidePrefServiceForUser(account_id_1);
2671   client->SwitchActiveUser(AccountId::FromUserEmail(kUser1));
2672   client->SetSessionState(SessionState::ACTIVE);
2673 
2674   // The user doesn't have wallpaper cache in the beginning.
2675   gfx::ImageSkia cached_wallpaper;
2676   EXPECT_FALSE(
2677       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
2678   base::FilePath path;
2679   EXPECT_FALSE(controller_->GetPathFromCache(account_id_1, &path));
2680 
2681   ClearWallpaperCount();
2682   controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
2683                                   file_name_1, WALLPAPER_LAYOUT_CENTER,
2684                                   CreateImage(640, 480, kWallpaperColor),
2685                                   /*preview_mode=*/false);
2686   RunAllTasksUntilIdle();
2687   EXPECT_EQ(1, GetWallpaperCount());
2688   EXPECT_EQ(CUSTOMIZED, controller_->GetWallpaperType());
2689   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2690 
2691   // The custom wallpaper is cached.
2692   EXPECT_TRUE(
2693       controller_->GetWallpaperFromCache(account_id_1, &cached_wallpaper));
2694   EXPECT_EQ(
2695       kWallpaperColor,
2696       cached_wallpaper.GetRepresentation(1.0f).GetBitmap().getColor(0, 0));
2697   EXPECT_TRUE(controller_->GetPathFromCache(account_id_1, &path));
2698 
2699   // Calling |ShowUserWallpaper| will continue showing the custom wallpaper
2700   // instead of reverting to the default.
2701   ClearWallpaperCount();
2702   controller_->ShowUserWallpaper(account_id_1);
2703   RunAllTasksUntilIdle();
2704   EXPECT_EQ(0, GetWallpaperCount());
2705   EXPECT_EQ(CUSTOMIZED, controller_->GetWallpaperType());
2706   EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
2707 }
2708 
TEST_F(WallpaperControllerTest,AlwaysOnTopWallpaper)2709 TEST_F(WallpaperControllerTest, AlwaysOnTopWallpaper) {
2710   CreateDefaultWallpapers();
2711   SetBypassDecode();
2712 
2713   // Show a default wallpaper.
2714   EXPECT_EQ(0, GetWallpaperCount());
2715   controller_->ShowSigninWallpaper();
2716   RunAllTasksUntilIdle();
2717   EXPECT_EQ(1, GetWallpaperCount());
2718   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
2719   EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
2720   EXPECT_EQ(0, ChildCountForContainer(kAlwaysOnTopWallpaperId));
2721 
2722   // Show an always-on-top wallpaper.
2723   const base::FilePath image_path =
2724       base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
2725           chromeos::switches::kGuestWallpaperLarge);
2726   controller_->ShowAlwaysOnTopWallpaper(image_path);
2727   RunAllTasksUntilIdle();
2728   EXPECT_EQ(2, GetWallpaperCount());
2729   EXPECT_EQ(controller_->GetWallpaperType(), ONE_SHOT);
2730   EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
2731   EXPECT_EQ(1, ChildCountForContainer(kAlwaysOnTopWallpaperId));
2732 
2733   // Subsequent wallpaper requests are ignored when the current wallpaper is
2734   // always-on-top.
2735   controller_->ShowSigninWallpaper();
2736   RunAllTasksUntilIdle();
2737   EXPECT_EQ(2, GetWallpaperCount());
2738   EXPECT_EQ(controller_->GetWallpaperType(), ONE_SHOT);
2739   EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
2740   EXPECT_EQ(1, ChildCountForContainer(kAlwaysOnTopWallpaperId));
2741 
2742   // The wallpaper reverts to the default after the always-on-top wallpaper is
2743   // removed.
2744   controller_->RemoveAlwaysOnTopWallpaper();
2745   RunAllTasksUntilIdle();
2746   EXPECT_EQ(3, GetWallpaperCount());
2747   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
2748   EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
2749   EXPECT_EQ(0, ChildCountForContainer(kAlwaysOnTopWallpaperId));
2750 
2751   // Calling |RemoveAlwaysOnTopWallpaper| is a no-op when the current wallpaper
2752   // is not always-on-top.
2753   controller_->RemoveAlwaysOnTopWallpaper();
2754   RunAllTasksUntilIdle();
2755   EXPECT_EQ(3, GetWallpaperCount());
2756   EXPECT_EQ(controller_->GetWallpaperType(), DEFAULT);
2757   EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
2758   EXPECT_EQ(0, ChildCountForContainer(kAlwaysOnTopWallpaperId));
2759 }
2760 
2761 namespace {
2762 
2763 class WallpaperControllerPrefTest : public AshTestBase {
2764  public:
WallpaperControllerPrefTest()2765   WallpaperControllerPrefTest() {
2766     auto property = std::make_unique<base::DictionaryValue>();
2767     property->SetInteger("rotation",
2768                          static_cast<int>(display::Display::ROTATE_90));
2769     property->SetInteger("width", 800);
2770     property->SetInteger("height", 600);
2771 
2772     DictionaryPrefUpdate update(local_state(), prefs::kDisplayProperties);
2773     update.Get()->Set("2200000000", std::move(property));
2774   }
2775 
2776   ~WallpaperControllerPrefTest() override = default;
2777 };
2778 
2779 }  // namespace
2780 
2781 // Make sure that the display and the wallpaper view are rotated correctly at
2782 // startup.
TEST_F(WallpaperControllerPrefTest,InitWithPrefs)2783 TEST_F(WallpaperControllerPrefTest, InitWithPrefs) {
2784   auto* wallpaper_view = Shell::GetPrimaryRootWindowController()
2785                              ->wallpaper_widget_controller()
2786                              ->wallpaper_view();
2787   auto* root_window =
2788       wallpaper_view->GetWidget()->GetNativeWindow()->GetRootWindow();
2789 
2790   EXPECT_EQ(gfx::Size(600, 800), display::Screen::GetScreen()
2791                                      ->GetDisplayNearestWindow(root_window)
2792                                      .size());
2793   EXPECT_EQ(root_window->bounds().size(), wallpaper_view->bounds().size());
2794 }
2795 
TEST_F(WallpaperControllerTest,NoAnimationForNewRootWindowWhenLocked)2796 TEST_F(WallpaperControllerTest, NoAnimationForNewRootWindowWhenLocked) {
2797   ui::ScopedAnimationDurationScaleMode test_duration_mode(
2798       ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2799   SetSessionState(SessionState::LOCKED);
2800   UpdateDisplay("800x600, 800x600");
2801   auto* secondary_root_window_controller =
2802       Shell::Get()->GetAllRootWindowControllers()[1];
2803   EXPECT_FALSE(secondary_root_window_controller->wallpaper_widget_controller()
2804                    ->IsAnimating());
2805   EXPECT_FALSE(secondary_root_window_controller->wallpaper_widget_controller()
2806                    ->GetWidget()
2807                    ->GetLayer()
2808                    ->GetAnimator()
2809                    ->is_animating());
2810 }
2811 
2812 }  // namespace ash
2813