device_settings_test_helper.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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#ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_ 6#define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_ 7 8#include <map> 9#include <string> 10#include <vector> 11 12#include "base/basictypes.h" 13#include "base/compiler_specific.h" 14#include "base/memory/ref_counted.h" 15#include "base/message_loop/message_loop.h" 16#include "base/strings/string_util.h" 17#include "chrome/browser/chromeos/policy/device_policy_builder.h" 18#include "chrome/browser/chromeos/settings/device_settings_service.h" 19#include "chrome/browser/chromeos/settings/device_settings_test_helper.h" 20#include "chrome/browser/chromeos/settings/mock_owner_key_util.h" 21#include "chromeos/dbus/session_manager_client.h" 22#include "content/public/test/test_browser_thread.h" 23#include "testing/gtest/include/gtest/gtest.h" 24 25namespace chromeos { 26 27// A helper class for tests mocking out session_manager's device settings 28// interface. The pattern is to initialize DeviceSettingsService with the helper 29// for the SessionManagerClient pointer. The helper records calls made by 30// DeviceSettingsService. The test can then verify state, after which it should 31// call one of the Flush() variants that will resume processing. 32class DeviceSettingsTestHelper : public SessionManagerClient { 33 public: 34 // Wraps a device settings service instance for testing. 35 DeviceSettingsTestHelper(); 36 virtual ~DeviceSettingsTestHelper(); 37 38 // Flushes operations on the current message loop and the blocking pool. 39 void FlushLoops(); 40 41 // Runs all pending store callbacks. 42 void FlushStore(); 43 44 // Runs all pending retrieve callbacks. 45 void FlushRetrieve(); 46 47 // Flushes all pending operations. 48 void Flush(); 49 50 // Checks whether any asynchronous Store/Retrieve operations are pending. 51 bool HasPendingOperations() const; 52 53 bool store_result() { 54 return device_policy_.store_result_; 55 } 56 void set_store_result(bool store_result) { 57 device_policy_.store_result_ = store_result; 58 } 59 60 const std::string& policy_blob() { 61 return device_policy_.policy_blob_; 62 } 63 void set_policy_blob(const std::string& policy_blob) { 64 device_policy_.policy_blob_ = policy_blob; 65 } 66 67 const std::string& device_local_account_policy_blob( 68 const std::string& id) const { 69 const std::map<std::string, PolicyState>::const_iterator entry = 70 device_local_account_policy_.find(id); 71 return entry == device_local_account_policy_.end() ? 72 EmptyString() : entry->second.policy_blob_; 73 } 74 75 void set_device_local_account_policy_blob(const std::string& id, 76 const std::string& policy_blob) { 77 device_local_account_policy_[id].policy_blob_ = policy_blob; 78 } 79 80 // SessionManagerClient: 81 virtual void Init(dbus::Bus* bus) OVERRIDE; 82 virtual void AddObserver(Observer* observer) OVERRIDE; 83 virtual void RemoveObserver(Observer* observer) OVERRIDE; 84 virtual bool HasObserver(Observer* observer) OVERRIDE; 85 virtual void EmitLoginPromptReady() OVERRIDE; 86 virtual void EmitLoginPromptVisible() OVERRIDE; 87 virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE; 88 virtual void RestartEntd() OVERRIDE; 89 virtual void StartSession(const std::string& user_email) OVERRIDE; 90 virtual void StopSession() OVERRIDE; 91 virtual void StartDeviceWipe() OVERRIDE; 92 virtual void RequestLockScreen() OVERRIDE; 93 virtual void NotifyLockScreenShown() OVERRIDE; 94 virtual void RequestUnlockScreen() OVERRIDE; 95 virtual void NotifyLockScreenDismissed() OVERRIDE; 96 virtual void RetrieveActiveSessions( 97 const ActiveSessionsCallback& callback) OVERRIDE; 98 virtual void RetrieveDevicePolicy( 99 const RetrievePolicyCallback& callback) OVERRIDE; 100 virtual void RetrievePolicyForUser( 101 const std::string& username, 102 const RetrievePolicyCallback& callback) OVERRIDE; 103 virtual std::string BlockingRetrievePolicyForUser( 104 const std::string& username) OVERRIDE; 105 virtual void RetrieveDeviceLocalAccountPolicy( 106 const std::string& account_id, 107 const RetrievePolicyCallback& callback) OVERRIDE; 108 virtual void StoreDevicePolicy(const std::string& policy_blob, 109 const StorePolicyCallback& callback) OVERRIDE; 110 virtual void StorePolicyForUser(const std::string& username, 111 const std::string& policy_blob, 112 const std::string& policy_key, 113 const StorePolicyCallback& callback) OVERRIDE; 114 virtual void StoreDeviceLocalAccountPolicy( 115 const std::string& account_id, 116 const std::string& policy_blob, 117 const StorePolicyCallback& callback) OVERRIDE; 118 virtual void SetFlagsForUser( 119 const std::string& account_id, 120 const std::vector<std::string>& flags) OVERRIDE; 121 122 private: 123 struct PolicyState { 124 bool store_result_; 125 std::string policy_blob_; 126 std::vector<StorePolicyCallback> store_callbacks_; 127 std::vector<RetrievePolicyCallback> retrieve_callbacks_; 128 129 PolicyState(); 130 ~PolicyState(); 131 132 bool HasPendingOperations() const { 133 return !store_callbacks_.empty() || !retrieve_callbacks_.empty(); 134 } 135 }; 136 137 PolicyState device_policy_; 138 std::map<std::string, PolicyState> device_local_account_policy_; 139 140 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestHelper); 141}; 142 143// Wraps the singleton device settings and initializes it to the point where it 144// reports OWNERSHIP_NONE for the ownership status. 145class ScopedDeviceSettingsTestHelper : public DeviceSettingsTestHelper { 146 public: 147 ScopedDeviceSettingsTestHelper(); 148 virtual ~ScopedDeviceSettingsTestHelper(); 149 150 private: 151 DISALLOW_COPY_AND_ASSIGN(ScopedDeviceSettingsTestHelper); 152}; 153 154// A convenience test base class that initializes a DeviceSettingsService 155// instance for testing and allows for straightforward updating of device 156// settings. |device_settings_service_| starts out in uninitialized state, so 157// startup code gets tested as well. 158class DeviceSettingsTestBase : public testing::Test { 159 protected: 160 DeviceSettingsTestBase(); 161 virtual ~DeviceSettingsTestBase(); 162 163 virtual void SetUp() OVERRIDE; 164 virtual void TearDown() OVERRIDE; 165 166 // Flushes any pending device settings operations. 167 void FlushDeviceSettings(); 168 169 // Triggers an owner key and device settings reload on 170 // |device_settings_service_| and flushes the resulting load operation. 171 void ReloadDeviceSettings(); 172 173 base::MessageLoop loop_; 174 content::TestBrowserThread ui_thread_; 175 content::TestBrowserThread file_thread_; 176 177 policy::DevicePolicyBuilder device_policy_; 178 179 DeviceSettingsTestHelper device_settings_test_helper_; 180 scoped_refptr<MockOwnerKeyUtil> owner_key_util_; 181 // Local DeviceSettingsService instance for tests. Avoid using in combination 182 // with the global instance (DeviceSettingsService::Get()). 183 DeviceSettingsService device_settings_service_; 184 185 private: 186 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestBase); 187}; 188 189} // namespace chromeos 190 191#endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_ 192