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. 4 5 #include <cstdlib> 6 #include <cstring> 7 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_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" 34 35 using namespace ash; 36 37 namespace chromeos { 38 39 class WallpaperManagerCacheTest : public test::AshTestBase { 40 public: 41 WallpaperManagerCacheTest() 42 : fake_user_manager_(new FakeUserManager()), 43 scoped_user_manager_(fake_user_manager_) { 44 } 45 46 protected: 47 virtual ~WallpaperManagerCacheTest() {} 48 49 FakeUserManager* fake_user_manager() { return fake_user_manager_; } 50 51 virtual void SetUp() OVERRIDE { 52 test::AshTestBase::SetUp(); 53 } 54 55 // Creates a test image of size 1x1. 56 gfx::ImageSkia CreateTestImage(SkColor color) { 57 SkBitmap bitmap; 58 bitmap.allocN32Pixels(1, 1); 59 bitmap.eraseColor(color); 60 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); 61 } 62 63 private: 64 FakeUserManager* fake_user_manager_; 65 ScopedUserManagerEnabler scoped_user_manager_; 66 }; 67 68 TEST_F(WallpaperManagerCacheTest, VerifyWallpaperCache) { 69 // Add three users to known users. 70 std::string test_user_1 = "test1 (at) example.com"; 71 std::string test_user_2 = "test2 (at) example.com"; 72 std::string test_user_3 = "test3 (at) example.com"; 73 fake_user_manager()->AddUser(test_user_1); 74 fake_user_manager()->AddUser(test_user_2); 75 fake_user_manager()->AddUser(test_user_3); 76 77 // Login two users. 78 fake_user_manager()->LoginUser(test_user_1); 79 fake_user_manager()->LoginUser(test_user_2); 80 81 scoped_ptr<WallpaperManager::TestApi> test_api; 82 test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get())); 83 84 gfx::ImageSkia test_user_1_wallpaper = CreateTestImage(SK_ColorRED); 85 gfx::ImageSkia test_user_2_wallpaper = CreateTestImage(SK_ColorGREEN); 86 gfx::ImageSkia test_user_3_wallpaper = CreateTestImage(SK_ColorWHITE); 87 test_api->SetWallpaperCache(test_user_1, test_user_1_wallpaper); 88 test_api->SetWallpaperCache(test_user_2, test_user_2_wallpaper); 89 test_api->SetWallpaperCache(test_user_3, test_user_3_wallpaper); 90 91 test_api->ClearDisposableWallpaperCache(); 92 93 gfx::ImageSkia cached_wallpaper; 94 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_1, &cached_wallpaper)); 95 // Logged in users' wallpaper cache should be kept. 96 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_1_wallpaper)); 97 EXPECT_TRUE(test_api->GetWallpaperFromCache(test_user_2, &cached_wallpaper)); 98 EXPECT_TRUE(cached_wallpaper.BackedBySameObjectAs(test_user_2_wallpaper)); 99 100 // Not logged in user's wallpaper cache should be cleared. 101 EXPECT_FALSE(test_api->GetWallpaperFromCache(test_user_3, &cached_wallpaper)); 102 } 103 104 } // namespace chromeos 105