Home | History | Annotate | Download | only in policy
      1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PROVIDER_TEST_H_
      6 #define CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PROVIDER_TEST_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback_forward.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/message_loop/message_loop.h"
     14 #include "chrome/browser/policy/policy_types.h"
     15 #include "content/public/test/test_browser_thread.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 namespace base {
     19 class DictionaryValue;
     20 class ListValue;
     21 class Value;
     22 }
     23 
     24 namespace policy {
     25 
     26 class ConfigurationPolicyProvider;
     27 struct PolicyDefinitionList;
     28 
     29 // A stripped-down policy definition list that contains entries for the
     30 // different policy setting types supported.
     31 namespace test_policy_definitions {
     32 
     33 // String policy keys.
     34 extern const char kKeyString[];
     35 extern const char kKeyBoolean[];
     36 extern const char kKeyInteger[];
     37 extern const char kKeyStringList[];
     38 extern const char kKeyDictionary[];
     39 
     40 // Policy definition list that contains entries for the keys above.
     41 extern const PolicyDefinitionList kList;
     42 
     43 }  // namespace test_policy_definitions
     44 
     45 class PolicyTestBase : public testing::Test {
     46  public:
     47   PolicyTestBase();
     48   virtual ~PolicyTestBase();
     49 
     50   // testing::Test:
     51   virtual void TearDown() OVERRIDE;
     52 
     53  protected:
     54   // Create an actual IO loop (needed by FilePathWatcher).
     55   base::MessageLoopForIO loop_;
     56 
     57  private:
     58   content::TestBrowserThread ui_thread_;
     59   content::TestBrowserThread file_thread_;
     60 
     61   DISALLOW_COPY_AND_ASSIGN(PolicyTestBase);
     62 };
     63 
     64 // An interface for creating a test policy provider and creating a policy
     65 // provider instance for testing. Used as the parameter to the abstract
     66 // ConfigurationPolicyProviderTest below.
     67 class PolicyProviderTestHarness {
     68  public:
     69   // |level| and |scope| are the level and scope of the policies returned by
     70   // the providers from CreateProvider().
     71   PolicyProviderTestHarness(PolicyLevel level, PolicyScope scope);
     72   virtual ~PolicyProviderTestHarness();
     73 
     74   // Actions to run at gtest SetUp() time.
     75   virtual void SetUp() = 0;
     76 
     77   // Create a new policy provider.
     78   virtual ConfigurationPolicyProvider* CreateProvider(
     79       const PolicyDefinitionList* policy_definition_list) = 0;
     80 
     81   // Returns the policy level and scope set by the policy provider.
     82   PolicyLevel policy_level() const;
     83   PolicyScope policy_scope() const;
     84 
     85   // Helpers to configure the environment the policy provider reads from.
     86   virtual void InstallEmptyPolicy() = 0;
     87   virtual void InstallStringPolicy(const std::string& policy_name,
     88                                    const std::string& policy_value) = 0;
     89   virtual void InstallIntegerPolicy(const std::string& policy_name,
     90                                     int policy_value) = 0;
     91   virtual void InstallBooleanPolicy(const std::string& policy_name,
     92                                     bool policy_value) = 0;
     93   virtual void InstallStringListPolicy(const std::string& policy_name,
     94                                        const base::ListValue* policy_value) = 0;
     95   virtual void InstallDictionaryPolicy(
     96       const std::string& policy_name,
     97       const base::DictionaryValue* policy_value) = 0;
     98 
     99   // Not every provider supports installing 3rd party policy. Those who do
    100   // should override this method; the default just makes the test fail.
    101   virtual void Install3rdPartyPolicy(const base::DictionaryValue* policies);
    102 
    103  private:
    104   PolicyLevel level_;
    105   PolicyScope scope_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(PolicyProviderTestHarness);
    108 };
    109 
    110 // A factory method for creating a test harness.
    111 typedef PolicyProviderTestHarness* (*CreatePolicyProviderTestHarness)();
    112 
    113 // Abstract policy provider test. This is meant to be instantiated for each
    114 // policy provider implementation, passing in a suitable harness factory
    115 // function as the test parameter.
    116 class ConfigurationPolicyProviderTest
    117     : public PolicyTestBase,
    118       public testing::WithParamInterface<CreatePolicyProviderTestHarness> {
    119  protected:
    120   ConfigurationPolicyProviderTest();
    121   virtual ~ConfigurationPolicyProviderTest();
    122 
    123   virtual void SetUp() OVERRIDE;
    124   virtual void TearDown() OVERRIDE;
    125 
    126   // Installs a valid policy and checks whether the provider returns the
    127   // |expected_value|.
    128   void CheckValue(const char* policy_name,
    129                   const base::Value& expected_value,
    130                   base::Closure install_value);
    131 
    132   scoped_ptr<PolicyProviderTestHarness> test_harness_;
    133   scoped_ptr<ConfigurationPolicyProvider> provider_;
    134 
    135  private:
    136   DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyProviderTest);
    137 };
    138 
    139 // An extension of ConfigurationPolicyProviderTest that also tests loading of
    140 // 3rd party policy. Policy provider implementations that support loading of
    141 // 3rd party policy should also instantiate these tests.
    142 class Configuration3rdPartyPolicyProviderTest
    143     : public ConfigurationPolicyProviderTest {
    144  protected:
    145   Configuration3rdPartyPolicyProviderTest();
    146   virtual ~Configuration3rdPartyPolicyProviderTest();
    147 
    148  private:
    149   DISALLOW_COPY_AND_ASSIGN(Configuration3rdPartyPolicyProviderTest);
    150 };
    151 
    152 }  // namespace policy
    153 
    154 #endif  // CHROME_BROWSER_POLICY_CONFIGURATION_POLICY_PROVIDER_TEST_H_
    155