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