device_settings_test_helper.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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.
4
5#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
6
7#include "base/message_loop.h"
8#include "base/run_loop.h"
9#include "base/threading/sequenced_worker_pool.h"
10#include "chrome/browser/chromeos/settings/device_settings_service.h"
11#include "chrome/browser/chromeos/settings/mock_owner_key_util.h"
12#include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h"
13#include "content/public/browser/browser_thread.h"
14
15namespace chromeos {
16
17DeviceSettingsTestHelper::DeviceSettingsTestHelper() {}
18
19DeviceSettingsTestHelper::~DeviceSettingsTestHelper() {}
20
21void DeviceSettingsTestHelper::FlushLoops() {
22  // DeviceSettingsService may trigger operations that hop back and forth
23  // between the message loop and the blocking pool. 2 iterations are currently
24  // sufficient (key loading, signing).
25  for (int i = 0; i < 2; ++i) {
26    base::MessageLoop::current()->RunUntilIdle();
27    content::BrowserThread::GetBlockingPool()->FlushForTesting();
28  }
29  base::MessageLoop::current()->RunUntilIdle();
30}
31
32void DeviceSettingsTestHelper::FlushStore() {
33  std::vector<StorePolicyCallback> callbacks;
34  callbacks.swap(device_policy_.store_callbacks_);
35  for (std::vector<StorePolicyCallback>::iterator cb(callbacks.begin());
36       cb != callbacks.end(); ++cb) {
37    cb->Run(device_policy_.store_result_);
38  }
39
40  std::map<std::string, PolicyState>::iterator device_local_account_state;
41  for (device_local_account_state = device_local_account_policy_.begin();
42       device_local_account_state != device_local_account_policy_.end();
43       ++device_local_account_state) {
44    callbacks.swap(device_local_account_state->second.store_callbacks_);
45    for (std::vector<StorePolicyCallback>::iterator cb(callbacks.begin());
46         cb != callbacks.end(); ++cb) {
47      cb->Run(device_local_account_state->second.store_result_);
48    }
49  }
50}
51
52void DeviceSettingsTestHelper::FlushRetrieve() {
53  std::vector<RetrievePolicyCallback> callbacks;
54  callbacks.swap(device_policy_.retrieve_callbacks_);
55  for (std::vector<RetrievePolicyCallback>::iterator cb(callbacks.begin());
56       cb != callbacks.end(); ++cb) {
57    cb->Run(device_policy_.policy_blob_);
58  }
59
60  std::map<std::string, PolicyState>::iterator device_local_account_state;
61  for (device_local_account_state = device_local_account_policy_.begin();
62       device_local_account_state != device_local_account_policy_.end();
63       ++device_local_account_state) {
64    callbacks.swap(device_local_account_state->second.retrieve_callbacks_);
65    for (std::vector<RetrievePolicyCallback>::iterator cb(callbacks.begin());
66         cb != callbacks.end(); ++cb) {
67      cb->Run(device_local_account_state->second.policy_blob_);
68    }
69  }
70}
71
72void DeviceSettingsTestHelper::Flush() {
73  do {
74    FlushLoops();
75    FlushStore();
76    FlushLoops();
77    FlushRetrieve();
78    FlushLoops();
79  } while (HasPendingOperations());
80}
81
82bool DeviceSettingsTestHelper::HasPendingOperations() const {
83  if (device_policy_.HasPendingOperations())
84    return true;
85
86  std::map<std::string, PolicyState>::const_iterator device_local_account_state;
87  for (device_local_account_state = device_local_account_policy_.begin();
88       device_local_account_state != device_local_account_policy_.end();
89       ++device_local_account_state) {
90    if (device_local_account_state->second.HasPendingOperations())
91      return true;
92  }
93
94  return false;
95}
96
97void DeviceSettingsTestHelper::AddObserver(Observer* observer) {}
98
99void DeviceSettingsTestHelper::RemoveObserver(Observer* observer) {}
100
101bool DeviceSettingsTestHelper::HasObserver(Observer* observer) {
102  return false;
103}
104
105void DeviceSettingsTestHelper::EmitLoginPromptReady() {}
106
107void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
108
109void DeviceSettingsTestHelper::RestartJob(int pid,
110                                          const std::string& command_line) {}
111
112void DeviceSettingsTestHelper::RestartEntd() {}
113
114void DeviceSettingsTestHelper::StartSession(const std::string& user_email) {}
115
116void DeviceSettingsTestHelper::StopSession() {}
117
118void DeviceSettingsTestHelper::StartDeviceWipe() {}
119
120void DeviceSettingsTestHelper::RequestLockScreen() {}
121
122void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
123
124void DeviceSettingsTestHelper::RequestUnlockScreen() {}
125
126void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
127
128void DeviceSettingsTestHelper::RetrieveActiveSessions(
129      const ActiveSessionsCallback& callback) {}
130
131void DeviceSettingsTestHelper::RetrieveDevicePolicy(
132    const RetrievePolicyCallback& callback) {
133  device_policy_.retrieve_callbacks_.push_back(callback);
134}
135
136void DeviceSettingsTestHelper::RetrievePolicyForUser(
137    const std::string& username,
138    const RetrievePolicyCallback& callback) {
139}
140
141void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy(
142    const std::string& account_id,
143    const RetrievePolicyCallback& callback) {
144  device_local_account_policy_[account_id].retrieve_callbacks_.push_back(
145      callback);
146}
147
148void DeviceSettingsTestHelper::StoreDevicePolicy(
149    const std::string& policy_blob,
150    const StorePolicyCallback& callback) {
151  device_policy_.policy_blob_ = policy_blob;
152  device_policy_.store_callbacks_.push_back(callback);
153}
154
155void DeviceSettingsTestHelper::StorePolicyForUser(
156    const std::string& username,
157    const std::string& policy_blob,
158    const std::string& policy_key,
159    const StorePolicyCallback& callback) {
160}
161
162void 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);
168}
169
170DeviceSettingsTestHelper::PolicyState::PolicyState()
171    : store_result_(true) {}
172
173DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
174
175ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
176  DeviceSettingsService::Initialize();
177  DeviceSettingsService::Get()->SetSessionManager(this, new MockOwnerKeyUtil());
178  DeviceSettingsService::Get()->Load();
179  Flush();
180}
181
182ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
183  Flush();
184  DeviceSettingsService::Get()->UnsetSessionManager();
185  DeviceSettingsService::Shutdown();
186}
187
188DeviceSettingsTestBase::DeviceSettingsTestBase()
189    : loop_(base::MessageLoop::TYPE_UI),
190      ui_thread_(content::BrowserThread::UI, &loop_),
191      file_thread_(content::BrowserThread::FILE, &loop_),
192      owner_key_util_(new MockOwnerKeyUtil()) {}
193
194DeviceSettingsTestBase::~DeviceSettingsTestBase() {
195  base::RunLoop().RunUntilIdle();
196}
197
198void DeviceSettingsTestBase::SetUp() {
199  device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
200      false);
201  owner_key_util_->SetPublicKeyFromPrivateKey(device_policy_.signing_key());
202  device_policy_.Build();
203  device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
204  device_settings_service_.SetSessionManager(&device_settings_test_helper_,
205                                             owner_key_util_);
206}
207
208void DeviceSettingsTestBase::TearDown() {
209  FlushDeviceSettings();
210  device_settings_service_.UnsetSessionManager();
211}
212
213void DeviceSettingsTestBase::FlushDeviceSettings() {
214  device_settings_test_helper_.Flush();
215}
216
217void DeviceSettingsTestBase::ReloadDeviceSettings() {
218  device_settings_service_.OwnerKeySet(true);
219  FlushDeviceSettings();
220}
221
222}  // namespace chromeos
223