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