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 #include "components/policy/core/common/cloud/cloud_policy_core.h"
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"
20 class CloudPolicyCoreTest : public testing::Test,
21 public CloudPolicyCore::Observer {
24 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
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);
38 ~CloudPolicyCoreTest() override { core_.RemoveObserver(this); }
40 void OnCoreConnected(CloudPolicyCore* core) override {
41 // Make sure core is connected at callback time.
43 core_connected_callback_count_++;
45 bad_callback_count_++;
48 void OnRefreshSchedulerStarted(CloudPolicyCore* core) override {
49 // Make sure refresh scheduler is started at callback time.
50 if (core_.refresh_scheduler())
51 refresh_scheduler_started_callback_count_++;
53 bad_callback_count_++;
56 void OnCoreDisconnecting(CloudPolicyCore* core) override {
57 // Make sure core is still connected at callback time.
59 core_disconnecting_callback_count_++;
61 bad_callback_count_++;
64 base::MessageLoop loop_;
66 TestingPrefServiceSimple prefs_;
67 MockCloudPolicyStore store_;
68 CloudPolicyCore core_;
70 int core_connected_callback_count_;
71 int refresh_scheduler_started_callback_count_;
72 int core_disconnecting_callback_count_;
73 int bad_callback_count_;
76 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreTest);
79 TEST_F(CloudPolicyCoreTest, ConnectAndDisconnect) {
80 EXPECT_TRUE(core_.store());
81 EXPECT_FALSE(core_.client());
82 EXPECT_FALSE(core_.service());
83 EXPECT_FALSE(core_.refresh_scheduler());
85 // Connect() brings up client and service.
86 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
87 EXPECT_TRUE(core_.client());
88 EXPECT_TRUE(core_.service());
89 EXPECT_FALSE(core_.refresh_scheduler());
90 EXPECT_EQ(1, core_connected_callback_count_);
91 EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
92 EXPECT_EQ(0, core_disconnecting_callback_count_);
94 // Disconnect() goes back to no client and service.
96 EXPECT_FALSE(core_.client());
97 EXPECT_FALSE(core_.service());
98 EXPECT_FALSE(core_.refresh_scheduler());
99 EXPECT_EQ(1, core_connected_callback_count_);
100 EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
101 EXPECT_EQ(1, core_disconnecting_callback_count_);
103 // Calling Disconnect() twice doesn't do bad things.
105 EXPECT_FALSE(core_.client());
106 EXPECT_FALSE(core_.service());
107 EXPECT_FALSE(core_.refresh_scheduler());
108 EXPECT_EQ(1, core_connected_callback_count_);
109 EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
110 EXPECT_EQ(1, core_disconnecting_callback_count_);
111 EXPECT_EQ(0, bad_callback_count_);
114 TEST_F(CloudPolicyCoreTest, RefreshScheduler) {
115 EXPECT_FALSE(core_.refresh_scheduler());
116 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
117 core_.StartRefreshScheduler();
118 ASSERT_TRUE(core_.refresh_scheduler());
120 int default_refresh_delay = core_.refresh_scheduler()->refresh_delay();
122 const int kRefreshRate = 1000 * 60 * 60;
123 prefs_.SetInteger(policy_prefs::kUserPolicyRefreshRate, kRefreshRate);
124 core_.TrackRefreshDelayPref(&prefs_, policy_prefs::kUserPolicyRefreshRate);
125 EXPECT_EQ(kRefreshRate, core_.refresh_scheduler()->refresh_delay());
127 prefs_.ClearPref(policy_prefs::kUserPolicyRefreshRate);
128 EXPECT_EQ(default_refresh_delay, core_.refresh_scheduler()->refresh_delay());
130 EXPECT_EQ(1, core_connected_callback_count_);
131 EXPECT_EQ(1, refresh_scheduler_started_callback_count_);
132 EXPECT_EQ(0, core_disconnecting_callback_count_);
133 EXPECT_EQ(0, bad_callback_count_);
136 } // namespace policy