1 // Copyright (c) 2012 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 "chrome/browser/chromeos/settings/device_settings_test_helper.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
11 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
12 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h"
13 #include "chrome/browser/chromeos/profiles/profile_helper.h"
14 #include "chrome/browser/chromeos/settings/device_settings_service.h"
15 #include "chrome/test/base/testing_browser_process.h"
16 #include "chrome/test/base/testing_profile.h"
17 #include "chromeos/dbus/dbus_thread_manager.h"
18 #include "components/ownership/mock_owner_key_util.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/test/test_utils.h"
24 DeviceSettingsTestHelper::DeviceSettingsTestHelper() {}
26 DeviceSettingsTestHelper::~DeviceSettingsTestHelper() {}
28 void DeviceSettingsTestHelper::FlushStore() {
29 std::vector
<StorePolicyCallback
> callbacks
;
30 callbacks
.swap(device_policy_
.store_callbacks_
);
31 for (std::vector
<StorePolicyCallback
>::iterator
cb(callbacks
.begin());
32 cb
!= callbacks
.end(); ++cb
) {
33 cb
->Run(device_policy_
.store_result_
);
36 std::map
<std::string
, PolicyState
>::iterator device_local_account_state
;
37 for (device_local_account_state
= device_local_account_policy_
.begin();
38 device_local_account_state
!= device_local_account_policy_
.end();
39 ++device_local_account_state
) {
40 callbacks
.swap(device_local_account_state
->second
.store_callbacks_
);
41 for (std::vector
<StorePolicyCallback
>::iterator
cb(callbacks
.begin());
42 cb
!= callbacks
.end(); ++cb
) {
43 cb
->Run(device_local_account_state
->second
.store_result_
);
48 void DeviceSettingsTestHelper::FlushRetrieve() {
49 std::vector
<RetrievePolicyCallback
> callbacks
;
50 callbacks
.swap(device_policy_
.retrieve_callbacks_
);
51 for (std::vector
<RetrievePolicyCallback
>::iterator
cb(callbacks
.begin());
52 cb
!= callbacks
.end(); ++cb
) {
53 cb
->Run(device_policy_
.policy_blob_
);
56 std::map
<std::string
, PolicyState
>::iterator device_local_account_state
;
57 for (device_local_account_state
= device_local_account_policy_
.begin();
58 device_local_account_state
!= device_local_account_policy_
.end();
59 ++device_local_account_state
) {
60 std::vector
<RetrievePolicyCallback
> callbacks
;
61 callbacks
.swap(device_local_account_state
->second
.retrieve_callbacks_
);
62 for (std::vector
<RetrievePolicyCallback
>::iterator
cb(callbacks
.begin());
63 cb
!= callbacks
.end(); ++cb
) {
64 cb
->Run(device_local_account_state
->second
.policy_blob_
);
69 void DeviceSettingsTestHelper::Flush() {
71 content::RunAllBlockingPoolTasksUntilIdle();
73 content::RunAllBlockingPoolTasksUntilIdle();
75 content::RunAllBlockingPoolTasksUntilIdle();
76 } while (HasPendingOperations());
79 bool DeviceSettingsTestHelper::HasPendingOperations() const {
80 if (device_policy_
.HasPendingOperations())
83 std::map
<std::string
, PolicyState
>::const_iterator device_local_account_state
;
84 for (device_local_account_state
= device_local_account_policy_
.begin();
85 device_local_account_state
!= device_local_account_policy_
.end();
86 ++device_local_account_state
) {
87 if (device_local_account_state
->second
.HasPendingOperations())
94 void DeviceSettingsTestHelper::Init(dbus::Bus
* bus
) {}
96 void DeviceSettingsTestHelper::SetStubDelegate(
97 SessionManagerClient::StubDelegate
* delegate
) {}
99 void DeviceSettingsTestHelper::AddObserver(Observer
* observer
) {}
101 void DeviceSettingsTestHelper::RemoveObserver(Observer
* observer
) {}
103 bool DeviceSettingsTestHelper::HasObserver(const Observer
* observer
) const {
107 bool DeviceSettingsTestHelper::IsScreenLocked() const { return false; }
109 void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
111 void DeviceSettingsTestHelper::RestartJob(int pid
,
112 const std::string
& command_line
) {}
114 void DeviceSettingsTestHelper::StartSession(const std::string
& user_email
) {}
116 void DeviceSettingsTestHelper::StopSession() {}
118 void DeviceSettingsTestHelper::NotifySupervisedUserCreationStarted() {}
120 void DeviceSettingsTestHelper::NotifySupervisedUserCreationFinished() {}
122 void DeviceSettingsTestHelper::StartDeviceWipe() {}
124 void DeviceSettingsTestHelper::RequestLockScreen() {}
126 void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
128 void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
130 void DeviceSettingsTestHelper::RetrieveActiveSessions(
131 const ActiveSessionsCallback
& callback
) {}
133 void DeviceSettingsTestHelper::RetrieveDevicePolicy(
134 const RetrievePolicyCallback
& callback
) {
135 device_policy_
.retrieve_callbacks_
.push_back(callback
);
138 void DeviceSettingsTestHelper::RetrievePolicyForUser(
139 const std::string
& username
,
140 const RetrievePolicyCallback
& callback
) {
143 std::string
DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
144 const std::string
& username
) {
148 void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy(
149 const std::string
& account_id
,
150 const RetrievePolicyCallback
& callback
) {
151 device_local_account_policy_
[account_id
].retrieve_callbacks_
.push_back(
155 void DeviceSettingsTestHelper::StoreDevicePolicy(
156 const std::string
& policy_blob
,
157 const StorePolicyCallback
& callback
) {
158 device_policy_
.policy_blob_
= policy_blob
;
159 device_policy_
.store_callbacks_
.push_back(callback
);
162 void DeviceSettingsTestHelper::StorePolicyForUser(
163 const std::string
& username
,
164 const std::string
& policy_blob
,
165 const StorePolicyCallback
& callback
) {
168 void DeviceSettingsTestHelper::StoreDeviceLocalAccountPolicy(
169 const std::string
& account_id
,
170 const std::string
& policy_blob
,
171 const StorePolicyCallback
& callback
) {
172 device_local_account_policy_
[account_id
].policy_blob_
= policy_blob
;
173 device_local_account_policy_
[account_id
].store_callbacks_
.push_back(callback
);
176 void DeviceSettingsTestHelper::SetFlagsForUser(
177 const std::string
& account_id
,
178 const std::vector
<std::string
>& flags
) {}
180 void DeviceSettingsTestHelper::GetServerBackedStateKeys(
181 const StateKeysCallback
& callback
) {}
183 DeviceSettingsTestHelper::PolicyState::PolicyState()
184 : store_result_(true) {}
186 DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
188 ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
189 DeviceSettingsService::Initialize();
190 DeviceSettingsService::Get()->SetSessionManager(
191 this, new ownership::MockOwnerKeyUtil());
192 DeviceSettingsService::Get()->Load();
196 ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
198 DeviceSettingsService::Get()->UnsetSessionManager();
199 DeviceSettingsService::Shutdown();
202 DeviceSettingsTestBase::DeviceSettingsTestBase()
203 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
204 user_manager_(new FakeChromeUserManager()),
205 user_manager_enabler_(user_manager_
),
206 owner_key_util_(new ownership::MockOwnerKeyUtil()) {
207 OwnerSettingsServiceChromeOSFactory::SetDeviceSettingsServiceForTesting(
208 &device_settings_service_
);
209 OwnerSettingsServiceChromeOSFactory::GetInstance()->SetOwnerKeyUtilForTesting(
213 DeviceSettingsTestBase::~DeviceSettingsTestBase() {
214 base::RunLoop().RunUntilIdle();
217 void DeviceSettingsTestBase::SetUp() {
218 // Initialize DBusThreadManager with a stub implementation.
219 dbus_setter_
= chromeos::DBusThreadManager::GetSetterForTesting();
221 base::RunLoop().RunUntilIdle();
223 device_policy_
.payload().mutable_metrics_enabled()->set_metrics_enabled(
225 owner_key_util_
->SetPublicKeyFromPrivateKey(*device_policy_
.GetSigningKey());
226 device_policy_
.Build();
227 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
228 device_settings_service_
.SetSessionManager(&device_settings_test_helper_
,
230 profile_
.reset(new TestingProfile());
233 void DeviceSettingsTestBase::TearDown() {
234 OwnerSettingsServiceChromeOSFactory::SetDeviceSettingsServiceForTesting(NULL
);
235 FlushDeviceSettings();
236 device_settings_service_
.UnsetSessionManager();
237 DBusThreadManager::Shutdown();
240 void DeviceSettingsTestBase::FlushDeviceSettings() {
241 device_settings_test_helper_
.Flush();
244 void DeviceSettingsTestBase::ReloadDeviceSettings() {
245 device_settings_service_
.OwnerKeySet(true);
246 FlushDeviceSettings();
249 void DeviceSettingsTestBase::InitOwner(const std::string
& user_id
,
251 const user_manager::User
* user
= user_manager_
->FindUser(user_id
);
253 user
= user_manager_
->AddUser(user_id
);
254 profile_
->set_profile_name(user_id
);
256 ProfileHelper::Get()->SetUserToProfileMappingForTesting(user
,
258 ProfileHelper::Get()->SetProfileToUserMappingForTesting(
259 const_cast<user_manager::User
*>(user
));
261 OwnerSettingsServiceChromeOS
* service
=
262 OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(profile_
.get());
265 service
->OnTPMTokenReady(true /* token is enabled */);
268 } // namespace chromeos