Chromium Code Reviews

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: review feedback Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | 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 "base/file_util.h"
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 not needed?
danno 2010/12/06 14:05:12 Done.
6 #include "base/scoped_temp_dir.h"
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 not needed?
danno 2010/12/06 14:05:12 Done.
7 #include "chrome/browser/policy/asynchronous_policy_loader.h"
8 #include "chrome/browser/policy/asynchronous_policy_provider.h"
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 Needed? You get that include from file_based_polic
danno 2010/12/06 14:05:12 Done.
9 #include "chrome/browser/policy/asynchronous_policy_test_base.h"
5 #include "chrome/browser/policy/configuration_policy_pref_store.h" 10 #include "chrome/browser/policy/configuration_policy_pref_store.h"
11 #include "chrome/browser/policy/configuration_policy_store_interface.h"
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 needed? You include the mock below and AFAICS, you
danno 2010/12/06 14:05:12 Done.
6 #include "chrome/browser/policy/file_based_policy_provider.h" 12 #include "chrome/browser/policy/file_based_policy_provider.h"
13 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
14 #include "chrome/browser/policy/mock_configuration_policy_store.h"
15 #include "chrome/common/policy_constants.h"
7 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
8 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
9 18
19 using testing::_;
20 using testing::InSequence;
10 using testing::Mock; 21 using testing::Mock;
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 you don't use this.
danno 2010/12/06 14:05:12 Done.
22 using testing::Return;
11 23
12 namespace policy { 24 namespace policy {
13 25
14 // Shorter reload intervals for testing FileBasedPolicyLoader. 26 class FileBasedPolicyProviderDelegateMock
15 const int kSettleIntervalSecondsForTesting = 0; 27 : 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: 28 public:
21 TestDelegate() 29 FileBasedPolicyProviderDelegateMock()
22 : FileBasedPolicyProvider::Delegate(FilePath(FILE_PATH_LITERAL("fake"))) { 30 : FileBasedPolicyProvider::ProviderDelegate(FilePath()) {}
23 } 31 MOCK_METHOD0(Load, DictionaryValue*());
24 32 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 }; 33 };
43 34
44 // A mock provider that allows us to capture reload notifications. 35 TEST_F(AsynchronousPolicyTestBase, ProviderInit) {
45 class MockPolicyProvider : public ConfigurationPolicyProvider, 36 base::Time last_modified;
46 public base::SupportsWeakPtr<MockPolicyProvider> { 37 scoped_ptr<FileBasedPolicyProviderDelegateMock> provider_delegate(
47 public: 38 new FileBasedPolicyProviderDelegateMock);
48 explicit MockPolicyProvider() 39 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
49 : ConfigurationPolicyProvider( 40 Return(last_modified));
50 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()) { 41 InSequence s;
51 } 42 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
52 43 new DictionaryValue));
53 virtual bool Provide(ConfigurationPolicyStoreInterface* store) { 44 DictionaryValue* policies = new DictionaryValue();
54 return true; 45 policies->SetBoolean(policy::key::kSyncDisabled, true);
55 } 46 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 Why do you install two expected calls on provider_
danno 2010/12/06 14:05:12 Done.
56 47 provider_.reset(new FileBasedPolicyProvider(
57 MOCK_METHOD0(NotifyStoreOfPolicyChange, void()); 48 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
58 }; 49 provider_delegate.release()));
59 50 EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
60 class FileBasedPolicyLoaderTest : public testing::Test { 51 loop_.RunAllPending();
61 protected: 52 provider_->Provide(store_.get());
62 FileBasedPolicyLoaderTest()
63 : ui_thread_(BrowserThread::UI, &loop_),
64 file_thread_(BrowserThread::FILE, &loop_) {}
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 } 53 }
96 54
97 TEST_F(FileBasedPolicyLoaderTest, TestRefresh) { 55 TEST_F(AsynchronousPolicyTestBase, ProviderRefresh) {
98 MockPolicyProvider provider; 56 base::Time last_modified;
99 TestDelegate* test_delegate = new TestDelegate; 57 scoped_ptr<FileBasedPolicyProviderDelegateMock> provider_delegate(
100 58 new FileBasedPolicyProviderDelegateMock);
101 scoped_refptr<FileBasedPolicyLoader> loader( 59 EXPECT_CALL(*provider_delegate, GetLastModification()).WillRepeatedly(
102 new FileBasedPolicyLoader(provider.AsWeakPtr(), 60 Return(last_modified));
103 test_delegate, 61 InSequence s;
104 kSettleIntervalSecondsForTesting, 62 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(
105 kReloadIntervalMinutesForTesting)); 63 new DictionaryValue));
106 scoped_ptr<DictionaryValue> policy(loader->GetPolicy()); 64 DictionaryValue* policies = new DictionaryValue();
107 EXPECT_TRUE(policy.get()); 65 policies->SetBoolean(policy::key::kSyncDisabled, true);
108 EXPECT_EQ(0U, policy->size()); 66 EXPECT_CALL(*provider_delegate, Load()).WillOnce(Return(policies));
109 67 FileBasedPolicyProvider* file_based_provider =
110 test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com"); 68 new FileBasedPolicyProvider(
111 69 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
112 EXPECT_CALL(provider, NotifyStoreOfPolicyChange()).Times(1); 70 provider_delegate.release());
113 loader->OnFilePathChanged(FilePath(FILE_PATH_LITERAL("fake"))); 71 provider_.reset(file_based_provider);
114 72 EXPECT_CALL(*store_, Apply(policy::kPolicySyncDisabled, _)).Times(1);
115 // Run the loop. The refresh should be handled immediately since the settle
116 // interval has been disabled.
117 loop_.RunAllPending(); 73 loop_.RunAllPending();
118 Mock::VerifyAndClearExpectations(&provider); 74 file_based_provider->loader()->Reload();
119 75 loop_.RunAllPending();
120 policy.reset(loader->GetPolicy()); 76 provider_->Provide(store_.get());
Mattias Nissler (ping if slow) 2010/12/06 10:26:20 Does this trigger the second EXPECT_CALL(*provider
danno 2010/12/06 14:05:12 Done.
121 EXPECT_TRUE(policy.get());
122 EXPECT_EQ(1U, policy->size());
123
124 std::string str_value;
125 EXPECT_TRUE(policy->GetString("HomepageLocation", &str_value));
126 EXPECT_EQ("http://www.google.com", str_value);
127
128 loader->Stop();
129 } 77 }
130 78
131 } // namespace policy 79 } // namespace policy
OLDNEW

Powered by Google App Engine