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_
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 "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"
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
{
41 // Wraps a device settings service instance for testing.
42 DeviceSettingsTestHelper();
43 ~DeviceSettingsTestHelper() override
;
45 // Runs all pending store callbacks.
48 // Runs all pending retrieve callbacks.
51 // Flushes all pending operations.
54 // Checks whether any asynchronous Store/Retrieve operations are pending.
55 bool HasPendingOperations() const;
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 void Init(dbus::Bus
* bus
) override
;
86 void SetStubDelegate(SessionManagerClient::StubDelegate
* delegate
) override
;
87 void AddObserver(Observer
* observer
) override
;
88 void RemoveObserver(Observer
* observer
) override
;
89 bool HasObserver(const Observer
* observer
) const override
;
90 bool IsScreenLocked() const override
;
91 void EmitLoginPromptVisible() override
;
92 void RestartJob(int pid
, const std::string
& command_line
) override
;
93 void StartSession(const std::string
& user_email
) override
;
94 void StopSession() override
;
95 void NotifySupervisedUserCreationStarted() override
;
96 void NotifySupervisedUserCreationFinished() override
;
97 void StartDeviceWipe() override
;
98 void RequestLockScreen() override
;
99 void NotifyLockScreenShown() override
;
100 void NotifyLockScreenDismissed() override
;
101 void RetrieveActiveSessions(const ActiveSessionsCallback
& callback
) override
;
102 void RetrieveDevicePolicy(const RetrievePolicyCallback
& callback
) override
;
103 void RetrievePolicyForUser(const std::string
& username
,
104 const RetrievePolicyCallback
& callback
) override
;
105 std::string
BlockingRetrievePolicyForUser(
106 const std::string
& username
) override
;
107 void RetrieveDeviceLocalAccountPolicy(
108 const std::string
& account_id
,
109 const RetrievePolicyCallback
& callback
) override
;
110 void StoreDevicePolicy(const std::string
& policy_blob
,
111 const StorePolicyCallback
& callback
) override
;
112 void StorePolicyForUser(const std::string
& username
,
113 const std::string
& policy_blob
,
114 const StorePolicyCallback
& callback
) override
;
115 void StoreDeviceLocalAccountPolicy(
116 const std::string
& account_id
,
117 const std::string
& policy_blob
,
118 const StorePolicyCallback
& callback
) override
;
119 void SetFlagsForUser(const std::string
& account_id
,
120 const std::vector
<std::string
>& flags
) override
;
121 void GetServerBackedStateKeys(const StateKeysCallback
& callback
) override
;
126 std::string policy_blob_
;
127 std::vector
<StorePolicyCallback
> store_callbacks_
;
128 std::vector
<RetrievePolicyCallback
> retrieve_callbacks_
;
133 bool HasPendingOperations() const {
134 return !store_callbacks_
.empty() || !retrieve_callbacks_
.empty();
138 PolicyState device_policy_
;
139 std::map
<std::string
, PolicyState
> device_local_account_policy_
;
141 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestHelper
);
144 // Wraps the singleton device settings and initializes it to the point where it
145 // reports OWNERSHIP_NONE for the ownership status.
146 class ScopedDeviceSettingsTestHelper
: public DeviceSettingsTestHelper
{
148 ScopedDeviceSettingsTestHelper();
149 ~ScopedDeviceSettingsTestHelper() override
;
152 DISALLOW_COPY_AND_ASSIGN(ScopedDeviceSettingsTestHelper
);
155 // A convenience test base class that initializes a DeviceSettingsService
156 // instance for testing and allows for straightforward updating of device
157 // settings. |device_settings_service_| starts out in uninitialized state, so
158 // startup code gets tested as well.
159 class DeviceSettingsTestBase
: public testing::Test
{
161 DeviceSettingsTestBase();
162 ~DeviceSettingsTestBase() override
;
164 void SetUp() override
;
165 void TearDown() override
;
167 // Flushes any pending device settings operations.
168 void FlushDeviceSettings();
170 // Triggers an owner key and device settings reload on
171 // |device_settings_service_| and flushes the resulting load operation.
172 void ReloadDeviceSettings();
174 void InitOwner(const std::string
& user_id
, bool tpm_is_ready
);
176 content::TestBrowserThreadBundle thread_bundle_
;
178 policy::DevicePolicyBuilder device_policy_
;
180 DeviceSettingsTestHelper device_settings_test_helper_
;
181 // Note that FakeUserManager is used by ProfileHelper, which some of the
182 // tested classes depend on implicitly.
183 FakeChromeUserManager
* user_manager_
;
184 ScopedUserManagerEnabler user_manager_enabler_
;
185 scoped_refptr
<ownership::MockOwnerKeyUtil
> owner_key_util_
;
186 // Local DeviceSettingsService instance for tests. Avoid using in combination
187 // with the global instance (DeviceSettingsService::Get()).
188 DeviceSettingsService device_settings_service_
;
189 scoped_ptr
<TestingProfile
> profile_
;
191 scoped_ptr
<DBusThreadManagerSetter
> dbus_setter_
;
194 DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestBase
);
197 } // namespace chromeos
199 #endif // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_