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

Side by Side Diff: chrome/browser/policy/file_based_policy_provider_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
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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/asynchronous_policy_loader.h"
6 #include "chrome/browser/policy/asynchronous_policy_test_base.h"
5 #include "chrome/browser/policy/configuration_policy_pref_store.h" 7 #include "chrome/browser/policy/configuration_policy_pref_store.h"
8 #include "chrome/browser/policy/configuration_policy_store_interface.h"
6 #include "chrome/browser/policy/file_based_policy_provider.h" 9 #include "chrome/browser/policy/file_based_policy_provider.h"
10 #include "chrome/common/policy_constants.h"
7 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
9 13
10 using testing::Mock; 14 using testing::_;
15 using testing::InSequence;
16 using testing::Return;
11 17
12 namespace policy { 18 namespace policy {
13 19
14 // Shorter reload intervals for testing FileBasedPolicyLoader. 20 class FileBasedPolicyProviderDelegateMock
15 const int kSettleIntervalSecondsForTesting = 0; 21 : public FileBasedPolicyProvider::ProviderDelegate {
16 const int kReloadIntervalMinutesForTesting = 1;
17
18 // A delegate for testing that can feed arbitrary information to the loader.
19 class TestDelegate : public FileBasedPolicyProvider::Delegate {
20 public: 22 public:
21 TestDelegate() 23 FileBasedPolicyProviderDelegateMock()
22 : FileBasedPolicyProvider::Delegate(FilePath(FILE_PATH_LITERAL("fake"))) { 24 : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {}
23 } 25 MOCK_METHOD0(Load, DictionaryValue*());
24 26 MOCK_METHOD0(GetLastModification, base::Time());
25 // FileBasedPolicyProvider::Delegate implementation:
26 virtual DictionaryValue* Load() {
27 return static_cast<DictionaryValue*>(dict_.DeepCopy());
28 }
29
30 virtual base::Time GetLastModification() {
31 return last_modification_;
32 }
33
34 DictionaryValue* dict() { return &dict_; }
35 void set_last_modification(const base::Time& last_modification) {
36 last_modification_ = last_modification;
37 }
38
39 private:
40 DictionaryValue dict_;
41 base::Time last_modification_;
42 }; 27 };
43 28
44 // A mock provider that allows us to capture reload notifications. 29 TEST_F(AsynchronousPolicyTestBase, ProviderInit) {
45 class MockPolicyProvider : public ConfigurationPolicyProvider, 30 base::Time last_modified;
46 public base::SupportsWeakPtr<MockPolicyProvider> { 31 FileBasedPolicyProviderDelegateMock* provider_delegate =
47 public: 32 new FileBasedPolicyProviderDelegateMock();
48 explicit MockPolicyProvider() 33 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
49 : ConfigurationPolicyProvider( 34 Return(last_modified));
50 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()) { 35 InSequence s;
51 } 36 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
52 37 new DictionaryValue));
53 virtual bool Provide(ConfigurationPolicyStoreInterface* store) { 38 DictionaryValue* policies = new DictionaryValue();
54 return true; 39 policies->SetBoolean(policy::key::kSyncDisabled, true);
55 } 40 // A second call to Load gets triggered during the provider's construction
56 41 // when the file watcher is initialized, since this file may have changed
57 MOCK_METHOD0(NotifyStoreOfPolicyChange, void()); 42 // between the initial load and creating watcher.
58 }; 43 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
59 44 FileBasedPolicyProvider provider(
60 class FileBasedPolicyLoaderTest : public testing::Test { 45 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
61 protected: 46 provider_delegate);
62 FileBasedPolicyLoaderTest() 47 loop_.RunAllPending();
63 : ui_thread_(BrowserThread::UI, &loop_), 48 EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
64 file_thread_(BrowserThread::FILE, &loop_) {} 49 provider.Provide(store_.get());
65
66 virtual void TearDown() {
67 loop_.RunAllPending();
68 }
69
70 MessageLoop loop_;
71
72 private:
73 BrowserThread ui_thread_;
74 BrowserThread file_thread_;
75 };
76
77 TEST_F(FileBasedPolicyLoaderTest, BasicLoad) {
78 TestDelegate* test_delegate = new TestDelegate;
79 test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com");
80
81 scoped_refptr<FileBasedPolicyLoader> loader(
82 new FileBasedPolicyLoader(base::WeakPtr<FileBasedPolicyProvider>(),
83 test_delegate,
84 kSettleIntervalSecondsForTesting,
85 kReloadIntervalMinutesForTesting));
86 scoped_ptr<DictionaryValue> policy(loader->GetPolicy());
87 EXPECT_TRUE(policy.get());
88 EXPECT_EQ(1U, policy->size());
89
90 std::string str_value;
91 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
92 EXPECT_EQ("http://www.google.com", str_value);
93
94 loader->Stop();
95 } 50 }
96 51
97 TEST_F(FileBasedPolicyLoaderTest, TestRefresh) { 52 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) {
98 MockPolicyProvider provider; 53 base::Time last_modified;
99 TestDelegate* test_delegate = new TestDelegate; 54 FileBasedPolicyProviderDelegateMock* provider_delegate =
100 55 new FileBasedPolicyProviderDelegateMock();
101 scoped_refptr<FileBasedPolicyLoader> loader( 56 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
102 new FileBasedPolicyLoader(provider.AsWeakPtr(), 57 Return(last_modified));
103 test_delegate, 58 InSequence s;
104 kSettleIntervalSecondsForTesting, 59 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
105 kReloadIntervalMinutesForTesting)); 60 new DictionaryValue));
106 scoped_ptr<DictionaryValue> policy(loader->GetPolicy()); 61 FileBasedPolicyProvider file_based_provider(
107 EXPECT_TRUE(policy.get()); 62 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
108 EXPECT_EQ(0U, policy->size()); 63 provider_delegate);
109 64 // A second call to Load gets triggered during the provider's construction
110 test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com"); 65 // when the file watcher is initialized, since this file may have changed
111 66 // between the initial load and creating watcher.
112 EXPECT_CALL(provider, NotifyStoreOfPolicyChange()).Times(1); 67 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
113 loader->OnFilePathChanged(FilePath(FILE_PATH_LITERAL("fake"))); 68 new DictionaryValue));
114
115 // Run the loop. The refresh should be handled immediately since the settle
116 // interval has been disabled.
117 loop_.RunAllPending(); 69 loop_.RunAllPending();
118 Mock::VerifyAndClearExpectations(&provider); 70 // A third and final call to Load is made by the explicit Reload. This
119 71 // should be the one that provides the current policy.
120 policy.reset(loader->GetPolicy()); 72 DictionaryValue* policies = new DictionaryValue();
121 EXPECT_TRUE(policy.get()); 73 policies->SetBoolean(policy::key::kSyncDisabled, true);
122 EXPECT_EQ(1U, policy->size()); 74 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
123 75 file_based_provider.loader()->Reload();
124 std::string str_value; 76 loop_.RunAllPending();
125 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value)); 77 EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
126 EXPECT_EQ("http://www.google.com", str_value); 78 file_based_provider.Provide(store_.get());
127
128 loader->Stop();
129 } 79 }
130 80
131 } // namespace policy 81 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/file_based_policy_provider.cc ('k') | chrome/browser/policy/mock_configuration_policy_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698