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#include "components/policy/core/common/cloud/cloud_policy_core.h"
6
7#include "base/basictypes.h"
8#include "base/message_loop/message_loop.h"
9#include "base/prefs/pref_registry_simple.h"
10#include "base/prefs/testing_pref_service.h"
11#include "components/policy/core/common/cloud/cloud_policy_constants.h"
12#include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
13#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
14#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
15#include "components/policy/core/common/policy_pref_names.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace policy {
19
20class CloudPolicyCoreTest : public testing::Test,
21                            public CloudPolicyCore::Observer {
22 protected:
23  CloudPolicyCoreTest()
24      : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
25                                 std::string()),
26              &store_,
27              loop_.message_loop_proxy()),
28        core_connected_callback_count_(0),
29        refresh_scheduler_started_callback_count_(0),
30        core_disconnecting_callback_count_(0),
31        bad_callback_count_(0) {
32    prefs_.registry()->RegisterIntegerPref(
33        policy_prefs::kUserPolicyRefreshRate,
34        CloudPolicyRefreshScheduler::kDefaultRefreshDelayMs);
35    core_.AddObserver(this);
36  }
37
38  virtual ~CloudPolicyCoreTest() {
39    core_.RemoveObserver(this);
40  }
41
42  virtual void OnCoreConnected(CloudPolicyCore* core) OVERRIDE {
43    // Make sure core is connected at callback time.
44    if (core_.client())
45      core_connected_callback_count_++;
46    else
47      bad_callback_count_++;
48  }
49
50  virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) OVERRIDE {
51    // Make sure refresh scheduler is started at callback time.
52    if (core_.refresh_scheduler())
53      refresh_scheduler_started_callback_count_++;
54    else
55      bad_callback_count_++;
56  }
57
58  virtual void OnCoreDisconnecting(CloudPolicyCore* core) OVERRIDE {
59    // Make sure core is still connected at callback time.
60    if (core_.client())
61      core_disconnecting_callback_count_++;
62    else
63      bad_callback_count_++;
64  }
65
66  base::MessageLoop loop_;
67
68  TestingPrefServiceSimple prefs_;
69  MockCloudPolicyStore store_;
70  CloudPolicyCore core_;
71
72  int core_connected_callback_count_;
73  int refresh_scheduler_started_callback_count_;
74  int core_disconnecting_callback_count_;
75  int bad_callback_count_;
76
77 private:
78  DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreTest);
79};
80
81TEST_F(CloudPolicyCoreTest, ConnectAndDisconnect) {
82  EXPECT_TRUE(core_.store());
83  EXPECT_FALSE(core_.client());
84  EXPECT_FALSE(core_.service());
85  EXPECT_FALSE(core_.refresh_scheduler());
86
87  // Connect() brings up client and service.
88  core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
89  EXPECT_TRUE(core_.client());
90  EXPECT_TRUE(core_.service());
91  EXPECT_FALSE(core_.refresh_scheduler());
92  EXPECT_EQ(1, core_connected_callback_count_);
93  EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
94  EXPECT_EQ(0, core_disconnecting_callback_count_);
95
96  // Disconnect() goes back to no client and service.
97  core_.Disconnect();
98  EXPECT_FALSE(core_.client());
99  EXPECT_FALSE(core_.service());
100  EXPECT_FALSE(core_.refresh_scheduler());
101  EXPECT_EQ(1, core_connected_callback_count_);
102  EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
103  EXPECT_EQ(1, core_disconnecting_callback_count_);
104
105  // Calling Disconnect() twice doesn't do bad things.
106  core_.Disconnect();
107  EXPECT_FALSE(core_.client());
108  EXPECT_FALSE(core_.service());
109  EXPECT_FALSE(core_.refresh_scheduler());
110  EXPECT_EQ(1, core_connected_callback_count_);
111  EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
112  EXPECT_EQ(1, core_disconnecting_callback_count_);
113  EXPECT_EQ(0, bad_callback_count_);
114}
115
116TEST_F(CloudPolicyCoreTest, RefreshScheduler) {
117  EXPECT_FALSE(core_.refresh_scheduler());
118  core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
119  core_.StartRefreshScheduler();
120  ASSERT_TRUE(core_.refresh_scheduler());
121
122  int default_refresh_delay = core_.refresh_scheduler()->refresh_delay();
123
124  const int kRefreshRate = 1000 * 60 * 60;
125  prefs_.SetInteger(policy_prefs::kUserPolicyRefreshRate, kRefreshRate);
126  core_.TrackRefreshDelayPref(&prefs_, policy_prefs::kUserPolicyRefreshRate);
127  EXPECT_EQ(kRefreshRate, core_.refresh_scheduler()->refresh_delay());
128
129  prefs_.ClearPref(policy_prefs::kUserPolicyRefreshRate);
130  EXPECT_EQ(default_refresh_delay, core_.refresh_scheduler()->refresh_delay());
131
132  EXPECT_EQ(1, core_connected_callback_count_);
133  EXPECT_EQ(1, refresh_scheduler_started_callback_count_);
134  EXPECT_EQ(0, core_disconnecting_callback_count_);
135  EXPECT_EQ(0, bad_callback_count_);
136}
137
138}  // namespace policy
139