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/policy/proto/chrome_device_policy.pb.h"
11 #include "chrome/browser/chromeos/settings/device_settings_service.h"
12 #include "chrome/browser/chromeos/settings/mock_owner_key_util.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/network/network_handler.h"
15 #include "content/public/browser/browser_thread.h"
19 DeviceSettingsTestHelper::DeviceSettingsTestHelper() {}
21 DeviceSettingsTestHelper::~DeviceSettingsTestHelper() {}
23 void DeviceSettingsTestHelper::FlushLoops() {
24 // DeviceSettingsService may trigger operations that hop back and forth
25 // between the message loop and the blocking pool. 2 iterations are currently
26 // sufficient (key loading, signing).
27 for (int i
= 0; i
< 2; ++i
) {
28 base::MessageLoop::current()->RunUntilIdle();
29 content::BrowserThread::GetBlockingPool()->FlushForTesting();
31 base::MessageLoop::current()->RunUntilIdle();
34 void DeviceSettingsTestHelper::FlushStore() {
35 std::vector
<StorePolicyCallback
> callbacks
;
36 callbacks
.swap(device_policy_
.store_callbacks_
);
37 for (std::vector
<StorePolicyCallback
>::iterator
cb(callbacks
.begin());
38 cb
!= callbacks
.end(); ++cb
) {
39 cb
->Run(device_policy_
.store_result_
);
42 std::map
<std::string
, PolicyState
>::iterator device_local_account_state
;
43 for (device_local_account_state
= device_local_account_policy_
.begin();
44 device_local_account_state
!= device_local_account_policy_
.end();
45 ++device_local_account_state
) {
46 callbacks
.swap(device_local_account_state
->second
.store_callbacks_
);
47 for (std::vector
<StorePolicyCallback
>::iterator
cb(callbacks
.begin());
48 cb
!= callbacks
.end(); ++cb
) {
49 cb
->Run(device_local_account_state
->second
.store_result_
);
54 void DeviceSettingsTestHelper::FlushRetrieve() {
55 std::vector
<RetrievePolicyCallback
> callbacks
;
56 callbacks
.swap(device_policy_
.retrieve_callbacks_
);
57 for (std::vector
<RetrievePolicyCallback
>::iterator
cb(callbacks
.begin());
58 cb
!= callbacks
.end(); ++cb
) {
59 cb
->Run(device_policy_
.policy_blob_
);
62 std::map
<std::string
, PolicyState
>::iterator device_local_account_state
;
63 for (device_local_account_state
= device_local_account_policy_
.begin();
64 device_local_account_state
!= device_local_account_policy_
.end();
65 ++device_local_account_state
) {
66 std::vector
<RetrievePolicyCallback
> callbacks
;
67 callbacks
.swap(device_local_account_state
->second
.retrieve_callbacks_
);
68 for (std::vector
<RetrievePolicyCallback
>::iterator
cb(callbacks
.begin());
69 cb
!= callbacks
.end(); ++cb
) {
70 cb
->Run(device_local_account_state
->second
.policy_blob_
);
75 void DeviceSettingsTestHelper::Flush() {
82 } while (HasPendingOperations());
85 bool DeviceSettingsTestHelper::HasPendingOperations() const {
86 if (device_policy_
.HasPendingOperations())
89 std::map
<std::string
, PolicyState
>::const_iterator device_local_account_state
;
90 for (device_local_account_state
= device_local_account_policy_
.begin();
91 device_local_account_state
!= device_local_account_policy_
.end();
92 ++device_local_account_state
) {
93 if (device_local_account_state
->second
.HasPendingOperations())
100 void DeviceSettingsTestHelper::Init(dbus::Bus
* bus
) {}
102 void DeviceSettingsTestHelper::AddObserver(Observer
* observer
) {}
104 void DeviceSettingsTestHelper::RemoveObserver(Observer
* observer
) {}
106 bool DeviceSettingsTestHelper::HasObserver(Observer
* observer
) {
110 void DeviceSettingsTestHelper::EmitLoginPromptReady() {}
112 void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
114 void DeviceSettingsTestHelper::RestartJob(int pid
,
115 const std::string
& command_line
) {}
117 void DeviceSettingsTestHelper::StartSession(const std::string
& user_email
) {}
119 void DeviceSettingsTestHelper::StopSession() {}
121 void DeviceSettingsTestHelper::StartDeviceWipe() {}
123 void DeviceSettingsTestHelper::RequestLockScreen() {}
125 void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
127 void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
129 void DeviceSettingsTestHelper::RetrieveActiveSessions(
130 const ActiveSessionsCallback
& callback
) {}
132 void DeviceSettingsTestHelper::RetrieveDevicePolicy(
133 const RetrievePolicyCallback
& callback
) {
134 device_policy_
.retrieve_callbacks_
.push_back(callback
);
137 void DeviceSettingsTestHelper::RetrievePolicyForUser(
138 const std::string
& username
,
139 const RetrievePolicyCallback
& callback
) {
142 std::string
DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
143 const std::string
& username
) {
147 void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy(
148 const std::string
& account_id
,
149 const RetrievePolicyCallback
& callback
) {
150 device_local_account_policy_
[account_id
].retrieve_callbacks_
.push_back(
154 void DeviceSettingsTestHelper::StoreDevicePolicy(
155 const std::string
& policy_blob
,
156 const StorePolicyCallback
& callback
) {
157 device_policy_
.policy_blob_
= policy_blob
;
158 device_policy_
.store_callbacks_
.push_back(callback
);
161 void DeviceSettingsTestHelper::StorePolicyForUser(
162 const std::string
& username
,
163 const std::string
& policy_blob
,
164 const std::string
& policy_key
,
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 DeviceSettingsTestHelper::PolicyState::PolicyState()
181 : store_result_(true) {}
183 DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
185 ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
186 DeviceSettingsService::Initialize();
187 DeviceSettingsService::Get()->SetSessionManager(this, new MockOwnerKeyUtil());
188 DeviceSettingsService::Get()->Load();
192 ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
194 DeviceSettingsService::Get()->UnsetSessionManager();
195 DeviceSettingsService::Shutdown();
198 DeviceSettingsTestBase::DeviceSettingsTestBase()
199 : ui_thread_(content::BrowserThread::UI
, &loop_
),
200 file_thread_(content::BrowserThread::FILE, &loop_
),
201 owner_key_util_(new MockOwnerKeyUtil()) {}
203 DeviceSettingsTestBase::~DeviceSettingsTestBase() {
204 base::RunLoop().RunUntilIdle();
207 void DeviceSettingsTestBase::SetUp() {
208 // Initialize DBusThreadManager with a stub implementation.
209 DBusThreadManager::InitializeWithStub();
210 NetworkHandler::Initialize();
211 loop_
.RunUntilIdle();
213 device_policy_
.payload().mutable_metrics_enabled()->set_metrics_enabled(
215 owner_key_util_
->SetPublicKeyFromPrivateKey(*device_policy_
.GetSigningKey());
216 device_policy_
.Build();
217 device_settings_test_helper_
.set_policy_blob(device_policy_
.GetBlob());
218 device_settings_service_
.SetSessionManager(&device_settings_test_helper_
,
222 void DeviceSettingsTestBase::TearDown() {
223 FlushDeviceSettings();
224 device_settings_service_
.UnsetSessionManager();
225 NetworkHandler::Shutdown();
226 DBusThreadManager::Shutdown();
229 void DeviceSettingsTestBase::FlushDeviceSettings() {
230 device_settings_test_helper_
.Flush();
233 void DeviceSettingsTestBase::ReloadDeviceSettings() {
234 device_settings_service_
.OwnerKeySet(true);
235 FlushDeviceSettings();
238 } // namespace chromeos