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

Side by Side Diff: chrome/browser/policy/asynchronous_policy_loader_unittest.cc

Issue 5562002: Refactor FileBasedPolicyProvider, introduce AsynchronousPolicyProvider. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix nits Created 10 years 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 "chrome/browser/browser_thread.h"
6 #include "chrome/browser/policy/asynchronous_policy_loader.h"
7 #include "chrome/browser/policy/asynchronous_policy_test_base.h"
8 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
9
10 using ::testing::_;
11 using ::testing::InSequence;
12 using ::testing::Return;
13
14 namespace policy {
15
16 const int kReloadIntervalMinutesForTesting = 1;
17
18 class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase {
19 public:
20 AsynchronousPolicyLoaderTest() {}
21 virtual ~AsynchronousPolicyLoaderTest() {}
22
23 virtual void SetUp() {
24 AsynchronousPolicyTestBase::SetUp();
25 provider_.reset(new MockConfigurationPolicyProvider());
26 }
27
28 protected:
29 scoped_ptr<MockConfigurationPolicyProvider> provider_;
30
31 private:
32 DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoaderTest);
33 };
34
35 ACTION_P(CreateTestDictionary, quit) {
36 if (quit) {
37 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(
38 &MessageLoopQuitNow));
39 }
40 return new DictionaryValue();
41 }
42
43 ACTION_P2(CreateSequencedTestDictionary, number, quit) {
44 DictionaryValue* test_dictionary = new DictionaryValue();
45 test_dictionary->SetInteger("id", ++(*number));
46 if (quit) {
47 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(
48 &MessageLoopQuitNow));
49 }
50 return test_dictionary;
51 }
52
53 ACTION(RescheduleImmediatePolicyReload) {
54 *arg1 = base::TimeDelta();
55 return false;
56 }
57
58 TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) {
59 DictionaryValue* template_dict(new DictionaryValue());
60 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly(
61 Return(true));
62 EXPECT_CALL(*delegate_, Load()).WillOnce(Return(template_dict));
63 scoped_refptr<AsynchronousPolicyLoader> loader =
64 new AsynchronousPolicyLoader(provider_->AsWeakPtr(),
65 delegate_.release(),
66 kReloadIntervalMinutesForTesting);
67 loader->Init();
68 scoped_ptr<DictionaryValue> loaded_dict(loader->GetPolicy());
69 EXPECT_TRUE(loaded_dict->Equals(template_dict));
70 }
71
72 // Verify that the fallback policy requests are made.
73 TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) {
74 int dictionary_number = 0;
75 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly(
76 Return(true));
77 InSequence s;
78 EXPECT_CALL(*delegate_, Load()).WillOnce(
79 CreateSequencedTestDictionary(&dictionary_number, false));
80 EXPECT_CALL(*delegate_, Load()).WillOnce(
81 CreateSequencedTestDictionary(&dictionary_number, true));
82 scoped_refptr<AsynchronousPolicyLoader> loader =
83 new AsynchronousPolicyLoader(provider_->AsWeakPtr(),
84 delegate_.release(),
85 0);
86 loader->Init();
87 loop_.RunAllPending();
88
89 scoped_ptr<DictionaryValue> loaded_dict(loader->GetPolicy());
90 int loaded_number;
91 EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number));
92 EXPECT_EQ(dictionary_number, loaded_number);
93
94 DontRunAllPendingDuringTearDown();
95 }
96
97 // Ensure that calling stop on the loader stops subsequent reloads from
98 // happening.
99 TEST_F(AsynchronousPolicyLoaderTest, Stop) {
100 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly(
101 Return(true));
102 ON_CALL(*delegate_, Load()).WillByDefault(CreateTestDictionary(false));
103 EXPECT_CALL(*delegate_, Load()).Times(1);
104 scoped_refptr<AsynchronousPolicyLoader> loader =
105 new AsynchronousPolicyLoader(provider_->AsWeakPtr(),
106 delegate_.release(),
107 0);
108 loader->Init();
109 loader->Stop();
110 }
111
112 // Verifies that if it's not safe to reload a policy, subsequent
113 // attempts will be made.
114 TEST_F(AsynchronousPolicyLoaderTest, UnsafeForPolicyReload) {
115 InSequence s;
116 EXPECT_CALL(*delegate_, Load()).WillOnce(CreateTestDictionary(false));
117 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce(
118 RescheduleImmediatePolicyReload());
119 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce(
120 Return(true));
121 // Successful reload, but determined unsafe after the load,
122 // should trigger another reload.
123 EXPECT_CALL(*delegate_, Load()).WillOnce(CreateTestDictionary(false));
124 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce(
125 RescheduleImmediatePolicyReload());
126 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce(
127 Return(true));
128 // Successful reload.
129 EXPECT_CALL(*delegate_, Load()).WillOnce(CreateTestDictionary(true));
130 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillOnce(
131 Return(true));
132 scoped_refptr<AsynchronousPolicyLoader> loader =
133 new AsynchronousPolicyLoader(provider_->AsWeakPtr(),
134 delegate_.release(),
135 0);
136 loader->Init();
137 }
138
139 // Verifies that the provider is notified upon policy reload, but only
140 // if the policy changed.
141 TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) {
142 EXPECT_CALL(*delegate_, IsSafeToReloadPolicy(_, _)).WillRepeatedly(
143 Return(true));
144 InSequence s;
145 int dictionary_number_1 = 0;
146 int dictionary_number_2 = 0;
147 EXPECT_CALL(*delegate_, Load()).WillOnce(
148 CreateSequencedTestDictionary(&dictionary_number_1, false));
149 EXPECT_CALL(*delegate_, Load()).WillOnce(
150 CreateSequencedTestDictionary(&dictionary_number_2, false));
151 EXPECT_CALL(*provider_, NotifyStoreOfPolicyChange()).Times(0);
152 EXPECT_CALL(*delegate_, Load()).WillOnce(
153 CreateSequencedTestDictionary(&dictionary_number_2, false));
154 EXPECT_CALL(*provider_, NotifyStoreOfPolicyChange()).Times(1);
155 EXPECT_CALL(*delegate_, Load()).WillOnce(
156 CreateSequencedTestDictionary(&dictionary_number_1, true));
157 scoped_refptr<AsynchronousPolicyLoader> loader =
158 new AsynchronousPolicyLoader(provider_->AsWeakPtr(),
159 delegate_.release(),
160 0);
161 loader->Init();
162
163 loop_.RunAllPending();
164 DontRunAllPendingDuringTearDown();
165 }
166
167 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698