| OLD | NEW |
| (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/configuration_policy_pref_store.h" |
| 6 #include "chrome/browser/policy/file_based_policy_provider.h" |
| 7 #include "testing/gmock/include/gmock/gmock.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 using testing::Mock; |
| 11 |
| 12 namespace policy { |
| 13 |
| 14 // Shorter reload intervals for testing FileBasedPolicyLoader. |
| 15 const int kSettleIntervalSecondsForTesting = 0; |
| 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: |
| 21 TestDelegate() |
| 22 : FileBasedPolicyProvider::Delegate(FilePath(FILE_PATH_LITERAL("fake"))) { |
| 23 } |
| 24 |
| 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 }; |
| 43 |
| 44 // A mock provider that allows us to capture reload notifications. |
| 45 class MockPolicyProvider : public ConfigurationPolicyProvider, |
| 46 public base::SupportsWeakPtr<MockPolicyProvider> { |
| 47 public: |
| 48 explicit MockPolicyProvider() |
| 49 : ConfigurationPolicyProvider( |
| 50 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()) { |
| 51 } |
| 52 |
| 53 virtual bool Provide(ConfigurationPolicyStore* store) { |
| 54 return true; |
| 55 } |
| 56 |
| 57 MOCK_METHOD0(NotifyStoreOfPolicyChange, void()); |
| 58 }; |
| 59 |
| 60 class FileBasedPolicyLoaderTest : public testing::Test { |
| 61 protected: |
| 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 } |
| 96 |
| 97 TEST_F(FileBasedPolicyLoaderTest, TestRefresh) { |
| 98 MockPolicyProvider provider; |
| 99 TestDelegate* test_delegate = new TestDelegate; |
| 100 |
| 101 scoped_refptr<FileBasedPolicyLoader> loader( |
| 102 new FileBasedPolicyLoader(provider.AsWeakPtr(), |
| 103 test_delegate, |
| 104 kSettleIntervalSecondsForTesting, |
| 105 kReloadIntervalMinutesForTesting)); |
| 106 scoped_ptr<DictionaryValue> policy(loader->GetPolicy()); |
| 107 EXPECT_TRUE(policy.get()); |
| 108 EXPECT_EQ(0U, policy->size()); |
| 109 |
| 110 test_delegate->dict()->SetString("HomepageLocation", "http://www.google.com"); |
| 111 |
| 112 EXPECT_CALL(provider, NotifyStoreOfPolicyChange()).Times(1); |
| 113 loader->OnFilePathChanged(FilePath(FILE_PATH_LITERAL("fake"))); |
| 114 |
| 115 // Run the loop. The refresh should be handled immediately since the settle |
| 116 // interval has been disabled. |
| 117 loop_.RunAllPending(); |
| 118 Mock::VerifyAndClearExpectations(&provider); |
| 119 |
| 120 policy.reset(loader->GetPolicy()); |
| 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 } |
| 130 |
| 131 } // namespace policy |
| OLD | NEW |