Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / settings / device_settings_test_helper.cc
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/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"
21
22 namespace chromeos {
23
24 DeviceSettingsTestHelper::DeviceSettingsTestHelper() {}
25
26 DeviceSettingsTestHelper::~DeviceSettingsTestHelper() {}
27
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_);
34   }
35
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_);
44     }
45   }
46 }
47
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_);
54   }
55
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_);
65     }
66   }
67 }
68
69 void DeviceSettingsTestHelper::Flush() {
70   do {
71     content::RunAllBlockingPoolTasksUntilIdle();
72     FlushStore();
73     content::RunAllBlockingPoolTasksUntilIdle();
74     FlushRetrieve();
75     content::RunAllBlockingPoolTasksUntilIdle();
76   } while (HasPendingOperations());
77 }
78
79 bool DeviceSettingsTestHelper::HasPendingOperations() const {
80   if (device_policy_.HasPendingOperations())
81     return true;
82
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())
88       return true;
89   }
90
91   return false;
92 }
93
94 void DeviceSettingsTestHelper::Init(dbus::Bus* bus) {}
95
96 void DeviceSettingsTestHelper::SetStubDelegate(
97     SessionManagerClient::StubDelegate* delegate) {}
98
99 void DeviceSettingsTestHelper::AddObserver(Observer* observer) {}
100
101 void DeviceSettingsTestHelper::RemoveObserver(Observer* observer) {}
102
103 bool DeviceSettingsTestHelper::HasObserver(Observer* observer) {
104   return false;
105 }
106
107 void DeviceSettingsTestHelper::EmitLoginPromptVisible() {}
108
109 void DeviceSettingsTestHelper::RestartJob(int pid,
110                                           const std::string& command_line) {}
111
112 void DeviceSettingsTestHelper::StartSession(const std::string& user_email) {}
113
114 void DeviceSettingsTestHelper::StopSession() {}
115
116 void DeviceSettingsTestHelper::StartDeviceWipe() {}
117
118 void DeviceSettingsTestHelper::RequestLockScreen() {}
119
120 void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
121
122 void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
123
124 void DeviceSettingsTestHelper::RetrieveActiveSessions(
125       const ActiveSessionsCallback& callback) {}
126
127 void DeviceSettingsTestHelper::RetrieveDevicePolicy(
128     const RetrievePolicyCallback& callback) {
129   device_policy_.retrieve_callbacks_.push_back(callback);
130 }
131
132 void DeviceSettingsTestHelper::RetrievePolicyForUser(
133     const std::string& username,
134     const RetrievePolicyCallback& callback) {
135 }
136
137 std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
138     const std::string& username) {
139   return "";
140 }
141
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(
146       callback);
147 }
148
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);
154 }
155
156 void DeviceSettingsTestHelper::StorePolicyForUser(
157     const std::string& username,
158     const std::string& policy_blob,
159     const StorePolicyCallback& callback) {
160 }
161
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);
168 }
169
170 void DeviceSettingsTestHelper::SetFlagsForUser(
171     const std::string& account_id,
172     const std::vector<std::string>& flags) {}
173
174 void DeviceSettingsTestHelper::GetServerBackedStateKeys(
175     const StateKeysCallback& callback) {}
176
177 DeviceSettingsTestHelper::PolicyState::PolicyState()
178     : store_result_(true) {}
179
180 DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
181
182 ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
183   DeviceSettingsService::Initialize();
184   DeviceSettingsService::Get()->SetSessionManager(
185       this, new ownership::MockOwnerKeyUtil());
186   DeviceSettingsService::Get()->Load();
187   Flush();
188 }
189
190 ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
191   Flush();
192   DeviceSettingsService::Get()->UnsetSessionManager();
193   DeviceSettingsService::Shutdown();
194 }
195
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(
201       owner_key_util_);
202 }
203
204 DeviceSettingsTestBase::~DeviceSettingsTestBase() {
205   base::RunLoop().RunUntilIdle();
206 }
207
208 void DeviceSettingsTestBase::SetUp() {
209   // Initialize DBusThreadManager with a stub implementation.
210   dbus_setter_ = chromeos::DBusThreadManager::GetSetterForTesting();
211
212   base::RunLoop().RunUntilIdle();
213
214   device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
215       false);
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_,
220                                              owner_key_util_);
221   OwnerSettingsServiceChromeOS::SetDeviceSettingsServiceForTesting(
222       &device_settings_service_);
223   profile_.reset(new TestingProfile());
224 }
225
226 void DeviceSettingsTestBase::TearDown() {
227   OwnerSettingsServiceChromeOS::SetDeviceSettingsServiceForTesting(NULL);
228   FlushDeviceSettings();
229   device_settings_service_.UnsetSessionManager();
230   DBusThreadManager::Shutdown();
231 }
232
233 void DeviceSettingsTestBase::FlushDeviceSettings() {
234   device_settings_test_helper_.Flush();
235 }
236
237 void DeviceSettingsTestBase::ReloadDeviceSettings() {
238   device_settings_service_.OwnerKeySet(true);
239   FlushDeviceSettings();
240 }
241
242 void DeviceSettingsTestBase::InitOwner(const std::string& user_id,
243                                        bool tpm_is_ready) {
244   const user_manager::User* user = user_manager_->FindUser(user_id);
245   if (!user) {
246     user = user_manager_->AddUser(user_id);
247     profile_->set_profile_name(user_id);
248
249     ProfileHelper::Get()->SetUserToProfileMappingForTesting(user,
250                                                             profile_.get());
251   }
252   OwnerSettingsServiceChromeOS* service =
253       OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get());
254   CHECK(service);
255   if (tpm_is_ready)
256     service->OnTPMTokenReady(true /* token is enabled */);
257 }
258
259 }  // namespace chromeos