Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / settings / device_settings_test_helper.h
blob823c088191cd043830782f044962d4f6b92126d2
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_chrome_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 "chromeos/dbus/session_manager_client.h"
23 #include "components/ownership/mock_owner_key_util.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 class TestingProfile;
29 namespace chromeos {
31 class DBusThreadManagerSetter;
33 // A helper class for tests mocking out session_manager's device settings
34 // interface. The pattern is to initialize DeviceSettingsService with the helper
35 // for the SessionManagerClient pointer. The helper records calls made by
36 // DeviceSettingsService. The test can then verify state, after which it should
37 // call one of the Flush() variants that will resume processing.
38 class DeviceSettingsTestHelper : public SessionManagerClient {
39 public:
40 // Wraps a device settings service instance for testing.
41 DeviceSettingsTestHelper();
42 ~DeviceSettingsTestHelper() override;
44 // Runs all pending store callbacks.
45 void FlushStore();
47 // Runs all pending retrieve callbacks.
48 void FlushRetrieve();
50 // Flushes all pending operations.
51 void Flush();
53 // Checks whether any asynchronous Store/Retrieve operations are pending.
54 bool HasPendingOperations() const;
56 bool store_result() {
57 return device_policy_.store_result_;
59 void set_store_result(bool store_result) {
60 device_policy_.store_result_ = store_result;
63 const std::string& policy_blob() {
64 return device_policy_.policy_blob_;
66 void set_policy_blob(const std::string& policy_blob) {
67 device_policy_.policy_blob_ = policy_blob;
70 const std::string& device_local_account_policy_blob(
71 const std::string& id) const {
72 const std::map<std::string, PolicyState>::const_iterator entry =
73 device_local_account_policy_.find(id);
74 return entry == device_local_account_policy_.end() ?
75 base::EmptyString() : entry->second.policy_blob_;
78 void set_device_local_account_policy_blob(const std::string& id,
79 const std::string& policy_blob) {
80 device_local_account_policy_[id].policy_blob_ = policy_blob;
83 // SessionManagerClient:
84 void Init(dbus::Bus* bus) override;
85 void SetStubDelegate(SessionManagerClient::StubDelegate* delegate) override;
86 void AddObserver(Observer* observer) override;
87 void RemoveObserver(Observer* observer) override;
88 bool HasObserver(const Observer* observer) const override;
89 bool IsScreenLocked() const override;
90 void EmitLoginPromptVisible() override;
91 void RestartJob(const std::vector<std::string>& argv) override;
92 void StartSession(const std::string& user_email) override;
93 void StopSession() override;
94 void NotifySupervisedUserCreationStarted() override;
95 void NotifySupervisedUserCreationFinished() override;
96 void StartDeviceWipe() override;
97 void RequestLockScreen() override;
98 void NotifyLockScreenShown() override;
99 void NotifyLockScreenDismissed() override;
100 void RetrieveActiveSessions(const ActiveSessionsCallback& callback) override;
101 void RetrieveDevicePolicy(const RetrievePolicyCallback& callback) override;
102 void RetrievePolicyForUser(const std::string& username,
103 const RetrievePolicyCallback& callback) override;
104 std::string BlockingRetrievePolicyForUser(
105 const std::string& username) override;
106 void RetrieveDeviceLocalAccountPolicy(
107 const std::string& account_id,
108 const RetrievePolicyCallback& callback) override;
109 void StoreDevicePolicy(const std::string& policy_blob,
110 const StorePolicyCallback& callback) override;
111 void StorePolicyForUser(const std::string& username,
112 const std::string& policy_blob,
113 const StorePolicyCallback& callback) override;
114 void StoreDeviceLocalAccountPolicy(
115 const std::string& account_id,
116 const std::string& policy_blob,
117 const StorePolicyCallback& callback) override;
118 void SetFlagsForUser(const std::string& account_id,
119 const std::vector<std::string>& flags) override;
120 void GetServerBackedStateKeys(const StateKeysCallback& callback) override;
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_;
129 PolicyState();
130 ~PolicyState();
132 bool HasPendingOperations() const {
133 return !store_callbacks_.empty() || !retrieve_callbacks_.empty();
137 PolicyState device_policy_;
138 std::map<std::string, PolicyState> device_local_account_policy_;
140 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestHelper);
143 // Wraps the singleton device settings and initializes it to the point where it
144 // reports OWNERSHIP_NONE for the ownership status.
145 class ScopedDeviceSettingsTestHelper : public DeviceSettingsTestHelper {
146 public:
147 ScopedDeviceSettingsTestHelper();
148 ~ScopedDeviceSettingsTestHelper() override;
150 private:
151 DISALLOW_COPY_AND_ASSIGN(ScopedDeviceSettingsTestHelper);
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.
158 class DeviceSettingsTestBase : public testing::Test {
159 protected:
160 DeviceSettingsTestBase();
161 ~DeviceSettingsTestBase() override;
163 void SetUp() override;
164 void TearDown() override;
166 // Flushes any pending device settings operations.
167 void FlushDeviceSettings();
169 // Triggers an owner key and device settings reload on
170 // |device_settings_service_| and flushes the resulting load operation.
171 void ReloadDeviceSettings();
173 void InitOwner(const std::string& user_id, bool tpm_is_ready);
175 content::TestBrowserThreadBundle thread_bundle_;
177 policy::DevicePolicyBuilder device_policy_;
179 DeviceSettingsTestHelper device_settings_test_helper_;
180 // Note that FakeUserManager is used by ProfileHelper, which some of the
181 // tested classes depend on implicitly.
182 FakeChromeUserManager* user_manager_;
183 ScopedUserManagerEnabler user_manager_enabler_;
184 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_;
185 // Local DeviceSettingsService instance for tests. Avoid using in combination
186 // with the global instance (DeviceSettingsService::Get()).
187 DeviceSettingsService device_settings_service_;
188 scoped_ptr<TestingProfile> profile_;
190 scoped_ptr<DBusThreadManagerSetter> dbus_setter_;
192 private:
193 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestBase);
196 } // namespace chromeos
198 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_