Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: chrome/browser/chromeos/login/users/user_manager_unittest.cc

Issue 1019283004: Switch to direct use of OwnerSettingsServiceChromeOS::Set() in tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <cstdlib> 5 #include <cstdlib>
6 #include <cstring> 6 #include <cstring>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/pref_service.h" 12 #include "base/prefs/pref_service.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chromeos/login/users/chrome_user_manager_impl.h" 15 #include "chrome/browser/chromeos/login/users/chrome_user_manager_impl.h"
17 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
18 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h" 17 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h" 18 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
20 #include "chrome/browser/chromeos/settings/device_settings_service.h"
21 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
22 #include "chrome/browser/profiles/profile_manager.h" 19 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/test/base/scoped_testing_local_state.h" 20 #include "chrome/test/base/scoped_testing_local_state.h"
24 #include "chrome/test/base/testing_browser_process.h" 21 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
26 #include "chromeos/chromeos_switches.h" 23 #include "chromeos/chromeos_switches.h"
27 #include "chromeos/dbus/dbus_thread_manager.h" 24 #include "chromeos/dbus/dbus_thread_manager.h"
28 #include "chromeos/settings/cros_settings_names.h" 25 #include "chromeos/settings/cros_settings_names.h"
29 #include "chromeos/settings/cros_settings_provider.h"
30 #include "components/user_manager/user.h" 26 #include "components/user_manager/user.h"
31 #include "components/user_manager/user_manager.h" 27 #include "components/user_manager/user_manager.h"
32 #include "content/public/common/content_switches.h" 28 #include "content/public/common/content_switches.h"
33 #include "content/public/test/test_browser_thread_bundle.h" 29 #include "content/public/test/test_browser_thread_bundle.h"
34 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
35 31
36 namespace chromeos { 32 namespace chromeos {
37 33
38 class UnittestProfileManager : public ::ProfileManagerWithoutInit { 34 class UnittestProfileManager : public ::ProfileManagerWithoutInit {
39 public: 35 public:
40 explicit UnittestProfileManager(const base::FilePath& user_data_dir) 36 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
41 : ::ProfileManagerWithoutInit(user_data_dir) {} 37 : ::ProfileManagerWithoutInit(user_data_dir) {}
42 38
43 protected: 39 protected:
44 Profile* CreateProfileHelper(const base::FilePath& file_path) override { 40 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
45 if (!base::PathExists(file_path)) { 41 if (!base::PathExists(file_path)) {
46 if (!base::CreateDirectory(file_path)) 42 if (!base::CreateDirectory(file_path))
47 return NULL; 43 return NULL;
48 } 44 }
49 return new TestingProfile(file_path, NULL); 45 return new TestingProfile(file_path, NULL);
50 } 46 }
51 }; 47 };
52 48
53
54 class UserManagerTest : public testing::Test { 49 class UserManagerTest : public testing::Test {
55 protected: 50 protected:
56 void SetUp() override { 51 void SetUp() override {
57 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); 52 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
58 command_line.AppendSwitch(::switches::kTestType); 53 command_line.AppendSwitch(::switches::kTestType);
59 command_line.AppendSwitch( 54 command_line.AppendSwitch(
60 chromeos::switches::kIgnoreUserProfileMappingForTests); 55 chromeos::switches::kIgnoreUserProfileMappingForTests);
61 56
62 cros_settings_ = CrosSettings::Get(); 57 settings_helper_.ReplaceProvider(kDeviceOwner);
63
64 // Replace the real DeviceSettingsProvider with a stub.
65 device_settings_provider_ =
66 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo);
67 EXPECT_TRUE(device_settings_provider_);
68 EXPECT_TRUE(
69 cros_settings_->RemoveSettingsProvider(device_settings_provider_));
70 cros_settings_->AddSettingsProvider(&stub_settings_provider_);
71 58
72 // Populate the stub DeviceSettingsProvider with valid values. 59 // Populate the stub DeviceSettingsProvider with valid values.
73 SetDeviceSettings(false, "", false); 60 SetDeviceSettings(false, "", false);
74 61
75 // Register an in-memory local settings instance. 62 // Register an in-memory local settings instance.
76 local_state_.reset( 63 local_state_.reset(
77 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal())); 64 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
78 65
79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
80 TestingBrowserProcess::GetGlobal()->SetProfileManager( 67 TestingBrowserProcess::GetGlobal()->SetProfileManager(
81 new UnittestProfileManager(temp_dir_.path())); 68 new UnittestProfileManager(temp_dir_.path()));
82 69
83 chromeos::DBusThreadManager::Initialize(); 70 chromeos::DBusThreadManager::Initialize();
84 71
85 ResetUserManager(); 72 ResetUserManager();
86 WallpaperManager::Initialize(); 73 WallpaperManager::Initialize();
87 } 74 }
88 75
89 void TearDown() override { 76 void TearDown() override {
90 // Unregister the in-memory local settings instance. 77 // Unregister the in-memory local settings instance.
91 local_state_.reset(); 78 local_state_.reset();
92 79
93 // Restore the real DeviceSettingsProvider.
94 EXPECT_TRUE(
95 cros_settings_->RemoveSettingsProvider(&stub_settings_provider_));
96 cros_settings_->AddSettingsProvider(device_settings_provider_);
97
98 // Shut down the DeviceSettingsService. 80 // Shut down the DeviceSettingsService.
99 DeviceSettingsService::Get()->UnsetSessionManager(); 81 DeviceSettingsService::Get()->UnsetSessionManager();
100 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL); 82 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
101 83
102 base::RunLoop().RunUntilIdle(); 84 base::RunLoop().RunUntilIdle();
103 chromeos::DBusThreadManager::Shutdown(); 85 chromeos::DBusThreadManager::Shutdown();
104 WallpaperManager::Shutdown(); 86 WallpaperManager::Shutdown();
105 } 87 }
106 88
107 ChromeUserManagerImpl* GetChromeUserManager() const { 89 ChromeUserManagerImpl* GetChromeUserManager() const {
(...skipping 25 matching lines...) Expand all
133 user_manager_enabler_.reset( 115 user_manager_enabler_.reset(
134 new ScopedUserManagerEnabler(new ChromeUserManagerImpl)); 116 new ScopedUserManagerEnabler(new ChromeUserManagerImpl));
135 117
136 // ChromeUserManagerImpl ctor posts a task to reload policies. 118 // ChromeUserManagerImpl ctor posts a task to reload policies.
137 base::RunLoop().RunUntilIdle(); 119 base::RunLoop().RunUntilIdle();
138 } 120 }
139 121
140 void SetDeviceSettings(bool ephemeral_users_enabled, 122 void SetDeviceSettings(bool ephemeral_users_enabled,
141 const std::string &owner, 123 const std::string &owner,
142 bool supervised_users_enabled) { 124 bool supervised_users_enabled) {
143 base::FundamentalValue 125 settings_helper_.SetBoolean(kAccountsPrefEphemeralUsersEnabled,
144 ephemeral_users_enabled_value(ephemeral_users_enabled); 126 ephemeral_users_enabled);
145 stub_settings_provider_.Set(kAccountsPrefEphemeralUsersEnabled, 127 settings_helper_.SetString(kDeviceOwner, owner);
146 ephemeral_users_enabled_value); 128 settings_helper_.SetBoolean(kAccountsPrefSupervisedUsersEnabled,
147 base::StringValue owner_value(owner); 129 supervised_users_enabled);
148 stub_settings_provider_.Set(kDeviceOwner, owner_value);
149 stub_settings_provider_.Set(kAccountsPrefSupervisedUsersEnabled,
150 base::FundamentalValue(supervised_users_enabled));
151 } 130 }
152 131
153 void RetrieveTrustedDevicePolicies() { 132 void RetrieveTrustedDevicePolicies() {
154 GetChromeUserManager()->RetrieveTrustedDevicePolicies(); 133 GetChromeUserManager()->RetrieveTrustedDevicePolicies();
155 } 134 }
156 135
157 protected: 136 protected:
158 content::TestBrowserThreadBundle thread_bundle_; 137 content::TestBrowserThreadBundle thread_bundle_;
159 138
160 CrosSettings* cros_settings_; 139 ScopedCrosSettingsTestHelper settings_helper_;
161 CrosSettingsProvider* device_settings_provider_;
162 StubCrosSettingsProvider stub_settings_provider_;
163 scoped_ptr<ScopedTestingLocalState> local_state_; 140 scoped_ptr<ScopedTestingLocalState> local_state_;
164 141
165 ScopedTestDeviceSettingsService test_device_settings_service_;
166 ScopedTestCrosSettings test_cros_settings_;
167
168 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_; 142 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
169 base::ScopedTempDir temp_dir_; 143 base::ScopedTempDir temp_dir_;
170 }; 144 };
171 145
172 TEST_F(UserManagerTest, RetrieveTrustedDevicePolicies) { 146 TEST_F(UserManagerTest, RetrieveTrustedDevicePolicies) {
173 SetUserManagerEphemeralUsersEnabled(true); 147 SetUserManagerEphemeralUsersEnabled(true);
174 SetUserManagerOwnerEmail(""); 148 SetUserManagerOwnerEmail("");
175 149
176 SetDeviceSettings(false, "owner@invalid.domain", false); 150 SetDeviceSettings(false, "owner@invalid.domain", false);
177 RetrieveTrustedDevicePolicies(); 151 RetrieveTrustedDevicePolicies();
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 "user0@invalid.domain", "user0@invalid.domain", false); 191 "user0@invalid.domain", "user0@invalid.domain", false);
218 ResetUserManager(); 192 ResetUserManager();
219 193
220 const user_manager::UserList* users = 194 const user_manager::UserList* users =
221 &user_manager::UserManager::Get()->GetUsers(); 195 &user_manager::UserManager::Get()->GetUsers();
222 EXPECT_EQ(1U, users->size()); 196 EXPECT_EQ(1U, users->size());
223 EXPECT_EQ((*users)[0]->email(), "owner@invalid.domain"); 197 EXPECT_EQ((*users)[0]->email(), "owner@invalid.domain");
224 } 198 }
225 199
226 } // namespace chromeos 200 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/kiosk_browsertest.cc ('k') | chrome/browser/chromeos/ownership/fake_owner_settings_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698