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