#include <array>
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_paths.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/personalization_app/time_of_day_test_utils.h"
#include "ash/public/cpp/schedule_enums.h"
#include "ash/public/cpp/session/session_controller.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/test/in_process_data_decoder.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/public/cpp/wallpaper/online_wallpaper_params.h"
#include "ash/public/cpp/wallpaper/online_wallpaper_variant.h"
#include "ash/public/cpp/wallpaper/sea_pen_image.h"
#include "ash/public/cpp/wallpaper/wallpaper_controller_client.h"
#include "ash/public/cpp/wallpaper/wallpaper_controller_observer.h"
#include "ash/public/cpp/wallpaper/wallpaper_info.h"
#include "ash/public/cpp/wallpaper/wallpaper_types.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/style/dark_light_mode_controller_impl.h"
#include "ash/system/geolocation/geolocation_controller.h"
#include "ash/system/geolocation/geolocation_controller_test_util.h"
#include "ash/system/geolocation/test_geolocation_url_loader_factory.h"
#include "ash/system/time/calendar_unittest_utils.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_util.h"
#include "ash/wallpaper/sea_pen_wallpaper_manager.h"
#include "ash/wallpaper/test_sea_pen_wallpaper_manager_session_delegate.h"
#include "ash/wallpaper/test_wallpaper_controller_client.h"
#include "ash/wallpaper/test_wallpaper_drivefs_delegate.h"
#include "ash/wallpaper/test_wallpaper_image_downloader.h"
#include "ash/wallpaper/views/wallpaper_view.h"
#include "ash/wallpaper/views/wallpaper_widget_controller.h"
#include "ash/wallpaper/wallpaper_blur_manager.h"
#include "ash/wallpaper/wallpaper_constants.h"
#include "ash/wallpaper/wallpaper_controller_impl.h"
#include "ash/wallpaper/wallpaper_daily_refresh_scheduler.h"
#include "ash/wallpaper/wallpaper_metrics_manager.h"
#include "ash/wallpaper/wallpaper_pref_manager.h"
#include "ash/wallpaper/wallpaper_time_of_day_scheduler.h"
#include "ash/wallpaper/wallpaper_utils/sea_pen_metadata_utils.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_file_utils.h"
#include "ash/wallpaper/wallpaper_utils/wallpaper_resizer.h"
#include "ash/webui/common/mojom/sea_pen.mojom-forward.h"
#include "ash/webui/personalization_app/proto/backdrop_wallpaper.pb.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/window_cycle/window_cycle_controller.h"
#include "ash/wm/window_state.h"
#include "base/command_line.h"
#include "base/containers/flat_map.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_writer.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/metrics_hashes.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/task/current_thread.h"
#include "base/task/task_observer.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/repeating_test_future.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_path_override.h"
#include "base/test/simple_test_clock.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "base/test/values_test_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/time/time_override.h"
#include "chromeos/ash/components/demo_mode/utils/demo_session_utils.h"
#include "chromeos/ash/components/geolocation/location_fetcher.h"
#include "chromeos/ash/components/geolocation/system_location_provider.h"
#include "chromeos/ash/components/install_attributes/stub_install_attributes.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "chromeos/ash/components/system/statistics_provider.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/user_manager/user_names.h"
#include "components/user_manager/user_type.h"
#include "google_apis/gaia/gaia_id.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "services/data_decoder/public/mojom/image_decoder.mojom-shared.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gtest/include/gtest/gtest.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.h"
#include "ui/compositor/layer_tree_owner.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/gfx/color_analysis.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/scoped_animation_duration_scale_mode.h"
#include "ui/views/view_tracker.h"
#include "ui/views/widget/widget.h"
using session_manager::SessionState;
namespace ash {
namespace {
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";
constexpr char kCustomizationSmallWallpaperName[] = "small_customization.jpeg";
constexpr char kCustomizationLargeWallpaperName[] = "large_customization.jpeg";
constexpr SkColor kLargeCustomWallpaperColor = SK_ColorDKGRAY;
constexpr SkColor kSmallCustomWallpaperColor = SK_ColorLTGRAY;
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 kAccountId1 =
AccountId::FromUserEmailGaiaId(kUser1, GaiaId("1111"));
const std::string kWallpaperFilesId1 = GetDummyFileId(kAccountId1);
const std::string kFileName1 = GetDummyFileName(kAccountId1);
constexpr char kUser2[] = "user2@test.com";
const AccountId kAccountId2 =
AccountId::FromUserEmailGaiaId(kUser2, GaiaId("2222"));
const std::string kWallpaperFilesId2 = GetDummyFileId(kAccountId2);
const std::string kFileName2 = GetDummyFileName(kAccountId2);
constexpr char kChildEmail[] = "child@test.com";
constexpr char kDummyUrl[] = "https://best_wallpaper/1";
constexpr char kDummyUrl2[] = "https://best_wallpaper/2";
constexpr char kDummyUrl3[] = "https://best_wallpaper/3";
constexpr char kDummyUrl4[] = "https://best_wallpaper/4";
const uint64_t kAssetId = 1;
const uint64_t kAssetId2 = 2;
const uint64_t kAssetId3 = 3;
const uint64_t kAssetId4 = 4;
const uint64_t kUnitId = 1;
const uint64_t kUnitId2 = 2;
const std::string kFakeGooglePhotosAlbumId = "fake_album";
const std::string kFakeGooglePhotosPhotoId = "fake_photo";
MATCHER_P(WallpaperChangeTimeNear, hours_elapsed_since_test_start, "") {
static constexpr base::TimeDelta kTolerance = base::Minutes(5);
base::TimeDelta expected_duration_since_test_start =
base::Hours(hours_elapsed_since_test_start);
return expected_duration_since_test_start - kTolerance <= arg &&
arg <= expected_duration_since_test_start + kTolerance;
}
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;
}
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());
}
void RunAnimationForLayer(ui::Layer* layer) {
ASSERT_NE(gfx::ScopedAnimationDurationScaleMode::duration_multiplier(),
gfx::ScopedAnimationDurationScaleMode::ZERO_DURATION);
ui::LayerAnimatorTestController controller(layer->GetAnimator());
while (controller.animator()->is_animating()) {
controller.StartThreadedAnimationsIfNeeded();
base::TimeTicks step_time = controller.animator()->last_step_time();
layer->GetAnimator()->Step(step_time + base::Milliseconds(1000));
}
}
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::optional<std::vector<uint8_t>> output =
gfx::JPEGCodec::Encode(bitmap, 80 );
if (!output) {
LOG(ERROR) << "Unable to encode " << width << "x" << height << " bitmap";
return false;
}
if (!base::WriteFile(path, output.value())) {
LOG(ERROR) << "Writing to " << path.value() << " failed.";
return false;
}
return true;
}
base::FilePath GetCustomWallpaperPath(const WallpaperControllerImpl& controller,
const char* sub_dir,
const std::string& wallpaper_files_id,
const std::string& file_name) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath wallpaper_path =
controller.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 WallpaperControllerImpl& controller,
const AccountId& account_id) {
const std::string wallpaper_file_id = GetDummyFileId(account_id);
base::FilePath small_wallpaper_dir =
controller.GetCustomWallpaperDir(kSmallWallpaperSubDir)
.Append(wallpaper_file_id);
base::FilePath large_wallpaper_dir =
controller.GetCustomWallpaperDir(kLargeWallpaperSubDir)
.Append(wallpaper_file_id);
base::FilePath original_wallpaper_dir =
controller.GetCustomWallpaperDir(kOriginalWallpaperSubDir)
.Append(wallpaper_file_id);
while (base::PathExists(small_wallpaper_dir) ||
base::PathExists(large_wallpaper_dir) ||
base::PathExists(original_wallpaper_dir)) {
}
}
class TaskObserver : public base::TaskObserver {
public:
TaskObserver() : processed_(false) {}
TaskObserver(const TaskObserver&) = delete;
TaskObserver& operator=(const TaskObserver&) = delete;
~TaskObserver() override = default;
void WillProcessTask(const base::PendingTask& ,
bool ) override {}
void DidProcessTask(const base::PendingTask& pending_task) override {
processed_ = true;
}
bool processed() const { return processed_; }
private:
bool processed_;
};
void RunAllTasksUntilIdle() {
while (true) {
TaskObserver task_observer;
base::CurrentThread::Get()->AddTaskObserver(&task_observer);
base::ThreadPoolInstance::Get()->FlushForTesting();
base::RunLoop().RunUntilIdle();
base::CurrentThread::Get()->RemoveTaskObserver(&task_observer);
if (!task_observer.processed())
break;
}
}
PrefService* GetProfilePrefService(const AccountId& account_id) {
return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
account_id);
}
WallpaperInfo InfoWithType(WallpaperType type) {
WallpaperInfo info(std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, type,
base::Time::Now());
if (IsOnlineWallpaper(type)) {
info.unit_id = 1234;
info.collection_id = "placeholder collection";
info.location = "https://example.com/example.jpeg";
}
if (type == WallpaperType::kOnceGooglePhotos)
info.dedup_key = "dedup_key";
return info;
}
base::Time DayBeforeYesterdayish() {
base::TimeDelta today_delta =
base::Time::Now().LocalMidnight().ToDeltaSinceWindowsEpoch();
base::TimeDelta yesterday_delta = today_delta - base::Days(2);
return base::Time::FromDeltaSinceWindowsEpoch(yesterday_delta);
}
class TestWallpaperControllerObserver : public WallpaperControllerObserver {
public:
explicit TestWallpaperControllerObserver(WallpaperController* controller)
: controller_(controller) {
controller_->AddObserver(this);
}
TestWallpaperControllerObserver(const TestWallpaperControllerObserver&) =
delete;
TestWallpaperControllerObserver& operator=(
const TestWallpaperControllerObserver&) = delete;
~TestWallpaperControllerObserver() override {
controller_->RemoveObserver(this);
}
void SetOnResizeCallback(base::RepeatingClosure callback) {
resize_callback_ = callback;
}
void SetOnColorsCalculatedCallback(base::RepeatingClosure callback) {
colors_calculated_callback_ = callback;
}
void OnWallpaperChanged() override { ++wallpaper_changed_count_; }
void OnWallpaperResized() override {
if (resize_callback_) {
resize_callback_.Run();
}
}
void OnWallpaperColorsChanged() override {
++colors_changed_count_;
if (colors_calculated_callback_) {
colors_calculated_callback_.Run();
}
}
void OnWallpaperBlurChanged() override { ++blur_changed_count_; }
void OnFirstWallpaperShown() override { ++first_shown_count_; }
void OnWallpaperPreviewStarted() override {
DCHECK(!is_in_wallpaper_preview_);
is_in_wallpaper_preview_ = true;
}
void OnWallpaperPreviewEnded() override {
DCHECK(is_in_wallpaper_preview_);
is_in_wallpaper_preview_ = false;
}
void OnDailyRefreshCheckpointChanged() override {
++daily_refresh_checkpoint_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_; }
int wallpaper_changed_count() const { return wallpaper_changed_count_; }
int daily_refresh_checkpoint_count() const {
return daily_refresh_checkpoint_count_;
}
bool is_in_wallpaper_preview() const { return is_in_wallpaper_preview_; }
void ClearDailyRefreshCheckpointCount() {
daily_refresh_checkpoint_count_ = 0;
}
private:
raw_ptr<WallpaperController> controller_;
base::RepeatingClosure resize_callback_;
base::RepeatingClosure colors_calculated_callback_;
int colors_changed_count_ = 0;
int blur_changed_count_ = 0;
int first_shown_count_ = 0;
int wallpaper_changed_count_ = 0;
int daily_refresh_checkpoint_count_ = 0;
bool is_in_wallpaper_preview_ = false;
};
class WallpaperChangedBarrier : public WallpaperControllerObserver {
public:
WallpaperChangedBarrier(WallpaperController* controller,
base::test::TaskEnvironment* task_environment)
: task_environment_(task_environment) {
CHECK(task_environment_);
controller_observation_.Observe(controller);
}
WallpaperChangedBarrier(const WallpaperChangedBarrier&) = delete;
WallpaperChangedBarrier& operator=(const WallpaperChangedBarrier&) = delete;
~WallpaperChangedBarrier() override = default;
void OnWallpaperChanged() override { wallpaper_changed_ = true; }
bool RunUntilNextWallpaperChange() {
wallpaper_changed_ = false;
while (!wallpaper_changed_) {
RunAllTasksUntilIdle();
base::TimeDelta delay_until_next_task =
task_environment_->NextMainThreadPendingTaskDelay();
if (delay_until_next_task == base::TimeDelta::Max()) {
return false;
}
task_environment_->FastForwardBy(delay_until_next_task);
}
return true;
}
private:
base::ScopedObservation<WallpaperController, WallpaperControllerObserver>
controller_observation_{this};
const raw_ptr<base::test::TaskEnvironment> task_environment_;
bool wallpaper_changed_ = false;
};
const backdrop::Image* GetImageMatchingUrl(
const GURL& url,
const std::vector<backdrop::Image>& backdrop_image_data) {
for (const backdrop::Image& image : backdrop_image_data) {
if (image.image_url() == url.spec()) {
return ℑ
}
}
return nullptr;
}
std::vector<backdrop::Image> TimeOfDayImageSet() {
const std::vector<backdrop::Image_ImageType> image_types = {
backdrop::Image::IMAGE_TYPE_LIGHT_MODE,
backdrop::Image::IMAGE_TYPE_MORNING_MODE,
backdrop::Image::IMAGE_TYPE_LATE_AFTERNOON_MODE,
backdrop::Image::IMAGE_TYPE_DARK_MODE};
std::vector<backdrop::Image> images;
uint64_t current_asset_id = 99;
for (const auto unit_id :
{wallpaper_constants::kDefaultTimeOfDayWallpaperUnitId,
wallpaper_constants::kAlternateTimeOfDayWallpaperUnitId}) {
for (const auto image_type : image_types) {
const uint64_t asset_id = current_asset_id;
current_asset_id++;
const std::string url = base::StringPrintf(
"https://preferred_wallpaper/images/%zu", asset_id);
backdrop::Image image;
image.set_asset_id(asset_id);
image.set_unit_id(unit_id);
image.set_image_type(image_type);
image.set_image_url(url);
images.push_back(image);
}
}
return images;
}
std::vector<backdrop::Image> ImageSet() {
const size_t image_size = 10;
const auto base_asset_id = rand() % 100;
std::vector<backdrop::Image> images;
for (size_t i = 0; i < image_size; ++i) {
const uint64_t asset_id = i + base_asset_id;
const std::string url =
base::StringPrintf("https://preferred_wallpaper/images/%zu", asset_id);
backdrop::Image image;
image.set_asset_id(asset_id);
image.set_unit_id(asset_id);
image.set_image_type(backdrop::Image::IMAGE_TYPE_UNKNOWN);
image.set_image_url(url);
images.push_back(image);
}
return images;
}
enum class TimeOfDayFeatureCombination { kDisabled, kTimeOfDay };
void InitTimeOfDayFeatureList(
TimeOfDayFeatureCombination time_of_day,
base::test::ScopedFeatureList& scoped_feature_list) {
std::vector<base::test::FeatureRef> enabled_features;
std::vector<base::test::FeatureRef> disabled_features;
switch (time_of_day) {
case TimeOfDayFeatureCombination::kDisabled:
disabled_features = personalization_app::GetTimeOfDayFeatures();
break;
case TimeOfDayFeatureCombination::kTimeOfDay:
enabled_features = personalization_app::GetTimeOfDayFeatures();
break;
}
enabled_features.push_back(features::kFeatureManagementSeaPen);
enabled_features.push_back(features::kSeaPenDemoMode);
enabled_features.push_back(features::kDemoModeWallpaperUpdate);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
}
}
class WallpaperControllerTestBase : public NoSessionAshTestBase {
public:
WallpaperControllerTestBase()
: NoSessionAshTestBase(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
WallpaperControllerTestBase(const WallpaperControllerTestBase&) = delete;
WallpaperControllerTestBase& operator=(const WallpaperControllerTestBase&) =
delete;
void SetUp() override {
auto pref_manager = WallpaperPrefManager::Create(local_state());
pref_manager_ = pref_manager.get();
WallpaperControllerImpl::SetWallpaperPrefManagerForTesting(
std::move(pref_manager));
WallpaperControllerImpl::SetWallpaperImageDownloaderForTesting(
std::make_unique<TestWallpaperImageDownloader>());
user_data_dir_path_override_.emplace(ash::DIR_USER_DATA);
NoSessionAshTestBase::SetUp();
SeaPenWallpaperManager::GetInstance()->SetSessionDelegateForTesting(
std::make_unique<TestSeaPenWallpaperManagerSessionDelegate>());
controller_ = Shell::Get()->wallpaper_controller();
controller_->set_wallpaper_reload_no_delay_for_test();
client_.ResetCounts();
controller_->SetClient(&client_);
std::unique_ptr<TestWallpaperDriveFsDelegate> drivefs_delegate =
std::make_unique<TestWallpaperDriveFsDelegate>();
drivefs_delegate_ = drivefs_delegate.get();
controller_->SetDriveFsDelegate(std::move(drivefs_delegate));
client_.set_fake_files_id_for_account_id(kAccountId1, kWallpaperFilesId1);
client_.set_fake_files_id_for_account_id(kAccountId2, kWallpaperFilesId2);
CreateDefaultWallpapers();
}
WallpaperView* wallpaper_view() {
return Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller()
->wallpaper_view();
}
protected:
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));
}
}
}
}
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()));
}
void EnableShelfColoring() {
const gfx::ImageSkia kImage = CreateImage(10, 10, kWallpaperColor);
controller_->ShowWallpaperImage(
kImage, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
false, false);
SetSessionState(SessionState::ACTIVE);
EXPECT_TRUE(ShouldCalculateColors());
}
void SetSessionState(SessionState session_state) {
GetSessionControllerClient()->SetSessionState(session_state);
}
WallpaperInfo CreateWallpaperInfo(WallpaperLayout layout) {
return WallpaperInfo(std::string(), layout, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
}
base::FilePath PrecacheWallpapers(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(
*controller_, kSmallWallpaperSubDir, wallpaper_files_id, file_name);
base::FilePath large_wallpaper_path = GetCustomWallpaperPath(
*controller_, kLargeWallpaperSubDir, wallpaper_files_id, file_name);
CHECK(WriteJPEGFile(small_wallpaper_path, kSmallWallpaperMaxWidth,
kSmallWallpaperMaxHeight, kSmallCustomWallpaperColor));
CHECK(WriteJPEGFile(large_wallpaper_path, kLargeWallpaperMaxWidth,
kLargeWallpaperMaxHeight, kLargeCustomWallpaperColor));
return base::FilePath(wallpaper_files_id).Append(file_name);
}
void CreateAndSaveWallpapers(const AccountId& account_id) {
base::FilePath relative_path = PrecacheWallpapers(account_id);
WallpaperInfo info = {
relative_path.value(), WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight()};
ASSERT_TRUE(pref_manager_->SetUserWallpaperInfo(account_id, info));
}
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(switches::kDefaultWallpaperSmall,
small_file.value());
const base::FilePath large_file =
default_wallpaper_path.Append(kDefaultLargeWallpaperName);
command_line->AppendSwitchASCII(switches::kDefaultWallpaperLarge,
large_file.value());
const base::FilePath guest_small_file =
default_wallpaper_path.Append(kGuestSmallWallpaperName);
command_line->AppendSwitchASCII(switches::kGuestWallpaperSmall,
guest_small_file.value());
const base::FilePath guest_large_file =
default_wallpaper_path.Append(kGuestLargeWallpaperName);
command_line->AppendSwitchASCII(switches::kGuestWallpaperLarge,
guest_large_file.value());
const base::FilePath child_small_file =
default_wallpaper_path.Append(kChildSmallWallpaperName);
command_line->AppendSwitchASCII(switches::kChildWallpaperSmall,
child_small_file.value());
const base::FilePath child_large_file =
default_wallpaper_path.Append(kChildLargeWallpaperName);
command_line->AppendSwitchASCII(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));
}
[[nodiscard]] std::pair<const base::FilePath, const base::FilePath>
CreateCustomizationWallpapers() {
base::ScopedAllowBlockingForTesting allow_blocking;
CHECK(customization_wallpaper_dir_.CreateUniqueTempDir());
base::FilePath root = customization_wallpaper_dir_.GetPath();
const base::FilePath small_file =
root.Append(kCustomizationSmallWallpaperName);
const base::FilePath large_file =
root.Append(kCustomizationLargeWallpaperName);
CHECK(WriteJPEGFile(small_file, 800, 800, SK_ColorGREEN));
CHECK(WriteJPEGFile(large_file, 2000, 2000, SK_ColorBLUE));
return {small_file, large_file};
}
void SetOnlineWallpaperFromImage(
const AccountId& account_id,
uint64_t asset_id,
const gfx::ImageSkia& image,
const std::string& url,
const std::string& collection_id,
WallpaperLayout layout,
bool preview_mode,
bool from_user,
uint64_t unit_id,
WallpaperController::SetWallpaperCallback callback) {
const OnlineWallpaperVariant variant(asset_id, GURL(url),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
const OnlineWallpaperParams params = {
account_id, collection_id, layout,
preview_mode, from_user, false,
unit_id, {variant}};
controller_->OnOnlineWallpaperDecoded(account_id, preview_mode,
WallpaperInfo(params, variant),
std::move(callback), image);
}
SkColor GetWallpaperColor() {
const gfx::ImageSkiaRep& representation =
controller_->GetWallpaper().GetRepresentation(1.0f);
return representation.GetBitmap().getColor(0, 0);
}
bool ShouldCalculateColors() { return controller_->ShouldCalculateColors(); }
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_;
}
const WallpaperInfo GetCurrentWallpaperInfo() {
WallpaperResizer* wallpaper = controller_->current_wallpaper_.get();
if (!wallpaper)
return WallpaperInfo();
return wallpaper->wallpaper_info();
}
void ClearWallpaperCount() { controller_->wallpaper_count_for_testing_ = 0; }
void ClearDecodeFilePaths() {
controller_->decode_requests_for_testing_.clear();
}
void ClearWallpaper() { controller_->current_wallpaper_.reset(); }
const base::HistogramTester& histogram_tester() const {
return histogram_tester_;
}
void CacheOnlineWallpaper(std::string path) {
ClearWallpaperCount();
controller_->SetOnlineWallpaper(
OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId,
{{kAssetId, GURL(path), backdrop::Image::IMAGE_TYPE_UNKNOWN}}),
base::DoNothing());
RunAllTasksUntilIdle();
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER_CROPPED,
false, base::DoNothing(), "",
CreateImage(640, 480, kWallpaperColor));
RunAllTasksUntilIdle();
}
void SetSeaPenWallpaper(const AccountId& account_id,
SkColor color,
uint32_t id,
bool preview_mode,
gfx::ImageSkia* image) {
TestWallpaperControllerObserver observer(controller_);
std::string jpg_bytes = CreateEncodedImageForTesting(
{1, 1}, color, data_decoder::mojom::ImageCodec::kDefault, image);
ASSERT_TRUE(!jpg_bytes.empty());
base::test::TestFuture<bool> save_sea_pen_image_future;
auto* sea_pen_wallpaper_manager = SeaPenWallpaperManager::GetInstance();
sea_pen_wallpaper_manager->SaveSeaPenImage(
account_id, {std::move(jpg_bytes), id},
personalization_app::mojom::SeaPenQuery::NewTextQuery("search_query"),
save_sea_pen_image_future.GetCallback());
ASSERT_TRUE(save_sea_pen_image_future.Get());
base::test::TestFuture<bool> set_wallpaper_future;
controller_->SetSeaPenWallpaper(account_id, id, preview_mode,
set_wallpaper_future.GetCallback());
EXPECT_TRUE(set_wallpaper_future.Take());
EXPECT_EQ(1, observer.wallpaper_changed_count());
histogram_tester().ExpectUniqueSample("Ash.Wallpaper.SeaPen.Result2",
SetWallpaperResult::kSuccess, 1);
}
TestWallpaperImageDownloader* test_wallpaper_image_downloader() {
return static_cast<TestWallpaperImageDownloader*>(
controller_->wallpaper_image_downloader_for_testing());
}
void WaitForWallpaperCount(int count) {
base::RunLoop run_loop;
base::RepeatingTimer repeating_timer;
repeating_timer.Start(FROM_HERE, base::Milliseconds(10),
base::BindLambdaForTesting([&]() {
if (GetWallpaperCount() >= count) {
repeating_timer.Stop();
run_loop.Quit();
}
}));
run_loop.Run();
}
base::Time GetLastModifiedTime(const base::FilePath& path) {
base::File::Info info;
base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (file.GetInfo(&info)) {
return info.last_modified;
}
return base::Time();
}
raw_ptr<WallpaperControllerImpl, DanglingUntriaged> controller_;
raw_ptr<WallpaperPrefManager, DanglingUntriaged> pref_manager_ =
nullptr;
base::FilePath GetUserDataDir() {
return base::PathService::CheckedGet(ash::DIR_USER_DATA);
}
base::FilePath GetOnlineWallpaperDir() {
return base::PathService::CheckedGet(ash::DIR_WALLPAPERS);
}
base::FilePath GetCustomWallpaperDir() {
return base::PathService::CheckedGet(ash::DIR_CUSTOM_WALLPAPERS);
}
std::optional<base::ScopedPathOverride> user_data_dir_path_override_;
base::ScopedTempDir default_wallpaper_dir_;
base::ScopedTempDir customization_wallpaper_dir_;
base::HistogramTester histogram_tester_;
TestWallpaperControllerClient client_;
raw_ptr<TestWallpaperDriveFsDelegate, DanglingUntriaged> drivefs_delegate_;
const AccountId kChildAccountId =
AccountId::FromUserEmailGaiaId(kChildEmail, GaiaId("child_gaia_id"));
ScopedStubInstallAttributes scoped_stub_install_attributes_;
private:
InProcessDataDecoder decoder_;
base::Time mock_clock_origin_;
};
class WallpaperControllerTest
: public WallpaperControllerTestBase,
public testing::WithParamInterface<TimeOfDayFeatureCombination> {
public:
WallpaperControllerTest() {
InitTimeOfDayFeatureList(GetParam(), scoped_feature_list_);
}
WallpaperControllerTest(const WallpaperControllerTest&) = delete;
WallpaperControllerTest& operator=(const WallpaperControllerTest&) = delete;
~WallpaperControllerTest() override = default;
bool IsTimeOfDayEnabled() const {
switch (GetParam()) {
case TimeOfDayFeatureCombination::kDisabled:
return false;
case TimeOfDayFeatureCombination::kTimeOfDay:
return true;
}
}
struct PrintToStringParamName {
std::string operator()(
const testing::TestParamInfo<ParamType>& info) const {
switch (info.param) {
case TimeOfDayFeatureCombination::kDisabled:
return "TimeOfDayOff";
case TimeOfDayFeatureCombination::kTimeOfDay:
return "TimeOfDayOn";
}
}
};
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
class WallpaperControllerAutoScheduleTest : public WallpaperControllerTest,
public ScheduledFeature::Clock {
protected:
static constexpr SimpleGeoposition kSanJoseGeoposition = {37.335480,
-121.893028};
static constexpr char kTestDateMidnight[] = "26 Sep 2023 00:00:00 PDT";
static constexpr char kPDTTimezone[] = "America/Los_Angeles";
WallpaperControllerAutoScheduleTest()
: task_environment_(task_environment()), timezone_pdt_(kPDTTimezone) {}
void SetUp() override {
ASSERT_TRUE(timezone_pdt_.is_success());
WallpaperControllerTest::SetUp();
task_environment_start_time_ = task_environment()->GetMockClock()->Now();
SetSimulatedStartTime(GetTestDateMidnight());
scoped_refptr<TestGeolocationUrlLoaderFactory>
geolocation_url_loader_factory =
base::MakeRefCounted<TestGeolocationUrlLoaderFactory>();
geolocation_url_loader_factory->SetValidPosition(
kSanJoseGeoposition.latitude, kSanJoseGeoposition.longitude, Now());
SystemLocationProvider::GetInstance()
->GetLocationProviderForTesting()
->GetLocationFetcherForTesting()
->SetSharedUrlLoaderFactoryForTesting(
std::move(geolocation_url_loader_factory));
GeopositionResponsesWaiter waiter(Shell::Get()->geolocation_controller());
waiter.Wait();
}
base::Time Now() const override {
base::TimeDelta test_time_elapsed =
task_environment_->GetMockClock()->Now() - task_environment_start_time_;
return simulated_start_time_ + test_time_elapsed;
}
base::TimeTicks NowTicks() const override {
return task_environment_->NowTicks();
}
base::Time GetTestDateMidnight() {
base::Time time;
CHECK(base::Time::FromString(kTestDateMidnight, &time));
return time;
}
void SetSimulatedStartTime(base::Time simulated_start_time) {
WallpaperTimeOfDayScheduler& time_of_day_scheduler =
*Shell::Get()
->wallpaper_controller()
->time_of_day_scheduler_for_testing();
Shell::Get()->dark_light_mode_controller()->SetAutoScheduleEnabled(false);
time_of_day_scheduler.SetScheduleType(ScheduleType::kNone);
simulated_start_time_ = simulated_start_time;
Shell::Get()->geolocation_controller()->SetClockForTesting(this);
Shell::Get()->dark_light_mode_controller()->SetClockForTesting(this);
time_of_day_scheduler.SetClockForTesting(this);
Shell::Get()->dark_light_mode_controller()->SetAutoScheduleEnabled(true);
time_of_day_scheduler.SetScheduleType(ScheduleType::kSunsetToSunrise);
}
const raw_ptr<base::test::TaskEnvironment> task_environment_;
const calendar_test_utils::ScopedLibcTimeZone timezone_pdt_;
base::Time simulated_start_time_;
base::Time task_environment_start_time_;
};
INSTANTIATE_TEST_SUITE_P(
,
WallpaperControllerTest,
::testing::Values(TimeOfDayFeatureCombination::kDisabled,
TimeOfDayFeatureCombination::kTimeOfDay),
WallpaperControllerTest::PrintToStringParamName());
INSTANTIATE_TEST_SUITE_P(
,
WallpaperControllerAutoScheduleTest,
::testing::Values(TimeOfDayFeatureCombination::kDisabled,
TimeOfDayFeatureCombination::kTimeOfDay),
WallpaperControllerTest::PrintToStringParamName());
TEST_P(WallpaperControllerTest, Client) {
SimulateUserLogin(kAccountId1);
EXPECT_EQ(0u, client_.open_count());
controller_->OpenWallpaperPickerIfAllowed();
EXPECT_EQ(1u, client_.open_count());
}
TEST_P(WallpaperControllerTest, BasicReparentingAndLayerOpacity) {
SimulateUserLogin(kAccountId1);
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
EXPECT_TRUE(wallpaper_view()->layer()->fills_bounds_opaquely());
controller->OnSessionStateChanged(session_manager::SessionState::LOCKED);
EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId));
EXPECT_FALSE(wallpaper_view()->layer()->fills_bounds_opaquely());
controller->OnSessionStateChanged(session_manager::SessionState::ACTIVE);
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
EXPECT_TRUE(wallpaper_view()->layer()->fills_bounds_opaquely());
}
TEST_P(WallpaperControllerTest, SwitchWallpapersWhenNewWallpaperAnimationEnds) {
SimulateUserLogin(kAccountId1);
gfx::ScopedAnimationDurationScaleMode test_duration_mode(
gfx::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
WallpaperWidgetController* widget_controller =
Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller();
EXPECT_TRUE(widget_controller->IsAnimating());
RunDesktopControllerAnimation();
EXPECT_FALSE(widget_controller->IsAnimating());
}
TEST_P(WallpaperControllerTest, WallpaperMovementDuringUnlock) {
SimulateUserLogin(kAccountId1);
gfx::ScopedAnimationDurationScaleMode test_duration_mode(
gfx::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
RunDesktopControllerAnimation();
controller->OnSessionStateChanged(session_manager::SessionState::LOCKED);
controller->CreateEmptyWallpaperForTesting();
WallpaperWidgetController* widget_controller =
Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller();
EXPECT_TRUE(widget_controller->IsAnimating());
EXPECT_EQ(0, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(1, ChildCountForContainer(kLockScreenWallpaperId));
ASSERT_EQ(4u, wallpaper_view()->layer()->parent()->children().size());
controller->OnSessionStateChanged(session_manager::SessionState::ACTIVE);
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
ASSERT_EQ(3u, wallpaper_view()->layer()->parent()->children().size());
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
RunDesktopControllerAnimation();
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
ASSERT_EQ(2u, wallpaper_view()->layer()->parent()->children().size());
EXPECT_EQ(0, ChildCountForContainer(kLockScreenWallpaperId));
}
TEST_P(WallpaperControllerTest, ChangeWallpaperQuick) {
SimulateUserLogin(kAccountId1);
gfx::ScopedAnimationDurationScaleMode test_duration_mode(
gfx::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
WallpaperControllerImpl* controller = Shell::Get()->wallpaper_controller();
controller->CreateEmptyWallpaperForTesting();
RunDesktopControllerAnimation();
controller->CreateEmptyWallpaperForTesting();
WallpaperWidgetController* widget_controller =
Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller();
EXPECT_TRUE(widget_controller->IsAnimating());
controller->CreateEmptyWallpaperForTesting();
RunDesktopControllerAnimation();
EXPECT_FALSE(widget_controller->IsAnimating());
}
TEST_P(WallpaperControllerTest, ResizeCustomWallpaper) {
SimulateUserLogin(kAccountId1);
UpdateDisplay("320x200");
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
controller_->ShowWallpaperImage(
image, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
false, false);
EXPECT_TRUE(gfx::test::AreImagesEqual(gfx::Image(controller_->GetWallpaper()),
gfx::Image(image)));
RunAllTasksUntilIdle();
gfx::ImageSkia resized_image = controller_->GetWallpaper();
EXPECT_FALSE(gfx::test::AreImagesEqual(
gfx::Image(controller_->GetWallpaper()), gfx::Image(image)));
EXPECT_EQ(gfx::Size(320, 200).ToString(), resized_image.size().ToString());
controller_->ShowWallpaperImage(
image, CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
false, false);
RunAllTasksUntilIdle();
EXPECT_TRUE(resized_image.BackedBySameObjectAs(controller_->GetWallpaper()));
}
TEST_P(WallpaperControllerTest, DontScaleWallpaperWithCenterLayout) {
SimulateUserLogin(kAccountId1);
gfx::ScopedAnimationDurationScaleMode test_duration_mode(
gfx::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),
false, 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),
false, 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),
false, 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),
false, false);
WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
low_resolution.width(),
low_resolution.height(), kWallpaperColor);
}
UpdateDisplay("1200x600/u@1.5");
{
SCOPED_TRACE(base::StringPrintf("1200x600/u@1.5 high resolution"));
controller_->ShowWallpaperImage(
image_high_res, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
false, 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),
false, false);
WallpaperFitToNativeResolution(wallpaper_view(), low_dsf,
low_resolution.width(),
low_resolution.height(), kWallpaperColor);
}
}
TEST_P(WallpaperControllerTest, ShouldCalculateColorsBasedOnImage) {
EnableShelfColoring();
EXPECT_TRUE(ShouldCalculateColors());
controller_->CreateEmptyWallpaperForTesting();
EXPECT_FALSE(ShouldCalculateColors());
}
TEST_P(WallpaperControllerTest, ShouldCalculateColorsBasedOnSessionState) {
EnableShelfColoring();
LoginScreen::Get()->GetModel()->NotifyOobeDialogState(
OobeDialogState::HIDDEN);
SetSessionState(SessionState::UNKNOWN);
EXPECT_FALSE(ShouldCalculateColors());
SetSessionState(SessionState::OOBE);
EXPECT_TRUE(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_P(WallpaperControllerTest, ShouldCalculateColorsBasedOnLoginDisplayState) {
EnableShelfColoring();
SetSessionState(SessionState::LOGIN_PRIMARY);
LoginScreen::Get()->GetModel()->NotifyOobeDialogState(
OobeDialogState::HIDDEN);
EXPECT_FALSE(ShouldCalculateColors());
LoginScreen::Get()->GetModel()->NotifyOobeDialogState(
OobeDialogState::GAIA_SIGNIN);
EXPECT_TRUE(ShouldCalculateColors());
}
TEST_P(WallpaperControllerTest, ColorsCalculatedForMostRecentWallpaper) {
TestWallpaperControllerObserver observer(controller_);
const int dimension = 20;
SetSessionState(SessionState::ACTIVE);
base::RunLoop run_loop;
observer.SetOnResizeCallback(run_loop.QuitClosure());
const gfx::ImageSkia old_image =
CreateImage(dimension, dimension, kWallpaperColor);
WallpaperInfo old_info = CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH);
old_info.location = "old";
controller_->ShowWallpaperImage(old_image, old_info,
false,
false);
run_loop.Run();
observer.SetOnResizeCallback(base::NullCallback());
base::RunLoop colors_loop;
observer.SetOnColorsCalculatedCallback(colors_loop.QuitClosure());
const gfx::ImageSkia image = CreateImage(dimension, dimension, SK_ColorBLUE);
WallpaperInfo info = CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH);
info.location = "new";
controller_->ShowWallpaperImage(image, info,
false,
false);
colors_loop.Run();
EXPECT_EQ(observer.colors_changed_count(), 1);
EXPECT_EQ(controller_->calculated_colors()->k_mean_color, SK_ColorBLUE);
EXPECT_FALSE(pref_manager_->GetCachedKMeanColor("old"));
EXPECT_TRUE(pref_manager_->GetCachedKMeanColor("new"));
base::RunLoop load_preview_image_loop;
controller_->LoadPreviewImage(base::BindLambdaForTesting(
[quit = load_preview_image_loop.QuitClosure()](
scoped_refptr<base::RefCountedMemory> image_bytes) {
EXPECT_TRUE(image_bytes);
std::move(quit).Run();
}));
load_preview_image_loop.Run();
}
TEST_P(WallpaperControllerTest, SaveCelebiColor) {
TestWallpaperControllerObserver observer(controller_);
const char location[] = "test_wallpaper_here";
WallpaperInfo wallpaper_info = CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH);
wallpaper_info.location = location;
const gfx::ImageSkia kImage = CreateImage(10, 10, kWallpaperColor);
controller_->ShowWallpaperImage(kImage, wallpaper_info,
false,
false);
base::RunLoop colors_loop;
observer.SetOnColorsCalculatedCallback(colors_loop.QuitClosure());
SimulateUserLogin(kAccountId1);
colors_loop.Run();
EXPECT_EQ(kWallpaperColor, pref_manager_->GetCelebiColor(location));
}
TEST_P(WallpaperControllerTest,
GetCachedWallpaperColorForUser_WithCelebiColor) {
SimulateUserLogin(kAccountId1);
base::FilePath relative_path = PrecacheWallpapers(kAccountId1);
WallpaperInfo info = InfoWithType(WallpaperType::kCustomized);
info.location = relative_path.value();
ASSERT_TRUE(pref_manager_->SetLocalWallpaperInfo(kAccountId1, info));
pref_manager_->CacheCelebiColor(relative_path.value(), kWallpaperColor);
GetSessionControllerClient()->RequestSignOut();
EXPECT_EQ(kWallpaperColor, controller_->GetCachedWallpaperColorForUser(
kAccountId1, false));
}
TEST_P(WallpaperControllerTest,
GetCachedWallpaperColorForUser_WithKMeansColor) {
SimulateUserLogin(kAccountId1);
base::FilePath relative_path = PrecacheWallpapers(kAccountId1);
WallpaperInfo info = InfoWithType(WallpaperType::kCustomized);
info.location = relative_path.value();
ASSERT_TRUE(pref_manager_->SetLocalWallpaperInfo(kAccountId1, info));
pref_manager_->CacheKMeanColor(relative_path.value(), kWallpaperColor);
GetSessionControllerClient()->RequestSignOut();
EXPECT_EQ(kWallpaperColor, controller_->GetCachedWallpaperColorForUser(
kAccountId1, true));
}
TEST_P(WallpaperControllerTest, EnableShelfColoringNotifiesObservers) {
SimulateUserLogin(kAccountId1);
TestWallpaperControllerObserver observer(controller_);
EXPECT_EQ(0, observer.colors_changed_count());
EnableShelfColoring();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, observer.colors_changed_count());
}
TEST_P(WallpaperControllerTest,
OnWallpaperColorsChangedAlwaysCalledOnFirstUpdate) {
SimulateUserLogin(kAccountId1);
TestWallpaperControllerObserver observer(controller_);
controller_->ShowUserWallpaper(kAccountId1, user_manager::UserType::kRegular);
task_environment()->RunUntilIdle();
EXPECT_EQ(observer.colors_changed_count(), 1);
controller_->ShowUserWallpaper(kAccountId2, user_manager::UserType::kRegular);
task_environment()->RunUntilIdle();
EXPECT_EQ(observer.colors_changed_count(), 1);
}
TEST_P(WallpaperControllerTest,
UpdatePrimaryUserWallpaperWhileSecondUserActive) {
WallpaperInfo wallpaper_info;
SimulateUserLogin(kAccountId1);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
controller_->SetOnlineWallpaper(params, base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(params, params.variants.front());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
SimulateUserLogin(kAccountId2);
RunAllTasksUntilIdle();
ClearWallpaperCount();
const OnlineWallpaperParams& new_params = OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId2,
{{kAssetId2, GURL(kDummyUrl2), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
controller_->SetOnlineWallpaper(new_params, base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info_2(new_params,
new_params.variants.front());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info_2));
}
TEST_P(WallpaperControllerTest, SetOnlineWallpaper) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER_CROPPED;
SimulateUserLogin(kAccountId1);
auto run_loop = std::make_unique<base::RunLoop>();
ClearWallpaperCount();
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId, layout,
false, true,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting([&run_loop](bool success) {
EXPECT_TRUE(success);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(params, params.variants.front());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
histogram_tester().ExpectBucketCount("Ash.Wallpaper.Image", kUnitId, 1);
histogram_tester().ExpectBucketCount(
"Ash.Wallpaper.Collection",
static_cast<int>(base::PersistentHash(
TestWallpaperControllerClient::kDummyCollectionId)),
1);
histogram_tester().ExpectBucketCount("Ash.Wallpaper.Type",
WallpaperType::kOnline, 1);
RunAllTasksUntilIdle();
EXPECT_TRUE(base::PathExists(
GetOnlineWallpaperDir().Append(GURL(kDummyUrl).ExtractFileName())));
}
TEST_P(WallpaperControllerTest,
SetOnlineWallpaper_FiresResizedSignalWhenSettingTheSameWallpaper) {
TestWallpaperControllerObserver observer(controller_);
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER_CROPPED;
SimulateUserLogin(kAccountId1);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId, layout,
false, true,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
{
base::RunLoop run_loop;
ClearWallpaperCount();
base::RunLoop resized_loop;
observer.SetOnResizeCallback(resized_loop.QuitClosure());
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](bool success) {
EXPECT_TRUE(success);
std::move(quit).Run();
}));
run_loop.Run();
resized_loop.Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
}
{
base::RunLoop run_loop;
base::RunLoop resized_loop;
observer.SetOnResizeCallback(resized_loop.QuitClosure());
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](bool success) {
EXPECT_TRUE(success);
std::move(quit).Run();
}));
run_loop.Run();
resized_loop.Run();
}
}
TEST_P(WallpaperControllerTest, SetTimeOfDayWallpaper) {
if (!IsTimeOfDayEnabled()) {
return;
}
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
SimulateUserLogin(kAccountId1);
base::RunLoop run_loop;
ClearWallpaperCount();
controller_->SetTimeOfDayWallpaper(
kAccountId1,
base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](uint64_t unit_id, bool success) {
EXPECT_EQ(unit_id,
wallpaper_constants::kDefaultTimeOfDayWallpaperUnitId);
EXPECT_TRUE(success);
std::move(quit).Run();
}));
run_loop.Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_EQ(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
wallpaper_info.collection_id);
EXPECT_EQ(WallpaperType::kOnline, wallpaper_info.type);
RunAllTasksUntilIdle();
EXPECT_TRUE(base::PathExists(GetOnlineWallpaperDir().Append(
GURL(images[1].image_url()).ExtractFileName())));
}
TEST_P(WallpaperControllerTest,
ActiveUserPrefServiceChanged_SetTimeOfDayWallpaper) {
if (!IsTimeOfDayEnabled()) {
return;
}
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
ASSERT_TRUE(pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info));
SetSessionState(SessionState::OOBE);
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kOnline, actual_info.type);
EXPECT_EQ(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
actual_info.collection_id);
EXPECT_EQ(wallpaper_constants::kDefaultTimeOfDayWallpaperUnitId,
actual_info.unit_id.value_or(0));
histogram_tester().ExpectUniqueSample(
"Ash.Wallpaper.SetTimeOfDayAfterOobe.Default", true, 1);
}
TEST_P(WallpaperControllerTest,
ActiveUserPrefServiceChanged_DemoMode_SetTimeOfDayWallpaper) {
if (!IsTimeOfDayEnabled()) {
return;
}
scoped_stub_install_attributes_.Get()->SetDemoMode();
ASSERT_TRUE(demo_mode::IsDeviceInDemoMode());
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
ASSERT_TRUE(pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info));
SetSessionState(SessionState::LOGIN_PRIMARY);
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kOnline, actual_info.type);
EXPECT_EQ(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
actual_info.collection_id);
EXPECT_EQ(wallpaper_constants::kDefaultTimeOfDayWallpaperUnitId,
actual_info.unit_id.value_or(0));
}
TEST_P(WallpaperControllerTest,
ActiveUserPrefServiceChanged_SetTimeOfDayWallpaperAlternateUnitId) {
if (!IsTimeOfDayEnabled()) {
return;
}
auto* fake_statistics_provider = static_cast<system::FakeStatisticsProvider*>(
system::StatisticsProvider::GetInstance());
fake_statistics_provider->ClearAllMachineStatistics();
fake_statistics_provider->SetMachineStatistic(
system::kCustomizationIdKey,
std::string(wallpaper_constants::kAlternateWallpaperCustomizationId));
base::test::TestFuture<void> on_machine_statistics_loaded_future;
fake_statistics_provider->ScheduleOnMachineStatisticsLoaded(
on_machine_statistics_loaded_future.GetCallback());
ASSERT_TRUE(on_machine_statistics_loaded_future.Wait());
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SetSessionState(SessionState::OOBE);
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kOnline, actual_info.type);
EXPECT_EQ(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
actual_info.collection_id);
EXPECT_EQ(wallpaper_constants::kAlternateTimeOfDayWallpaperUnitId,
actual_info.unit_id.value_or(0));
histogram_tester().ExpectUniqueSample(
"Ash.Wallpaper.SetTimeOfDayAfterOobe.Alternate", true, 1);
}
TEST_P(WallpaperControllerTest,
TimeOfDayWallpaper_ReplacedByUserWallpaper_DuringOobe) {
if (!IsTimeOfDayEnabled()) {
return;
}
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SetSessionState(SessionState::OOBE);
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kOnline, actual_info.type);
EXPECT_EQ(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
actual_info.collection_id);
SetSessionState(SessionState::OOBE);
OnlineWallpaperVariant variant(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo synced_info = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId, {variant}),
variant);
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
RunAllTasksUntilIdle();
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(TestWallpaperControllerClient::kDummyCollectionId,
actual_info.collection_id);
EXPECT_TRUE(base::PathExists(
GetOnlineWallpaperDir().Append(GURL(kDummyUrl).ExtractFileName())));
}
TEST_P(WallpaperControllerTest,
ActiveUserPrefServiceChanged_OOBEForSecondUser_SetTimeOfDayWallpaper) {
if (!IsTimeOfDayEnabled()) {
return;
}
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SetSessionState(SessionState::LOGIN_PRIMARY);
LoginScreen::Get()->GetModel()->NotifyOobeDialogState(
OobeDialogState::GAIA_SIGNIN);
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kOnline, actual_info.type);
EXPECT_EQ(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
actual_info.collection_id);
histogram_tester().ExpectUniqueSample(
"Ash.Wallpaper.SetTimeOfDayAfterOobe.Default", true, 1);
}
TEST_P(
WallpaperControllerTest,
ActiveUserPrefServiceChanged_OOBEForSecondUser_SetPolicyWallpaper_TimeOfDayEnabled) {
if (!IsTimeOfDayEnabled()) {
return;
}
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SetSessionState(SessionState::LOGIN_PRIMARY);
LoginScreen::Get()->GetModel()->NotifyOobeDialogState(
OobeDialogState::GAIA_SIGNIN);
SimulateUserLogin(kAccountId1);
controller_->SetPolicyWallpaper(
kAccountId1, user_manager::UserType::kRegular,
CreateEncodedImageForTesting(gfx::Size(10, 10)));
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
WallpaperInfo policy_wallpaper_info(base::FilePath(kWallpaperFilesId1)
.Append("policy-controlled.jpeg")
.value(),
WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kPolicy,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(actual_info.MatchesSelection(policy_wallpaper_info));
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
}
TEST_P(WallpaperControllerTest,
ActiveUserPrefServiceChanged_NonOOBE_SetTimeOfDayWallpaper) {
if (!IsTimeOfDayEnabled()) {
return;
}
auto images = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
images);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_TRUE(local_info.MatchesAsset(actual_info));
histogram_tester().ExpectTotalCount("Ash.Wallpaper.IsSetToTimeOfDayAfterOobe",
0);
}
TEST_P(WallpaperControllerTest, SetAndRemovePolicyWallpaper) {
WallpaperInfo wallpaper_info;
EXPECT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_FALSE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ASSERT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ClearWallpaperCount();
controller_->SetPolicyWallpaper(
kAccountId1, user_manager::UserType::kRegular,
CreateEncodedImageForTesting(gfx::Size(10, 10)));
RunAllTasksUntilIdle();
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo policy_wallpaper_info(base::FilePath(kWallpaperFilesId1)
.Append("policy-controlled.jpeg")
.value(),
WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kPolicy,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(policy_wallpaper_info));
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
EXPECT_EQ(0, GetWallpaperCount());
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
EXPECT_EQ(1, GetWallpaperCount());
ASSERT_EQ(controller_->GetWallpaperType(), WallpaperType::kPolicy);
ClearWallpaper();
ClearLogin();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kPolicy);
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
ClearWallpaperCount();
controller_->RemovePolicyWallpaper(kAccountId1);
WaitUntilCustomWallpapersDeleted(*controller_, kAccountId1);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(default_wallpaper_info));
EXPECT_FALSE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kPolicy);
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
}
TEST_P(WallpaperControllerTest, ShowUserWallpaper_OriginalFallback) {
CreateAndSaveWallpapers(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo wallpaper_info;
ASSERT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
ASSERT_EQ(WallpaperType::kCustomized, wallpaper_info.type);
ASSERT_EQ("user1@test.com-hash/user1@test.com-file", wallpaper_info.location);
base::FilePath saved_wallpaper = GetCustomWallpaperDir().Append(
"small/user1@test.com-hash/user1@test.com-file");
ASSERT_TRUE(base::PathExists(saved_wallpaper));
base::CreateDirectory(controller_->GetCustomWallpaperDir("original")
.Append("user1@test.com-hash"));
ASSERT_TRUE(base::PathExists(controller_->GetCustomWallpaperDir("original")));
ASSERT_TRUE(
base::Move(saved_wallpaper, controller_->GetCustomWallpaperDir("original")
.Append(wallpaper_info.location)));
ASSERT_FALSE(base::PathExists(saved_wallpaper));
ClearDecodeFilePaths();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_FALSE(GetDecodeFilePaths().empty());
EXPECT_THAT(
GetDecodeFilePaths().back().value(),
testing::EndsWith("original/user1@test.com-hash/user1@test.com-file"));
}
TEST_P(WallpaperControllerTest, ShowUserWallpaper_MissingFile) {
CreateAndSaveWallpapers(kAccountId1);
RunAllTasksUntilIdle();
WallpaperInfo wallpaper_info;
ASSERT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
ASSERT_EQ(WallpaperType::kCustomized, wallpaper_info.type);
ASSERT_EQ("user1@test.com-hash/user1@test.com-file", wallpaper_info.location);
controller_->RemoveUserWallpaper(kAccountId1, base::DoNothing());
WaitUntilCustomWallpapersDeleted(*controller_, kAccountId1);
ClearDecodeFilePaths();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_FALSE(GetDecodeFilePaths().empty());
EXPECT_THAT(GetDecodeFilePaths().back().value(),
testing::EndsWith(kDefaultSmallWallpaperName));
}
TEST_P(WallpaperControllerTest, RemovePolicyWallpaperNoOp) {
auto verify_custom_wallpaper_info = [&]() {
EXPECT_EQ(WallpaperType::kCustomized, controller_->GetWallpaperType());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName1).value(),
WALLPAPER_LAYOUT_CENTER, WallpaperType::kCustomized,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
};
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "",
CreateImage(640, 480, kWallpaperColor));
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_FALSE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
verify_custom_wallpaper_info();
controller_->RemovePolicyWallpaper(kAccountId1);
RunAllTasksUntilIdle();
verify_custom_wallpaper_info();
}
TEST_P(WallpaperControllerTest, SetThirdPartyWallpaper) {
SimulateUserLogin(kAccountId1);
WallpaperInfo wallpaper_info;
EXPECT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia third_party_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_TRUE(controller_->SetThirdPartyWallpaper(
kAccountId1, kFileName1, layout, third_party_wallpaper));
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName1).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
EXPECT_EQ(kAccountId1, drivefs_delegate_->get_save_wallpaper_account_id());
}
TEST_P(WallpaperControllerTest, SetThirdPartyWallpaper_NonactiveUser) {
SimulateUserLogin(kAccountId2);
WallpaperInfo wallpaper_info;
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia third_party_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_FALSE(controller_->SetThirdPartyWallpaper(
kAccountId1, kFileName2, layout, third_party_wallpaper));
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info_2(
base::FilePath(kWallpaperFilesId1).Append(kFileName2).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info_2));
}
TEST_P(WallpaperControllerTest, SetThirdPartyWallpaper_PolicyWallpaper) {
SimulateUserLogin(kAccountId2);
WallpaperInfo wallpaper_info;
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia third_party_wallpaper = CreateImage(640, 480, kWallpaperColor);
controller_->SetPolicyWallpaper(
kAccountId2, user_manager::UserType::kRegular,
CreateEncodedImageForTesting(gfx::Size(10, 10)));
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId2));
EXPECT_TRUE(controller_->IsActiveUserWallpaperControlledByPolicy());
ClearWallpaperCount();
EXPECT_FALSE(controller_->SetThirdPartyWallpaper(
kAccountId2, kFileName1, layout, third_party_wallpaper));
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId2));
EXPECT_TRUE(controller_->IsActiveUserWallpaperControlledByPolicy());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId2, &wallpaper_info));
WallpaperInfo policy_wallpaper_info(base::FilePath(kWallpaperFilesId2)
.Append("policy-controlled.jpeg")
.value(),
WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kPolicy,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(policy_wallpaper_info));
}
TEST_P(WallpaperControllerTest, SetSeaPenWallpaper) {
SimulateUserLogin(kAccountId1);
WallpaperInfo wallpaper_info;
ASSERT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
gfx::ImageSkia expected_image;
SetSeaPenWallpaper(kAccountId1, SK_ColorGREEN, 777u,
false, &expected_image);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_EQ(WallpaperType::kSeaPen, wallpaper_info.type);
EXPECT_EQ("777", wallpaper_info.location);
EXPECT_TRUE(wallpaper_info.user_file_path.empty());
EXPECT_TRUE(gfx::test::AreBitmapsClose(
*expected_image.bitmap(), *controller_->GetWallpaperImage().bitmap(),
1));
base::FileEnumerator file_enumerator(GetOnlineWallpaperDir(),
true,
base::FileEnumerator::FileType::FILES);
std::vector<base::FilePath> wallpaper_files;
for (auto path = file_enumerator.Next(); !path.empty();
path = file_enumerator.Next()) {
wallpaper_files.push_back(path);
}
EXPECT_EQ(std::vector<base::FilePath>(
{GetOnlineWallpaperDir()
.Append(wallpaper_constants::kSeaPenWallpaperDirName)
.Append(kAccountId1.GetAccountIdKey())
.Append("777")
.AddExtension(".jpg")}),
wallpaper_files);
}
TEST_P(WallpaperControllerTest,
SeaPenWallpaperRemovedAfterSettingAnotherWallpaperType) {
const auto global_sea_pen_dir =
GetOnlineWallpaperDir().Append("sea_pen").Append(
kAccountId1.GetAccountIdKey());
SimulateUserLogin(kAccountId1);
WallpaperInfo wallpaper_info;
ASSERT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
{
gfx::ImageSkia expected_image;
SetSeaPenWallpaper(kAccountId1, SK_ColorGREEN, 848u,
false, &expected_image);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_EQ(WallpaperType::kSeaPen, wallpaper_info.type);
EXPECT_EQ("848", wallpaper_info.location);
EXPECT_TRUE(wallpaper_info.user_file_path.empty());
ASSERT_TRUE(
base::PathExists(global_sea_pen_dir.Append(wallpaper_info.location)
.ReplaceExtension(".jpg")));
}
{
base::RunLoop run_loop;
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](bool success) {
EXPECT_TRUE(success);
std::move(quit).Run();
}));
run_loop.Run();
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
}
RunAllTasksUntilIdle();
ASSERT_FALSE(
base::PathExists(global_sea_pen_dir.Append(wallpaper_info.location)
.ReplaceExtension(".jpg")));
}
TEST_P(WallpaperControllerTest, ShowSeaPenWallpaperOnLogin) {
SimulateUserLogin(kAccountId1);
WallpaperInfo wallpaper_info;
ASSERT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
gfx::ImageSkia expected_image;
SetSeaPenWallpaper(kAccountId1, SK_ColorBLUE, 888u, false,
&expected_image);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_EQ(WallpaperType::kSeaPen, wallpaper_info.type);
EXPECT_EQ("888", wallpaper_info.location);
EXPECT_TRUE(wallpaper_info.user_file_path.empty());
controller_->ReloadWallpaperForTesting(true);
ClearWallpaper();
ClearLogin();
SimulateUserLogin(kAccountId1);
const AccountId active_account_id =
Shell::Get()->session_controller()->GetActiveAccountId();
controller_->ShowUserWallpaper(active_account_id);
RunAllTasksUntilIdle();
WallpaperInfo new_wallpaper_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(active_account_id,
&new_wallpaper_info));
EXPECT_EQ(WallpaperType::kSeaPen, new_wallpaper_info.type);
EXPECT_TRUE(wallpaper_info.MatchesAsset(new_wallpaper_info));
EXPECT_TRUE(gfx::test::AreBitmapsClose(
*expected_image.bitmap(), *controller_->GetWallpaperImage().bitmap(),
1));
}
TEST_P(WallpaperControllerTest, LoadsSeaPenWallpaperWithInvalidUserFilePath) {
SimulateUserLogin(kAccountId1);
gfx::ImageSkia expected_image;
SetSeaPenWallpaper(kAccountId1, SK_ColorBLUE, 888u, false,
&expected_image);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
ASSERT_TRUE(pref_manager_->SetUserWallpaperInfo(
kAccountId1, WallpaperInfo(wallpaper_info.location, wallpaper_info.layout,
WallpaperType::kSeaPen, base::Time::Now(),
"invalid_user_file_path")));
{
controller_->ReloadWallpaperForTesting(true);
ClearWallpaper();
ClearLogin();
SimulateUserLogin(kAccountId1);
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
}
EXPECT_TRUE(gfx::test::AreBitmapsClose(
*expected_image.bitmap(), *controller_->GetWallpaperImage().bitmap(),
1));
}
TEST_P(WallpaperControllerTest, DISABLED_SetSeaPenWallpaperFromFile) {
SimulateUserLogin(kAccountId1);
TestWallpaperControllerObserver observer(controller_);
WallpaperInfo wallpaper_info;
ASSERT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
gfx::ImageSkia expected_image;
std::string jpg_bytes = CreateEncodedImageForTesting(
{1, 1}, SK_ColorGREEN, data_decoder::mojom::ImageCodec::kDefault,
&expected_image);
ASSERT_TRUE(!jpg_bytes.empty());
base::ScopedTempDir scoped_temp_dir;
ASSERT_TRUE(scoped_temp_dir.CreateUniqueTempDir());
base::FilePath file_path = scoped_temp_dir.GetPath().Append("111.jpg");
ASSERT_TRUE(base::WriteFile(file_path, jpg_bytes));
ASSERT_TRUE(base::TouchFile(file_path, base::Time::Now() - base::Minutes(5),
base::Time::Now() - base::Minutes(5)));
base::Time old_last_modified_time = GetLastModifiedTime(file_path);
base::test::TestFuture<bool> set_wallpaper_future;
controller_->SetSeaPenWallpaper(kAccountId1, 111u, false,
set_wallpaper_future.GetCallback());
EXPECT_TRUE(set_wallpaper_future.Take());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_EQ(WallpaperType::kSeaPen, wallpaper_info.type);
EXPECT_EQ(1, observer.wallpaper_changed_count());
histogram_tester().ExpectUniqueSample("Ash.Wallpaper.SeaPen.Result2",
SetWallpaperResult::kSuccess, 1);
EXPECT_TRUE(gfx::test::AreBitmapsClose(
*expected_image.bitmap(), *controller_->GetWallpaperImage().bitmap(),
1));
EXPECT_TRUE(GetLastModifiedTime(file_path) > old_last_modified_time);
}
TEST_P(WallpaperControllerTest, CancelSetSeaPenWallpaperInTabletMode) {
SimulateUserLogin(kAccountId1);
WallpaperInfo wallpaper_info;
ASSERT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
gfx::ImageSkia expected_image;
TestWallpaperControllerObserver observer(controller_);
SetSeaPenWallpaper(kAccountId1, SK_ColorBLUE, 777u,
true, &expected_image);
RunAllTasksUntilIdle();
EXPECT_TRUE(observer.is_in_wallpaper_preview());
controller_->CancelPreviewWallpaper();
EXPECT_FALSE(observer.is_in_wallpaper_preview());
ASSERT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
}
TEST_P(WallpaperControllerTest, ConfirmSetSeaPenWallpaperInTabletMode) {
SimulateUserLogin(kAccountId1);
WallpaperInfo wallpaper_info;
ASSERT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
gfx::ImageSkia expected_image;
TestWallpaperControllerObserver observer(controller_);
SetSeaPenWallpaper(kAccountId1, SK_ColorGREEN, 777u,
true, &expected_image);
RunAllTasksUntilIdle();
EXPECT_TRUE(observer.is_in_wallpaper_preview());
controller_->ConfirmPreviewWallpaper();
EXPECT_FALSE(observer.is_in_wallpaper_preview());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_EQ(WallpaperType::kSeaPen, wallpaper_info.type);
EXPECT_EQ("777", wallpaper_info.location);
EXPECT_TRUE(wallpaper_info.user_file_path.empty());
EXPECT_TRUE(gfx::test::AreBitmapsClose(
*expected_image.bitmap(), *controller_->GetWallpaperImage().bitmap(),
1));
base::FileEnumerator file_enumerator(GetOnlineWallpaperDir(),
true,
base::FileEnumerator::FileType::FILES);
std::vector<base::FilePath> wallpaper_files;
for (auto path = file_enumerator.Next(); !path.empty();
path = file_enumerator.Next()) {
wallpaper_files.push_back(path);
}
EXPECT_EQ(std::vector<base::FilePath>(
{GetOnlineWallpaperDir()
.Append(wallpaper_constants::kSeaPenWallpaperDirName)
.Append(kAccountId1.GetAccountIdKey())
.Append("777")
.AddExtension(".jpg")}),
wallpaper_files);
}
TEST_P(WallpaperControllerTest, SetSeaPenWallpaperForPublicAccount) {
const auto account_id = SimulateUserLogin(
{"public_session", user_manager::UserType::kPublicAccount});
gfx::ImageSkia expected_image;
SetSeaPenWallpaper(account_id, SK_ColorBLUE, 12345u, false,
&expected_image);
WallpaperInfo wallpaper_info;
ASSERT_TRUE(pref_manager_->GetUserWallpaperInfo(account_id, &wallpaper_info));
EXPECT_EQ(WallpaperType::kSeaPen, wallpaper_info.type);
EXPECT_EQ("12345", wallpaper_info.location);
EXPECT_TRUE(gfx::test::AreBitmapsClose(
*expected_image.bitmap(), *controller_->GetWallpaperImage().bitmap(),
1));
base::FileEnumerator file_enumerator(GetOnlineWallpaperDir(),
true,
base::FileEnumerator::FileType::FILES);
std::vector<base::FilePath> wallpaper_files;
for (auto path = file_enumerator.Next(); !path.empty();
path = file_enumerator.Next()) {
wallpaper_files.push_back(path);
}
EXPECT_TRUE(wallpaper_files.empty());
}
TEST_P(WallpaperControllerTest, SetDefaultWallpaperForRegularAccount) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
SimulateUserLogin(kAccountId1);
controller_->ShowUserWallpaper(kAccountId1);
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "", image);
RunAllTasksUntilIdle();
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_NE(wallpaper_info.type, default_wallpaper_info.type);
UpdateDisplay("1600x1200");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
controller_->SetDefaultWallpaper(kAccountId1, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultLargeWallpaperName),
GetDecodeFilePaths()[0]);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(default_wallpaper_info));
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "", image);
RunAllTasksUntilIdle();
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
controller_->SetDefaultWallpaper(kAccountId1, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultSmallWallpaperName),
GetDecodeFilePaths()[0]);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(default_wallpaper_info));
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "", image);
RunAllTasksUntilIdle();
UpdateDisplay("800x600/r");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
controller_->SetDefaultWallpaper(kAccountId1, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(0u, GetDecodeFilePaths().size());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(default_wallpaper_info));
}
TEST_P(WallpaperControllerTest, SetDefaultWallpaperForChildAccount) {
SimulateUserLogin({.user_type = user_manager::UserType::kChild},
kChildAccountId);
UpdateDisplay("1600x1200");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
controller_->SetDefaultWallpaper(kChildAccountId, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kChildLargeWallpaperName),
GetDecodeFilePaths()[0]);
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
controller_->SetDefaultWallpaper(kChildAccountId, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kChildSmallWallpaperName),
GetDecodeFilePaths()[0]);
}
TEST_P(WallpaperControllerTest,
SetDefaultWallpaperForGuestSessionUnaffectedByWallpaperPolicy) {
ClearWallpaperCount();
SimulateGuestLogin();
UpdateDisplay("1600x1200");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
ClearLogin();
const auto guest_id = SimulateGuestLogin();
controller_->SetDefaultWallpaper(guest_id, true,
base::DoNothing());
RunAllTasksUntilIdle();
WallpaperInfo wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(guest_id, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(default_wallpaper_info));
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestLargeWallpaperName),
GetDecodeFilePaths()[0]);
WallpaperInfo policy_wallpaper_info;
controller_->SetPolicyWallpaper(
kAccountId1, user_manager::UserType::kRegular,
CreateEncodedImageForTesting(gfx::Size(10, 10)));
RunAllTasksUntilIdle();
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &policy_wallpaper_info));
WallpaperInfo expected_policy_wallpaper_info(
base::FilePath(kWallpaperFilesId1)
.Append("policy-controlled.jpeg")
.value(),
WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kPolicy,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
policy_wallpaper_info.MatchesSelection(expected_policy_wallpaper_info));
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(guest_id, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(default_wallpaper_info));
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestLargeWallpaperName),
GetDecodeFilePaths()[0]);
}
TEST_P(WallpaperControllerTest, SetDefaultWallpaperForGuestSessionAndPreview) {
const AccountId guest_id =
AccountId::FromUserEmail(user_manager::kGuestUserName);
controller_->ShowUserWallpaper(guest_id);
SimulateUserLogin({.user_type = user_manager::UserType::kGuest}, guest_id);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(guest_id, &wallpaper_info));
EXPECT_EQ(wallpaper_info.type, WallpaperType::kDefault);
}
TEST_P(WallpaperControllerTest, SetDefaultWallpaperForGuestSession) {
SimulateUserLogin(kAccountId1);
CreateAndSaveWallpapers(kAccountId1);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_NE(wallpaper_info.type, default_wallpaper_info.type);
ClearLogin();
const AccountId guest_id = SimulateGuestLogin();
UpdateDisplay("1600x1200");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
controller_->SetDefaultWallpaper(guest_id, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(guest_id, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(default_wallpaper_info));
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestLargeWallpaperName),
GetDecodeFilePaths()[0]);
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
controller_->SetDefaultWallpaper(guest_id, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kGuestSmallWallpaperName),
GetDecodeFilePaths()[0]);
}
TEST_P(WallpaperControllerTest, SetDefaultWallpaperCallbackTiming) {
SimulateUserLogin(kAccountId1);
CreateAndSaveWallpapers(kAccountId1);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_NE(wallpaper_info.type, WallpaperType::kDefault);
TestWallpaperControllerObserver observer(controller_);
base::RunLoop loop;
controller_->SetDefaultWallpaper(
kAccountId1, true,
base::BindLambdaForTesting([&loop, &observer](bool success) {
ASSERT_TRUE(success);
ASSERT_EQ(0, observer.wallpaper_changed_count());
loop.Quit();
}));
loop.Run();
EXPECT_EQ(1, observer.wallpaper_changed_count());
}
TEST_P(WallpaperControllerTest, IgnoreWallpaperRequestInKioskMode) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
SimulateUserLogin({"kiosk", user_manager::UserType::kKioskChromeApp});
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "", image);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
WallpaperInfo wallpaper_info;
EXPECT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
ClearWallpaperCount();
controller_->SetOnlineWallpaper(
OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER,
false, false,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}}),
base::BindLambdaForTesting([&run_loop](bool success) {
EXPECT_FALSE(success);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
ClearWallpaperCount();
controller_->SetDefaultWallpaper(kAccountId1, true ,
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_FALSE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
}
TEST_P(WallpaperControllerTest, IgnoreWallpaperRequestWhenPolicyIsEnforced) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
SimulateUserLogin(kAccountId1);
controller_->SetPolicyWallpaper(
kAccountId1, user_manager::UserType::kRegular,
CreateEncodedImageForTesting(gfx::Size(10, 10)));
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
WallpaperInfo wallpaper_info;
WallpaperInfo policy_wallpaper_info(base::FilePath(kWallpaperFilesId1)
.Append("policy-controlled.jpeg")
.value(),
WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kPolicy,
base::Time::Now().LocalMidnight());
{
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "", image);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(policy_wallpaper_info));
}
{
base::RunLoop run_loop;
ClearWallpaperCount();
controller_->SetCustomWallpaper(
kAccountId1, base::FilePath(kFileName1), WALLPAPER_LAYOUT_CENTER,
false,
base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](bool success) {
EXPECT_FALSE(success);
std::move(quit).Run();
}));
run_loop.Run();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(policy_wallpaper_info));
}
{
base::RunLoop run_loop;
ClearWallpaperCount();
controller_->SetOnlineWallpaper(
OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}}),
base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](bool success) {
EXPECT_FALSE(success);
std::move(quit).Run();
}));
run_loop.Run();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(policy_wallpaper_info));
}
{
base::RunLoop run_loop;
ClearWallpaperCount();
controller_->SetOnlineWallpaper(
OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}}),
base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](bool success) {
EXPECT_FALSE(success);
std::move(quit).Run();
}));
run_loop.Run();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(policy_wallpaper_info));
}
{
base::RunLoop run_loop;
ClearWallpaperCount();
controller_->SetDefaultWallpaper(
kAccountId1, true ,
base::BindLambdaForTesting(
[quit = run_loop.QuitClosure()](bool success) {
EXPECT_FALSE(success);
std::move(quit).Run();
}));
run_loop.Run();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(policy_wallpaper_info));
}
}
TEST_P(WallpaperControllerTest, VerifyWallpaperCache) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
SimulateUserLogin(kAccountId1);
gfx::ImageSkia cached_wallpaper;
EXPECT_FALSE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
base::FilePath path;
EXPECT_FALSE(controller_->GetPathFromCache(kAccountId1, &path));
controller_->SetOnlineWallpaper(
OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}}),
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_TRUE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
EXPECT_TRUE(controller_->GetPathFromCache(kAccountId1, &path));
SimulateUserLogin({kUser2});
SwitchActiveUser(kAccountId1);
EXPECT_TRUE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
EXPECT_TRUE(controller_->GetPathFromCache(kAccountId1, &path));
controller_->SetDefaultWallpaper(kAccountId1, true ,
base::DoNothing());
EXPECT_FALSE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
EXPECT_FALSE(controller_->GetPathFromCache(kAccountId1, &path));
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "", image);
RunAllTasksUntilIdle();
EXPECT_TRUE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
EXPECT_TRUE(controller_->GetPathFromCache(kAccountId1, &path));
controller_->RemoveUserWallpaper(kAccountId1, base::DoNothing());
EXPECT_FALSE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
EXPECT_FALSE(controller_->GetPathFromCache(kAccountId1, &path));
}
TEST_P(WallpaperControllerTest, ShowCustomWallpaperWithCorrectResolution) {
const base::FilePath small_custom_wallpaper_path = GetCustomWallpaperPath(
*controller_, kSmallWallpaperSubDir, kWallpaperFilesId1, kFileName1);
const base::FilePath large_custom_wallpaper_path = GetCustomWallpaperPath(
*controller_, kLargeWallpaperSubDir, kWallpaperFilesId1, kFileName1);
CreateAndSaveWallpapers(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(small_custom_wallpaper_path, GetDecodeFilePaths()[0]);
ClearWallpaperCount();
ClearDecodeFilePaths();
UpdateDisplay("800x600,800x600");
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(0u, GetDecodeFilePaths().size());
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
UpdateDisplay("800x600,3000x2000");
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(large_custom_wallpaper_path, GetDecodeFilePaths()[0]);
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
ClearWallpaperCount();
ClearDecodeFilePaths();
UpdateDisplay("800x600,3000x2000");
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(large_custom_wallpaper_path, GetDecodeFilePaths()[0]);
}
TEST_P(WallpaperControllerTest, SigninWallpaperIsKeptAfterRotation) {
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
controller_->ShowSigninWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(1u, GetDecodeFilePaths().size());
EXPECT_EQ(default_wallpaper_dir_.GetPath().Append(kDefaultSmallWallpaperName),
GetDecodeFilePaths()[0]);
ClearWallpaperCount();
ClearDecodeFilePaths();
UpdateDisplay("800x600/r");
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ASSERT_EQ(0u, GetDecodeFilePaths().size());
}
TEST_P(WallpaperControllerTest, ReloadWallpaper) {
CreateAndSaveWallpapers(kAccountId1);
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ClearWallpaperCount();
UpdateDisplay("800x600/r");
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
SimulateUserLogin(kAccountId1);
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
true, base::DoNothing(), "",
CreateImage(640, 480, kWallpaperColor));
ClearWallpaperCount();
UpdateDisplay("800x600");
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ClearWallpaperCount();
controller_->CancelPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
const base::FilePath image_path =
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
switches::kGuestWallpaperLarge);
ClearWallpaperCount();
controller_->ShowOverrideWallpaper(image_path, true);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
ClearWallpaperCount();
UpdateDisplay("800x600/r");
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
}
TEST_P(WallpaperControllerTest, UpdateCurrentWallpaperLayout) {
gfx::ImageSkia image = CreateImage(640, 480, kSmallCustomWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_STRETCH;
WallpaperLayout new_layout = WALLPAPER_LAYOUT_CENTER;
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
false,
base::DoNothing(),
"", image);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperLayout(), layout);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_custom_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName1).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_custom_wallpaper_info));
ClearWallpaperCount();
controller_->UpdateCurrentWallpaperLayout(kAccountId1, new_layout);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperLayout(), new_layout);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
expected_custom_wallpaper_info.layout = new_layout;
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_custom_wallpaper_info));
{
ClearWallpaperCount();
controller_->SetGooglePhotosWallpaper(
GooglePhotosWallpaperParams(kAccountId1, "id",
false, layout,
false, "dedup_key"),
base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(),
WallpaperType::kOnceGooglePhotos);
EXPECT_EQ(controller_->GetWallpaperLayout(), layout);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(
WallpaperInfo(GooglePhotosWallpaperParams(
kAccountId1, "id", false, layout,
false, "dedup_key"))));
ClearWallpaperCount();
controller_->UpdateCurrentWallpaperLayout(kAccountId1, new_layout);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperLayout(), new_layout);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(
WallpaperInfo(GooglePhotosWallpaperParams(
kAccountId1, "id", false, new_layout,
false, "dedup_key"))));
}
image = CreateImage(640, 480, kWallpaperColor);
ClearWallpaperCount();
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1,
std::string(), layout,
false, false,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
controller_->SetOnlineWallpaper(params, base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
EXPECT_EQ(controller_->GetWallpaperLayout(), layout);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_online_wallpaper_info(params, params.variants.front());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_online_wallpaper_info));
ClearWallpaperCount();
controller_->UpdateCurrentWallpaperLayout(kAccountId1, new_layout);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperLayout(), layout);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_online_wallpaper_info));
}
TEST_P(WallpaperControllerTest, RemoveUserWithCustomWallpaper) {
SimulateUserLogin(kAccountId1);
base::FilePath small_wallpaper_path_1 = GetCustomWallpaperPath(
*controller_, kSmallWallpaperSubDir, kWallpaperFilesId1, kFileName1);
CreateAndSaveWallpapers(kAccountId1);
EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
SimulateUserLogin(kAccountId2);
base::FilePath small_wallpaper_path_2 =
GetCustomWallpaperPath(*controller_, kSmallWallpaperSubDir,
kWallpaperFilesId2, GetDummyFileName(kAccountId2));
CreateAndSaveWallpapers(kAccountId2);
EXPECT_TRUE(base::PathExists(small_wallpaper_path_2));
controller_->RemoveUserWallpaper(kAccountId2, base::DoNothing());
WaitUntilCustomWallpapersDeleted(*controller_, kAccountId2);
EXPECT_FALSE(base::PathExists(small_wallpaper_path_2));
EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
}
TEST_P(WallpaperControllerTest, RemoveUserWithDefaultWallpaper) {
SimulateUserLogin(kAccountId1);
base::FilePath small_wallpaper_path_1 = GetCustomWallpaperPath(
*controller_, kSmallWallpaperSubDir, kWallpaperFilesId1, kFileName1);
CreateAndSaveWallpapers(kAccountId1);
EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
SimulateUserLogin(kAccountId2);
controller_->SetDefaultWallpaper(kAccountId2, true ,
base::DoNothing());
controller_->RemoveUserWallpaper(kAccountId2,
base::DoNothing());
EXPECT_TRUE(base::PathExists(small_wallpaper_path_1));
}
TEST_P(WallpaperControllerTest, RemoveUserWallpaperOnRemoveCallbackCalled) {
SimulateUserLogin(kAccountId1);
controller_->SetDefaultWallpaper(kAccountId1, true,
base::DoNothing());
base::test::TestFuture<void> remove_was_called;
controller_->RemoveUserWallpaper(kAccountId1,
remove_was_called.GetCallback());
ASSERT_TRUE(remove_was_called.Wait());
}
TEST_P(WallpaperControllerTest, IsActiveUserWallpaperControlledByPolicy) {
EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
SimulateUserLogin(kAccountId1);
EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
controller_->SetPolicyWallpaper(
kAccountId1, user_manager::UserType::kRegular,
CreateEncodedImageForTesting(gfx::Size(10, 10)));
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->IsActiveUserWallpaperControlledByPolicy());
SimulateUserLogin(kAccountId2);
EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
ClearLogin();
EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
}
TEST_P(WallpaperControllerTest,
IsManagedGuestSessionWallpaperControlledByPolicy) {
EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
controller_->SetPolicyWallpaper(
kAccountId1, user_manager::UserType::kPublicAccount,
CreateEncodedImageForTesting(gfx::Size(10, 10)));
SimulateUserLogin({.user_type = user_manager::UserType::kPublicAccount},
kAccountId1);
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->IsWallpaperControlledByPolicy(kAccountId1));
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
EXPECT_EQ(1, GetWallpaperCount());
ASSERT_EQ(controller_->GetWallpaperType(), WallpaperType::kPolicy);
SimulateUserLogin(kAccountId2);
EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
ClearLogin();
EXPECT_FALSE(controller_->IsActiveUserWallpaperControlledByPolicy());
}
TEST_P(WallpaperControllerTest, WallpaperBlur) {
TestWallpaperControllerObserver observer(controller_);
ASSERT_TRUE(controller_->blur_manager()->IsBlurAllowedForLockState(
controller_->GetWallpaperType()));
ASSERT_FALSE(controller_->IsWallpaperBlurredForLockState());
SetSessionState(SessionState::ACTIVE);
EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(0, observer.blur_changed_count());
SetSessionState(SessionState::LOCKED);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(1, observer.blur_changed_count());
SetSessionState(SessionState::LOGGED_IN_NOT_ACTIVE);
EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(2, observer.blur_changed_count());
SetSessionState(SessionState::LOGIN_SECONDARY);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(3, observer.blur_changed_count());
SetSessionState(SessionState::LOGIN_PRIMARY);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(3, observer.blur_changed_count());
SetSessionState(SessionState::OOBE);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(3, observer.blur_changed_count());
SetSessionState(SessionState::UNKNOWN);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(3, observer.blur_changed_count());
}
TEST_P(WallpaperControllerTest, WallpaperBlurDuringLockScreenTransition) {
SimulateUserLogin(kAccountId1);
gfx::ScopedAnimationDurationScaleMode test_duration_mode(
gfx::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
gfx::ImageSkia image = CreateImage(600, 400, kWallpaperColor);
controller_->ShowWallpaperImage(
image, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
false, false);
TestWallpaperControllerObserver observer(controller_);
ASSERT_TRUE(controller_->blur_manager()->IsBlurAllowedForLockState(
controller_->GetWallpaperType()));
ASSERT_FALSE(controller_->IsWallpaperBlurredForLockState());
ASSERT_EQ(3u, wallpaper_view()->layer()->parent()->children().size());
EXPECT_EQ(ui::LAYER_SOLID_COLOR,
wallpaper_view()->layer()->parent()->children()[0]->type());
EXPECT_EQ(ui::LAYER_TEXTURED,
wallpaper_view()->layer()->parent()->children()[1]->type());
EXPECT_EQ(ui::LAYER_TEXTURED,
wallpaper_view()->layer()->parent()->children()[2]->type());
controller_->UpdateWallpaperBlurForLockState(true);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(1, observer.blur_changed_count());
SetSessionState(SessionState::LOCKED);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
ASSERT_EQ(4u, wallpaper_view()->layer()->parent()->children().size());
EXPECT_EQ(ui::LAYER_SOLID_COLOR,
wallpaper_view()->layer()->parent()->children()[0]->type());
EXPECT_EQ(ui::LAYER_SOLID_COLOR,
wallpaper_view()->layer()->parent()->children()[1]->type());
EXPECT_EQ(ui::LAYER_TEXTURED,
wallpaper_view()->layer()->parent()->children()[2]->type());
EXPECT_EQ(ui::LAYER_TEXTURED,
wallpaper_view()->layer()->parent()->children()[3]->type());
SetSessionState(SessionState::ACTIVE);
EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(2, observer.blur_changed_count());
ASSERT_EQ(3u, wallpaper_view()->layer()->parent()->children().size());
EXPECT_EQ(ui::LAYER_SOLID_COLOR,
wallpaper_view()->layer()->parent()->children()[0]->type());
EXPECT_EQ(ui::LAYER_TEXTURED,
wallpaper_view()->layer()->parent()->children()[1]->type());
EXPECT_EQ(ui::LAYER_TEXTURED,
wallpaper_view()->layer()->parent()->children()[2]->type());
}
TEST_P(WallpaperControllerTest, LockDuringOverview) {
SimulateUserLogin(kAccountId1);
gfx::ImageSkia image = CreateImage(600, 400, kWallpaperColor);
controller_->ShowWallpaperImage(
image, CreateWallpaperInfo(WALLPAPER_LAYOUT_CENTER),
false, false);
TestWallpaperControllerObserver observer(controller_);
EnterOverview();
EXPECT_FALSE(controller_->IsWallpaperBlurredForLockState());
EXPECT_EQ(0, observer.blur_changed_count());
SetSessionState(SessionState::LOCKED);
EXPECT_TRUE(controller_->IsWallpaperBlurredForLockState());
auto* wallpaper_view = Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller()
->wallpaper_view();
ASSERT_EQ(30, wallpaper_view->blur_sigma());
}
TEST_P(WallpaperControllerTest, DontLeakShieldView) {
SimulateUserLogin(kAccountId1);
SetSessionState(SessionState::LOCKED);
views::View* shield_view = wallpaper_view()->shield_view_for_testing();
ASSERT_TRUE(shield_view);
views::ViewTracker view_tracker(shield_view);
SetSessionState(SessionState::ACTIVE);
EXPECT_EQ(nullptr, wallpaper_view()->shield_view_for_testing());
EXPECT_EQ(nullptr, view_tracker.view());
}
TEST_P(WallpaperControllerTest, OnlyShowDevicePolicyWallpaperOnLoginScreen) {
ASSERT_TRUE(WriteJPEGFile(
GetUserDataDir().Append(base::FilePath(kDefaultSmallWallpaperName)),
2, 2, kWallpaperColor));
SetSessionState(SessionState::LOGIN_PRIMARY);
controller_->SetDevicePolicyWallpaperPath(
GetUserDataDir().Append(base::FilePath(kDefaultSmallWallpaperName)));
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_TRUE(IsDevicePolicyWallpaper());
ASSERT_FALSE(controller_->blur_manager()->IsBlurAllowedForLockState(
controller_->GetWallpaperType()));
ASSERT_FALSE(controller_->IsWallpaperBlurredForLockState());
SetSessionState(SessionState::LOGGED_IN_NOT_ACTIVE);
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_FALSE(IsDevicePolicyWallpaper());
SetSessionState(SessionState::ACTIVE);
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_FALSE(IsDevicePolicyWallpaper());
SetSessionState(SessionState::LOCKED);
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_FALSE(IsDevicePolicyWallpaper());
SetSessionState(SessionState::LOGIN_SECONDARY);
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_FALSE(IsDevicePolicyWallpaper());
}
TEST_P(WallpaperControllerTest, ShouldShowInitialAnimationAfterBoot) {
CreateAndSaveWallpapers(kChildAccountId);
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kFirstExecAfterBoot);
base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kLoginManager);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->ShouldShowInitialAnimation());
EXPECT_EQ(1, GetWallpaperCount());
ClearWallpaperCount();
controller_->ShowUserWallpaper(kChildAccountId);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
EXPECT_EQ(1, GetWallpaperCount());
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
EXPECT_EQ(1, GetWallpaperCount());
}
TEST_P(WallpaperControllerTest, ShouldNotShowInitialAnimationAfterSignOut) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kLoginManager);
CreateAndSaveWallpapers(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
EXPECT_EQ(1, GetWallpaperCount());
ClearWallpaperCount();
controller_->ShowUserWallpaper(kChildAccountId);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
EXPECT_EQ(1, GetWallpaperCount());
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->ShouldShowInitialAnimation());
EXPECT_EQ(1, GetWallpaperCount());
}
TEST_P(WallpaperControllerTest, ClosePreviewWallpaperOnOverviewStart) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
ClearWallpaperCount();
TestWallpaperControllerObserver observer(controller_);
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
true,
base::DoNothing(),
"", custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_TRUE(observer.is_in_wallpaper_preview());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
ClearWallpaperCount();
EnterOverview();
RunAllTasksUntilIdle();
EXPECT_FALSE(observer.is_in_wallpaper_preview());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
EXPECT_TRUE(OverviewController::Get()->InOverviewSession());
}
TEST_P(WallpaperControllerTest, ClosePreviewWallpaperOnWindowCycleStart) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
TestWallpaperControllerObserver observer(controller_);
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
true,
base::DoNothing(),
"", custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_TRUE(observer.is_in_wallpaper_preview());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
ClearWallpaperCount();
Shell::Get()->window_cycle_controller()->HandleCycleWindow(
WindowCycleController::WindowCyclingDirection::kForward);
RunAllTasksUntilIdle();
EXPECT_FALSE(observer.is_in_wallpaper_preview());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
EXPECT_TRUE(Shell::Get()->window_cycle_controller()->IsCycling());
}
TEST_P(WallpaperControllerTest,
ClosePreviewWallpaperOnActiveUserSessionChanged) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
TestWallpaperControllerObserver observer(controller_);
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
true,
base::DoNothing(),
"", custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_TRUE(observer.is_in_wallpaper_preview());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
ClearWallpaperCount();
SimulateUserLogin(kAccountId2);
controller_->ShowUserWallpaper(kAccountId2);
RunAllTasksUntilIdle();
EXPECT_FALSE(observer.is_in_wallpaper_preview());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId2, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
}
TEST_P(WallpaperControllerTest, ConfirmPreviewWallpaper) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
true,
base::DoNothing(),
"", custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
histogram_tester().ExpectTotalCount("Ash.Wallpaper.Preview.Show", 1);
ClearWallpaperCount();
controller_->ConfirmPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
WallpaperInfo custom_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName1).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(custom_wallpaper_info));
ClearWallpaperCount();
std::unique_ptr<base::RunLoop> run_loop = std::make_unique<base::RunLoop>();
SetOnlineWallpaperFromImage(
kAccountId1, kAssetId, gfx::ImageSkia(), kDummyUrl,
TestWallpaperControllerClient::kDummyCollectionId, layout,
true, true, kUnitId,
base::BindLambdaForTesting([&run_loop](bool success) {
EXPECT_FALSE(success);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(0, GetWallpaperCount());
const SkColor online_wallpaper_color = SK_ColorCYAN;
gfx::ImageSkia online_wallpaper =
CreateImage(640, 480, online_wallpaper_color);
EXPECT_NE(online_wallpaper_color, GetWallpaperColor());
TestWallpaperControllerObserver observer(controller_);
run_loop = std::make_unique<base::RunLoop>();
observer.SetOnResizeCallback(run_loop->QuitClosure());
SetOnlineWallpaperFromImage(
kAccountId1, kAssetId, online_wallpaper, kDummyUrl,
TestWallpaperControllerClient::kDummyCollectionId, layout,
true, true, kUnitId,
base::BindLambdaForTesting([](bool success) { EXPECT_TRUE(success); }));
run_loop->Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(online_wallpaper_color, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(custom_wallpaper_info));
ClearWallpaperCount();
controller_->ConfirmPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(online_wallpaper_color, GetWallpaperColor());
OnlineWallpaperVariant variant(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo online_wallpaper_info(
OnlineWallpaperParams(kAccountId1,
TestWallpaperControllerClient::kDummyCollectionId,
layout,
false,
true,
false, kUnitId,
{variant}),
variant);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(online_wallpaper_info));
}
TEST_P(WallpaperControllerTest, CancelPreviewWallpaper) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
true,
base::DoNothing(),
"", custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
ClearWallpaperCount();
controller_->CancelPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
const SkColor online_wallpaper_color = SK_ColorCYAN;
gfx::ImageSkia online_wallpaper =
CreateImage(640, 480, online_wallpaper_color);
EXPECT_NE(online_wallpaper_color, GetWallpaperColor());
ClearWallpaperCount();
SetOnlineWallpaperFromImage(
kAccountId1, kAssetId, online_wallpaper, kDummyUrl,
TestWallpaperControllerClient::kDummyCollectionId, layout,
true, true, kUnitId, base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(online_wallpaper_color, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
ClearWallpaperCount();
controller_->CancelPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_NE(online_wallpaper_color, GetWallpaperColor());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
}
TEST_P(WallpaperControllerTest, WallpaperSyncedDuringPreview) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
const WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
true,
base::DoNothing(),
"", custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
const SkColor synced_custom_wallpaper_color = SK_ColorBLUE;
gfx::ImageSkia synced_custom_wallpaper =
CreateImage(640, 480, synced_custom_wallpaper_color);
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName2, layout,
false, base::DoNothing(),
"", synced_custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
WallpaperInfo synced_custom_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName2).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(synced_custom_wallpaper_info));
ClearWallpaperCount();
controller_->CancelPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(synced_custom_wallpaper_color, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(synced_custom_wallpaper_info));
gfx::ImageSkia online_wallpaper = CreateImage(640, 480, kWallpaperColor);
EXPECT_NE(kWallpaperColor, GetWallpaperColor());
ClearWallpaperCount();
SetOnlineWallpaperFromImage(
kAccountId1, kAssetId, online_wallpaper, kDummyUrl,
TestWallpaperControllerClient::kDummyCollectionId, layout,
true, true, kUnitId, base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(synced_custom_wallpaper_info));
const SkColor synced_online_wallpaper_color = SK_ColorCYAN;
gfx::ImageSkia synced_online_wallpaper =
CreateImage(640, 480, synced_online_wallpaper_color);
ClearWallpaperCount();
SetOnlineWallpaperFromImage(
kAccountId1, kAssetId, synced_online_wallpaper, kDummyUrl2,
TestWallpaperControllerClient::kDummyCollectionId, layout,
false,
true, kUnitId, base::DoNothing());
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
OnlineWallpaperVariant variant(kAssetId, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo synced_online_wallpaper_info = WallpaperInfo(
OnlineWallpaperParams(kAccountId1,
TestWallpaperControllerClient::kDummyCollectionId,
layout,
false,
true,
false, kUnitId,
{variant}),
variant);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(synced_online_wallpaper_info));
ClearWallpaperCount();
controller_->CancelPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(synced_online_wallpaper_color, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(synced_online_wallpaper_info));
}
TEST_P(WallpaperControllerTest, AddFirstWallpaperAnimationEndCallback) {
gfx::ScopedAnimationDurationScaleMode test_duration_mode(
gfx::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
std::unique_ptr<aura::Window> test_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
base::RunLoop test_loop;
controller_->AddFirstWallpaperAnimationEndCallback(test_loop.QuitClosure(),
test_window.get());
task_environment()->RunUntilIdle();
EXPECT_FALSE(test_loop.AnyQuitCalled());
controller_->ShowDefaultWallpaperForTesting();
controller_->AddFirstWallpaperAnimationEndCallback(test_loop.QuitClosure(),
test_window.get());
task_environment()->RunUntilIdle();
EXPECT_FALSE(test_loop.AnyQuitCalled());
RunDesktopControllerAnimation();
test_loop.Run();
EXPECT_TRUE(test_loop.AnyQuitCalled());
bool is_third_callback_run = false;
controller_->AddFirstWallpaperAnimationEndCallback(
base::BindLambdaForTesting(
[&is_third_callback_run]() { is_third_callback_run = true; }),
test_window.get());
EXPECT_TRUE(is_third_callback_run);
}
TEST_P(WallpaperControllerTest, ShowOneShotWallpaper) {
gfx::ImageSkia custom_wallpaper = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
false,
base::DoNothing(),
"", custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(WallpaperType::kCustomized, controller_->GetWallpaperType());
const WallpaperInfo expected_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName1).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
ClearWallpaperCount();
constexpr SkColor kOneShotWallpaperColor = SK_ColorWHITE;
gfx::ImageSkia one_shot_wallpaper =
CreateImage(640, 480, kOneShotWallpaperColor);
controller_->ShowOneShotWallpaper(one_shot_wallpaper);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kOneShotWallpaperColor, GetWallpaperColor());
EXPECT_EQ(WallpaperType::kOneShot, controller_->GetWallpaperType());
EXPECT_FALSE(controller_->blur_manager()->IsBlurAllowedForLockState(
controller_->GetWallpaperType()));
EXPECT_FALSE(controller_->ShouldApplyShield());
controller_->ReloadWallpaperForTesting(false);
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_EQ(kOneShotWallpaperColor, GetWallpaperColor());
EXPECT_EQ(WallpaperType::kOneShot, controller_->GetWallpaperType());
EXPECT_FALSE(controller_->blur_manager()->IsBlurAllowedForLockState(
controller_->GetWallpaperType()));
EXPECT_FALSE(controller_->ShouldApplyShield());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(WallpaperType::kCustomized, controller_->GetWallpaperType());
}
TEST_P(WallpaperControllerTest, OnFirstWallpaperShown) {
TestWallpaperControllerObserver observer(controller_);
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(0, observer.first_shown_count());
controller_->ShowWallpaperImage(CreateImage(640, 480, SK_ColorBLUE),
CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
false,
false);
RunAllTasksUntilIdle();
EXPECT_EQ(SK_ColorBLUE, GetWallpaperColor());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(1, observer.first_shown_count());
controller_->ShowWallpaperImage(CreateImage(640, 480, SK_ColorCYAN),
CreateWallpaperInfo(WALLPAPER_LAYOUT_STRETCH),
false,
false);
RunAllTasksUntilIdle();
EXPECT_EQ(SK_ColorCYAN, GetWallpaperColor());
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_EQ(1, observer.first_shown_count());
}
class WallpaperControllerEphemeralTest
: public WallpaperControllerTestBase,
public testing::WithParamInterface<
std::tuple<TimeOfDayFeatureCombination, user_manager::UserType>> {
public:
WallpaperControllerEphemeralTest() {
InitTimeOfDayFeatureList(std::get<0>(GetParam()), scoped_feature_list_);
}
WallpaperControllerEphemeralTest(const WallpaperControllerTest&) = delete;
WallpaperControllerEphemeralTest& operator=(const WallpaperControllerTest&) =
delete;
~WallpaperControllerEphemeralTest() override = default;
struct PrintToStringParamName {
std::string operator()(
const testing::TestParamInfo<ParamType>& info) const {
std::ostringstream ss;
switch (std::get<0>(info.param)) {
case TimeOfDayFeatureCombination::kDisabled:
ss << "TimeOfDayOff_";
break;
case TimeOfDayFeatureCombination::kTimeOfDay:
ss << "TimeOfDayOn_";
break;
}
ss << std::get<1>(info.param);
std::string r = ss.str();
std::replace(r.begin(), r.end(), '-', '_');
return r;
}
};
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(
,
WallpaperControllerEphemeralTest,
::testing::Values(std::make_tuple(TimeOfDayFeatureCombination::kDisabled,
user_manager::UserType::kGuest),
std::make_tuple(TimeOfDayFeatureCombination::kTimeOfDay,
user_manager::UserType::kGuest),
std::make_tuple(TimeOfDayFeatureCombination::kDisabled,
user_manager::UserType::kPublicAccount),
std::make_tuple(TimeOfDayFeatureCombination::kTimeOfDay,
user_manager::UserType::kPublicAccount),
std::make_tuple(TimeOfDayFeatureCombination::kDisabled,
user_manager::UserType::kRegular),
std::make_tuple(TimeOfDayFeatureCombination::kTimeOfDay,
user_manager::UserType::kRegular)),
WallpaperControllerEphemeralTest::PrintToStringParamName());
TEST_P(WallpaperControllerEphemeralTest, ShowWallpaperForEphemeralUser) {
local_state()->ClearPref(prefs::kUserWallpaperInfo);
auto user_type = std::get<1>(GetParam());
SimulateUserLogin({.user_type = user_type, .is_ephemeral = true},
kAccountId1);
gfx::ImageSkia cached_wallpaper;
EXPECT_FALSE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
base::FilePath path;
EXPECT_FALSE(controller_->GetPathFromCache(kAccountId1, &path));
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER,
false, base::DoNothing(), "",
CreateImage(640, 480, kWallpaperColor));
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(WallpaperType::kCustomized, controller_->GetWallpaperType());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
auto* dict = local_state()->GetUserPrefValue(prefs::kUserWallpaperInfo);
ASSERT_FALSE(dict) << *dict;
EXPECT_TRUE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
EXPECT_EQ(
kWallpaperColor,
cached_wallpaper.GetRepresentation(1.0f).GetBitmap().getColor(0, 0));
EXPECT_TRUE(controller_->GetPathFromCache(kAccountId1, &path));
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(WallpaperType::kCustomized, controller_->GetWallpaperType());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
}
class WallpaperControllerOobeWallpaperTest
: public WallpaperControllerTestBase,
public testing::WithParamInterface</*BootAnimation*/ bool> {
public:
WallpaperControllerOobeWallpaperTest() {
const bool boot_animation = GetParam();
scoped_feature_list_.InitWithFeatureStates({
{features::kFeatureManagementOobeSimon, boot_animation},
});
}
~WallpaperControllerOobeWallpaperTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(All,
WallpaperControllerOobeWallpaperTest,
testing::Bool());
TEST_P(WallpaperControllerOobeWallpaperTest, ShowOobeWallpaper) {
controller_->ShowDefaultWallpaperForTesting();
RunAllTasksUntilIdle();
SetSessionState(SessionState::OOBE);
controller_->ReloadWallpaperForTesting(false);
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->IsOobeWallpaper());
controller_->ShowSigninWallpaper();
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->IsOobeWallpaper());
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->IsOobeWallpaper());
SetSessionState(SessionState::LOGGED_IN_NOT_ACTIVE);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->IsOobeWallpaper());
SetSessionState(SessionState::ACTIVE);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->IsOobeWallpaper());
SetSessionState(SessionState::LOCKED);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->IsOobeWallpaper());
SetSessionState(SessionState::LOGIN_SECONDARY);
RunAllTasksUntilIdle();
EXPECT_FALSE(controller_->IsOobeWallpaper());
}
class WallpaperControllerOverrideWallpaperTest
: public WallpaperControllerTestBase,
public testing::WithParamInterface</*always_on_top=*/bool> {
public:
bool always_on_top() const { return GetParam(); }
};
INSTANTIATE_TEST_SUITE_P(All,
WallpaperControllerOverrideWallpaperTest,
testing::Bool());
TEST_P(WallpaperControllerOverrideWallpaperTest, OverrideWallpaper) {
SimulateUserLogin(kAccountId1);
EXPECT_EQ(0, GetWallpaperCount());
controller_->ShowSigninWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(0, ChildCountForContainer(kAlwaysOnTopWallpaperId));
const base::FilePath image_path =
base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(
switches::kGuestWallpaperLarge);
controller_->ShowOverrideWallpaper(image_path, always_on_top());
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOneShot);
EXPECT_EQ(always_on_top() ? 0 : 1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(always_on_top() ? 1 : 0,
ChildCountForContainer(kAlwaysOnTopWallpaperId));
controller_->ShowSigninWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOneShot);
EXPECT_EQ(always_on_top() ? 0 : 1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(always_on_top() ? 1 : 0,
ChildCountForContainer(kAlwaysOnTopWallpaperId));
controller_->RemoveOverrideWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(3, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(0, ChildCountForContainer(kAlwaysOnTopWallpaperId));
controller_->RemoveOverrideWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(3, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_EQ(1, ChildCountForContainer(kWallpaperId));
EXPECT_EQ(0, ChildCountForContainer(kAlwaysOnTopWallpaperId));
}
namespace {
class WallpaperControllerPrefTest : public AshTestBase {
public:
WallpaperControllerPrefTest() {
scoped_feature_list_.InitWithFeatures(
personalization_app::GetTimeOfDayFeatures(), {});
base::Value::Dict property;
property.Set("rotation", static_cast<int>(display::Display::ROTATE_90));
property.Set("width", 800);
property.Set("height", 600);
ScopedDictPrefUpdate update(local_state(), prefs::kDisplayProperties);
update->Set("2200000000", std::move(property));
}
~WallpaperControllerPrefTest() override = default;
void SetUp() override { AshTestBase::SetUp(); }
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
}
TEST_F(WallpaperControllerPrefTest, InitWithPrefs) {
auto* wallpaper_view = Shell::GetPrimaryRootWindowController()
->wallpaper_widget_controller()
->wallpaper_view();
auto* root_window =
wallpaper_view->GetWidget()->GetNativeWindow()->GetRootWindow();
EXPECT_EQ(
gfx::Size(600, 800),
display::Screen::Get()->GetDisplayNearestWindow(root_window).size());
EXPECT_EQ(root_window->bounds().size(), wallpaper_view->bounds().size());
}
TEST_P(WallpaperControllerTest, NoAnimationForNewRootWindowWhenLocked) {
gfx::ScopedAnimationDurationScaleMode test_duration_mode(
gfx::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
SimulateUserLogin(kAccountId1);
SetSessionState(SessionState::LOCKED);
UpdateDisplay("800x600, 800x600");
auto* secondary_root_window_controller =
Shell::Get()->GetAllRootWindowControllers()[1];
EXPECT_FALSE(secondary_root_window_controller->wallpaper_widget_controller()
->IsAnimating());
EXPECT_FALSE(secondary_root_window_controller->wallpaper_widget_controller()
->GetWidget()
->GetLayer()
->GetAnimator()
->is_animating());
}
TEST_P(WallpaperControllerTest, SetCustomWallpaper) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
false,
base::DoNothing(),
"", image);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(kWallpaperColor, GetWallpaperColor());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kCustomized);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName1).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
EXPECT_EQ(kAccountId1, drivefs_delegate_->get_save_wallpaper_account_id());
SimulateUserLogin(kAccountId2);
RunAllTasksUntilIdle();
auto second_wallpaper_color = GetWallpaperColor();
const SkColor custom_wallpaper_color = SK_ColorCYAN;
image = CreateImage(640, 480, custom_wallpaper_color);
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
false,
base::DoNothing(),
"", image);
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_EQ(second_wallpaper_color, GetWallpaperColor());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
ClearWallpaperCount();
SwitchActiveUser(kAccountId1);
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(custom_wallpaper_color, GetWallpaperColor());
}
TEST_P(WallpaperControllerTest, OldOnlineInfoSynced_Discarded) {
SimulateUserLogin(kAccountId1);
base::Value::Dict wallpaper_info_dict;
wallpaper_info_dict.Set(
WallpaperInfo::kNewWallpaperDateNodeName,
base::NumberToString(
base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds()));
wallpaper_info_dict.Set(WallpaperInfo::kNewWallpaperLocationNodeName,
"location");
wallpaper_info_dict.Set(WallpaperInfo::kNewWallpaperUserFilePathNodeName,
"user_file_path");
wallpaper_info_dict.Set(WallpaperInfo::kNewWallpaperLayoutNodeName,
WallpaperLayout::WALLPAPER_LAYOUT_CENTER);
wallpaper_info_dict.Set(WallpaperInfo::kNewWallpaperTypeNodeName,
static_cast<int>(WallpaperType::kOnline));
{
ScopedDictPrefUpdate wallpaper_update(GetProfilePrefService(kAccountId1),
prefs::kSyncableWallpaperInfo);
wallpaper_update->Set(kAccountId1.GetUserEmail(),
std::move(wallpaper_info_dict));
}
ClearLogin();
SimulateUserLogin(kAccountId1);
task_environment()->RunUntilIdle();
WallpaperInfo actual;
EXPECT_FALSE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual));
}
TEST_P(WallpaperControllerTest, MigrateWallpaperInfo_Online) {
SimulateUserLogin(kAccountId1);
WallpaperInfo expected_info = InfoWithType(WallpaperType::kOnline);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, expected_info);
ClearLogin();
SimulateUserLogin(kAccountId1);
WallpaperInfo info;
ASSERT_TRUE(pref_manager_->GetSyncedWallpaperInfo(kAccountId1, &info));
EXPECT_TRUE(info.MatchesSelection(expected_info));
}
TEST_P(WallpaperControllerTest, MigrateWallpaperInfoCustomized) {
SimulateUserLogin(kAccountId1);
WallpaperInfo expected_info = InfoWithType(WallpaperType::kCustomized);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, expected_info);
ClearLogin();
SimulateUserLogin(kAccountId1);
WallpaperInfo info;
ASSERT_TRUE(pref_manager_->GetSyncedWallpaperInfo(kAccountId1, &info));
EXPECT_TRUE(info.MatchesSelection(expected_info));
}
TEST_P(WallpaperControllerTest, MigrateWallpaperInfoDaily) {
SimulateUserLogin(kAccountId1);
OnlineWallpaperVariant variant(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo expected_info = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER, false, false,
false, kUnitId, {variant}),
variant);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, expected_info);
ClearLogin();
SimulateUserLogin(kAccountId1);
WallpaperInfo info;
ASSERT_TRUE(pref_manager_->GetSyncedWallpaperInfo(kAccountId1, &info));
EXPECT_TRUE(info.MatchesSelection(expected_info));
}
TEST_P(WallpaperControllerTest,
MigrateWallpaperInfoDoesntHappenWhenSyncedInfoAlreadyExists) {
SimulateUserLogin(kAccountId1);
OnlineWallpaperVariant local_variant(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo local_info = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER, false, false,
false, kUnitId, {local_variant}),
local_variant);
OnlineWallpaperVariant synced_variant(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo synced_info = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER, false, false,
false, kUnitId, {synced_variant}),
synced_variant);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
ClearLogin();
SimulateUserLogin(kAccountId1);
WallpaperInfo info;
ASSERT_TRUE(pref_manager_->GetSyncedWallpaperInfo(kAccountId1, &info));
EXPECT_TRUE(synced_info.MatchesSelection(info));
}
TEST_P(WallpaperControllerTest,
ActiveUserPrefServiceChangedSyncedInfoHandledLocally) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
WallpaperInfo synced_info = {kDummyUrl, WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kOnline, base::Time::Now()};
synced_info.unit_id = kUnitId;
synced_info.collection_id = TestWallpaperControllerClient::kDummyCollectionId;
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
WallpaperInfo local_info = InfoWithType(WallpaperType::kCustomized);
local_info.date = DayBeforeYesterdayish();
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
client_.ResetCounts();
controller_->OnActiveUserPrefServiceChanged(
GetProfilePrefService(kAccountId1));
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kOnline, actual_info.type);
}
TEST_P(WallpaperControllerTest, ActiveUserPrefServiceChanged_SyncDisabled) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
WallpaperInfo synced_info = {kDummyUrl, WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kOnline, base::Time::Now()};
synced_info.unit_id = kUnitId;
synced_info.collection_id = TestWallpaperControllerClient::kDummyCollectionId;
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
WallpaperInfo local_info = InfoWithType(WallpaperType::kDefault);
local_info.date = DayBeforeYesterdayish();
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
client_.ResetCounts();
client_.set_wallpaper_sync_enabled(false);
controller_->OnActiveUserPrefServiceChanged(
GetProfilePrefService(kAccountId1));
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kDefault, actual_info.type);
}
TEST_P(WallpaperControllerTest, HandleWallpaperInfoSyncedLocalIsPolicy) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
ClearLogin();
pref_manager_->SetLocalWallpaperInfo(kAccountId1,
InfoWithType(WallpaperType::kPolicy));
SimulateUserLogin(kAccountId1);
WallpaperInfo synced_info = {kDummyUrl, WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kOnline, base::Time::Now()};
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_NE(WallpaperType::kOnline, actual_info.type);
}
TEST_P(WallpaperControllerTest,
HandleWallpaperInfoSyncedLocalIsCustomizedAndOlder) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
ClearLogin();
WallpaperInfo local_info = InfoWithType(WallpaperType::kCustomized);
local_info.date = DayBeforeYesterdayish();
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SimulateUserLogin(kAccountId1);
WallpaperInfo synced_info = {kDummyUrl, WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kOnline, base::Time::Now()};
synced_info.unit_id = kUnitId;
synced_info.collection_id = TestWallpaperControllerClient::kDummyCollectionId;
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kOnline, actual_info.type);
}
TEST_P(WallpaperControllerTest,
HandleWallpaperInfoSyncedLocalIsCustomizedAndNewer) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
pref_manager_->SetLocalWallpaperInfo(
kAccountId1, InfoWithType(WallpaperType::kCustomized));
WallpaperInfo synced_info = {kDummyUrl, WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kOnline, DayBeforeYesterdayish()};
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
ClearLogin();
SimulateUserLogin(kAccountId1);
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_EQ(WallpaperType::kCustomized, actual_info.type);
}
TEST_P(WallpaperControllerTest, HandleWallpaperInfoSyncedOnline) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
ClearWallpaperCount();
OnlineWallpaperVariant variant(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo info = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER, false, false,
false, kUnitId, {variant}),
variant);
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, info);
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
}
TEST_P(WallpaperControllerTest, HandleWallpaperInfoSyncedInactiveUser) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
SimulateUserLogin(kAccountId2);
RunAllTasksUntilIdle();
ClearWallpaperCount();
pref_manager_->SetSyncedWallpaperInfo(kAccountId1,
InfoWithType(WallpaperType::kOnline));
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
EXPECT_NE(controller_->GetWallpaperType(), WallpaperType::kOnline);
}
TEST_P(WallpaperControllerTest, UpdateDailyRefreshWallpaper) {
std::string expected{"fun_collection"};
SimulateUserLogin(kAccountId1);
WallpaperInfo info = {std::string(), WALLPAPER_LAYOUT_CENTER,
WallpaperType::kDaily, DayBeforeYesterdayish()};
info.unit_id = kUnitId;
info.collection_id = expected;
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
controller_->UpdateDailyRefreshWallpaper();
EXPECT_EQ(expected, client_.get_fetch_daily_refresh_wallpaper_param());
}
TEST_P(WallpaperControllerTest, UpdateDailyRefreshWallpaperCalledOnLogin) {
SimulateUserLogin(kAccountId1);
OnlineWallpaperVariant variant(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo info = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED, false,
false,
true, kUnitId, {variant}),
variant);
info.date = DayBeforeYesterdayish();
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
ClearLogin();
SimulateUserLogin(kAccountId1);
task_environment()->FastForwardBy(base::Hours(1));
RunAllTasksUntilIdle();
EXPECT_EQ(TestWallpaperControllerClient::kDummyCollectionId,
client_.get_fetch_daily_refresh_wallpaper_param());
}
TEST_P(WallpaperControllerTest, UpdateDailyRefreshWallpaper_NotEnabled) {
SimulateUserLogin(kAccountId1);
WallpaperInfo info = {std::string(), WALLPAPER_LAYOUT_CENTER,
WallpaperType::kOnline, DayBeforeYesterdayish()};
info.collection_id = "fun_collection";
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
controller_->UpdateDailyRefreshWallpaper();
EXPECT_EQ(std::string(), client_.get_fetch_daily_refresh_wallpaper_param());
}
TEST_P(WallpaperControllerTest, UpdateDailyRefreshWallpaper_NoCollectionId) {
SimulateUserLogin(kAccountId1);
pref_manager_->SetUserWallpaperInfo(
kAccountId1,
WallpaperInfo(std::string(), WALLPAPER_LAYOUT_CENTER,
WallpaperType::kDaily, DayBeforeYesterdayish()));
controller_->UpdateDailyRefreshWallpaper();
EXPECT_EQ(std::string(), client_.get_fetch_daily_refresh_wallpaper_param());
}
TEST_P(WallpaperControllerTest, MigrateCustomWallpaper) {
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
SimulateUserLogin(kAccountId1);
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
false,
base::DoNothing(),
"", image);
RunAllTasksUntilIdle();
ClearLogin();
SimulateUserLogin(kAccountId1);
EXPECT_EQ(kAccountId1, drivefs_delegate_->get_save_wallpaper_account_id());
}
TEST_P(WallpaperControllerTest, OnGoogleDriveMounted) {
WallpaperInfo local_info = InfoWithType(WallpaperType::kCustomized);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SimulateUserLogin(kAccountId1);
controller_->SyncLocalAndRemotePrefs(kAccountId1);
EXPECT_EQ(kAccountId1, drivefs_delegate_->get_save_wallpaper_account_id());
}
TEST_P(WallpaperControllerTest, OnGoogleDriveMounted_WallpaperIsntCustom) {
WallpaperInfo local_info = InfoWithType(WallpaperType::kOnline);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
controller_->SyncLocalAndRemotePrefs(kAccountId1);
EXPECT_TRUE(drivefs_delegate_->get_save_wallpaper_account_id().empty());
}
TEST_P(WallpaperControllerTest, OnGoogleDriveMounted_AlreadySynced) {
WallpaperInfo local_info = InfoWithType(WallpaperType::kCustomized);
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
SimulateUserLogin(kAccountId1);
gfx::ImageSkia image = CreateImage(640, 480, kWallpaperColor);
WallpaperLayout layout = WALLPAPER_LAYOUT_CENTER;
controller_->SetDecodedCustomWallpaper(kAccountId1, kFileName1, layout,
false,
base::DoNothing(),
"", image);
RunAllTasksUntilIdle();
drivefs_delegate_->Reset();
controller_->SyncLocalAndRemotePrefs(kAccountId1);
EXPECT_FALSE(drivefs_delegate_->get_save_wallpaper_account_id().is_valid());
}
TEST_P(WallpaperControllerTest, OnGoogleDriveMounted_OldLocalInfo) {
SimulateUserLogin(kAccountId1);
WallpaperInfo local_info = WallpaperInfo(
"a_url", WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kCustomized,
DayBeforeYesterdayish(), "/test/a_url");
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
WallpaperInfo synced_info = WallpaperInfo(
"b_url", WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kCustomized,
base::Time::Now().LocalMidnight(), "/test/b_url");
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
ClearLogin();
SimulateUserLogin(kAccountId1);
controller_->SyncLocalAndRemotePrefs(kAccountId1);
EXPECT_FALSE(drivefs_delegate_->get_save_wallpaper_account_id().is_valid());
}
TEST_P(WallpaperControllerTest, OnGoogleDriveMounted_NewLocalInfo) {
SimulateUserLogin(kAccountId1);
WallpaperInfo local_info = WallpaperInfo(
"a_url", WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kCustomized,
base::Time::Now().LocalMidnight(), "/test/a_url");
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
WallpaperInfo synced_info = WallpaperInfo(
"b_url", WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kCustomized,
DayBeforeYesterdayish(), "/test/b_url");
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
ClearLogin();
SimulateUserLogin(kAccountId1);
controller_->SyncLocalAndRemotePrefs(kAccountId1);
EXPECT_EQ(kAccountId1, drivefs_delegate_->get_save_wallpaper_account_id());
}
TEST_P(WallpaperControllerTest, SetDailyRefreshCollectionId_Empty) {
std::string collection_id = "fun_collection";
WallpaperInfo info = {std::string(), WALLPAPER_LAYOUT_CENTER,
WallpaperType::kDaily, DayBeforeYesterdayish()};
info.collection_id = collection_id;
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
controller_->SetDailyRefreshCollectionId(kAccountId1, std::string());
WallpaperInfo expected = {std::string(), WALLPAPER_LAYOUT_CENTER,
WallpaperType::kOnline, DayBeforeYesterdayish()};
expected.collection_id = collection_id;
WallpaperInfo actual;
pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual);
EXPECT_TRUE(actual.MatchesSelection(expected));
EXPECT_EQ(std::string(),
controller_->GetDailyRefreshCollectionId(kAccountId1));
}
TEST_P(WallpaperControllerTest,
SetDailyRefreshCollectionId_Empty_NotTypeDaily) {
pref_manager_->SetUserWallpaperInfo(
kAccountId1,
WallpaperInfo(std::string(), WALLPAPER_LAYOUT_CENTER,
WallpaperType::kCustomized, DayBeforeYesterdayish()));
controller_->SetDailyRefreshCollectionId(kAccountId1, std::string());
WallpaperInfo expected =
WallpaperInfo(std::string(), WALLPAPER_LAYOUT_CENTER,
WallpaperType::kCustomized, DayBeforeYesterdayish());
WallpaperInfo actual;
pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual);
EXPECT_TRUE(actual.MatchesSelection(expected));
EXPECT_EQ(std::string(),
controller_->GetDailyRefreshCollectionId(kAccountId1));
}
TEST_P(WallpaperControllerTest, UpdateWallpaperOnScheduleCheckpointChanged) {
for (const bool is_guest : {false, true}) {
if (is_guest) {
SimulateGuestLogin();
} else {
SimulateUserLogin(kAccountId1);
}
RunAllTasksUntilIdle();
const AccountId active_account_id =
Shell::Get()->session_controller()->GetActiveAccountId();
Shell::Get()->dark_light_mode_controller()->SetAutoScheduleEnabled(false);
Shell::Get()->dark_light_mode_controller()->SetDarkModeEnabledForTest(true);
auto run_loop = std::make_unique<base::RunLoop>();
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
active_account_id, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting([&run_loop](bool success) {
EXPECT_TRUE(success);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
WallpaperInfo actual;
ASSERT_TRUE(
pref_manager_->GetUserWallpaperInfo(active_account_id, &actual));
EXPECT_EQ(actual.location, kDummyUrl);
base::Time original_timestamp = actual.date;
task_environment()->FastForwardBy(base::Hours(1));
EXPECT_TRUE(
Shell::Get()->dark_light_mode_controller()->IsDarkModeEnabled());
Shell::Get()->dark_light_mode_controller()->ToggleColorMode();
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
ASSERT_TRUE(
pref_manager_->GetUserWallpaperInfo(active_account_id, &actual));
EXPECT_EQ(actual.location, kDummyUrl2);
EXPECT_TRUE(
actual.MatchesSelection(WallpaperInfo(params, variants.back())));
EXPECT_EQ(actual.date, original_timestamp);
}
}
TEST_P(WallpaperControllerAutoScheduleTest, UpdateWallpaperOnAutoColorMode) {
auto time = Shell::Get()->geolocation_controller()->GetSunRiseSetTime();
ASSERT_TRUE(time.has_value());
SetSimulatedStartTime(time->sunrise);
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
true, kUnitId, variants);
base::test::TestFuture<bool> future;
controller_->SetOnlineWallpaper(params, future.GetCallback());
ASSERT_TRUE(future.Get());
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDaily);
WallpaperInfo actual;
ASSERT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual));
EXPECT_EQ(actual.location, kDummyUrl2);
base::Time original_timestamp = actual.date;
ASSERT_GT(time->sunset, Now());
task_environment()->FastForwardBy(time->sunset - Now());
RunAllTasksUntilIdle();
WallpaperInfo expected = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED, false,
true,
true, kUnitId, variants),
variants.back());
ASSERT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual));
EXPECT_EQ(actual.location, kDummyUrl);
EXPECT_TRUE(actual.MatchesSelection(expected));
EXPECT_EQ(actual.date, original_timestamp);
}
TEST_P(WallpaperControllerAutoScheduleTest,
UpdateTimeOfDayWallpaperWithAutoColorModeOff) {
static constexpr gfx::Size kTestImageSize = gfx::Size(100, 100);
static constexpr SkColor kSunriseImageColor = SK_ColorRED;
static constexpr SkColor kMorningImageColor = SK_ColorGREEN;
static constexpr SkColor kLateAfternoonImageColor = SK_ColorBLUE;
static constexpr SkColor kSunsetImageColor = SK_ColorYELLOW;
if (!IsTimeOfDayEnabled()) {
return;
}
const auto backdrop_image_data = TimeOfDayImageSet();
client_.AddCollection(wallpaper_constants::kTimeOfDayWallpaperCollectionId,
backdrop_image_data);
const base::flat_map<backdrop::Image_ImageType, gfx::ImageSkia> test_images =
{{backdrop::Image::IMAGE_TYPE_LIGHT_MODE,
CreateSolidColorTestImage(kTestImageSize, kSunriseImageColor)},
{backdrop::Image::IMAGE_TYPE_MORNING_MODE,
CreateSolidColorTestImage(kTestImageSize, kMorningImageColor)},
{backdrop::Image::IMAGE_TYPE_LATE_AFTERNOON_MODE,
CreateSolidColorTestImage(kTestImageSize, kLateAfternoonImageColor)},
{backdrop::Image::IMAGE_TYPE_DARK_MODE,
CreateSolidColorTestImage(kTestImageSize, kSunsetImageColor)}};
test_wallpaper_image_downloader()->set_image_generator(
base::BindLambdaForTesting([backdrop_image_data,
test_images](const GURL& url) {
const backdrop::Image* match_found =
GetImageMatchingUrl(url, backdrop_image_data);
return match_found
? test_images.at(match_found->image_type())
: CreateSolidColorTestImage(kTestImageSize, SK_ColorBLACK);
}));
SimulateUserLogin(kAccountId1);
Shell::Get()->dark_light_mode_controller()->SetAutoScheduleEnabled(false);
OnlineWallpaperParams params(
kAccountId1, wallpaper_constants::kTimeOfDayWallpaperCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false,
wallpaper_constants::kDefaultTimeOfDayWallpaperUnitId, {});
for (const backdrop::Image& backdrop_image : backdrop_image_data) {
params.variants.emplace_back(backdrop_image.asset_id(),
GURL(backdrop_image.image_url()),
backdrop_image.image_type());
}
const auto wallpaper_has_color = [this](SkColor color) {
return gfx::test::AreImagesClose(
gfx::Image(controller_->GetWallpaper()),
gfx::Image(CreateSolidColorTestImage(controller_->GetWallpaper().size(),
color)),
1);
};
base::test::TestFuture<bool> future;
controller_->SetOnlineWallpaper(params, future.GetCallback());
ASSERT_TRUE(future.Get());
EXPECT_TRUE(wallpaper_has_color(kSunsetImageColor));
WallpaperChangedBarrier barrier(controller_, task_environment());
ASSERT_TRUE(barrier.RunUntilNextWallpaperChange());
EXPECT_THAT(Now() - simulated_start_time_, WallpaperChangeTimeNear(7));
EXPECT_TRUE(wallpaper_has_color(kSunriseImageColor));
ASSERT_TRUE(barrier.RunUntilNextWallpaperChange());
EXPECT_THAT(Now() - simulated_start_time_, WallpaperChangeTimeNear(11));
EXPECT_TRUE(wallpaper_has_color(kMorningImageColor));
ASSERT_TRUE(barrier.RunUntilNextWallpaperChange());
EXPECT_THAT(Now() - simulated_start_time_, WallpaperChangeTimeNear(17));
EXPECT_TRUE(wallpaper_has_color(kLateAfternoonImageColor));
ASSERT_TRUE(barrier.RunUntilNextWallpaperChange());
EXPECT_THAT(Now() - simulated_start_time_, WallpaperChangeTimeNear(19));
EXPECT_TRUE(wallpaper_has_color(kSunsetImageColor));
}
TEST_P(WallpaperControllerTest,
UpdateWallpaperOnScheduleCheckpointChanged_WithReplacedAsset) {
SimulateUserLogin(kAccountId1);
Shell::Get()->dark_light_mode_controller()->SetAutoScheduleEnabled(false);
Shell::Get()->dark_light_mode_controller()->SetDarkModeEnabledForTest(true);
auto run_loop = std::make_unique<base::RunLoop>();
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting([&run_loop](bool success) {
EXPECT_TRUE(success);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
WallpaperInfo local_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &local_info));
std::vector<OnlineWallpaperVariant> updated_variants;
const std::string updated_light_url = "https://light/new_light_url.jpg";
updated_variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
updated_variants.emplace_back(kAssetId2, GURL(updated_light_url),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
local_info.variants = updated_variants;
EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(kAccountId1, local_info));
EXPECT_TRUE(Shell::Get()->dark_light_mode_controller()->IsDarkModeEnabled());
Shell::Get()->dark_light_mode_controller()->ToggleColorMode();
RunAllTasksUntilIdle();
EXPECT_EQ(2, GetWallpaperCount());
WallpaperInfo expected = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED, false,
true,
false, kUnitId, updated_variants),
updated_variants.back());
WallpaperInfo actual;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual));
EXPECT_TRUE(actual.MatchesAsset(expected));
EXPECT_TRUE(base::PathExists(GetOnlineWallpaperDir().Append(
GURL(updated_light_url).ExtractFileName())));
}
TEST_P(WallpaperControllerTest,
DoesNotCrashOnScheduleCheckpointChangedWhenDownloadFails) {
SimulateUserLogin(kAccountId1);
Shell::Get()->dark_light_mode_controller()->SetAutoScheduleEnabled(false);
Shell::Get()->dark_light_mode_controller()->SetDarkModeEnabledForTest(true);
auto run_loop = std::make_unique<base::RunLoop>();
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting([&run_loop](bool success) {
EXPECT_TRUE(success);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
ClearWallpaperCount();
WallpaperInfo local_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &local_info));
std::vector<OnlineWallpaperVariant> updated_variants;
const std::string updated_light_url = "https://light/new_light_url.jpg";
updated_variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
updated_variants.emplace_back(kAssetId2, GURL(updated_light_url),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
local_info.variants = updated_variants;
EXPECT_TRUE(pref_manager_->SetUserWallpaperInfo(kAccountId1, local_info));
test_wallpaper_image_downloader()->set_image_generator(
base::BindLambdaForTesting([](const GURL&) { return gfx::ImageSkia(); }));
EXPECT_TRUE(Shell::Get()->dark_light_mode_controller()->IsDarkModeEnabled());
Shell::Get()->dark_light_mode_controller()->ToggleColorMode();
RunAllTasksUntilIdle();
EXPECT_EQ(0, GetWallpaperCount());
WallpaperInfo expected = WallpaperInfo(params, updated_variants.front());
WallpaperInfo actual;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual));
EXPECT_EQ(actual.asset_id, expected.asset_id);
EXPECT_EQ(actual.location, expected.location);
EXPECT_FALSE(actual.MatchesAsset(expected));
}
TEST_P(WallpaperControllerTest,
DoesNotUpdateWallpaperOnColorModeChangedWithNoVariants) {
SimulateUserLogin(kAccountId1);
auto run_loop = std::make_unique<base::RunLoop>();
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
controller_->SetOnlineWallpaper(
params, base::BindLambdaForTesting([&run_loop](bool success) {
EXPECT_TRUE(success);
run_loop->Quit();
}));
run_loop->Run();
EXPECT_EQ(1, GetWallpaperCount());
Shell::Get()->dark_light_mode_controller()->ToggleColorMode();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
Shell::Get()->dark_light_mode_controller()->ToggleColorMode();
RunAllTasksUntilIdle();
EXPECT_EQ(1, GetWallpaperCount());
}
TEST_P(WallpaperControllerTest,
UpdateWallpaperInfoWithOnlineWallpaperVariants) {
SimulateUserLogin(kAccountId1);
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
pref_manager_->SetUserWallpaperInfo(kAccountId1,
WallpaperInfo(params, variants.front()));
WallpaperInfo expected = WallpaperInfo(params, variants.front());
WallpaperInfo actual;
pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual);
EXPECT_TRUE(actual.MatchesSelection(expected));
}
TEST_P(WallpaperControllerTest, SetOnlineWallpaperWithoutInternet) {
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
SimulateUserLogin(kAccountId1);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
ClearWallpaperCount();
controller_->SetOnlineWallpaper(params, base::DoNothing());
RunAllTasksUntilIdle();
ASSERT_EQ(1, GetWallpaperCount());
ASSERT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, WALLPAPER_LAYOUT_CENTER_CROPPED,
false, base::DoNothing(),
"", CreateImage(640, 480, kWallpaperColor));
RunAllTasksUntilIdle();
ASSERT_EQ(1, GetWallpaperCount());
ASSERT_EQ(controller_->GetWallpaperType(), WallpaperType::kCustomized);
test_wallpaper_image_downloader()->set_image_generator(
base::BindLambdaForTesting([](const GURL&) { return gfx::ImageSkia(); }));
ClearWallpaperCount();
base::RunLoop run_loop;
controller_->SetOnlineWallpaper(
params, 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(), WallpaperType::kOnline);
}
TEST_P(WallpaperControllerTest,
HandleWallpaperInfoSyncedForDarkLightWallpapers_NotSynced) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
WallpaperInfo local_info = WallpaperInfo(params, variants.front());
local_info.unit_id = std::nullopt;
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
const WallpaperInfo& synced_info = WallpaperInfo(params, variants.back());
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_TRUE(actual_info.MatchesSelection(synced_info));
EXPECT_EQ(1, GetWallpaperCount());
}
TEST_P(WallpaperControllerTest,
HandleWallpaperInfoSyncedForDarkLightWallpapers_AlreadySynced) {
SimulateUserLogin(kAccountId1);
CacheOnlineWallpaper(kDummyUrl);
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
const WallpaperInfo& local_info = WallpaperInfo(params, variants.front());
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
const WallpaperInfo& synced_info = WallpaperInfo(params, variants.back());
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_TRUE(local_info.MatchesSelection(synced_info));
EXPECT_TRUE(local_info.MatchesSelection(actual_info));
EXPECT_EQ(0, GetWallpaperCount());
}
TEST_P(WallpaperControllerTest, WallpaperCustomization_Used) {
GetSessionControllerClient()->RequestSignOut();
controller_->ShowSigninWallpaper();
task_environment()->RunUntilIdle();
std::pair<const base::FilePath, const base::FilePath> paths =
CreateCustomizationWallpapers();
ASSERT_FALSE(paths.first.empty());
ASSERT_FALSE(paths.second.empty());
controller_->SetCustomizedDefaultWallpaperPaths(paths.first, paths.second);
task_environment()->RunUntilIdle();
EXPECT_THAT(GetCurrentWallpaperInfo().location,
testing::EndsWith(kCustomizationSmallWallpaperName));
}
TEST_P(WallpaperControllerTest, WallpaperCustomization_UnusedForNonDefault) {
SimulateUserLogin(kAccountId1);
controller_->SetOnlineWallpaper(
OnlineWallpaperParams(
kAccountId1,
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED,
false, false,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}}),
base::DoNothing());
task_environment()->RunUntilIdle();
std::pair<const base::FilePath, const base::FilePath> paths =
CreateCustomizationWallpapers();
ASSERT_FALSE(paths.first.empty());
ASSERT_FALSE(paths.second.empty());
controller_->SetCustomizedDefaultWallpaperPaths(paths.first, paths.second);
task_environment()->RunUntilIdle();
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
}
TEST_P(WallpaperControllerTest, TimeOfDayWallpapers_NotSyncedIn) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
variants.emplace_back(kAssetId3, GURL(kDummyUrl3),
backdrop::Image::IMAGE_TYPE_MORNING_MODE);
variants.emplace_back(kAssetId4, GURL(kDummyUrl4),
backdrop::Image::IMAGE_TYPE_LATE_AFTERNOON_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
WallpaperInfo local_info = WallpaperInfo(params, variants.front());
local_info.unit_id = kUnitId;
local_info.date = DayBeforeYesterdayish();
pref_manager_->SetLocalWallpaperInfo(kAccountId1, local_info);
WallpaperInfo synced_info = {kDummyUrl, WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kOnline, base::Time::Now()};
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &actual_info));
EXPECT_TRUE(actual_info.MatchesSelection(local_info));
EXPECT_EQ(0, GetWallpaperCount());
}
TEST_P(WallpaperControllerTest, TimeOfDayWallpapers_NotSyncedOut) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
WallpaperInfo synced_info = {kDummyUrl, WALLPAPER_LAYOUT_CENTER_CROPPED,
WallpaperType::kOnline, base::Time::Now()};
synced_info.date = DayBeforeYesterdayish();
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
std::vector<OnlineWallpaperVariant> variants;
variants.emplace_back(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_LIGHT_MODE);
variants.emplace_back(kAssetId2, GURL(kDummyUrl2),
backdrop::Image::IMAGE_TYPE_DARK_MODE);
variants.emplace_back(kAssetId3, GURL(kDummyUrl3),
backdrop::Image::IMAGE_TYPE_MORNING_MODE);
variants.emplace_back(kAssetId4, GURL(kDummyUrl4),
backdrop::Image::IMAGE_TYPE_LATE_AFTERNOON_MODE);
const OnlineWallpaperParams& params = OnlineWallpaperParams(
kAccountId1, wallpaper_constants::kTimeOfDayWallpaperCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId, variants);
WallpaperInfo local_info = WallpaperInfo(params, variants.front());
local_info.unit_id = kUnitId;
pref_manager_->SetUserWallpaperInfo(kAccountId1, local_info);
RunAllTasksUntilIdle();
WallpaperInfo actual_info;
EXPECT_TRUE(pref_manager_->GetSyncedWallpaperInfo(kAccountId1, &actual_info));
EXPECT_TRUE(actual_info.MatchesSelection(synced_info));
}
TEST_P(WallpaperControllerTest, SetGooglePhotosWallpaper) {
SimulateUserLogin(kAccountId1);
base::test::TestFuture<bool> online_future;
controller_->SetOnlineWallpaper(
{kAccountId1,
TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false,
true,
false,
kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}}},
online_future.GetCallback());
ASSERT_TRUE(online_future.Wait());
ASSERT_EQ(1, GetWallpaperCount());
ASSERT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
ClearWallpaperCount();
int expected_wallpaper_count = 0;
ASSERT_EQ(expected_wallpaper_count, GetWallpaperCount());
GooglePhotosWallpaperParams params(kAccountId1, kFakeGooglePhotosPhotoId,
false,
WallpaperLayout::WALLPAPER_LAYOUT_STRETCH,
false, "dedup_key");
controller_->SetGooglePhotosWallpaper(params, base::DoNothing());
++expected_wallpaper_count;
WaitForWallpaperCount(expected_wallpaper_count);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnceGooglePhotos);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(params);
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
}
TEST_P(WallpaperControllerTest, SetGooglePhotosWallpaperFails) {
SimulateUserLogin(kAccountId1);
base::test::TestFuture<bool> online_future;
OnlineWallpaperParams online_params(
{kAccountId1,
TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false,
true,
false,
kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}}});
controller_->SetOnlineWallpaper(online_params, online_future.GetCallback());
ASSERT_TRUE(online_future.Wait());
ASSERT_EQ(1, GetWallpaperCount());
ASSERT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnline);
client_.set_fetch_google_photos_photo_fails(true);
ClearWallpaperCount();
ASSERT_EQ(0, GetWallpaperCount());
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, kFakeGooglePhotosPhotoId, false,
WallpaperLayout::WALLPAPER_LAYOUT_STRETCH, false, "dedup_key"},
google_photos_future.GetCallback());
EXPECT_FALSE(google_photos_future.Get());
EXPECT_NE(controller_->GetWallpaperType(), WallpaperType::kOnceGooglePhotos);
WallpaperInfo wallpaper_info;
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info));
WallpaperInfo expected_wallpaper_info(online_params,
online_params.variants.front());
EXPECT_TRUE(wallpaper_info.MatchesSelection(expected_wallpaper_info));
}
TEST_P(WallpaperControllerTest, ResetToDefaultForDeletedPhotoOnStalenessCheck) {
SimulateUserLogin(kAccountId1);
WallpaperInfo info = {kFakeGooglePhotosPhotoId, WALLPAPER_LAYOUT_CENTER,
WallpaperType::kOnceGooglePhotos,
DayBeforeYesterdayish()};
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
client_.set_google_photo_has_been_deleted(true);
controller_->OnActiveUserSessionChanged(kAccountId1);
WaitForWallpaperCount(1);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
}
TEST_P(WallpaperControllerTest, HandleSyncDeletedGooglePhotosPhoto) {
SimulateUserLogin(kAccountId1);
WallpaperInfo synced_info = InfoWithType(WallpaperType::kOnceGooglePhotos);
WallpaperInfo local_info = InfoWithType(WallpaperType::kOnline);
local_info.date -= base::Days(2);
pref_manager_->SetUserWallpaperInfo(kAccountId1, local_info);
pref_manager_->SetSyncedWallpaperInfo(kAccountId1, synced_info);
ASSERT_FALSE(controller_->HasShownAnyWallpaper());
ASSERT_THAT(client_.fetch_google_photos_photo_id(), testing::IsEmpty());
client_.set_google_photo_has_been_deleted(true);
ClearLogin();
SimulateUserLogin(kAccountId1);
EXPECT_EQ(synced_info.location, client_.fetch_google_photos_photo_id());
RunAllTasksUntilIdle();
WallpaperInfo final_local_info;
ASSERT_TRUE(
pref_manager_->GetLocalWallpaperInfo(kAccountId1, &final_local_info));
EXPECT_TRUE(final_local_info.MatchesAsset(local_info));
histogram_tester().ExpectUniqueSample(
"Ash.Wallpaper.OnceGooglePhotos.Result2",
SetWallpaperResult::kFileNotFound, 1);
}
TEST_P(WallpaperControllerTest, GooglePhotosAreCachedOnDisk) {
SimulateUserLogin(kAccountId1);
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, kFakeGooglePhotosPhotoId, false,
WALLPAPER_LAYOUT_STRETCH,
false, "dedup_key"},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
base::FilePath saved_wallpaper = GetOnlineWallpaperDir()
.Append("google_photos/")
.Append(kAccountId1.GetAccountIdKey())
.Append(kFakeGooglePhotosPhotoId);
ASSERT_TRUE(base::PathExists(saved_wallpaper));
}
TEST_P(WallpaperControllerTest, GooglePhotosAreCachedInMemory) {
SimulateUserLogin(kAccountId1);
base::FilePath path;
EXPECT_FALSE(controller_->GetPathFromCache(kAccountId1, &path));
gfx::ImageSkia cached_wallpaper;
EXPECT_FALSE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, kFakeGooglePhotosPhotoId, false,
WALLPAPER_LAYOUT_STRETCH,
false, "dedup_key"},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
EXPECT_TRUE(controller_->GetPathFromCache(kAccountId1, &path));
EXPECT_TRUE(path.empty());
EXPECT_TRUE(
controller_->GetWallpaperFromCache(kAccountId1, &cached_wallpaper));
}
TEST_P(WallpaperControllerTest, GooglePhotosAreReadFromCache) {
SimulateUserLogin(kAccountId1);
base::test::TestFuture<bool> google_photos_future;
GooglePhotosWallpaperParams params({kAccountId1, kFakeGooglePhotosPhotoId,
false,
WALLPAPER_LAYOUT_STRETCH,
false, "dedup_key"});
controller_->SetGooglePhotosWallpaper(params,
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
const size_t expected_decodes = 0;
const WallpaperType expected_type = WallpaperType::kOnceGooglePhotos;
EXPECT_EQ(expected_decodes, GetDecodeFilePaths().size());
EXPECT_EQ(expected_type, controller_->GetWallpaperType());
}
TEST_P(WallpaperControllerTest, ConfirmGooglePhotosPreviewWallpaper) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
const WallpaperLayout layout = WALLPAPER_LAYOUT_STRETCH;
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ClearWallpaperCount();
std::string photo_id = "foobar";
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, photo_id, false, layout,
true, "dedup_key"},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnceGooglePhotos);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
histogram_tester().ExpectTotalCount("Ash.Wallpaper.Preview.Show", 1);
ClearWallpaperCount();
controller_->ConfirmPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 0);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnceGooglePhotos);
WallpaperInfo google_photos_wallpaper_info(photo_id, layout,
WallpaperType::kOnceGooglePhotos,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(google_photos_wallpaper_info));
}
TEST_P(WallpaperControllerTest, CancelGooglePhotosPreviewWallpaper) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ClearWallpaperCount();
std::string photo_id = "foobar";
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, photo_id, false,
WALLPAPER_LAYOUT_STRETCH, true, "dedup_key"},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnceGooglePhotos);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
histogram_tester().ExpectTotalCount("Ash.Wallpaper.Preview.Show", 1);
ClearWallpaperCount();
controller_->CancelPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
}
TEST_P(WallpaperControllerTest, GooglePhotosWallpaperSyncedDuringPreview) {
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->ShowUserWallpaper(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
WallpaperInfo user_wallpaper_info;
WallpaperInfo default_wallpaper_info(
std::string(), WALLPAPER_LAYOUT_CENTER_CROPPED, WallpaperType::kDefault,
base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
std::unique_ptr<aura::Window> wallpaper_picker_window(
CreateTestWindow(gfx::Rect(0, 0, 100, 100)));
WindowState::Get(wallpaper_picker_window.get())->Activate();
const WallpaperLayout layout = WALLPAPER_LAYOUT_STRETCH;
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
ClearWallpaperCount();
std::string photo_id = "foobar";
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, photo_id, false, layout,
true, "dedup_key"},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnceGooglePhotos);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(user_wallpaper_info.MatchesSelection(default_wallpaper_info));
histogram_tester().ExpectTotalCount("Ash.Wallpaper.Preview.Show", 1);
gfx::ImageSkia synced_custom_wallpaper =
CreateImage(640, 480, kWallpaperColor);
ClearWallpaperCount();
controller_->SetDecodedCustomWallpaper(
kAccountId1, kFileName1, layout,
false, base::DoNothing(),
"", synced_custom_wallpaper);
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 0);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kOnceGooglePhotos);
WallpaperInfo synced_custom_wallpaper_info(
base::FilePath(kWallpaperFilesId1).Append(kFileName1).value(), layout,
WallpaperType::kCustomized, base::Time::Now().LocalMidnight());
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(synced_custom_wallpaper_info));
ClearWallpaperCount();
controller_->CancelPreviewWallpaper();
RunAllTasksUntilIdle();
EXPECT_EQ(GetWallpaperCount(), 1);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kCustomized);
EXPECT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &user_wallpaper_info));
EXPECT_TRUE(
user_wallpaper_info.MatchesSelection(synced_custom_wallpaper_info));
}
TEST_P(WallpaperControllerTest, UpdateGooglePhotosDailyRefreshWallpaper) {
std::string expected_photo_id = kFakeGooglePhotosAlbumId;
std::ranges::reverse(expected_photo_id);
SimulateUserLogin(kAccountId1);
GooglePhotosWallpaperParams params(
kAccountId1, kFakeGooglePhotosAlbumId,
true, WALLPAPER_LAYOUT_CENTER_CROPPED,
false, std::nullopt);
WallpaperInfo info(params);
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
controller_->UpdateDailyRefreshWallpaper();
RunAllTasksUntilIdle();
WallpaperInfo expected_info;
EXPECT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &expected_info));
EXPECT_EQ(expected_photo_id, expected_info.location);
EXPECT_EQ(kFakeGooglePhotosAlbumId, expected_info.collection_id);
}
TEST_P(WallpaperControllerTest, EmptyDailyGooglePhotosAlbumsDoNothing) {
SimulateUserLogin(kAccountId1);
GooglePhotosWallpaperParams daily_google_photos_params(
kAccountId1, kFakeGooglePhotosAlbumId, true,
WALLPAPER_LAYOUT_CENTER_CROPPED, false,
std::nullopt);
OnlineWallpaperParams online_params(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED,
false, true,
false, kUnitId,
{{kAssetId, GURL(kDummyUrl), backdrop::Image::IMAGE_TYPE_UNKNOWN}});
WallpaperInfo online_info(online_params, online_params.variants.front());
pref_manager_->SetUserWallpaperInfo(kAccountId1, online_info);
client_.set_fetch_google_photos_photo_fails(true);
controller_->SetGooglePhotosWallpaper(daily_google_photos_params,
base::DoNothing());
RunAllTasksUntilIdle();
WallpaperInfo current_info;
pref_manager_->GetUserWallpaperInfo(kAccountId1, ¤t_info);
EXPECT_TRUE(online_info.MatchesSelection(current_info));
}
TEST_P(WallpaperControllerTest,
ResetToDefaultForDeletedDailyGooglePhotosAlbums) {
SimulateUserLogin(kAccountId1);
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, kFakeGooglePhotosAlbumId, true,
WallpaperLayout::WALLPAPER_LAYOUT_CENTER_CROPPED,
false, std::nullopt},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
WallpaperInfo current_info;
pref_manager_->GetUserWallpaperInfo(kAccountId1, ¤t_info);
EXPECT_EQ(WallpaperType::kDailyGooglePhotos, current_info.type);
client_.set_google_photo_has_been_deleted(true);
controller_->UpdateDailyRefreshWallpaper();
RunAllTasksUntilIdle();
pref_manager_->GetUserWallpaperInfo(kAccountId1, ¤t_info);
EXPECT_EQ(WallpaperType::kDefault, current_info.type);
}
TEST_P(WallpaperControllerTest, DailyGooglePhotosAreCached) {
SimulateUserLogin(kAccountId1);
std::string expected_photo_id = kFakeGooglePhotosAlbumId;
std::ranges::reverse(expected_photo_id);
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, kFakeGooglePhotosAlbumId, true,
WallpaperLayout::WALLPAPER_LAYOUT_CENTER_CROPPED,
false, std::nullopt},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
base::FilePath saved_wallpaper = GetOnlineWallpaperDir()
.Append("google_photos/")
.Append(kAccountId1.GetAccountIdKey())
.Append(expected_photo_id);
ASSERT_TRUE(base::PathExists(saved_wallpaper));
}
TEST_P(WallpaperControllerTest,
ResetToDefaultForDisabledGooglePhotosIntegrationPolicyOnStalenessCheck) {
SimulateUserLogin(kAccountId1);
WallpaperInfo info = {kFakeGooglePhotosPhotoId, WALLPAPER_LAYOUT_CENTER,
WallpaperType::kOnceGooglePhotos,
DayBeforeYesterdayish()};
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
client_.set_wallpaper_google_photos_integration_enabled_for_account_id(
kAccountId1, false);
controller_->OnActiveUserSessionChanged(kAccountId1);
WaitForWallpaperCount(1);
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
}
TEST_P(
WallpaperControllerTest,
ResetToDefaultForDisabledGooglePhotosIntegrationPolicyDailyGooglePhotosAlbums) {
SimulateUserLogin(kAccountId1);
base::test::TestFuture<bool> google_photos_future;
controller_->SetGooglePhotosWallpaper(
{kAccountId1, kFakeGooglePhotosAlbumId, true,
WallpaperLayout::WALLPAPER_LAYOUT_CENTER_CROPPED,
false, std::nullopt},
google_photos_future.GetCallback());
EXPECT_TRUE(google_photos_future.Get());
RunAllTasksUntilIdle();
WallpaperInfo current_info;
pref_manager_->GetUserWallpaperInfo(kAccountId1, ¤t_info);
EXPECT_EQ(WallpaperType::kDailyGooglePhotos, current_info.type);
client_.set_wallpaper_google_photos_integration_enabled_for_account_id(
kAccountId1, false);
controller_->UpdateDailyRefreshWallpaper();
RunAllTasksUntilIdle();
pref_manager_->GetUserWallpaperInfo(kAccountId1, ¤t_info);
EXPECT_EQ(WallpaperType::kDefault, current_info.type);
}
class WallpaperControllerDailyRefreshSchedulerTest
: public WallpaperControllerTest,
public ScheduledFeature::Clock {
public:
WallpaperControllerDailyRefreshSchedulerTest() {
base::Time start_time = base::Time::Now();
clock_.SetNow(start_time);
tick_clock_.SetNowTicks(base::TimeTicks() + (start_time - base::Time()));
}
void SetUp() override {
WallpaperControllerTest::SetUp();
auto daily_refresh_scheduler =
controller_->daily_refresh_scheduler_for_testing();
daily_refresh_scheduler->SetScheduleType(ScheduleType::kNone);
daily_refresh_scheduler->SetClockForTesting(this);
daily_refresh_scheduler->SetScheduleType(ScheduleType::kCustom);
}
void TearDown() override { WallpaperControllerTest::TearDown(); }
base::Time Now() const override { return clock_.Now(); }
base::TimeTicks NowTicks() const override { return tick_clock_.NowTicks(); }
bool AdvanceClock(base::TimeDelta total) {
const auto advance_time = [this](base::TimeDelta advancement) {
clock_.Advance(advancement);
tick_clock_.Advance(advancement);
};
bool checkpoint_reached = false;
auto* timer = Shell::Get()
->wallpaper_controller()
->daily_refresh_scheduler_for_testing()
->timer();
while (total.is_positive()) {
base::TimeDelta advance_increment;
if (timer->IsRunning() &&
timer->desired_run_time() <= NowTicks() + total) {
advance_increment = timer->desired_run_time() - NowTicks();
advance_time(advance_increment);
timer->FireNow();
checkpoint_reached = true;
} else {
advance_increment = total;
advance_time(advance_increment);
}
CHECK_LE(advance_increment, total);
total -= advance_increment;
}
return checkpoint_reached;
}
private:
base::SimpleTestClock clock_;
base::SimpleTestTickClock tick_clock_;
};
INSTANTIATE_TEST_SUITE_P(
,
WallpaperControllerDailyRefreshSchedulerTest,
::testing::Values(TimeOfDayFeatureCombination::kDisabled,
TimeOfDayFeatureCombination::kTimeOfDay),
WallpaperControllerTest::PrintToStringParamName());
TEST_P(WallpaperControllerDailyRefreshSchedulerTest,
OnCheckpointChanged_WallpaperDailyRefreshScheduler) {
SimulateUserLogin(kAccountId1);
TestWallpaperControllerObserver observer(controller_);
EXPECT_EQ(0, observer.daily_refresh_checkpoint_count());
pref_manager_->SetUserWallpaperInfo(kAccountId1,
InfoWithType(WallpaperType::kDefault));
ClearLogin();
SimulateUserLogin(kAccountId1);
observer.ClearDailyRefreshCheckpointCount();
EXPECT_TRUE(AdvanceClock(base::Days(1)));
EXPECT_EQ(2, observer.daily_refresh_checkpoint_count());
}
TEST_P(WallpaperControllerDailyRefreshSchedulerTest,
OnCheckpointChanged_CalledOnLogin) {
TestWallpaperControllerObserver observer(controller_);
EXPECT_EQ(0, observer.daily_refresh_checkpoint_count());
pref_manager_->SetUserWallpaperInfo(kAccountId1,
InfoWithType(WallpaperType::kDaily));
SimulateUserLogin(kAccountId1);
RunAllTasksUntilIdle();
EXPECT_GE(observer.daily_refresh_checkpoint_count(), 1);
}
TEST_P(WallpaperControllerDailyRefreshSchedulerTest,
SetDailyRefreshCollectionId_UpdatesCheckTimes) {
auto daily_refresh_scheduler =
controller_->daily_refresh_scheduler_for_testing();
auto first_check_time = daily_refresh_scheduler->GetCustomStartTime();
auto second_check_time = daily_refresh_scheduler->GetCustomEndTime();
pref_manager_->SetUserWallpaperInfo(kAccountId1,
InfoWithType(WallpaperType::kOnline));
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->SetDailyRefreshCollectionId(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId);
controller_->UpdateDailyRefreshWallpaper();
WaitForWallpaperCount(1);
EXPECT_EQ(TestWallpaperControllerClient::kDummyCollectionId,
client_.get_fetch_daily_refresh_wallpaper_param());
WallpaperInfo expected;
ASSERT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &expected));
EXPECT_EQ(WallpaperType::kDaily, expected.type);
EXPECT_NE(first_check_time, daily_refresh_scheduler->GetCustomStartTime());
EXPECT_NE(second_check_time, daily_refresh_scheduler->GetCustomEndTime());
}
TEST_P(WallpaperControllerDailyRefreshSchedulerTest,
SetGooglePhotosDailyRefreshAlbumId_UpdatesCheckTimes) {
auto daily_refresh_scheduler =
controller_->daily_refresh_scheduler_for_testing();
auto first_check_time = daily_refresh_scheduler->GetCustomStartTime();
auto second_check_time = daily_refresh_scheduler->GetCustomEndTime();
pref_manager_->SetUserWallpaperInfo(kAccountId1,
InfoWithType(WallpaperType::kOnline));
SimulateUserLogin(kAccountId1);
ClearWallpaperCount();
controller_->SetGooglePhotosDailyRefreshAlbumId(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId);
controller_->UpdateDailyRefreshWallpaper();
WaitForWallpaperCount(1);
WallpaperInfo expected;
ASSERT_TRUE(pref_manager_->GetUserWallpaperInfo(kAccountId1, &expected));
EXPECT_EQ(WallpaperType::kDailyGooglePhotos, expected.type);
EXPECT_NE(first_check_time, daily_refresh_scheduler->GetCustomStartTime());
EXPECT_NE(second_check_time, daily_refresh_scheduler->GetCustomEndTime());
}
TEST_P(WallpaperControllerDailyRefreshSchedulerTest,
UpdateDailyRefreshWallpaper_OnLogin) {
SimulateUserLogin(kAccountId1);
OnlineWallpaperVariant variant(kAssetId, GURL(kDummyUrl),
backdrop::Image::IMAGE_TYPE_UNKNOWN);
WallpaperInfo info = WallpaperInfo(
OnlineWallpaperParams(
kAccountId1, TestWallpaperControllerClient::kDummyCollectionId,
WALLPAPER_LAYOUT_CENTER_CROPPED, false,
false,
true, kUnitId,
{variant}),
variant);
info.date = DayBeforeYesterdayish();
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
ClearLogin();
SimulateUserLogin(kAccountId1);
AdvanceClock(base::Hours(1));
RunAllTasksUntilIdle();
EXPECT_EQ(TestWallpaperControllerClient::kDummyCollectionId,
client_.get_fetch_daily_refresh_wallpaper_param());
}
TEST_P(WallpaperControllerDailyRefreshSchedulerTest,
UpdateDailyRefreshWallpaper_OnCheckpointChanged) {
auto images = ImageSet();
std::string collection_id{"my_wallpaper_collection"};
client_.AddCollection(collection_id, images);
pref_manager_->SetUserWallpaperInfo(kAccountId1,
InfoWithType(WallpaperType::kDaily));
SimulateUserLogin(kAccountId1);
base::RunLoop run_loop;
ClearWallpaperCount();
controller_->SetDailyRefreshCollectionId(kAccountId1, collection_id);
controller_->UpdateDailyRefreshWallpaper(
base::BindLambdaForTesting([quit = run_loop.QuitClosure()](bool success) {
EXPECT_TRUE(success);
std::move(quit).Run();
}));
run_loop.Run();
EXPECT_EQ(1, GetWallpaperCount());
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDaily);
WallpaperInfo wallpaper_info_1;
ASSERT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info_1));
EXPECT_EQ(collection_id, wallpaper_info_1.collection_id);
EXPECT_EQ(WallpaperType::kDaily, wallpaper_info_1.type);
EXPECT_TRUE(AdvanceClock(base::Hours(25)));
RunAllTasksUntilIdle();
WallpaperInfo wallpaper_info_2;
ASSERT_TRUE(
pref_manager_->GetUserWallpaperInfo(kAccountId1, &wallpaper_info_2));
EXPECT_FALSE(wallpaper_info_1.MatchesSelection(wallpaper_info_2));
EXPECT_EQ(collection_id, wallpaper_info_2.collection_id);
EXPECT_EQ(WallpaperType::kDaily, wallpaper_info_2.type);
}
TEST_P(WallpaperControllerDailyRefreshSchedulerTest,
CheckGooglePhotosStaleness_OnCheckpointChanged) {
SimulateUserLogin(kAccountId1);
WallpaperInfo info = {kFakeGooglePhotosPhotoId, WALLPAPER_LAYOUT_CENTER,
WallpaperType::kOnceGooglePhotos,
DayBeforeYesterdayish()};
pref_manager_->SetUserWallpaperInfo(kAccountId1, info);
client_.set_google_photo_has_been_deleted(true);
EXPECT_TRUE(AdvanceClock(base::Hours(25)));
RunAllTasksUntilIdle();
EXPECT_EQ(controller_->GetWallpaperType(), WallpaperType::kDefault);
}
}