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

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: review feedback 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"
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 Do you need that header?
danno 2010/12/06 14:05:12 Done.
6 #include "chrome/browser/policy/asynchronous_policy_loader.h"
7 #include "chrome/browser/policy/asynchronous_policy_provider.h"
8 #include "chrome/browser/policy/asynchronous_policy_test_base.h"
9 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
10 #include "chrome/common/notification_registrar.h"
11 #include "chrome/common/notification_service.h"
12 #include "chrome/common/notification_source.h"
13 #include "chrome/test/mock_notification_observer.h"
14
15 using ::testing::_;
16 using ::testing::InSequence;
17 using ::testing::Return;
18
19 namespace policy {
20
21 const int kReloadIntervalMinutesForTesting = 1;
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 This is unused.
danno 2010/12/06 14:05:12 Done.
22
23 class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase {
24 public:
25 AsynchronousPolicyLoaderTest() {}
26 virtual ~AsynchronousPolicyLoaderTest() {}
27
28 virtual void SetUp() {
29 AsynchronousPolicyTestBase::SetUp();
30 mock_provider_ = new MockConfigurationPolicyProvider();
31 provider_.reset(mock_provider_);
32 }
33
34 protected:
35 MockConfigurationPolicyProvider* mock_provider_; // weak
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoaderTest);
39 };
40
41 ACTION(CreateTestDictionary) {
42 return new DictionaryValue();
43 }
44
45 ACTION_P(CreateSequencedTestDictionary, number) {
46 DictionaryValue* test_dictionary = new DictionaryValue();
47 test_dictionary->SetInteger("id", ++(*number));
48 return test_dictionary;
49 }
50
51 ACTION(RescheduleImmediatePolicyReload) {
52 *arg1 = base::TimeDelta();
53 return false;
54 }
55
56 TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) {
57 DictionaryValue* template_dict(new DictionaryValue());
58 EXPECT_CALL(*delegate_, Load()).WillOnce(Return(template_dict));
59 scoped_refptr<AsynchronousPolicyLoader> loader =
60 new AsynchronousPolicyLoader(delegate_.release());
61 loader->Init();
62 const DictionaryValue* loaded_dict(loader->policy());
63 EXPECT_TRUE(loaded_dict->Equals(template_dict));
64 }
65
66 // Verify that the fallback policy requests are made.
67 TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) {
68 int dictionary_number = 0;
69 InSequence s;
70 EXPECT_CALL(*delegate_, Load()).WillOnce(
71 CreateSequencedTestDictionary(&dictionary_number));
72 EXPECT_CALL(*delegate_, Load()).WillOnce(
73 CreateSequencedTestDictionary(&dictionary_number));
74 scoped_refptr<AsynchronousPolicyLoader> loader =
75 new AsynchronousPolicyLoader(delegate_.release());
76 loader->Init();
77 loop_.RunAllPending();
78 loader->Reload();
79 loop_.RunAllPending();
80
81 const DictionaryValue* loaded_dict(loader->policy());
82 int loaded_number;
83 EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number));
84 EXPECT_EQ(dictionary_number, loaded_number);
85 }
86
87 // Ensure that calling stop on the loader stops subsequent reloads from
88 // happening.
89 TEST_F(AsynchronousPolicyLoaderTest, Stop) {
90 ON_CALL(*delegate_, Load()).WillByDefault(CreateTestDictionary());
91 EXPECT_CALL(*delegate_, Load()).Times(1);
92 scoped_refptr<AsynchronousPolicyLoader> loader =
93 new AsynchronousPolicyLoader(delegate_.release());
94 loader->Init();
95 loop_.RunAllPending();
96 loader->Stop();
97 loop_.RunAllPending();
98 loader->Reload();
99 loop_.RunAllPending();
100 }
101
102 // Verifies that the provider is notified upon policy reload, but only
103 // if the policy changed.
104 TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) {
105 InSequence s;
106 MockNotificationObserver observer;
107 // NotificationService service;
108 NotificationRegistrar registrar;
109 registrar.Add(&observer,
110 NotificationType::POLICY_CHANGED,
111 NotificationService::AllSources());
112 int dictionary_number_1 = 0;
113 int dictionary_number_2 = 0;
114 EXPECT_CALL(*delegate_, Load()).WillOnce(
115 CreateSequencedTestDictionary(&dictionary_number_1));
116 EXPECT_CALL(*delegate_, Load()).WillOnce(
117 CreateSequencedTestDictionary(&dictionary_number_2));
118 EXPECT_CALL(observer, Observe(_, _, _)).Times(0);
119 EXPECT_CALL(*delegate_, Load()).WillOnce(
120 CreateSequencedTestDictionary(&dictionary_number_2));
121 EXPECT_CALL(observer, Observe(_, _, _)).Times(1);
122 EXPECT_CALL(*delegate_, Load()).WillOnce(
123 CreateSequencedTestDictionary(&dictionary_number_1));
124 scoped_refptr<AsynchronousPolicyLoader> loader =
125 new AsynchronousPolicyLoader(delegate_.release());
126 AsynchronousPolicyProvider provider(NULL, loader);
127 loop_.RunAllPending();
128 loader->Reload();
129 loop_.RunAllPending();
130 loader->Reload();
131 loop_.RunAllPending();
132 loader->Reload();
133 loop_.RunAllPending();
134 }
135
136 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698