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

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

Powered by Google App Engine
This is Rietveld 408576698