Update To 11.40.268.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::NotifySupervisedUserCreationStarted() {}
117
118 void DeviceSettingsTestHelper::NotifySupervisedUserCreationFinished() {}
119
120 void DeviceSettingsTestHelper::StartDeviceWipe() {}
121
122 void DeviceSettingsTestHelper::RequestLockScreen() {}
123
124 void DeviceSettingsTestHelper::NotifyLockScreenShown() {}
125
126 void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {}
127
128 void DeviceSettingsTestHelper::RetrieveActiveSessions(
129       const ActiveSessionsCallback& callback) {}
130
131 void DeviceSettingsTestHelper::RetrieveDevicePolicy(
132     const RetrievePolicyCallback& callback) {
133   device_policy_.retrieve_callbacks_.push_back(callback);
134 }
135
136 void DeviceSettingsTestHelper::RetrievePolicyForUser(
137     const std::string& username,
138     const RetrievePolicyCallback& callback) {
139 }
140
141 std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser(
142     const std::string& username) {
143   return "";
144 }
145
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(
150       callback);
151 }
152
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);
158 }
159
160 void DeviceSettingsTestHelper::StorePolicyForUser(
161     const std::string& username,
162     const std::string& policy_blob,
163     const StorePolicyCallback& callback) {
164 }
165
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);
172 }
173
174 void DeviceSettingsTestHelper::SetFlagsForUser(
175     const std::string& account_id,
176     const std::vector<std::string>& flags) {}
177
178 void DeviceSettingsTestHelper::GetServerBackedStateKeys(
179     const StateKeysCallback& callback) {}
180
181 DeviceSettingsTestHelper::PolicyState::PolicyState()
182     : store_result_(true) {}
183
184 DeviceSettingsTestHelper::PolicyState::~PolicyState() {}
185
186 ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() {
187   DeviceSettingsService::Initialize();
188   DeviceSettingsService::Get()->SetSessionManager(
189       this, new ownership::MockOwnerKeyUtil());
190   DeviceSettingsService::Get()->Load();
191   Flush();
192 }
193
194 ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() {
195   Flush();
196   DeviceSettingsService::Get()->UnsetSessionManager();
197   DeviceSettingsService::Shutdown();
198 }
199
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(
207       owner_key_util_);
208 }
209
210 DeviceSettingsTestBase::~DeviceSettingsTestBase() {
211   base::RunLoop().RunUntilIdle();
212 }
213
214 void DeviceSettingsTestBase::SetUp() {
215   // Initialize DBusThreadManager with a stub implementation.
216   dbus_setter_ = chromeos::DBusThreadManager::GetSetterForTesting();
217
218   base::RunLoop().RunUntilIdle();
219
220   device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled(
221       false);
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_,
226                                              owner_key_util_);
227   profile_.reset(new TestingProfile());
228 }
229
230 void DeviceSettingsTestBase::TearDown() {
231   OwnerSettingsServiceChromeOSFactory::SetDeviceSettingsServiceForTesting(NULL);
232   FlushDeviceSettings();
233   device_settings_service_.UnsetSessionManager();
234   DBusThreadManager::Shutdown();
235 }
236
237 void DeviceSettingsTestBase::FlushDeviceSettings() {
238   device_settings_test_helper_.Flush();
239 }
240
241 void DeviceSettingsTestBase::ReloadDeviceSettings() {
242   device_settings_service_.OwnerKeySet(true);
243   FlushDeviceSettings();
244 }
245
246 void DeviceSettingsTestBase::InitOwner(const std::string& user_id,
247                                        bool tpm_is_ready) {
248   const user_manager::User* user = user_manager_->FindUser(user_id);
249   if (!user) {
250     user = user_manager_->AddUser(user_id);
251     profile_->set_profile_name(user_id);
252
253     ProfileHelper::Get()->SetUserToProfileMappingForTesting(user,
254                                                             profile_.get());
255   }
256   OwnerSettingsServiceChromeOS* service =
257       OwnerSettingsServiceChromeOSFactory::GetForProfile(profile_.get());
258   CHECK(service);
259   if (tpm_is_ready)
260     service->OnTPMTokenReady(true /* token is enabled */);
261 }
262
263 }  // namespace chromeos