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

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

Issue 10448118: Add the AsyncPolicyProvider and AsyncPolicyLoader (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/policy/async_policy_provider.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "chrome/browser/policy/async_policy_provider.h"
6
7 #include "base/message_loop.h"
8 #include "base/values.h"
9 #include "chrome/browser/policy/async_policy_loader.h"
10 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/test/test_browser_thread.h"
13 #include "policy/policy_constants.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using testing::Mock;
18 using testing::Return;
19 using testing::Sequence;
20
21 namespace policy {
22
23 namespace {
24
25 // Helper to write a policy in |bundle| with less code.
26 void SetPolicy(PolicyBundle* bundle,
27 const std::string& name,
28 const std::string& value) {
29 bundle->Get(POLICY_DOMAIN_CHROME, "")
30 .Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
31 base::Value::CreateStringValue(value));
32 }
33
34 class MockPolicyLoader : public AsyncPolicyLoader {
35 public:
36 MockPolicyLoader();
37 virtual ~MockPolicyLoader();
38
39 // Load() returns a scoped_ptr<PolicyBundle> but it can't be mocked because
40 // scoped_ptr is moveable but not copyable. This override forwards the
41 // call to MockLoad() which returns a PolicyBundle*, and returns a copy
42 // wrapped in a passed scoped_ptr.
43 virtual scoped_ptr<PolicyBundle> Load() OVERRIDE;
44
45 MOCK_METHOD0(MockLoad, const PolicyBundle*());
46 MOCK_METHOD0(InitOnFile, void());
47 MOCK_METHOD0(LastModificationTime, base::Time());
48
49 private:
50 DISALLOW_COPY_AND_ASSIGN(MockPolicyLoader);
51 };
52
53 MockPolicyLoader::MockPolicyLoader() {}
54
55 MockPolicyLoader::~MockPolicyLoader() {}
56
57 scoped_ptr<PolicyBundle> MockPolicyLoader::Load() {
58 scoped_ptr<PolicyBundle> bundle;
59 const PolicyBundle* loaded = MockLoad();
60 if (loaded) {
61 bundle.reset(new PolicyBundle());
62 bundle->CopyFrom(*loaded);
63 }
64 return bundle.Pass();
65 }
66
67 } // namespace
68
69 class AsyncPolicyProviderTest : public testing::Test {
70 protected:
71 AsyncPolicyProviderTest();
72 virtual ~AsyncPolicyProviderTest();
73
74 virtual void SetUp() OVERRIDE;
75 virtual void TearDown() OVERRIDE;
76
77 PolicyBundle initial_bundle_;
78 MockPolicyLoader* loader_;
79 scoped_ptr<AsyncPolicyProvider> provider_;
80
81 MessageLoop loop_;
82
83 private:
84 content::TestBrowserThread ui_thread_;
85 content::TestBrowserThread file_thread_;
86
87 DISALLOW_COPY_AND_ASSIGN(AsyncPolicyProviderTest);
88 };
89
90 AsyncPolicyProviderTest::AsyncPolicyProviderTest()
91 : ui_thread_(content::BrowserThread::UI, &loop_),
92 file_thread_(content::BrowserThread::FILE, &loop_) {}
93
94 AsyncPolicyProviderTest::~AsyncPolicyProviderTest() {}
95
96 void AsyncPolicyProviderTest::SetUp() {
97 SetPolicy(&initial_bundle_, "policy", "initial");
98 loader_ = new MockPolicyLoader();
99 EXPECT_CALL(*loader_, LastModificationTime())
100 .WillRepeatedly(Return(base::Time()));
101 EXPECT_CALL(*loader_, InitOnFile()).Times(1);
102 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_));
103
104 provider_.reset(
105 new AsyncPolicyProvider(GetChromePolicyDefinitionList(),
106 scoped_ptr<AsyncPolicyLoader>(loader_)));
107 // Verify that the initial load is done synchronously:
108 EXPECT_TRUE(provider_->policies().Equals(initial_bundle_));
109
110 loop_.RunAllPending();
111 Mock::VerifyAndClearExpectations(loader_);
112
113 EXPECT_CALL(*loader_, LastModificationTime())
114 .WillRepeatedly(Return(base::Time()));
115 }
116
117 void AsyncPolicyProviderTest::TearDown() {
118 provider_.reset();
119 loop_.RunAllPending();
120 }
121
122 TEST_F(AsyncPolicyProviderTest, RefreshPolicies) {
123 PolicyBundle refreshed_bundle;
124 SetPolicy(&refreshed_bundle, "policy", "refreshed");
125 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle));
126
127 MockConfigurationPolicyObserver observer;
128 ConfigurationPolicyObserverRegistrar registrar;
129 registrar.Init(provider_.get(), &observer);
130 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
131 provider_->RefreshPolicies();
132 loop_.RunAllPending();
133 // The refreshed policies are now provided.
134 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle));
135 }
136
137 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) {
138 PolicyBundle refreshed_bundle;
139 SetPolicy(&refreshed_bundle, "policy", "refreshed");
140 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle));
141
142 MockConfigurationPolicyObserver observer;
143 ConfigurationPolicyObserverRegistrar registrar;
144 registrar.Init(provider_.get(), &observer);
145 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
146 provider_->RefreshPolicies();
147 // Doesn't refresh before going through the FILE thread.
148 Mock::VerifyAndClearExpectations(&observer);
149
150 // Doesn't refresh if another RefreshPolicies request is made.
151 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
152 provider_->RefreshPolicies();
153 Mock::VerifyAndClearExpectations(&observer);
154
155 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
156 loop_.RunAllPending();
157 // The refreshed policies are now provided.
158 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle));
159 Mock::VerifyAndClearExpectations(&observer);
160 }
161
162 TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) {
163 PolicyBundle reloaded_bundle;
164 SetPolicy(&reloaded_bundle, "policy", "reloaded");
165 PolicyBundle refreshed_bundle;
166 SetPolicy(&refreshed_bundle, "policy", "refreshed");
167
168 Sequence load_sequence;
169 // Reload.
170 EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence)
171 .WillOnce(Return(&reloaded_bundle));
172 // RefreshPolicies.
173 EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence)
174 .WillOnce(Return(&refreshed_bundle));
175
176 MockConfigurationPolicyObserver observer;
177 ConfigurationPolicyObserverRegistrar registrar;
178 registrar.Init(provider_.get(), &observer);
179 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
180
181 // A Reload is triggered before RefreshPolicies, and it shouldn't trigger
182 // notifications.
183 loader_->Reload(true);
184 Mock::VerifyAndClearExpectations(&observer);
185
186 // Doesn't refresh before going through the FILE thread.
187 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
188 provider_->RefreshPolicies();
189 Mock::VerifyAndClearExpectations(&observer);
190
191 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
192 loop_.RunAllPending();
193 // The refreshed policies are now provided, and the |reloaded_bundle| was
194 // dropped.
195 EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle));
196 Mock::VerifyAndClearExpectations(&observer);
197 }
198
199 TEST_F(AsyncPolicyProviderTest, Shutdown) {
200 EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&initial_bundle_));
201
202 MockConfigurationPolicyObserver observer;
203 ConfigurationPolicyObserverRegistrar registrar;
204 registrar.Init(provider_.get(), &observer);
205
206 // Though there is a pending Reload, the provider and the loader can be
207 // deleted at any time.
208 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
209 loader_->Reload(true);
210 Mock::VerifyAndClearExpectations(&observer);
211
212 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
213 EXPECT_CALL(observer, OnProviderGoingAway(provider_.get()));
214 provider_.reset();
215 loop_.RunAllPending();
216 Mock::VerifyAndClearExpectations(&observer);
217 }
218
219 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/async_policy_provider.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698