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(Observer* observer) {
107 void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
109 void DeviceSettingsTestHelper::RestartJob(int pid,
110 const std::string& command_line) {}
112 void DeviceSettingsTestHelper::StartSession(const std::string& user_email) {}
114 void DeviceSettingsTestHelper::StopSession() {}
116 void DeviceSettingsTestHelper::NotifySupervisedUserCreationStarted() {}
118 void DeviceSettingsTestHelper::NotifySupervisedUserCreationFinished() {}
120 void DeviceSettingsTestHelper::StartDeviceWipe() {}
122 void DeviceSettingsTestHelper::RequestLockScreen() {}
124 void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
126 void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
128 void DeviceSettingsTestHelper::RetrieveActiveSessions(
129 const ActiveSessionsCallback& callback) {}
131 void DeviceSettingsTestHelper::RetrieveDevicePolicy(
132 const RetrievePolicyCallback& callback) {
133 device_policy_.retrieve_callbacks_.push_back(callback);
136 void DeviceSettingsTestHelper::RetrievePolicyForUser(
137 const std::string& username,
138 const RetrievePolicyCallback& callback) {
141 std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
142 const std::string& username) {
146 void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy(
147 const std::string& account_id,
148 const RetrievePolicyCallback& callback) {
149 device_local_account_policy_[account_id].retrieve_callbacks_.push_back(
153 void DeviceSettingsTestHelper::StoreDevicePolicy(
154 const std::string& policy_blob,
155 const StorePolicyCallback& callback) {
156 device_policy_.policy_blob_ = policy_blob;
157 device_policy_.store_callbacks_.push_back(callback);
160 void DeviceSettingsTestHelper::StorePolicyForUser(
161 const std::string& username,
162 const std::string& policy_blob,
163 const StorePolicyCallback& callback) {
166 void DeviceSettingsTestHelper::StoreDeviceLocalAccountPolicy(
167 const std::string& account_id,
168 const std::string& policy_blob,
169 const StorePolicyCallback& callback) {
170 device_local_account_policy_[account_id].policy_blob_ = policy_blob;
171 device_local_account_policy_[account_id].store_callbacks_.push_back(callback);
174 void DeviceSettingsTestHelper::SetFlagsForUser(
175 const std::string& account_id,
176 const std::vector<std::string>& flags) {}
178 void DeviceSettingsTestHelper::GetServerBackedStateKeys(
179 const StateKeysCallback& callback) {}
181 DeviceSettingsTestHelper::PolicyState::PolicyState()
182 : store_result_(true) {}
184 DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
186 ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
187 DeviceSettingsService::Initialize();
188 DeviceSettingsService::Get()->SetSessionManager(
189 this, new ownership::MockOwnerKeyUtil());
190 DeviceSettingsService::Get()->Load();
194 ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
196 DeviceSettingsService::Get()->UnsetSessionManager();
197 DeviceSettingsService::Shutdown();
200 DeviceSettingsTestBase::DeviceSettingsTestBase()
201 : user_manager_(new FakeUserManager()),
202 user_manager_enabler_(user_manager_),
203 owner_key_util_(new ownership::MockOwnerKeyUtil()) {
204 OwnerSettingsServiceChromeOSFactory::SetDeviceSettingsServiceForTesting(
205 &device_settings_service_);
206 OwnerSettingsServiceChromeOSFactory::GetInstance()->SetOwnerKeyUtilForTesting(
210 DeviceSettingsTestBase::~DeviceSettingsTestBase() {
211 base::RunLoop().RunUntilIdle();
214 void DeviceSettingsTestBase::SetUp() {
215 // Initialize DBusThreadManager with a stub implementation.
216 dbus_setter_ = chromeos::DBusThreadManager::GetSetterForTesting();
218 base::RunLoop().RunUntilIdle();
220 device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
222 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
223 device_policy_.Build();
224 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
225 device_settings_service_.SetSessionManager(&device_settings_test_helper_,
227 profile_.reset(new TestingProfile());
230 void DeviceSettingsTestBase::TearDown() {
231 OwnerSettingsServiceChromeOSFactory::SetDeviceSettingsServiceForTesting(NULL);
232 FlushDeviceSettings();
233 device_settings_service_.UnsetSessionManager();
234 DBusThreadManager::Shutdown();
237 void DeviceSettingsTestBase::FlushDeviceSettings() {
238 device_settings_test_helper_.Flush();
241 void DeviceSettingsTestBase::ReloadDeviceSettings() {
242 device_settings_service_.OwnerKeySet(true);
243 FlushDeviceSettings();
246 void DeviceSettingsTestBase::InitOwner(const std::string& user_id,
248 const user_manager::User* user = user_manager_->FindUser(user_id);
250 user = user_manager_->AddUser(user_id);
251 profile_->set_profile_name(user_id);
253 ProfileHelper::Get()->SetUserToProfileMappingForTesting(user,
256 OwnerSettingsServiceChromeOS* service =
257 OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get());
260 service->OnTPMTokenReady(true /* token is enabled */);
263 } // namespace chromeos