1 // Copyright 2013 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/ash_resources/grit/ash_resources.h" 9 #include "ash/desktop_background/desktop_background_controller.h" 10 #include "ash/shell.h" 11 #include "ash/test/ash_test_base.h" 12 #include "base/command_line.h" 13 #include "base/file_util.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/prefs/pref_service.h" 16 #include "base/prefs/testing_pref_service.h" 17 #include "chrome/browser/chromeos/cros/network_library.h" 18 #include "chrome/browser/chromeos/login/startup_utils.h" 19 #include "chrome/browser/chromeos/login/user_manager.h" 20 #include "chrome/browser/chromeos/login/user_manager_impl.h" 21 #include "chrome/browser/chromeos/login/wallpaper_manager.h" 22 #include "chrome/browser/chromeos/settings/cros_settings.h" 23 #include "chrome/browser/chromeos/settings/cros_settings_names.h" 24 #include "chrome/browser/chromeos/settings/cros_settings_provider.h" 25 #include "chrome/browser/chromeos/settings/device_settings_service.h" 26 #include "chrome/test/base/testing_browser_process.h" 27 #include "chromeos/chromeos_switches.h" 28 #include "content/public/test/test_browser_thread.h" 29 #include "testing/gtest/include/gtest/gtest.h" 30 #include "ui/base/resource/resource_bundle.h" 31 32 using namespace ash; 33 34 namespace { 35 36 const char kTestUser1[] = "test-user (at) example.com"; 37 38 const int kLargeWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_LARGE; 39 const int kSmallWallpaperResourceId = IDR_AURA_WALLPAPER_DEFAULT_SMALL; 40 41 } // namespace 42 43 namespace chromeos { 44 45 class WallpaperManagerTest : public test::AshTestBase { 46 public: 47 WallpaperManagerTest() : command_line_(CommandLine::NO_PROGRAM) {} 48 49 virtual ~WallpaperManagerTest() {} 50 51 virtual void SetUp() OVERRIDE { 52 test::AshTestBase::SetUp(); 53 54 // Register an in-memory local settings instance. 55 local_state_.reset(new TestingPrefServiceSimple); 56 TestingBrowserProcess::GetGlobal()->SetLocalState(local_state_.get()); 57 UserManager::RegisterPrefs(local_state_->registry()); 58 // Wallpaper manager and user image managers prefs will be accessed by the 59 // unit-test as well. 60 UserImageManager::RegisterPrefs(local_state_->registry()); 61 WallpaperManager::RegisterPrefs(local_state_->registry()); 62 63 StartupUtils::RegisterPrefs(local_state_->registry()); 64 StartupUtils::MarkDeviceRegistered(); 65 66 ResetUserManager(); 67 } 68 69 virtual void TearDown() OVERRIDE { 70 // Unregister the in-memory local settings instance. 71 TestingBrowserProcess::GetGlobal()->SetLocalState(0); 72 73 // Shut down the DeviceSettingsService. 74 DeviceSettingsService::Get()->UnsetSessionManager(); 75 76 test::AshTestBase::TearDown(); 77 } 78 79 void ResetUserManager() { 80 // Reset the UserManager singleton. 81 user_manager_enabler_.reset(); 82 // Initialize the UserManager singleton to a fresh UserManagerImpl instance. 83 user_manager_enabler_.reset( 84 new ScopedUserManagerEnabler(new UserManagerImpl)); 85 } 86 87 void AppendGuestSwitch() { 88 command_line_.AppendSwitch(switches::kGuestSession); 89 WallpaperManager::Get()->set_command_line_for_testing(&command_line_); 90 } 91 92 protected: 93 CommandLine command_line_; 94 95 scoped_ptr<TestingPrefServiceSimple> local_state_; 96 97 ScopedTestDeviceSettingsService test_device_settings_service_; 98 ScopedStubNetworkLibraryEnabler stub_network_library_enabler_; 99 ScopedTestCrosSettings test_cros_settings_; 100 101 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_; 102 103 private: 104 DISALLOW_COPY_AND_ASSIGN(WallpaperManagerTest); 105 }; 106 107 // Test for crbug.com/260755. If this test fails, it is probably because the 108 // wallpaper of last logged in user is set as guest wallpaper. 109 TEST_F(WallpaperManagerTest, GuestUserUseGuestWallpaper) { 110 UserManager::Get()->UserLoggedIn(kTestUser1, kTestUser1, false); 111 112 base::FilePath old_wallpaper_path = WallpaperManager::Get()-> 113 GetOriginalWallpaperPathForUser(kTestUser1); 114 115 // Saves wallpaper info to local state for user |kTestUser1|. 116 WallpaperInfo info = { 117 "DUMMY", 118 WALLPAPER_LAYOUT_CENTER_CROPPED, 119 User::CUSTOMIZED, 120 base::Time::Now().LocalMidnight() 121 }; 122 WallpaperManager::Get()->SetUserWallpaperInfo(kTestUser1, info, true); 123 ResetUserManager(); 124 125 AppendGuestSwitch(); 126 scoped_ptr<WallpaperManager::TestApi> test_api; 127 test_api.reset(new WallpaperManager::TestApi(WallpaperManager::Get())); 128 // If last logged in user's wallpaper is used in function InitializeWallpaper, 129 // this test will crash. InitializeWallpaper should be a noop after 130 // AppendGuestSwitch being called. 131 WallpaperManager::Get()->InitializeWallpaper(); 132 EXPECT_TRUE(test_api->current_wallpaper_path().empty()); 133 UserManager::Get()->UserLoggedIn(UserManager::kGuestUserName, 134 UserManager::kGuestUserName, false); 135 EXPECT_FALSE(ash::Shell::GetInstance()->desktop_background_controller()-> 136 SetDefaultWallpaper(true)); 137 } 138 139 } // namespace chromeos 140