[Android] Allow multiple --install in bb_device_steps.py.
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / device_settings_test_helper.h
blob14147a10d1a3a33e6ba857cb8b3a59c6e7527a77
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_
6 #define CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/strings/string_util.h"
18 #include "chrome/browser/chromeos/login/users/fake_user_manager.h"
19 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
20 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
21 #include "chrome/browser/chromeos/settings/device_settings_service.h"
22 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
23 #include "chromeos/dbus/session_manager_client.h"
24 #include "components/ownership/mock_owner_key_util.h"
25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 class TestingProfile;
30 namespace chromeos {
32 class DBusThreadManagerSetter;
34 // A helper class for tests mocking out session_manager's device settings
35 // interface. The pattern is to initialize DeviceSettingsService with the helper
36 // for the SessionManagerClient pointer. The helper records calls made by
37 // DeviceSettingsService. The test can then verify state, after which it should
38 // call one of the Flush() variants that will resume processing.
39 class DeviceSettingsTestHelper : public SessionManagerClient {
40 public:
41 // Wraps a device settings service instance for testing.
42 DeviceSettingsTestHelper();
43 virtual ~DeviceSettingsTestHelper();
45 // Runs all pending store callbacks.
46 void FlushStore();
48 // Runs all pending retrieve callbacks.
49 void FlushRetrieve();
51 // Flushes all pending operations.
52 void Flush();
54 // Checks whether any asynchronous Store/Retrieve operations are pending.
55 bool HasPendingOperations() const;
57 bool store_result() {
58 return device_policy_.store_result_;
60 void set_store_result(bool store_result) {
61 device_policy_.store_result_ = store_result;
64 const std::string& policy_blob() {
65 return device_policy_.policy_blob_;
67 void set_policy_blob(const std::string& policy_blob) {
68 device_policy_.policy_blob_ = policy_blob;
71 const std::string& device_local_account_policy_blob(
72 const std::string& id) const {
73 const std::map<std::string, PolicyState>::const_iterator entry =
74 device_local_account_policy_.find(id);
75 return entry == device_local_account_policy_.end() ?
76 base::EmptyString() : entry->second.policy_blob_;
79 void set_device_local_account_policy_blob(const std::string& id,
80 const std::string& policy_blob) {
81 device_local_account_policy_[id].policy_blob_ = policy_blob;
84 // SessionManagerClient:
85 virtual void Init(dbus::Bus* bus) override;
86 virtual void SetStubDelegate(SessionManagerClient::StubDelegate* delegate)
87 override;
88 virtual void AddObserver(Observer* observer) override;
89 virtual void RemoveObserver(Observer* observer) override;
90 virtual bool HasObserver(const Observer* observer) const override;
91 virtual void EmitLoginPromptVisible() override;
92 virtual void RestartJob(int pid, const std::string& command_line) override;
93 virtual void StartSession(const std::string& user_email) override;
94 virtual void StopSession() override;
95 virtual void NotifySupervisedUserCreationStarted() override;
96 virtual void NotifySupervisedUserCreationFinished() override;
97 virtual void StartDeviceWipe() override;
98 virtual void RequestLockScreen() override;
99 virtual void NotifyLockScreenShown() override;
100 virtual void NotifyLockScreenDismissed() override;
101 virtual void RetrieveActiveSessions(
102 const ActiveSessionsCallback& callback) override;
103 virtual void RetrieveDevicePolicy(
104 const RetrievePolicyCallback& callback) override;
105 virtual void RetrievePolicyForUser(
106 const std::string& username,
107 const RetrievePolicyCallback& callback) override;
108 virtual std::string BlockingRetrievePolicyForUser(
109 const std::string& username) override;
110 virtual void RetrieveDeviceLocalAccountPolicy(
111 const std::string& account_id,
112 const RetrievePolicyCallback& callback) override;
113 virtual void StoreDevicePolicy(const std::string& policy_blob,
114 const StorePolicyCallback& callback) override;
115 virtual void StorePolicyForUser(const std::string& username,
116 const std::string& policy_blob,
117 const StorePolicyCallback& callback) override;
118 virtual void StoreDeviceLocalAccountPolicy(
119 const std::string& account_id,
120 const std::string& policy_blob,
121 const StorePolicyCallback& callback) override;
122 virtual void SetFlagsForUser(
123 const std::string& account_id,
124 const std::vector<std::string>& flags) override;
125 virtual void GetServerBackedStateKeys(
126 const StateKeysCallback& callback) override;
128 private:
129 struct PolicyState {
130 bool store_result_;
131 std::string policy_blob_;
132 std::vector<StorePolicyCallback> store_callbacks_;
133 std::vector<RetrievePolicyCallback> retrieve_callbacks_;
135 PolicyState();
136 ~PolicyState();
138 bool HasPendingOperations() const {
139 return !store_callbacks_.empty() || !retrieve_callbacks_.empty();
143 PolicyState device_policy_;
144 std::map<std::string, PolicyState> device_local_account_policy_;
146 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestHelper);
149 // Wraps the singleton device settings and initializes it to the point where it
150 // reports OWNERSHIP_NONE for the ownership status.
151 class ScopedDeviceSettingsTestHelper : public DeviceSettingsTestHelper {
152 public:
153 ScopedDeviceSettingsTestHelper();
154 virtual ~ScopedDeviceSettingsTestHelper();
156 private:
157 DISALLOW_COPY_AND_ASSIGN(ScopedDeviceSettingsTestHelper);
160 // A convenience test base class that initializes a DeviceSettingsService
161 // instance for testing and allows for straightforward updating of device
162 // settings. |device_settings_service_| starts out in uninitialized state, so
163 // startup code gets tested as well.
164 class DeviceSettingsTestBase : public testing::Test {
165 protected:
166 DeviceSettingsTestBase();
167 virtual ~DeviceSettingsTestBase();
169 virtual void SetUp() override;
170 virtual void TearDown() override;
172 // Flushes any pending device settings operations.
173 void FlushDeviceSettings();
175 // Triggers an owner key and device settings reload on
176 // |device_settings_service_| and flushes the resulting load operation.
177 void ReloadDeviceSettings();
179 void InitOwner(const std::string& user_id, bool tpm_is_ready);
181 content::TestBrowserThreadBundle thread_bundle_;
183 policy::DevicePolicyBuilder device_policy_;
185 DeviceSettingsTestHelper device_settings_test_helper_;
186 // Note that FakeUserManager is used by ProfileHelper, which some of the
187 // tested classes depend on implicitly.
188 FakeUserManager* user_manager_;
189 ScopedUserManagerEnabler user_manager_enabler_;
190 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_;
191 // Local DeviceSettingsService instance for tests. Avoid using in combination
192 // with the global instance (DeviceSettingsService::Get()).
193 DeviceSettingsService device_settings_service_;
194 scoped_ptr<TestingProfile> profile_;
196 scoped_ptr<DBusThreadManagerSetter> dbus_setter_;
198 private:
199 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestBase);
202 } // namespace chromeos
204 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_