forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwallpaper_controller_unittest.cc
2812 lines (2465 loc) · 118 KB
/
wallpaper_controller_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wallpaper/wallpaper_controller_impl.h"
#include <cmath>
#include <cstdlib>
#include "ash/public/cpp/ash_pref_names.h"
#include "ash/public/cpp/ash_switches.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/public/cpp/wallpaper_controller_client.h"
#include "ash/public/cpp/wallpaper_controller_observer.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/session/test_session_controller_client.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_resizer.h"
#include "ash/wallpaper/wallpaper_view.h"
#include "ash/wallpaper/wallpaper_widget_controller.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/window_cycle_controller.h"
#include "ash/wm/window_state.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/task/current_thread.h"
#include "base/task/post_task.h"
#include "base/task/task_observer.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/bind.h"
#include "base/time/time_override.h"
#include "chromeos/constants/chromeos_switches.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_names.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/aura/window.h"
#include "ui/compositor/layer_tree_owner.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/test/layer_animator_test_controller.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/codec/jpeg_codec.h"
#include "ui/views/widget/widget.h"
using session_manager::SessionState;
namespace ash {
namespace {
// Containers IDs used for tests.
constexpr int kWallpaperId = kShellWindowId_WallpaperContainer;
constexpr int kLockScreenWallpaperId =
kShellWindowId_LockScreenWallpaperContainer;
constexpr int kAlwaysOnTopWallpaperId =
kShellWindowId_AlwaysOnTopWallpaperContainer;
constexpr char kDefaultSmallWallpaperName[] = "small.jpg";
constexpr char kDefaultLargeWallpaperName[] = "large.jpg";
constexpr char kGuestSmallWallpaperName[] = "guest_small.jpg";
constexpr char kGuestLargeWallpaperName[] = "guest_large.jpg";
constexpr char kChildSmallWallpaperName[] = "child_small.jpg";
constexpr char kChildLargeWallpaperName[] = "child_large.jpg";
// Colors used to distinguish between wallpapers with large and small
// resolution.
constexpr SkColor kLargeCustomWallpaperColor = SK_ColorDKGRAY;
constexpr SkColor kSmallCustomWallpaperColor = SK_ColorLTGRAY;
// A color that can be passed to |CreateImage|. Specifically chosen to not
// conflict with any of the custom wallpaper colors.
constexpr SkColor kWallpaperColor = SK_ColorMAGENTA;
std::string GetDummyFileId(const AccountId& account_id) {
return account_id.GetUserEmail() + "-hash";
}
std::string GetDummyFileName(const AccountId& account_id) {
return account_id.GetUserEmail() + "-file";
}
constexpr char kUser1[] = "user1@test.com";
const AccountId account_id_1 = AccountId::FromUserEmail(kUser1);
const std::string wallpaper_files_id_1 = GetDummyFileId(account_id_1);
const std::string file_name_1 = GetDummyFileName(account_id_1);
constexpr char kUser2[] = "user2@test.com";
const AccountId account_id_2 = AccountId::FromUserEmail(kUser2);
const std::string wallpaper_files_id_2 = GetDummyFileId(account_id_2);
const std::string file_name_2 = GetDummyFileName(account_id_2);
const std::string kDummyUrl = "https://best_wallpaper/1";
const std::string kDummyUrl2 = "https://best_wallpaper/2";
// Creates an image of size |size|.
gfx::ImageSkia CreateImage(int width, int height, SkColor color) {
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
bitmap.eraseColor(color);
gfx::ImageSkia image = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
return image;
}
// Returns number of child windows in a shell window container.
int ChildCountForContainer(int container_id) {
aura::Window* root = Shell::Get()->GetPrimaryRootWindow();
aura::Window* container = root->GetChildById(container_id);
return static_cast<int>(container->children().size());
}
// Steps a layer animation until it is completed. Animations must be enabled.
void RunAnimationForLayer(ui::Layer* layer) {
// Animations must be enabled for stepping to work.
ASSERT_NE(ui::ScopedAnimationDurationScaleMode::duration_multiplier(),
ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
ui::LayerAnimatorTestController controller(layer->GetAnimator());
// Multiple steps are required to complete complex animations.
// TODO(vollick): This should not be necessary. crbug.com/154017
while (controller.animator()->is_animating()) {
controller.StartThreadedAnimationsIfNeeded();
base::TimeTicks step_time = controller.animator()->last_step_time();
layer->GetAnimator()->Step(step_time +
base::TimeDelta::FromMilliseconds(1000));
}
}
// Writes a JPEG image of the specified size and color to |path|. Returns true
// on success.
bool WriteJPEGFile(const base::FilePath& path,
int width,
int height,
SkColor color) {
base::ScopedAllowBlockingForTesting allow_blocking;
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
bitmap.eraseColor(color);
std::vector<unsigned char> output;
if (!gfx::JPEGCodec::Encode(bitmap, 80 /*quality*/, &output)) {
LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
return false;
}
size_t bytes_written = base::WriteFile(
path, reinterpret_cast<const char*>(&output[0]), output.size());
if (bytes_written != output.size()) {
LOG(ERROR) << "Wrote " << bytes_written << " byte(s) instead of "
<< output.size() << " to " << path.value();
return false;
}
return true;
}
// Returns custom wallpaper path. Creates the directory if it doesn't exist.
base::FilePath GetCustomWallpaperPath(const char* sub_dir,
const std::string& wallpaper_files_id,
const std::string& file_name) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath wallpaper_path =
WallpaperControllerImpl::GetCustomWallpaperPath(
sub_dir, wallpaper_files_id, file_name);
if (!base::DirectoryExists(wallpaper_path.DirName()))
base::CreateDirectory(wallpaper_path.DirName());
return wallpaper_path;
}
void WaitUntilCustomWallpapersDeleted(const AccountId& account_id) {
const std::string wallpaper_file_id = GetDummyFileId(account_id);
base::FilePath small_wallpaper_dir =
WallpaperControllerImpl::GetCustomWallpaperDir(
WallpaperControllerImpl::kSmallWallpaperSubDir)
.Append(wallpaper_file_id);
base::FilePath large_wallpaper_dir =
WallpaperControllerImpl::GetCustomWallpaperDir(
WallpaperControllerImpl::kLargeWallpaperSubDir)
.Append(wallpaper_file_id);
base::FilePath original_wallpaper_dir =
WallpaperControllerImpl::GetCustomWallpaperDir(
WallpaperControllerImpl::kOriginalWallpaperSubDir)
.Append(wallpaper_file_id);
while (base::PathExists(small_wallpaper_dir) ||
base::PathExists(large_wallpaper_dir) ||
base::PathExists(original_wallpaper_dir)) {
}
}
// Monitors if any task is processed by the message loop.
class TaskObserver : public base::TaskObserver {
public:
TaskObserver() : processed_(false) {}
~TaskObserver() override = default;
// TaskObserver:
void WillProcessTask(const base::PendingTask& /* pending_task */,
bool /* was_blocked_or_low_priority */) override {}
void DidProcessTask(const base::PendingTask& pending_task) override {
processed_ = true;
}
// Returns true if any task was processed.
bool processed() const { return processed_; }
private:
bool processed_;
DISALLOW_COPY_AND_ASSIGN(TaskObserver);
};
// See content::RunAllTasksUntilIdle().
void RunAllTasksUntilIdle() {
while (true) {
TaskObserver task_observer;
base::CurrentThread::Get()->AddTaskObserver(&task_observer);
// May spin message loop.
base::ThreadPoolInstance::Get()->FlushForTesting();
base::RunLoop().RunUntilIdle();
base::CurrentThread::Get()->RemoveTaskObserver(&task_observer);
if (!task_observer.processed())
break;
}
}
// A test wallpaper controller client class.
class TestWallpaperControllerClient : public WallpaperControllerClient {
public:
TestWallpaperControllerClient() = default;
virtual ~TestWallpaperControllerClient() = default;
size_t open_count() const { return open_count_; }
size_t close_preview_count() const { return close_preview_count_; }
// WallpaperControllerClient:
void OpenWallpaperPicker() override { open_count_++; }
void MaybeClosePreviewWallpaper() override { close_preview_count_++; }
private:
size_t open_count_ = 0;
size_t close_preview_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(TestWallpaperControllerClient);
};
// A test implementation of the WallpaperControllerObserver interface.
class TestWallpaperControllerObserver : public WallpaperControllerObserver {
public:
explicit TestWallpaperControllerObserver(WallpaperController* controller)
: controller_(controller) {
controller_->AddObserver(this);
}
~TestWallpaperControllerObserver() override {
controller_->RemoveObserver(this);
}
// WallpaperControllerObserver
void OnWallpaperColorsChanged() override { ++colors_changed_count_; }
void OnWallpaperBlurChanged() override { ++blur_changed_count_; }
void OnFirstWallpaperShown() override { ++first_shown_count_; }
int colors_changed_count() const { return colors_changed_count_; }
int blur_changed_count() const { return blur_changed_count_; }
int first_shown_count() const { return first_shown_count_; }
private:
WallpaperController* controller_;
int colors_changed_count_ = 0;
int blur_changed_count_ = 0;
int first_shown_count_ = 0;
DISALLOW_COPY_AND_ASSIGN(TestWallpaperControllerObserver);
};
} // namespace
class WallpaperControllerTest : public AshTestBase {
public:
WallpaperControllerTest() = default;
~WallpaperControllerTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
auto fake_user_manager = std::make_unique<user_manager::FakeUserManager>();
fake_user_manager_ = fake_user_manager.get();
scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
std::move(fake_user_manager));
// This is almost certainly not what was originally intended for these
// tests, but they have never actually exercised properly decoded
// wallpapers, as they've never actually been connected to a Data Decoder.
// We simulate a "crashing" ImageDcoder to get the behavior the tests were
// written around, but at some point they should probably be fixed.
in_process_data_decoder_.service().SimulateImageDecoderCrashForTesting(
true);
controller_ = Shell::Get()->wallpaper_controller();
controller_->set_wallpaper_reload_no_delay_for_test();
ASSERT_TRUE(user_data_dir_.CreateUniqueTempDir());
ASSERT_TRUE(online_wallpaper_dir_.CreateUniqueTempDir());
ASSERT_TRUE(custom_wallpaper_dir_.CreateUniqueTempDir());
base::FilePath policy_wallpaper;
controller_->Init(user_data_dir_.GetPath(), online_wallpaper_dir_.GetPath(),
custom_wallpaper_dir_.GetPath(), policy_wallpaper);
}
WallpaperView* wallpaper_view() {
return Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller()
->wallpaper_view();
}
protected:
// Helper function that tests the wallpaper is always fitted to the native
// display resolution when the layout is WALLPAPER_LAYOUT_CENTER.
void WallpaperFitToNativeResolution(WallpaperView* view,
float device_scale_factor,
int image_width,
int image_height,
SkColor color) {
gfx::Size size = view->bounds().size();
gfx::Canvas canvas(size, device_scale_factor, true);
view->OnPaint(&canvas);
SkBitmap bitmap = canvas.GetBitmap();
int bitmap_width = bitmap.width();
int bitmap_height = bitmap.height();
for (int i = 0; i < bitmap_width; i++) {
for (int j = 0; j < bitmap_height; j++) {
if (i >= (bitmap_width - image_width) / 2 &&
i < (bitmap_width + image_width) / 2 &&
j >= (bitmap_height - image_height) / 2 &&
j < (bitmap_height + image_height) / 2) {
EXPECT_EQ(color, bitmap.getColor(i, j));
} else {
EXPECT_EQ(SK_ColorBLACK, bitmap.getColor(i, j));
}
}
}
}
// Runs AnimatingWallpaperWidgetController's animation to completion.
void RunDesktopControllerAnimation() {
WallpaperWidgetController* controller =
Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller();
ASSERT_TRUE(controller);
ui::LayerTreeOwner* owner = controller->old_layer_tree_owner_for_testing();
if (!owner)
return;
ASSERT_NO_FATAL_FAILURE(RunAnimationForLayer(owner->root()));
}
// Convenience function to ensure ShouldCalculateColors() returns true.
void EnableShelfColoring() {
const gfx::ImageSkia kImage = CreateImage(10, 10, kWallpaperColor);
controller_->ShowWallpaperImage(
kImage, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
/*preview_mode=*/false, /*always_on_top=*/false);
SetSessionState(SessionState::ACTIVE);
EXPECT_TRUE(ShouldCalculateColors());
}
// Convenience function to set the SessionState.
void SetSessionState(SessionState session_state) {
GetSessionControllerClient()->SetSessionState(session_state);
}
// Helper function to create a |WallpaperInfo| struct with dummy values
// given the desired layout.
WallpaperInfo CreateWallpaperInfo(WallpaperLayout layout) {
return WallpaperInfo("", layout, DEFAULT,
base::Time::Now().LocalMidnight());
}
// Saves images with different resolution to corresponding paths and saves
// wallpaper info to local state, so that subsequent calls of |ShowWallpaper|
// can retrieve the images and info.
void CreateAndSaveWallpapers(const AccountId& account_id) {
std::string wallpaper_files_id = GetDummyFileId(account_id);
std::string file_name = GetDummyFileName(account_id);
base::FilePath small_wallpaper_path =
GetCustomWallpaperPath(WallpaperControllerImpl::kSmallWallpaperSubDir,
wallpaper_files_id, file_name);
base::FilePath large_wallpaper_path =
GetCustomWallpaperPath(WallpaperControllerImpl::kLargeWallpaperSubDir,
wallpaper_files_id, file_name);
// Saves the small/large resolution wallpapers to small/large custom
// wallpaper paths.
ASSERT_TRUE(WriteJPEGFile(small_wallpaper_path, kSmallWallpaperMaxWidth,
kSmallWallpaperMaxHeight,
kSmallCustomWallpaperColor));
ASSERT_TRUE(WriteJPEGFile(large_wallpaper_path, kLargeWallpaperMaxWidth,
kLargeWallpaperMaxHeight,
kLargeCustomWallpaperColor));
std::string relative_path =
base::FilePath(wallpaper_files_id).Append(file_name).value();
// Saves wallpaper info to local state for user.
WallpaperInfo info = {relative_path, WALLPAPER_LAYOUT_CENTER_CROPPED,
CUSTOMIZED, base::Time::Now().LocalMidnight()};
ASSERT_TRUE(controller_->SetUserWallpaperInfo(account_id, info));
}
// Simulates setting a custom wallpaper by directly setting the wallpaper
// info.
void SimulateSettingCustomWallpaper(const AccountId& account_id) {
ASSERT_TRUE(controller_->SetUserWallpaperInfo(
account_id,
WallpaperInfo("dummy_file_location", WALLPAPER_LAYOUT_CENTER,
CUSTOMIZED, base::Time::Now().LocalMidnight())));
}
// Initializes default wallpaper paths "*default_*file" and writes JPEG
// wallpaper images to them. Only needs to be called (once) by tests that
// want to test loading of default wallpapers.
void CreateDefaultWallpapers() {
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(default_wallpaper_dir_.CreateUniqueTempDir());
const base::FilePath default_wallpaper_path =
default_wallpaper_dir_.GetPath();
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
const base::FilePath small_file =
default_wallpaper_path.Append(kDefaultSmallWallpaperName);
command_line->AppendSwitchASCII(chromeos::switches::kDefaultWallpaperSmall,
small_file.value());
const base::FilePath large_file =
default_wallpaper_path.Append(kDefaultLargeWallpaperName);
command_line->AppendSwitchASCII(chromeos::switches::kDefaultWallpaperLarge,
large_file.value());
const base::FilePath guest_small_file =
default_wallpaper_path.Append(kGuestSmallWallpaperName);
command_line->AppendSwitchASCII(chromeos::switches::kGuestWallpaperSmall,
guest_small_file.value());
const base::FilePath guest_large_file =
default_wallpaper_path.Append(kGuestLargeWallpaperName);
command_line->AppendSwitchASCII(chromeos::switches::kGuestWallpaperLarge,
guest_large_file.value());
const base::FilePath child_small_file =
default_wallpaper_path.Append(kChildSmallWallpaperName);
command_line->AppendSwitchASCII(chromeos::switches::kChildWallpaperSmall,
child_small_file.value());
const base::FilePath child_large_file =
default_wallpaper_path.Append(kChildLargeWallpaperName);
command_line->AppendSwitchASCII(chromeos::switches::kChildWallpaperLarge,
child_large_file.value());
const int kWallpaperSize = 2;
ASSERT_TRUE(WriteJPEGFile(small_file, kWallpaperSize, kWallpaperSize,
kWallpaperColor));
ASSERT_TRUE(WriteJPEGFile(large_file, kWallpaperSize, kWallpaperSize,
kWallpaperColor));
ASSERT_TRUE(WriteJPEGFile(guest_small_file, kWallpaperSize, kWallpaperSize,
kWallpaperColor));
ASSERT_TRUE(WriteJPEGFile(guest_large_file, kWallpaperSize, kWallpaperSize,
kWallpaperColor));
ASSERT_TRUE(WriteJPEGFile(child_small_file, kWallpaperSize, kWallpaperSize,
kWallpaperColor));
ASSERT_TRUE(WriteJPEGFile(child_large_file, kWallpaperSize, kWallpaperSize,
kWallpaperColor));
}
// A helper to test the behavior of setting online wallpaper after the image
// is decoded. This is needed because image decoding is not supported in unit
// tests.
void SetOnlineWallpaperFromImage(
const AccountId& account_id,
const gfx::ImageSkia& image,
const std::string& url,
WallpaperLayout layout,
bool save_file,
bool preview_mode,
WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback callback) {
const WallpaperControllerImpl::OnlineWallpaperParams params = {
account_id, url, layout, preview_mode};
controller_->OnOnlineWallpaperDecoded(params, save_file,
std::move(callback), image);
}
// Returns color of the current wallpaper. Note: this function assumes the
// wallpaper has a solid color.
SkColor GetWallpaperColor() {
const gfx::ImageSkiaRep& representation =
controller_->GetWallpaper().GetRepresentation(1.0f);
return representation.GetBitmap().getColor(0, 0);
}
// Wrapper for private ShouldCalculateColors().
bool ShouldCalculateColors() { return controller_->ShouldCalculateColors(); }
// Wrapper for private IsDevicePolicyWallpaper().
bool IsDevicePolicyWallpaper() {
return controller_->IsDevicePolicyWallpaper();
}
int GetWallpaperCount() { return controller_->wallpaper_count_for_testing_; }
const std::vector<base::FilePath>& GetDecodeFilePaths() {
return controller_->decode_requests_for_testing_;
}
void SetBypassDecode() { controller_->set_bypass_decode_for_testing(); }
void ClearWallpaperCount() { controller_->wallpaper_count_for_testing_ = 0; }
void ClearDecodeFilePaths() {
controller_->decode_requests_for_testing_.clear();
}
void ClearWallpaper() { controller_->current_wallpaper_.reset(); }
int GetWallpaperContainerId() {
return controller_->GetWallpaperContainerId(controller_->locked_);
}
WallpaperControllerImpl* controller_ = nullptr; // Not owned.
base::ScopedTempDir user_data_dir_;
base::ScopedTempDir online_wallpaper_dir_;
base::ScopedTempDir custom_wallpaper_dir_;
base::ScopedTempDir default_wallpaper_dir_;
user_manager::FakeUserManager* fake_user_manager_ = nullptr;
std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;
private:
data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
DISALLOW_COPY_AND_ASSIGN(WallpaperControllerTest);
};
TEST_F(WallpaperControllerTest, Client) {
TestWallpaperControllerClient client;
controller_->SetClient(&client);
base::FilePath empty_path;
controller_->Init(empty_path, empty_path, empty_path, empty_path);
EXPECT_EQ(0u, client.open_count());
EXPECT_TRUE(controller_->CanOpenWallpaperPicker());
controller_->OpenWallpaperPickerIfAllowed();
EXPECT_EQ(1u, client.open_count());
}
TEST_F(WallpaperControllerTest, BasicReparenting) {
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
// Wallpaper view/window exists in the wallpaper container and nothing is in
// the lock screen wallpaper container.
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
controller->OnSessionStateChanged(session_manager::SessionState::LOCKED);
// One window is moved from desktop to lock container.
EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId));
controller->OnSessionStateChanged(session_manager::SessionState::ACTIVE);
// One window is moved from lock to desktop container.
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
}
TEST_F(WallpaperControllerTest, SwitchWallpapersWhenNewWallpaperAnimationEnds) {
// We cannot short-circuit animations for this test.
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
// Create the wallpaper and its view.
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
// The new wallpaper is ready to animate.
WallpaperWidgetController* widget_controller =
Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller();
EXPECT_TRUE(widget_controller->IsAnimating());
// Force the animation to play to completion.
RunDesktopControllerAnimation();
EXPECT_FALSE(widget_controller->IsAnimating());
}
// Test for crbug.com/149043 "Unlock screen, no launcher appears". Ensure we
// move all wallpaper views if there are more than one.
TEST_F(WallpaperControllerTest, WallpaperMovementDuringUnlock) {
// We cannot short-circuit animations for this test.
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
// Reset wallpaper state, see ControllerOwnership above.
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
// Run wallpaper show animation to completion.
RunDesktopControllerAnimation();
// User locks the screen, which moves the wallpaper forward.
controller->OnSessionStateChanged(session_manager::SessionState::LOCKED);
// Suspend/resume cycle causes wallpaper to refresh, loading a new wallpaper
// that will animate in on top of the old one.
controller->CreateEmptyWallpaperForTesting();
// In this state we have a wallpaper views stored in
// LockScreenWallpaperContainer.
WallpaperWidgetController* widget_controller =
Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller();
EXPECT_TRUE(widget_controller->IsAnimating());
EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId));
// There must be three layers, shield, original and old layers.
ASSERT_EQ(3u, wallpaper_view()->layer()->parent()->children().size());
// Before the wallpaper's animation completes, user unlocks the screen, which
// moves the wallpaper to the back.
controller->OnSessionStateChanged(session_manager::SessionState::ACTIVE);
// Ensure that widget has moved.
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
// There must be two layers, original and old layers while animating.
ASSERT_EQ(2u, wallpaper_view()->layer()->parent()->children().size());
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
// Finish the new wallpaper animation.
RunDesktopControllerAnimation();
// Now there is one wallpaper and layer.
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
ASSERT_EQ(1u, wallpaper_view()->layer()->parent()->children().size());
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
}
// Test for crbug.com/156542. Animating wallpaper should immediately finish
// animation and replace current wallpaper before next animation starts.
TEST_F(WallpaperControllerTest, ChangeWallpaperQuick) {
// We cannot short-circuit animations for this test.
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
// Reset wallpaper state, see ControllerOwnership above.
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
// Run wallpaper show animation to completion.
RunDesktopControllerAnimation();
// Change to a new wallpaper.
controller->CreateEmptyWallpaperForTesting();
WallpaperWidgetController* widget_controller =
Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller();
EXPECT_TRUE(widget_controller->IsAnimating());
// Change to another wallpaper before animation finished.
controller->CreateEmptyWallpaperForTesting();
// Run wallpaper show animation to completion.
RunDesktopControllerAnimation();
EXPECT_FALSE(widget_controller->IsAnimating());
}
TEST_F(WallpaperControllerTest, ResizeCustomWallpaper) {
UpdateDisplay("320x200");
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
// Set the image as custom wallpaper, wait for the resize to finish, and check
// that the resized image is the expected size.
controller_->ShowWallpaperImage(
image, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
/*preview_mode=*/false, /*always_on_top=*/false);
EXPECT_TRUE(image.BackedBySameObjectAs(controller_->GetWallpaper()));
RunAllTasksUntilIdle();
gfx::ImageSkia resized_image = controller_->GetWallpaper();
EXPECT_FALSE(image.BackedBySameObjectAs(resized_image));
EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString());
// Load the original wallpaper again and check that we're still using the
// previously-resized image instead of doing another resize
// (http://crbug.com/321402).
controller_->ShowWallpaperImage(
image, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
/*preview_mode=*/false, /*always_on_top=*/false);
RunAllTasksUntilIdle();
EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
}
TEST_F(WallpaperControllerTest, GetMaxDisplaySize) {
// Device scale factor shouldn't affect the native size.
UpdateDisplay("1000x300*2");
EXPECT_EQ("1000x300",
WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
// Rotated display should return the rotated size.
UpdateDisplay("1000x300*2/r");
EXPECT_EQ("300x1000",
WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
// UI Scaling shouldn't affect the native size.
UpdateDisplay("1000x300*2@1.5");
EXPECT_EQ("1000x300",
WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
// First display has maximum size.
UpdateDisplay("400x300,100x100");
EXPECT_EQ("400x300",
WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
// Second display has maximum size.
UpdateDisplay("400x300,500x600");
EXPECT_EQ("500x600",
WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
// Maximum width and height belongs to different displays.
UpdateDisplay("400x300,100x500");
EXPECT_EQ("400x500",
WallpaperControllerImpl::GetMaxDisplaySizeInNative().ToString());
}
// Test that the wallpaper is always fitted to the native display resolution
// when the layout is WALLPAPER_LAYOUT_CENTER to prevent blurry images.
TEST_F(WallpaperControllerTest, DontScaleWallpaperWithCenterLayout) {
// We cannot short-circuit animations for this test.
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
const gfx::Size high_resolution(3600, 2400);
const gfx::Size low_resolution(360, 240);
const float high_dsf = 2.0f;
const float low_dsf = 1.0f;
gfx::ImageSkia image_high_res = CreateImage(
high_resolution.width(), high_resolution.height(), kWallpaperColor);
gfx::ImageSkia image_low_res = CreateImage(
low_resolution.width(), low_resolution.height(), kWallpaperColor);
UpdateDisplay("1200x600*2");
{
SCOPED_TRACE(base::StringPrintf("1200x600*2 high resolution"));
controller_->ShowWallpaperImage(
image_high_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
/*preview_mode=*/false, /*always_on_top=*/false);
WallpaperFitToNativeResolution(wallpaper_view(), high_dsf,
high_resolution.width(),
high_resolution.height(), kWallpaperColor);
}
{
SCOPED_TRACE(base::StringPrintf("1200x600*2 low resolution"));
controller_->ShowWallpaperImage(
image_low_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
/*preview_mode=*/false, /*always_on_top=*/false);
WallpaperFitToNativeResolution(wallpaper_view(), high_dsf,
low_resolution.width(),
low_resolution.height(), kWallpaperColor);
}
UpdateDisplay("1200x600");
{
SCOPED_TRACE(base::StringPrintf("1200x600 high resolution"));
controller_->ShowWallpaperImage(
image_high_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
/*preview_mode=*/false, /*always_on_top=*/false);
WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
high_resolution.width(),
high_resolution.height(), kWallpaperColor);
}
{
SCOPED_TRACE(base::StringPrintf("1200x600 low resolution"));
controller_->ShowWallpaperImage(
image_low_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
/*preview_mode=*/false, /*always_on_top=*/false);
WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
low_resolution.width(),
low_resolution.height(), kWallpaperColor);
}
UpdateDisplay("1200x600/u@1.5"); // 1.5 ui scale
{
SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 high resolution"));
controller_->ShowWallpaperImage(
image_high_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
/*preview_mode=*/false, /*always_on_top=*/false);
WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
high_resolution.width(),
high_resolution.height(), kWallpaperColor);
}
{
SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 low resolution"));
controller_->ShowWallpaperImage(
image_low_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
/*preview_mode=*/false, /*always_on_top=*/false);
WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
low_resolution.width(),
low_resolution.height(), kWallpaperColor);
}
}
TEST_F(WallpaperControllerTest, ShouldCalculateColorsBasedOnImage) {
EnableShelfColoring();
EXPECT_TRUE(ShouldCalculateColors());
controller_->CreateEmptyWallpaperForTesting();
EXPECT_FALSE(ShouldCalculateColors());
}
TEST_F(WallpaperControllerTest, ShouldCalculateColorsBasedOnSessionState) {
EnableShelfColoring();
SetSessionState(SessionState::UNKNOWN);
EXPECT_FALSE(ShouldCalculateColors());
SetSessionState(SessionState::OOBE);
EXPECT_FALSE(ShouldCalculateColors());
SetSessionState(SessionState::LOGIN_PRIMARY);
EXPECT_FALSE(ShouldCalculateColors());
SetSessionState(SessionState::LOGGED_IN_NOT_ACTIVE);
EXPECT_FALSE(ShouldCalculateColors());
SetSessionState(SessionState::ACTIVE);
EXPECT_TRUE(ShouldCalculateColors());
SetSessionState(SessionState::LOCKED);
EXPECT_FALSE(ShouldCalculateColors());
SetSessionState(SessionState::LOGIN_SECONDARY);
EXPECT_FALSE(ShouldCalculateColors());
}
TEST_F(WallpaperControllerTest, EnableShelfColoringNotifiesObservers) {
TestWallpaperControllerObserver observer(controller_);
EXPECT_EQ(0, observer.colors_changed_count());
// Enable shelf coloring will set a customized wallpaper image and change
// session state to ACTIVE, which will trigger wallpaper colors calculation.
EnableShelfColoring();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, observer.colors_changed_count());
}
TEST_F(WallpaperControllerTest, SetCustomWallpaper) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
SimulateUserLogin(kUser1);
// Set a custom wallpaper for |kUser1|. Verify the wallpaper is set
// successfully and wallpaper info is updated.
ClearWallpaperCount();
controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
file_name_1, layout, image,
false /*preview_mode=*/);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(controller_->GetWallpaperType(), CUSTOMIZED);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(
base::FilePath(wallpaper_files_id_1).Append(file_name_1).value(), layout,
CUSTOMIZED, base::Time::Now().LocalMidnight());
EXPECT_EQ(wallpaper_info, expected_wallpaper_info);
// Now set another custom wallpaper for |kUser1|. Verify that the on-screen
// wallpaper doesn't change since |kUser1| is not active, but wallpaper info
// is updated properly.
SimulateUserLogin(kUser2);
const SkColor custom_wallpaper_color = SK_ColorCYAN;
image = CreateImage(640, 480, custom_wallpaper_color);
ClearWallpaperCount();
controller_->SetCustomWallpaper(account_id_1, wallpaper_files_id_1,
file_name_1, layout, image,
false /*preview_mode=*/);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
EXPECT_EQ(wallpaper_info, expected_wallpaper_info);
// Verify the updated wallpaper is shown after |kUser1| becomes active again.
SimulateUserLogin(kUser1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(account_id_1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(custom_wallpaper_color, GetWallpaperColor());
}
TEST_F(WallpaperControllerTest, SetOnlineWallpaper) {
SetBypassDecode();
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER_CROPPED;
SimulateUserLogin(kUser1);
// Verify that there's no offline wallpaper available in the beginning.
std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
controller_->GetOfflineWallpaperList(base::BindLambdaForTesting(
[&run_loop](const std::vector<std::string>& url_list) {
EXPECT_TRUE(url_list.empty());
run_loop->Quit();
}));
run_loop->Run();
// Verify that the attempt to set an online wallpaper without providing image
// data fails.
run_loop.reset(new base::RunLoop());
ClearWallpaperCount();
controller_->SetOnlineWallpaperIfExists(
account_id_1, kDummyUrl, layout, false /*preview_mode=*/,
base::BindLambdaForTesting([&run_loop](bool file_exists) {
EXPECT_FALSE(file_exists);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(0, GetWallpaperCount());
// Set an online wallpaper with image data. Verify that the wallpaper is set
// successfully.
ClearWallpaperCount();
controller_->SetOnlineWallpaperFromData(
account_id_1, std::string() /*image_data=*/, kDummyUrl, layout,
false /*preview_mode=*/,
WallpaperControllerImpl::SetOnlineWallpaperFromDataCallback());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), ONLINE);
// Verify that the user wallpaper info is updated.
WallpaperInfo wallpaper_info;
EXPECT_TRUE(controller_->GetUserWallpaperInfo(account_id_1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(kDummyUrl, layout, ONLINE,
base::Time::Now().LocalMidnight());
EXPECT_EQ(wallpaper_info, expected_wallpaper_info);
// Change the on-screen wallpaper to a different one. (Otherwise the
// subsequent calls will be no-op since we intentionally prevent reloading the
// same wallpaper.)
ClearWallpaperCount();
controller_->SetCustomWallpaper(
account_id_1, wallpaper_files_id_1, file_name_1, layout,
CreateImage(640, 480, kWallpaperColor), false /*preview_mode=*/);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), CUSTOMIZED);
// Attempt to set an online wallpaper without providing the image data. Verify
// it succeeds this time because |SetOnlineWallpaperFromData| has saved the
// file.
ClearWallpaperCount();
run_loop.reset(new base::RunLoop());
controller_->SetOnlineWallpaperIfExists(
account_id_1, kDummyUrl, layout, false /*preview_mode=*/,
base::BindLambdaForTesting([&run_loop](bool file_exists) {
EXPECT_TRUE(file_exists);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), ONLINE);
// Verify that the wallpaper with |url| is available offline, and the returned
// file name should not contain the small wallpaper suffix.
run_loop.reset(new base::RunLoop());
controller_->GetOfflineWallpaperList(base::BindLambdaForTesting(
[&run_loop](const std::vector<std::string>& url_list) {
EXPECT_EQ(1U, url_list.size());