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/memory/scoped_ptr.h"
      6 #include "components/policy/core/common/policy_provider_android.h"
      7 #include "components/policy/core/common/policy_provider_android_delegate.h"
      8 #include "testing/gmock/include/gmock/gmock.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace policy {
     12 
     13 namespace {
     14 
     15 // Helper to write a policy in |bundle| with less code.
     16 void SetPolicy(PolicyBundle* bundle,
     17                const std::string& name,
     18                const std::string& value) {
     19   bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
     20       .Set(name,
     21            POLICY_LEVEL_MANDATORY,
     22            POLICY_SCOPE_USER,
     23            new base::StringValue(value),
     24            NULL);
     25 }
     26 
     27 class MockPolicyProviderAndroidDelegate : public PolicyProviderAndroidDelegate {
     28  public:
     29   MockPolicyProviderAndroidDelegate() {}
     30   virtual ~MockPolicyProviderAndroidDelegate() {}
     31 
     32   MOCK_METHOD0(RefreshPolicies, void());
     33   MOCK_METHOD0(PolicyProviderShutdown, void());
     34 
     35  private:
     36   DISALLOW_COPY_AND_ASSIGN(MockPolicyProviderAndroidDelegate);
     37 };
     38 
     39 // Test fixture that makes sure that we always call Shutdown() before destroying
     40 // the policy provider. Allocate this just like a PolicyProviderAndroid and use
     41 // Get() to get the policy provider.
     42 class PolicyProviderAndroidTestFixture {
     43  public:
     44   PolicyProviderAndroidTestFixture() {}
     45   ~PolicyProviderAndroidTestFixture() {
     46     provider_.Shutdown();
     47   }
     48 
     49   PolicyProviderAndroid* Get() {
     50     return &provider_;
     51   }
     52 
     53  private:
     54   PolicyProviderAndroid provider_;
     55   DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTestFixture);
     56 };
     57 
     58 }  // namespace
     59 
     60 class PolicyProviderAndroidTest : public ::testing::Test {
     61  protected:
     62   PolicyProviderAndroidTest();
     63   virtual ~PolicyProviderAndroidTest();
     64 
     65   virtual void SetUp() OVERRIDE;
     66   virtual void TearDown() OVERRIDE;
     67 
     68  private:
     69   DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTest);
     70 };
     71 
     72 PolicyProviderAndroidTest::PolicyProviderAndroidTest() {}
     73 PolicyProviderAndroidTest::~PolicyProviderAndroidTest() {}
     74 
     75 void PolicyProviderAndroidTest::SetUp() {}
     76 
     77 void PolicyProviderAndroidTest::TearDown() {
     78   PolicyProviderAndroid::SetShouldWaitForPolicy(false);
     79 }
     80 
     81 TEST_F(PolicyProviderAndroidTest, InitializationCompleted) {
     82   PolicyProviderAndroidTestFixture provider;
     83   EXPECT_TRUE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME));
     84 
     85   const PolicyBundle kEmptyBundle;
     86   EXPECT_TRUE(provider.Get()->policies().Equals(kEmptyBundle));
     87 }
     88 
     89 TEST_F(PolicyProviderAndroidTest, WaitForInitialization) {
     90   PolicyProviderAndroid::SetShouldWaitForPolicy(true);
     91   PolicyProviderAndroidTestFixture provider;
     92   EXPECT_FALSE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME));
     93 
     94   scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle);
     95   SetPolicy(policy_bundle.get(), "key", "value");
     96   PolicyBundle expected_policy_bundle;
     97   expected_policy_bundle.CopyFrom(*policy_bundle);
     98   provider.Get()->SetPolicies(policy_bundle.Pass());
     99   EXPECT_TRUE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME));
    100   EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle));
    101 }
    102 
    103 TEST_F(PolicyProviderAndroidTest, RefreshPolicies) {
    104   MockPolicyProviderAndroidDelegate delegate;
    105   PolicyProviderAndroidTestFixture provider;
    106 
    107   provider.Get()->SetDelegate(&delegate);
    108 
    109   scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle);
    110   SetPolicy(policy_bundle.get(), "key", "old_value");
    111   PolicyBundle expected_policy_bundle;
    112   expected_policy_bundle.CopyFrom(*policy_bundle);
    113   provider.Get()->SetPolicies(policy_bundle.Pass());
    114   EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle));
    115 
    116   EXPECT_CALL(delegate, RefreshPolicies()).Times(1);
    117   provider.Get()->RefreshPolicies();
    118 
    119   policy_bundle.reset(new PolicyBundle);
    120   SetPolicy(policy_bundle.get(), "key", "new_value");
    121   expected_policy_bundle.CopyFrom(*policy_bundle);
    122   provider.Get()->SetPolicies(policy_bundle.Pass());
    123   EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle));
    124 
    125   EXPECT_CALL(delegate, PolicyProviderShutdown()).Times(1);
    126 }
    127 
    128 }  // namespace policy
    129