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/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"
27
28class TestingProfile;
29
30namespace chromeos {
31
32class DBusThreadManagerSetter;
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  // Runs all pending store callbacks.
46  void FlushStore();
47
48  // Runs all pending retrieve callbacks.
49  void FlushRetrieve();
50
51  // Flushes all pending operations.
52  void Flush();
53
54  // Checks whether any asynchronous Store/Retrieve operations are pending.
55  bool HasPendingOperations() const;
56
57  bool store_result() {
58    return device_policy_.store_result_;
59  }
60  void set_store_result(bool store_result) {
61    device_policy_.store_result_ = store_result;
62  }
63
64  const std::string& policy_blob() {
65    return device_policy_.policy_blob_;
66  }
67  void set_policy_blob(const std::string& policy_blob) {
68    device_policy_.policy_blob_ = policy_blob;
69  }
70
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_;
77  }
78
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;
82  }
83
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(Observer* observer) 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 StartDeviceWipe() OVERRIDE;
96  virtual void RequestLockScreen() OVERRIDE;
97  virtual void NotifyLockScreenShown() OVERRIDE;
98  virtual void NotifyLockScreenDismissed() OVERRIDE;
99  virtual void RetrieveActiveSessions(
100      const ActiveSessionsCallback& callback) OVERRIDE;
101  virtual void RetrieveDevicePolicy(
102      const RetrievePolicyCallback& callback) OVERRIDE;
103  virtual void RetrievePolicyForUser(
104      const std::string& username,
105      const RetrievePolicyCallback& callback) OVERRIDE;
106  virtual std::string BlockingRetrievePolicyForUser(
107      const std::string& username) OVERRIDE;
108  virtual void RetrieveDeviceLocalAccountPolicy(
109      const std::string& account_id,
110      const RetrievePolicyCallback& callback) OVERRIDE;
111  virtual void StoreDevicePolicy(const std::string& policy_blob,
112                                 const StorePolicyCallback& callback) OVERRIDE;
113  virtual void StorePolicyForUser(const std::string& username,
114                                  const std::string& policy_blob,
115                                  const StorePolicyCallback& callback) OVERRIDE;
116  virtual void StoreDeviceLocalAccountPolicy(
117      const std::string& account_id,
118      const std::string& policy_blob,
119      const StorePolicyCallback& callback) OVERRIDE;
120  virtual void SetFlagsForUser(
121      const std::string& account_id,
122      const std::vector<std::string>& flags) OVERRIDE;
123  virtual void GetServerBackedStateKeys(
124      const StateKeysCallback& callback) OVERRIDE;
125
126 private:
127  struct PolicyState {
128    bool store_result_;
129    std::string policy_blob_;
130    std::vector<StorePolicyCallback> store_callbacks_;
131    std::vector<RetrievePolicyCallback> retrieve_callbacks_;
132
133    PolicyState();
134    ~PolicyState();
135
136    bool HasPendingOperations() const {
137      return !store_callbacks_.empty() || !retrieve_callbacks_.empty();
138    }
139  };
140
141  PolicyState device_policy_;
142  std::map<std::string, PolicyState> device_local_account_policy_;
143
144  DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestHelper);
145};
146
147// Wraps the singleton device settings and initializes it to the point where it
148// reports OWNERSHIP_NONE for the ownership status.
149class ScopedDeviceSettingsTestHelper : public DeviceSettingsTestHelper {
150 public:
151  ScopedDeviceSettingsTestHelper();
152  virtual ~ScopedDeviceSettingsTestHelper();
153
154 private:
155  DISALLOW_COPY_AND_ASSIGN(ScopedDeviceSettingsTestHelper);
156};
157
158// A convenience test base class that initializes a DeviceSettingsService
159// instance for testing and allows for straightforward updating of device
160// settings. |device_settings_service_| starts out in uninitialized state, so
161// startup code gets tested as well.
162class DeviceSettingsTestBase : public testing::Test {
163 protected:
164  DeviceSettingsTestBase();
165  virtual ~DeviceSettingsTestBase();
166
167  virtual void SetUp() OVERRIDE;
168  virtual void TearDown() OVERRIDE;
169
170  // Flushes any pending device settings operations.
171  void FlushDeviceSettings();
172
173  // Triggers an owner key and device settings reload on
174  // |device_settings_service_| and flushes the resulting load operation.
175  void ReloadDeviceSettings();
176
177  void InitOwner(const std::string& user_id, bool tpm_is_ready);
178
179  content::TestBrowserThreadBundle thread_bundle_;
180
181  policy::DevicePolicyBuilder device_policy_;
182
183  DeviceSettingsTestHelper device_settings_test_helper_;
184  // Note that FakeUserManager is used by ProfileHelper, which some of the
185  // tested classes depend on implicitly.
186  FakeUserManager* user_manager_;
187  ScopedUserManagerEnabler user_manager_enabler_;
188  scoped_ptr<TestingProfile> profile_;
189  scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_;
190  // Local DeviceSettingsService instance for tests. Avoid using in combination
191  // with the global instance (DeviceSettingsService::Get()).
192  DeviceSettingsService device_settings_service_;
193
194  scoped_ptr<DBusThreadManagerSetter> dbus_setter_;
195
196 private:
197  DISALLOW_COPY_AND_ASSIGN(DeviceSettingsTestBase);
198};
199
200}  // namespace chromeos
201
202#endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_DEVICE_SETTINGS_TEST_HELPER_H_
203