Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / policy / core / common / cloud / cloud_policy_core_unittest.cc
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
18 namespace policy {
19
20 class 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   ~CloudPolicyCoreTest() override { core_.RemoveObserver(this); }
39
40   void OnCoreConnected(CloudPolicyCore* core) override {
41     // Make sure core is connected at callback time.
42     if (core_.client())
43       core_connected_callback_count_++;
44     else
45       bad_callback_count_++;
46   }
47
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_++;
52     else
53       bad_callback_count_++;
54   }
55
56   void OnCoreDisconnecting(CloudPolicyCore* core) override {
57     // Make sure core is still connected at callback time.
58     if (core_.client())
59       core_disconnecting_callback_count_++;
60     else
61       bad_callback_count_++;
62   }
63
64   base::MessageLoop loop_;
65
66   TestingPrefServiceSimple prefs_;
67   MockCloudPolicyStore store_;
68   CloudPolicyCore core_;
69
70   int core_connected_callback_count_;
71   int refresh_scheduler_started_callback_count_;
72   int core_disconnecting_callback_count_;
73   int bad_callback_count_;
74
75  private:
76   DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreTest);
77 };
78
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());
84
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_);
93
94   // Disconnect() goes back to no client and service.
95   core_.Disconnect();
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_);
102
103   // Calling Disconnect() twice doesn't do bad things.
104   core_.Disconnect();
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_);
112 }
113
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());
119
120   int default_refresh_delay = core_.refresh_scheduler()->refresh_delay();
121
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());
126
127   prefs_.ClearPref(policy_prefs::kUserPolicyRefreshRate);
128   EXPECT_EQ(default_refresh_delay, core_.refresh_scheduler()->refresh_delay());
129
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_);
134 }
135
136 }  // namespace policy