Home | History | Annotate | Download | only in common
      1 // Copyright 2014 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 "base/basictypes.h"
      6 #include "base/callback.h"
      7 #include "components/policy/core/common/external_data_fetcher.h"
      8 #include "components/policy/core/common/mock_configuration_policy_provider.h"
      9 #include "components/policy/core/common/proxy_policy_provider.h"
     10 #include "components/policy/core/common/schema_registry.h"
     11 #include "testing/gmock/include/gmock/gmock.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 using testing::Mock;
     15 
     16 namespace policy {
     17 
     18 class ProxyPolicyProviderTest : public testing::Test {
     19  protected:
     20   ProxyPolicyProviderTest() {
     21     mock_provider_.Init();
     22     proxy_provider_.Init(&schema_registry_);
     23     proxy_provider_.AddObserver(&observer_);
     24   }
     25 
     26   virtual ~ProxyPolicyProviderTest() {
     27     proxy_provider_.RemoveObserver(&observer_);
     28     proxy_provider_.Shutdown();
     29     mock_provider_.Shutdown();
     30   }
     31 
     32   SchemaRegistry schema_registry_;
     33   MockConfigurationPolicyObserver observer_;
     34   MockConfigurationPolicyProvider mock_provider_;
     35   ProxyPolicyProvider proxy_provider_;
     36 
     37   static scoped_ptr<PolicyBundle> CopyBundle(const PolicyBundle& bundle) {
     38     scoped_ptr<PolicyBundle> copy(new PolicyBundle());
     39     copy->CopyFrom(bundle);
     40     return copy.Pass();
     41   }
     42 
     43   DISALLOW_COPY_AND_ASSIGN(ProxyPolicyProviderTest);
     44 };
     45 
     46 TEST_F(ProxyPolicyProviderTest, Init) {
     47   EXPECT_TRUE(proxy_provider_.IsInitializationComplete(POLICY_DOMAIN_CHROME));
     48   EXPECT_TRUE(PolicyBundle().Equals(proxy_provider_.policies()));
     49 }
     50 
     51 TEST_F(ProxyPolicyProviderTest, Delegate) {
     52   PolicyBundle bundle;
     53   bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
     54       .Set("policy",
     55            POLICY_LEVEL_MANDATORY,
     56            POLICY_SCOPE_USER,
     57            new base::StringValue("value"),
     58            NULL);
     59   mock_provider_.UpdatePolicy(CopyBundle(bundle));
     60 
     61   EXPECT_CALL(observer_, OnUpdatePolicy(&proxy_provider_));
     62   proxy_provider_.SetDelegate(&mock_provider_);
     63   Mock::VerifyAndClearExpectations(&observer_);
     64   EXPECT_TRUE(bundle.Equals(proxy_provider_.policies()));
     65 
     66   EXPECT_CALL(observer_, OnUpdatePolicy(&proxy_provider_));
     67   bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
     68       .Set("policy",
     69            POLICY_LEVEL_MANDATORY,
     70            POLICY_SCOPE_USER,
     71            new base::StringValue("new value"),
     72            NULL);
     73   mock_provider_.UpdatePolicy(CopyBundle(bundle));
     74   Mock::VerifyAndClearExpectations(&observer_);
     75   EXPECT_TRUE(bundle.Equals(proxy_provider_.policies()));
     76 
     77   EXPECT_CALL(observer_, OnUpdatePolicy(&proxy_provider_));
     78   proxy_provider_.SetDelegate(NULL);
     79   EXPECT_TRUE(PolicyBundle().Equals(proxy_provider_.policies()));
     80 }
     81 
     82 TEST_F(ProxyPolicyProviderTest, RefreshPolicies) {
     83   EXPECT_CALL(observer_, OnUpdatePolicy(&proxy_provider_));
     84   proxy_provider_.RefreshPolicies();
     85   Mock::VerifyAndClearExpectations(&observer_);
     86 
     87   EXPECT_CALL(observer_, OnUpdatePolicy(&proxy_provider_));
     88   proxy_provider_.SetDelegate(&mock_provider_);
     89   Mock::VerifyAndClearExpectations(&observer_);
     90 
     91   EXPECT_CALL(observer_, OnUpdatePolicy(&proxy_provider_)).Times(0);
     92   EXPECT_CALL(mock_provider_, RefreshPolicies());
     93   proxy_provider_.RefreshPolicies();
     94   Mock::VerifyAndClearExpectations(&observer_);
     95   Mock::VerifyAndClearExpectations(&mock_provider_);
     96 
     97   EXPECT_CALL(observer_, OnUpdatePolicy(&proxy_provider_));
     98   mock_provider_.UpdatePolicy(scoped_ptr<PolicyBundle>(new PolicyBundle()));
     99   Mock::VerifyAndClearExpectations(&observer_);
    100 }
    101 
    102 }  // namespace policy
    103