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::StartDeviceWipe() {}
118 void DeviceSettingsTestHelper::RequestLockScreen() {}
120 void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
122 void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
124 void DeviceSettingsTestHelper::RetrieveActiveSessions(
125 const ActiveSessionsCallback& callback) {}
127 void DeviceSettingsTestHelper::RetrieveDevicePolicy(
128 const RetrievePolicyCallback& callback) {
129 device_policy_.retrieve_callbacks_.push_back(callback);
132 void DeviceSettingsTestHelper::RetrievePolicyForUser(
133 const std::string& username,
134 const RetrievePolicyCallback& callback) {
137 std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
138 const std::string& username) {
142 void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy(
143 const std::string& account_id,
144 const RetrievePolicyCallback& callback) {
145 device_local_account_policy_[account_id].retrieve_callbacks_.push_back(
149 void DeviceSettingsTestHelper::StoreDevicePolicy(
150 const std::string& policy_blob,
151 const StorePolicyCallback& callback) {
152 device_policy_.policy_blob_ = policy_blob;
153 device_policy_.store_callbacks_.push_back(callback);
156 void DeviceSettingsTestHelper::StorePolicyForUser(
157 const std::string& username,
158 const std::string& policy_blob,
159 const StorePolicyCallback& callback) {
162 void DeviceSettingsTestHelper::StoreDeviceLocalAccountPolicy(
163 const std::string& account_id,
164 const std::string& policy_blob,
165 const StorePolicyCallback& callback) {
166 device_local_account_policy_[account_id].policy_blob_ = policy_blob;
167 device_local_account_policy_[account_id].store_callbacks_.push_back(callback);
170 void DeviceSettingsTestHelper::SetFlagsForUser(
171 const std::string& account_id,
172 const std::vector<std::string>& flags) {}
174 void DeviceSettingsTestHelper::GetServerBackedStateKeys(
175 const StateKeysCallback& callback) {}
177 DeviceSettingsTestHelper::PolicyState::PolicyState()
178 : store_result_(true) {}
180 DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
182 ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
183 DeviceSettingsService::Initialize();
184 DeviceSettingsService::Get()->SetSessionManager(
185 this, new ownership::MockOwnerKeyUtil());
186 DeviceSettingsService::Get()->Load();
190 ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
192 DeviceSettingsService::Get()->UnsetSessionManager();
193 DeviceSettingsService::Shutdown();
196 DeviceSettingsTestBase::DeviceSettingsTestBase()
197 : user_manager_(new FakeUserManager()),
198 user_manager_enabler_(user_manager_),
199 owner_key_util_(new ownership::MockOwnerKeyUtil()) {
200 OwnerSettingsServiceChromeOSFactory::GetInstance()->SetOwnerKeyUtilForTesting(
204 DeviceSettingsTestBase::~DeviceSettingsTestBase() {
205 base::RunLoop().RunUntilIdle();
208 void DeviceSettingsTestBase::SetUp() {
209 // Initialize DBusThreadManager with a stub implementation.
210 dbus_setter_ = chromeos::DBusThreadManager::GetSetterForTesting();
212 base::RunLoop().RunUntilIdle();
214 device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
216 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
217 device_policy_.Build();
218 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
219 device_settings_service_.SetSessionManager(&device_settings_test_helper_,
221 OwnerSettingsServiceChromeOS::SetDeviceSettingsServiceForTesting(
222 &device_settings_service_);
223 profile_.reset(new TestingProfile());
226 void DeviceSettingsTestBase::TearDown() {
227 OwnerSettingsServiceChromeOS::SetDeviceSettingsServiceForTesting(NULL);
228 FlushDeviceSettings();
229 device_settings_service_.UnsetSessionManager();
230 DBusThreadManager::Shutdown();
233 void DeviceSettingsTestBase::FlushDeviceSettings() {
234 device_settings_test_helper_.Flush();
237 void DeviceSettingsTestBase::ReloadDeviceSettings() {
238 device_settings_service_.OwnerKeySet(true);
239 FlushDeviceSettings();
242 void DeviceSettingsTestBase::InitOwner(const std::string& user_id,
244 const user_manager::User* user = user_manager_->FindUser(user_id);
246 user = user_manager_->AddUser(user_id);
247 profile_->set_profile_name(user_id);
249 ProfileHelper::Get()->SetUserToProfileMappingForTesting(user,
252 OwnerSettingsServiceChromeOS* service =
253 OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get());
256 service->OnTPMTokenReady(true /* token is enabled */);
259 } // namespace chromeos