Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / wallpaper / wallpaper_manager_unittest.cc
blob9c9f6154f958db9ac4bb7739d02cfb3455b0015b
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <cstdlib>
6 #include <cstring>
8 #include "ash/desktop_background/desktop_background_controller.h"
9 #include "ash/desktop_background/desktop_background_controller_observer.h"
10 #include "ash/shell.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/display_manager_test_api.h"
13 #include "ash/test/test_user_wallpaper_delegate.h"
14 #include "base/command_line.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/files/scoped_temp_dir.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/prefs/testing_pref_service.h"
21 #include "chrome/browser/chromeos/login/startup_utils.h"
22 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
23 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
24 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
25 #include "chrome/browser/chromeos/settings/cros_settings.h"
26 #include "chrome/browser/chromeos/settings/device_settings_service.h"
27 #include "chrome/browser/prefs/browser_prefs.h"
28 #include "chrome/test/base/testing_browser_process.h"
29 #include "chromeos/chromeos_switches.h"
30 #include "chromeos/settings/cros_settings_names.h"
31 #include "chromeos/settings/cros_settings_provider.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33 #include "ui/gfx/image/image.h"
35 using namespace ash;
37 namespace chromeos {
39 class WallpaperManagerCacheTest : public test::AshTestBase {
40 public:
41 WallpaperManagerCacheTest()
42 : fake_user_manager_(new FakeChromeUserManager()),
43 scoped_user_manager_(fake_user_manager_) {}
45 protected:
46 ~WallpaperManagerCacheTest() override {}
48 FakeChromeUserManager* fake_user_manager() { return fake_user_manager_; }
50 void SetUp() override {
51 test::AshTestBase::SetUp();
52 WallpaperManager::Initialize();
55 void TearDown() override {
56 WallpaperManager::Shutdown();
57 test::AshTestBase::TearDown();
60 // Creates a test image of size 1x1.
61 gfx::ImageSkia CreateTestImage(SkColor color) {
62 SkBitmap bitmap;
63 bitmap.allocN32Pixels(1, 1);
64 bitmap.eraseColor(color);
65 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
68 private:
69 FakeChromeUserManager* fake_user_manager_;
70 ScopedUserManagerEnabler scoped_user_manager_;
73 TEST_F(WallpaperManagerCacheTest, VerifyWallpaperCache) {
74 // Add three users to known users.
75 std::string test_user_1 = "test1@example.com";
76 std::string test_user_2 = "test2@example.com";
77 std::string test_user_3 = "test3@example.com";
78 base::FilePath path1("path1");
79 base::FilePath path2("path2");
80 base::FilePath path3("path3");
81 fake_user_manager()->AddUser(test_user_1);
82 fake_user_manager()->AddUser(test_user_2);
83 fake_user_manager()->AddUser(test_user_3);
85 // Login two users.
86 fake_user_manager()->LoginUser(test_user_1);
87 fake_user_manager()->LoginUser(test_user_2);
89 scoped_ptr<WallpaperManager::TestApi> test_api;
90 test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get()));
92 gfx::ImageSkia test_user_1_wallpaper = CreateTestImage(SK_ColorRED);
93 gfx::ImageSkia test_user_2_wallpaper = CreateTestImage(SK_ColorGREEN);
94 gfx::ImageSkia test_user_3_wallpaper = CreateTestImage(SK_ColorWHITE);
95 test_api->SetWallpaperCache(test_user_1, path1, test_user_1_wallpaper);
96 test_api->SetWallpaperCache(test_user_2, path2, test_user_2_wallpaper);
97 test_api->SetWallpaperCache(test_user_3, path3, test_user_3_wallpaper);
99 test_api->ClearDisposableWallpaperCache();
101 gfx::ImageSkia cached_wallpaper;
102 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_1, &cached_wallpaper));
103 base::FilePath path;
104 EXPECT_TRUE(test_api->GetPathFromCache(test_user_1, &path));
105 // Logged in users' wallpaper cache should be kept.
106 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_1_wallpaper));
107 EXPECT_EQ(path, path1);
108 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_2, &cached_wallpaper));
109 EXPECT_TRUE(test_api->GetPathFromCache(test_user_2, &path));
110 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_2_wallpaper));
111 EXPECT_EQ(path, path2);
113 // Not logged in user's wallpaper cache should be cleared.
114 EXPECT_FALSE(test_api->GetWallpaperFromCache(test_user_3, &cached_wallpaper));
115 EXPECT_FALSE(test_api->GetPathFromCache(test_user_3, &path));
118 } // namespace chromeos