Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: chrome/browser/policy/cloud/cloud_policy_core_unittest.cc

Issue 23592017: Fix policy invalidator lifecycle bugs for Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/cloud/cloud_policy_core.h" 5 #include "chrome/browser/policy/cloud/cloud_policy_core.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/testing_pref_service.h" 9 #include "base/prefs/testing_pref_service.h"
10 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" 10 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
11 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" 11 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h"
12 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" 12 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h"
13 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" 13 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h"
14 #include "chrome/browser/prefs/browser_prefs.h" 14 #include "chrome/browser/prefs/browser_prefs.h"
15 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 namespace policy { 18 namespace policy {
19 19
20 class CloudPolicyCoreTest : public testing::Test { 20 class CloudPolicyCoreTest : public testing::Test,
21 public CloudPolicyCore::Observer {
21 protected: 22 protected:
22 CloudPolicyCoreTest() 23 CloudPolicyCoreTest()
23 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType, 24 : core_(PolicyNamespaceKey(dm_protocol::kChromeUserPolicyType,
24 std::string()), 25 std::string()),
25 &store_) { 26 &store_),
27 core_connected_callback_count_(0),
28 refresh_scheduler_started_callback_count_(0),
29 core_disconnecting_callback_count_(0),
30 bad_callback_count_(0) {
26 chrome::RegisterLocalState(prefs_.registry()); 31 chrome::RegisterLocalState(prefs_.registry());
32 core_.AddObserver(this);
33 }
34
35 virtual ~CloudPolicyCoreTest() {
36 core_.RemoveObserver(this);
37 }
38
39 virtual void OnCoreConnected(CloudPolicyCore* core) OVERRIDE {
40 // Make sure core is connected at callback time.
41 if (core_.client())
42 core_connected_callback_count_++;
43 else
44 bad_callback_count_++;
45 }
46
47 virtual void OnRefreshSchedulerStarted(CloudPolicyCore* core) OVERRIDE {
48 // Make sure refresh scheduler is started at callback time.
49 if (core_.refresh_scheduler())
50 refresh_scheduler_started_callback_count_++;
51 else
52 bad_callback_count_++;
53 }
54
55 virtual void OnCoreDisconnecting(CloudPolicyCore* core) OVERRIDE {
56 // Make sure core is still connected at callback time.
57 if (core_.client())
58 core_disconnecting_callback_count_++;
59 else
60 bad_callback_count_++;
27 } 61 }
28 62
29 base::MessageLoop loop_; 63 base::MessageLoop loop_;
30 64
31 TestingPrefServiceSimple prefs_; 65 TestingPrefServiceSimple prefs_;
32 MockCloudPolicyStore store_; 66 MockCloudPolicyStore store_;
33 CloudPolicyCore core_; 67 CloudPolicyCore core_;
34 68
69 int core_connected_callback_count_;
70 int refresh_scheduler_started_callback_count_;
71 int core_disconnecting_callback_count_;
72 int bad_callback_count_;
73
35 private: 74 private:
36 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreTest); 75 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreTest);
37 }; 76 };
38 77
39 TEST_F(CloudPolicyCoreTest, ConnectAndDisconnect) { 78 TEST_F(CloudPolicyCoreTest, ConnectAndDisconnect) {
40 EXPECT_TRUE(core_.store()); 79 EXPECT_TRUE(core_.store());
41 EXPECT_FALSE(core_.client()); 80 EXPECT_FALSE(core_.client());
42 EXPECT_FALSE(core_.service()); 81 EXPECT_FALSE(core_.service());
43 EXPECT_FALSE(core_.refresh_scheduler()); 82 EXPECT_FALSE(core_.refresh_scheduler());
44 83
45 // Connect() brings up client and service. 84 // Connect() brings up client and service.
46 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient())); 85 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
47 EXPECT_TRUE(core_.client()); 86 EXPECT_TRUE(core_.client());
48 EXPECT_TRUE(core_.service()); 87 EXPECT_TRUE(core_.service());
49 EXPECT_FALSE(core_.refresh_scheduler()); 88 EXPECT_FALSE(core_.refresh_scheduler());
89 EXPECT_EQ(1, core_connected_callback_count_);
90 EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
91 EXPECT_EQ(0, core_disconnecting_callback_count_);
50 92
51 // Disconnect() goes back to no client and service. 93 // Disconnect() goes back to no client and service.
52 core_.Disconnect(); 94 core_.Disconnect();
53 EXPECT_FALSE(core_.client()); 95 EXPECT_FALSE(core_.client());
54 EXPECT_FALSE(core_.service()); 96 EXPECT_FALSE(core_.service());
55 EXPECT_FALSE(core_.refresh_scheduler()); 97 EXPECT_FALSE(core_.refresh_scheduler());
98 EXPECT_EQ(1, core_connected_callback_count_);
99 EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
100 EXPECT_EQ(1, core_disconnecting_callback_count_);
56 101
57 // Calling Disconnect() twice doesn't do bad things. 102 // Calling Disconnect() twice doesn't do bad things.
58 core_.Disconnect(); 103 core_.Disconnect();
59 EXPECT_FALSE(core_.client()); 104 EXPECT_FALSE(core_.client());
60 EXPECT_FALSE(core_.service()); 105 EXPECT_FALSE(core_.service());
61 EXPECT_FALSE(core_.refresh_scheduler()); 106 EXPECT_FALSE(core_.refresh_scheduler());
107 EXPECT_EQ(1, core_connected_callback_count_);
108 EXPECT_EQ(0, refresh_scheduler_started_callback_count_);
109 EXPECT_EQ(1, core_disconnecting_callback_count_);
110 EXPECT_EQ(0, bad_callback_count_);
62 } 111 }
63 112
64 TEST_F(CloudPolicyCoreTest, RefreshScheduler) { 113 TEST_F(CloudPolicyCoreTest, RefreshScheduler) {
65 EXPECT_FALSE(core_.refresh_scheduler()); 114 EXPECT_FALSE(core_.refresh_scheduler());
66 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient())); 115 core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient()));
67 core_.StartRefreshScheduler(); 116 core_.StartRefreshScheduler();
68 ASSERT_TRUE(core_.refresh_scheduler()); 117 ASSERT_TRUE(core_.refresh_scheduler());
69 118
70 int default_refresh_delay = core_.refresh_scheduler()->refresh_delay(); 119 int default_refresh_delay = core_.refresh_scheduler()->refresh_delay();
71 120
72 const int kRefreshRate = 1000 * 60 * 60; 121 const int kRefreshRate = 1000 * 60 * 60;
73 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, kRefreshRate); 122 prefs_.SetInteger(prefs::kUserPolicyRefreshRate, kRefreshRate);
74 core_.TrackRefreshDelayPref(&prefs_, prefs::kUserPolicyRefreshRate); 123 core_.TrackRefreshDelayPref(&prefs_, prefs::kUserPolicyRefreshRate);
75 EXPECT_EQ(kRefreshRate, core_.refresh_scheduler()->refresh_delay()); 124 EXPECT_EQ(kRefreshRate, core_.refresh_scheduler()->refresh_delay());
76 125
77 prefs_.ClearPref(prefs::kUserPolicyRefreshRate); 126 prefs_.ClearPref(prefs::kUserPolicyRefreshRate);
78 EXPECT_EQ(default_refresh_delay, core_.refresh_scheduler()->refresh_delay()); 127 EXPECT_EQ(default_refresh_delay, core_.refresh_scheduler()->refresh_delay());
128
129 EXPECT_EQ(1, core_connected_callback_count_);
130 EXPECT_EQ(1, refresh_scheduler_started_callback_count_);
131 EXPECT_EQ(0, core_disconnecting_callback_count_);
132 EXPECT_EQ(0, bad_callback_count_);
79 } 133 }
80 134
81 } // namespace policy 135 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud/cloud_policy_core.cc ('k') | chrome/browser/policy/cloud/cloud_policy_invalidator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698