device_settings_test_helper.h revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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/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/user_manager.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 "chrome/browser/chromeos/settings/mock_owner_key_util.h"
24#include "chromeos/dbus/session_manager_client.h"
25#include "content/public/test/test_browser_thread.h"
26#include "testing/gtest/include/gtest/gtest.h"
27
28class TestingProfile;
29
30namespace chromeos {
31
32class FakeDBusThreadManager;
33
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.
39class DeviceSettingsTestHelper : public SessionManagerClient {
40 public:
41  // Wraps a device settings service instance for testing.
42  DeviceSettingsTestHelper();
43  virtual ~DeviceSettingsTestHelper();
44
45  // Flushes operations on the current message loop and the blocking pool.
46  void FlushLoops();
47
48  // Runs all pending store callbacks.
49  void FlushStore();
50
51  // Runs all pending retrieve callbacks.
52  void FlushRetrieve();
53
54  // Flushes all pending operations.
55  void Flush();
56
57  // Checks whether any asynchronous Store/Retrieve operations are pending.
58  bool HasPendingOperations() const;
59
60  bool store_result() {
61    return device_policy_.store_result_;
62  }
63  void set_store_result(bool store_result) {
64    device_policy_.store_result_ = store_result;
65  }
66
67  const std::string& policy_blob() {
68    return device_policy_.policy_blob_;
69  }
70  void set_policy_blob(const std::string& policy_blob) {
71    device_policy_.policy_blob_ = policy_blob;
72  }
73
74  const std::string& device_local_account_policy_blob(
75      const std::string& id) const {
76    const std::map<std::string, PolicyState>::const_iterator entry =
77        device_local_account_policy_.find(id);
78    return entry == device_local_account_policy_.end() ?
79        base::EmptyString() : entry->second.policy_blob_;
80  }
81
82  void set_device_local_account_policy_blob(const std::string& id,
83                                            const std::string& policy_blob) {
84    device_local_account_policy_[id].policy_blob_ = policy_blob;
85  }
86
87  // SessionManagerClient:
88  virtual void Init(dbus::Bus* bus) OVERRIDE;
89  virtual void SetStubDelegate(SessionManagerClient::StubDelegate* delegate)
90      OVERRIDE;
91  virtual void AddObserver(Observer* observer) OVERRIDE;
92  virtual void RemoveObserver(Observer* observer) OVERRIDE;
93  virtual bool HasObserver(Observer* observer) OVERRIDE;
94  virtual void EmitLoginPromptVisible() OVERRIDE;
95  virtual void RestartJob(int pid, const std::string& command_line) OVERRIDE;
96  virtual void StartSession(const std::string& user_email) OVERRIDE;
97  virtual void StopSession() OVERRIDE;
98  virtual void StartDeviceWipe() OVERRIDE;
99  virtual void RequestLockScreen() OVERRIDE;
100  virtual void NotifyLockScreenShown() OVERRIDE;
101  virtual void NotifyLockScreenDismissed() OVERRIDE;
102  virtual void RetrieveActiveSessions(
103      const ActiveSessionsCallback& callback) OVERRIDE;
104  virtual void RetrieveDevicePolicy(
105      const RetrievePolicyCallback& callback) OVERRIDE;
106  virtual void RetrievePolicyForUser(
107      const std::string& username,
108      const RetrievePolicyCallback& callback) OVERRIDE;
109  virtual std::string BlockingRetrievePolicyForUser(
110      const std::string& username) OVERRIDE;
111  virtual void RetrieveDeviceLocalAccountPolicy(
112      const std::string& account_id,
113      const RetrievePolicyCallback& callback) OVERRIDE;
114  virtual void StoreDevicePolicy(const std::string& policy_blob,
115                                 const StorePolicyCallback& callback) OVERRIDE;
116  virtual void StorePolicyForUser(const std::string& username,
117                                  const std::string& policy_blob,
118                                  const StorePolicyCallback& callback) OVERRIDE;
119  virtual void StoreDeviceLocalAccountPolicy(
120      const std::string& account_id,
121      const std::string& policy_blob,
122      const StorePolicyCallback& callback) OVERRIDE;
123  virtual void SetFlagsForUser(
124      const std::string& account_id,
125      const std::vector<std::string>& flags) OVERRIDE;
126  virtual void GetServerBackedStateKeys(
127      const StateKeysCallback& callback) OVERRIDE;
128
129 private:
130  struct PolicyState {
131    bool store_result_;
132    std::string policy_blob_;
133    std::vector<StorePolicyCallback> store_callbacks_;
134    std::vector<RetrievePolicyCallback> retrieve_callbacks_;
135
136    PolicyState();
137    ~PolicyState();
138
139    bool HasPendingOperations() const {
140      return !store_callbacks_.empty() || !retrieve_callbacks_.empty();
141    }
142  };
143
144  PolicyState device_policy_;
145  std::map<std::string, PolicyState> device_local_account_policy_;
146
147  DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestHelper);
148};
149
150// Wraps the singleton device settings and initializes it to the point where it
151// reports OWNERSHIP_NONE for the ownership status.
152class ScopedDeviceSettingsTestHelper : public DeviceSettingsTestHelper {
153 public:
154  ScopedDeviceSettingsTestHelper();
155  virtual ~ScopedDeviceSettingsTestHelper();
156
157 private:
158  DISALLOW_COPY_AND_ASSIGN(ScopedDeviceSettingsTestHelper);
159};
160
161// A convenience test base class that initializes a DeviceSettingsService
162// instance for testing and allows for straightforward updating of device
163// settings. |device_settings_service_| starts out in uninitialized state, so
164// startup code gets tested as well.
165class DeviceSettingsTestBase : public testing::Test {
166 protected:
167  DeviceSettingsTestBase();
168  virtual ~DeviceSettingsTestBase();
169
170  virtual void SetUp() OVERRIDE;
171  virtual void TearDown() OVERRIDE;
172
173  // Flushes any pending device settings operations.
174  void FlushDeviceSettings();
175
176  // Triggers an owner key and device settings reload on
177  // |device_settings_service_| and flushes the resulting load operation.
178  void ReloadDeviceSettings();
179
180  void InitOwner(const std::string& user_id, bool tpm_is_ready);
181
182  base::MessageLoopForUI loop_;
183  content::TestBrowserThread ui_thread_;
184  content::TestBrowserThread file_thread_;
185
186  policy::DevicePolicyBuilder device_policy_;
187
188  DeviceSettingsTestHelper device_settings_test_helper_;
189  FakeUserManager* user_manager_;
190  ScopedUserManagerEnabler user_manager_enabler_;
191  scoped_ptr<TestingProfile> profile_;
192  scoped_refptr<MockOwnerKeyUtil> owner_key_util_;
193  // Local DeviceSettingsService instance for tests. Avoid using in combination
194  // with the global instance (DeviceSettingsService::Get()).
195  DeviceSettingsService device_settings_service_;
196
197  chromeos::FakeDBusThreadManager* fake_dbus_thread_manager_;
198
199 private:
200  DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestBase);
201};
202
203}  // namespace chromeos
204
205#endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_
206