device_settings_test_helper.cc revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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/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 "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 callbacks.swap(device_local_account_state->second.retrieve_callbacks_); 67 for (std::vector<RetrievePolicyCallback>::iterator cb(callbacks.begin()); 68 cb != callbacks.end(); ++cb) { 69 cb->Run(device_local_account_state->second.policy_blob_); 70 } 71 } 72} 73 74void DeviceSettingsTestHelper::Flush() { 75 do { 76 FlushLoops(); 77 FlushStore(); 78 FlushLoops(); 79 FlushRetrieve(); 80 FlushLoops(); 81 } while (HasPendingOperations()); 82} 83 84bool DeviceSettingsTestHelper::HasPendingOperations() const { 85 if (device_policy_.HasPendingOperations()) 86 return true; 87 88 std::map<std::string, PolicyState>::const_iterator device_local_account_state; 89 for (device_local_account_state = device_local_account_policy_.begin(); 90 device_local_account_state != device_local_account_policy_.end(); 91 ++device_local_account_state) { 92 if (device_local_account_state->second.HasPendingOperations()) 93 return true; 94 } 95 96 return false; 97} 98 99void DeviceSettingsTestHelper::AddObserver(Observer* observer) {} 100 101void DeviceSettingsTestHelper::RemoveObserver(Observer* observer) {} 102 103bool DeviceSettingsTestHelper::HasObserver(Observer* observer) { 104 return false; 105} 106 107void DeviceSettingsTestHelper::EmitLoginPromptReady() {} 108 109void DeviceSettingsTestHelper::EmitLoginPromptVisible() {} 110 111void DeviceSettingsTestHelper::RestartJob(int pid, 112 const std::string& command_line) {} 113 114void DeviceSettingsTestHelper::RestartEntd() {} 115 116void DeviceSettingsTestHelper::StartSession(const std::string& user_email) {} 117 118void DeviceSettingsTestHelper::StopSession() {} 119 120void DeviceSettingsTestHelper::StartDeviceWipe() {} 121 122void DeviceSettingsTestHelper::RequestLockScreen() {} 123 124void DeviceSettingsTestHelper::NotifyLockScreenShown() {} 125 126void DeviceSettingsTestHelper::RequestUnlockScreen() {} 127 128void DeviceSettingsTestHelper::NotifyLockScreenDismissed() {} 129 130void DeviceSettingsTestHelper::RetrieveActiveSessions( 131 const ActiveSessionsCallback& callback) {} 132 133void DeviceSettingsTestHelper::RetrieveDevicePolicy( 134 const RetrievePolicyCallback& callback) { 135 device_policy_.retrieve_callbacks_.push_back(callback); 136} 137 138void DeviceSettingsTestHelper::RetrievePolicyForUser( 139 const std::string& username, 140 const RetrievePolicyCallback& callback) { 141} 142 143std::string DeviceSettingsTestHelper::BlockingRetrievePolicyForUser( 144 const std::string& username) { 145 return ""; 146} 147 148void DeviceSettingsTestHelper::RetrieveDeviceLocalAccountPolicy( 149 const std::string& account_id, 150 const RetrievePolicyCallback& callback) { 151 device_local_account_policy_[account_id].retrieve_callbacks_.push_back( 152 callback); 153} 154 155void DeviceSettingsTestHelper::StoreDevicePolicy( 156 const std::string& policy_blob, 157 const StorePolicyCallback& callback) { 158 device_policy_.policy_blob_ = policy_blob; 159 device_policy_.store_callbacks_.push_back(callback); 160} 161 162void DeviceSettingsTestHelper::StorePolicyForUser( 163 const std::string& username, 164 const std::string& policy_blob, 165 const std::string& policy_key, 166 const StorePolicyCallback& callback) { 167} 168 169void DeviceSettingsTestHelper::StoreDeviceLocalAccountPolicy( 170 const std::string& account_id, 171 const std::string& policy_blob, 172 const StorePolicyCallback& callback) { 173 device_local_account_policy_[account_id].policy_blob_ = policy_blob; 174 device_local_account_policy_[account_id].store_callbacks_.push_back(callback); 175} 176 177void DeviceSettingsTestHelper::SetFlagsForUser( 178 const std::string& account_id, 179 const std::vector<std::string>& flags) {} 180 181DeviceSettingsTestHelper::PolicyState::PolicyState() 182 : store_result_(true) {} 183 184DeviceSettingsTestHelper::PolicyState::~PolicyState() {} 185 186ScopedDeviceSettingsTestHelper::ScopedDeviceSettingsTestHelper() { 187 DeviceSettingsService::Initialize(); 188 DeviceSettingsService::Get()->SetSessionManager(this, new MockOwnerKeyUtil()); 189 DeviceSettingsService::Get()->Load(); 190 Flush(); 191} 192 193ScopedDeviceSettingsTestHelper::~ScopedDeviceSettingsTestHelper() { 194 Flush(); 195 DeviceSettingsService::Get()->UnsetSessionManager(); 196 DeviceSettingsService::Shutdown(); 197} 198 199DeviceSettingsTestBase::DeviceSettingsTestBase() 200 : loop_(base::MessageLoop::TYPE_UI), 201 ui_thread_(content::BrowserThread::UI, &loop_), 202 file_thread_(content::BrowserThread::FILE, &loop_), 203 owner_key_util_(new MockOwnerKeyUtil()) {} 204 205DeviceSettingsTestBase::~DeviceSettingsTestBase() { 206 base::RunLoop().RunUntilIdle(); 207} 208 209void DeviceSettingsTestBase::SetUp() { 210 // Initialize DBusThreadManager with a stub implementation. 211 DBusThreadManager::InitializeWithStub(); 212 NetworkHandler::Initialize(); 213 loop_.RunUntilIdle(); 214 215 device_policy_.payload().mutable_metrics_enabled()->set_metrics_enabled( 216 false); 217 owner_key_util_->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey()); 218 device_policy_.Build(); 219 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); 220 device_settings_service_.SetSessionManager(&device_settings_test_helper_, 221 owner_key_util_); 222} 223 224void DeviceSettingsTestBase::TearDown() { 225 FlushDeviceSettings(); 226 device_settings_service_.UnsetSessionManager(); 227 NetworkHandler::Shutdown(); 228 DBusThreadManager::Shutdown(); 229} 230 231void DeviceSettingsTestBase::FlushDeviceSettings() { 232 device_settings_test_helper_.Flush(); 233} 234 235void DeviceSettingsTestBase::ReloadDeviceSettings() { 236 device_settings_service_.OwnerKeySet(true); 237 FlushDeviceSettings(); 238} 239 240} // namespace chromeos 241