1// Copyright (c) 2011 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/browser/policy/mock_configuration_policy_store.h" 10#include "testing/gmock/include/gmock/gmock.h" 11 12using ::testing::_; 13using ::testing::InSequence; 14using ::testing::Return; 15 16namespace policy { 17 18class MockConfigurationPolicyObserver 19 : public ConfigurationPolicyProvider::Observer { 20 public: 21 MOCK_METHOD0(OnUpdatePolicy, void()); 22 void OnProviderGoingAway() {} 23}; 24 25class AsynchronousPolicyLoaderTest : public AsynchronousPolicyTestBase { 26 public: 27 AsynchronousPolicyLoaderTest() {} 28 virtual ~AsynchronousPolicyLoaderTest() {} 29 30 virtual void SetUp() { 31 AsynchronousPolicyTestBase::SetUp(); 32 mock_provider_.reset(new MockConfigurationPolicyProvider()); 33 } 34 35 protected: 36 scoped_ptr<MockConfigurationPolicyProvider> mock_provider_; 37 38 private: 39 DISALLOW_COPY_AND_ASSIGN(AsynchronousPolicyLoaderTest); 40}; 41 42ACTION(CreateTestDictionary) { 43 return new DictionaryValue(); 44} 45 46ACTION_P(CreateSequencedTestDictionary, number) { 47 DictionaryValue* test_dictionary = new DictionaryValue(); 48 test_dictionary->SetInteger("id", ++(*number)); 49 return test_dictionary; 50} 51 52ACTION(RescheduleImmediatePolicyReload) { 53 *arg1 = base::TimeDelta(); 54 return false; 55} 56 57TEST_F(AsynchronousPolicyLoaderTest, InitialLoad) { 58 DictionaryValue* template_dict(new DictionaryValue()); 59 EXPECT_CALL(*delegate_, Load()).WillOnce(Return(template_dict)); 60 scoped_refptr<AsynchronousPolicyLoader> loader = 61 new AsynchronousPolicyLoader(delegate_.release(), 10); 62 loader->Init(); 63 const DictionaryValue* loaded_dict(loader->policy()); 64 EXPECT_TRUE(loaded_dict->Equals(template_dict)); 65} 66 67// Verify that the fallback policy requests are made. 68TEST_F(AsynchronousPolicyLoaderTest, InitialLoadWithFallback) { 69 int dictionary_number = 0; 70 InSequence s; 71 EXPECT_CALL(*delegate_, Load()).WillOnce( 72 CreateSequencedTestDictionary(&dictionary_number)); 73 EXPECT_CALL(*delegate_, Load()).WillOnce( 74 CreateSequencedTestDictionary(&dictionary_number)); 75 scoped_refptr<AsynchronousPolicyLoader> loader = 76 new AsynchronousPolicyLoader(delegate_.release(), 10); 77 loader->Init(); 78 loop_.RunAllPending(); 79 loader->Reload(); 80 loop_.RunAllPending(); 81 82 const DictionaryValue* loaded_dict(loader->policy()); 83 int loaded_number; 84 EXPECT_TRUE(loaded_dict->GetInteger("id", &loaded_number)); 85 EXPECT_EQ(dictionary_number, loaded_number); 86} 87 88// Ensure that calling stop on the loader stops subsequent reloads from 89// happening. 90TEST_F(AsynchronousPolicyLoaderTest, Stop) { 91 ON_CALL(*delegate_, Load()).WillByDefault(CreateTestDictionary()); 92 EXPECT_CALL(*delegate_, Load()).Times(1); 93 scoped_refptr<AsynchronousPolicyLoader> loader = 94 new AsynchronousPolicyLoader(delegate_.release(), 10); 95 loader->Init(); 96 loop_.RunAllPending(); 97 loader->Stop(); 98 loop_.RunAllPending(); 99 loader->Reload(); 100 loop_.RunAllPending(); 101} 102 103// Verifies that the provider is notified upon policy reload, but only 104// if the policy changed. 105TEST_F(AsynchronousPolicyLoaderTest, ProviderNotificationOnPolicyChange) { 106 InSequence s; 107 MockConfigurationPolicyObserver observer; 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, OnUpdatePolicy()).Times(0); 115 EXPECT_CALL(*delegate_, Load()).WillOnce( 116 CreateSequencedTestDictionary(&dictionary_number_2)); 117 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); 118 EXPECT_CALL(*delegate_, Load()).WillOnce( 119 CreateSequencedTestDictionary(&dictionary_number_1)); 120 scoped_refptr<AsynchronousPolicyLoader> loader = 121 new AsynchronousPolicyLoader(delegate_.release(), 10); 122 AsynchronousPolicyProvider provider(NULL, loader); 123 // |registrar| must be declared last so that it is destroyed first. 124 ConfigurationPolicyObserverRegistrar registrar; 125 registrar.Init(&provider, &observer); 126 loop_.RunAllPending(); 127 loader->Reload(); 128 loop_.RunAllPending(); 129 loader->Reload(); 130 loop_.RunAllPending(); 131 loader->Reload(); 132 loop_.RunAllPending(); 133} 134 135} // namespace policy 136