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 #include "base/callback.h"
      6 #include "base/memory/scoped_ptr.h"
      7 #include "base/prefs/pref_value_map.h"
      8 #include "components/policy/core/browser/configuration_policy_handler.h"
      9 #include "components/policy/core/browser/policy_error_map.h"
     10 #include "components/policy/core/common/policy_map.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 // Note: this file should move to components/policy/core/browser, but the
     14 // components_unittests runner does not load the ResourceBundle as
     15 // ChromeTestSuite::Initialize does, which leads to failures using
     16 // PolicyErrorMap.
     17 
     18 namespace policy {
     19 
     20 namespace {
     21 
     22 StringToIntEnumListPolicyHandler::MappingEntry kTestTypeMap[] = {
     23   { "one", 1 },
     24   { "two", 2 },
     25 };
     26 
     27 const char kTestPolicy[] = "unit_test.test_policy";
     28 const char kTestPref[] = "unit_test.test_pref";
     29 
     30 }  // namespace
     31 
     32 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) {
     33   base::ListValue list;
     34   PolicyMap policy_map;
     35   PolicyErrorMap errors;
     36   StringToIntEnumListPolicyHandler handler(
     37       kTestPolicy,
     38       kTestPref,
     39       kTestTypeMap,
     40       kTestTypeMap + arraysize(kTestTypeMap));
     41 
     42   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
     43                  POLICY_SCOPE_USER, list.DeepCopy(), NULL);
     44   errors.Clear();
     45   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
     46   EXPECT_TRUE(errors.empty());
     47 
     48   list.AppendString("one");
     49   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
     50                  POLICY_SCOPE_USER, list.DeepCopy(), NULL);
     51   errors.Clear();
     52   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
     53   EXPECT_TRUE(errors.empty());
     54 
     55   list.AppendString("invalid");
     56   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
     57                  POLICY_SCOPE_USER, list.DeepCopy(), NULL);
     58   errors.Clear();
     59   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
     60   EXPECT_FALSE(errors.empty());
     61   EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
     62 
     63   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
     64                  POLICY_SCOPE_USER,
     65                  base::Value::CreateStringValue("no list"), NULL);
     66   errors.Clear();
     67   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
     68   EXPECT_FALSE(errors.empty());
     69   EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
     70 }
     71 
     72 TEST(StringToIntEnumListPolicyHandlerTest, ApplyPolicySettings) {
     73   base::ListValue list;
     74   base::ListValue expected;
     75   PolicyMap policy_map;
     76   PrefValueMap prefs;
     77   base::Value* value;
     78   StringToIntEnumListPolicyHandler handler(
     79       kTestPolicy,
     80       kTestPref,
     81       kTestTypeMap,
     82       kTestTypeMap + arraysize(kTestTypeMap));
     83 
     84   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
     85                  POLICY_SCOPE_USER, list.DeepCopy(), NULL);
     86   handler.ApplyPolicySettings(policy_map, &prefs);
     87   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
     88   EXPECT_TRUE(base::Value::Equals(&expected, value));
     89 
     90   list.AppendString("two");
     91   expected.AppendInteger(2);
     92   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
     93                  POLICY_SCOPE_USER, list.DeepCopy(), NULL);
     94   handler.ApplyPolicySettings(policy_map, &prefs);
     95   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
     96   EXPECT_TRUE(base::Value::Equals(&expected, value));
     97 
     98   list.AppendString("invalid");
     99   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    100                  POLICY_SCOPE_USER, list.DeepCopy(), NULL);
    101   handler.ApplyPolicySettings(policy_map, &prefs);
    102   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    103   EXPECT_TRUE(base::Value::Equals(&expected, value));
    104 }
    105 
    106 TEST(IntRangePolicyHandler, CheckPolicySettingsClamp) {
    107   PolicyMap policy_map;
    108   PolicyErrorMap errors;
    109 
    110   // This tests needs to modify an int policy. The exact policy used and its
    111   // semantics outside the test are irrelevant.
    112   IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
    113 
    114   // Check that values lying in the accepted range are not rejected.
    115   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    116                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    117   errors.Clear();
    118   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    119   EXPECT_TRUE(errors.empty());
    120 
    121   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    122                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    123   errors.Clear();
    124   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    125   EXPECT_TRUE(errors.empty());
    126 
    127   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    128                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    129   errors.Clear();
    130   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    131   EXPECT_TRUE(errors.empty());
    132 
    133   // Check that values lying outside the accepted range are not rejected
    134   // (because clamping is enabled) but do yield a warning message.
    135   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    136                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
    137   errors.Clear();
    138   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    139   EXPECT_FALSE(errors.empty());
    140 
    141   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    142                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
    143   errors.Clear();
    144   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    145   EXPECT_FALSE(errors.empty());
    146 
    147   // Check that an entirely invalid value is rejected and yields an error
    148   // message.
    149   policy_map.Set(kTestPolicy,
    150                  POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
    151                  base::Value::CreateStringValue("invalid"), NULL);
    152   errors.Clear();
    153   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    154   EXPECT_FALSE(errors.empty());
    155 }
    156 
    157 TEST(IntRangePolicyHandler, CheckPolicySettingsDontClamp) {
    158   PolicyMap policy_map;
    159   PolicyErrorMap errors;
    160 
    161   // This tests needs to modify an int policy. The exact policy used and its
    162   // semantics outside the test are irrelevant.
    163   IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, false);
    164 
    165   // Check that values lying in the accepted range are not rejected.
    166   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    167                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    168   errors.Clear();
    169   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    170   EXPECT_TRUE(errors.empty());
    171 
    172   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    173                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    174   errors.Clear();
    175   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    176   EXPECT_TRUE(errors.empty());
    177 
    178   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    179                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    180   errors.Clear();
    181   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    182   EXPECT_TRUE(errors.empty());
    183 
    184   // Check that values lying outside the accepted range are rejected and yield
    185   // an error message.
    186   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    187                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
    188   errors.Clear();
    189   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    190   EXPECT_FALSE(errors.empty());
    191 
    192   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    193                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
    194   errors.Clear();
    195   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    196   EXPECT_FALSE(errors.empty());
    197 
    198   // Check that an entirely invalid value is rejected and yields an error
    199   // message.
    200   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
    201                  base::Value::CreateStringValue("invalid"), NULL);
    202   errors.Clear();
    203   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    204   EXPECT_FALSE(errors.empty());
    205 }
    206 
    207 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) {
    208   PolicyMap policy_map;
    209   PrefValueMap prefs;
    210   scoped_ptr<base::Value> expected;
    211   const base::Value* value;
    212 
    213   // This tests needs to modify an int policy. The exact policy used and its
    214   // semantics outside the test are irrelevant.
    215   IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
    216 
    217   // Check that values lying in the accepted range are written to the pref.
    218   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    219                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    220   prefs.Clear();
    221   handler.ApplyPolicySettings(policy_map, &prefs);
    222   expected.reset(base::Value::CreateIntegerValue(0));
    223   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    224   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    225 
    226   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    227                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    228   prefs.Clear();
    229   handler.ApplyPolicySettings(policy_map, &prefs);
    230   expected.reset(base::Value::CreateIntegerValue(5));
    231   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    232   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    233 
    234   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    235                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    236   prefs.Clear();
    237   handler.ApplyPolicySettings(policy_map, &prefs);
    238   expected.reset(base::Value::CreateIntegerValue(10));
    239   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    240   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    241 
    242   // Check that values lying outside the accepted range are clamped and written
    243   // to the pref.
    244   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    245                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
    246   prefs.Clear();
    247   handler.ApplyPolicySettings(policy_map, &prefs);
    248   expected.reset(base::Value::CreateIntegerValue(0));
    249   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    250   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    251 
    252   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    253                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
    254   prefs.Clear();
    255   handler.ApplyPolicySettings(policy_map, &prefs);
    256   expected.reset(base::Value::CreateIntegerValue(10));
    257   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    258   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    259 }
    260 
    261 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) {
    262   PolicyMap policy_map;
    263   PrefValueMap prefs;
    264   scoped_ptr<base::Value> expected;
    265   const base::Value* value;
    266 
    267   // This tests needs to modify an int policy. The exact policy used and its
    268   // semantics outside the test are irrelevant.
    269   IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
    270 
    271   // Check that values lying in the accepted range are written to the pref.
    272   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    273                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    274   prefs.Clear();
    275   handler.ApplyPolicySettings(policy_map, &prefs);
    276   expected.reset(base::Value::CreateIntegerValue(0));
    277   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    278   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    279 
    280   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    281                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    282   prefs.Clear();
    283   handler.ApplyPolicySettings(policy_map, &prefs);
    284   expected.reset(base::Value::CreateIntegerValue(5));
    285   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    286   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    287 
    288   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    289                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    290   prefs.Clear();
    291   handler.ApplyPolicySettings(policy_map, &prefs);
    292   expected.reset(base::Value::CreateIntegerValue(10));
    293   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    294   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    295 }
    296 
    297 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsClamp) {
    298   PolicyMap policy_map;
    299   PolicyErrorMap errors;
    300 
    301   // This tests needs to modify an int policy. The exact policy used and its
    302   // semantics outside the test are irrelevant.
    303   IntPercentageToDoublePolicyHandler handler(
    304       kTestPolicy, kTestPref, 0, 10, true);
    305 
    306   // Check that values lying in the accepted range are not rejected.
    307   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    308                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    309   errors.Clear();
    310   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    311   EXPECT_TRUE(errors.empty());
    312 
    313   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    314                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    315   errors.Clear();
    316   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    317   EXPECT_TRUE(errors.empty());
    318 
    319   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    320                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    321   errors.Clear();
    322   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    323   EXPECT_TRUE(errors.empty());
    324 
    325   // Check that values lying outside the accepted range are not rejected
    326   // (because clamping is enabled) but do yield a warning message.
    327   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    328                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
    329   errors.Clear();
    330   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    331   EXPECT_FALSE(errors.empty());
    332 
    333   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    334                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
    335   errors.Clear();
    336   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    337   EXPECT_FALSE(errors.empty());
    338 
    339   // Check that an entirely invalid value is rejected and yields an error
    340   // message.
    341   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
    342                  base::Value::CreateStringValue("invalid"), NULL);
    343   errors.Clear();
    344   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    345   EXPECT_FALSE(errors.empty());
    346 }
    347 
    348 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsDontClamp) {
    349   PolicyMap policy_map;
    350   PolicyErrorMap errors;
    351 
    352   // This tests needs to modify an int policy. The exact policy used and its
    353   // semantics outside the test are irrelevant.
    354   IntPercentageToDoublePolicyHandler handler(
    355       kTestPolicy, kTestPref, 0, 10, false);
    356 
    357   // Check that values lying in the accepted range are not rejected.
    358   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    359                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    360   errors.Clear();
    361   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    362   EXPECT_TRUE(errors.empty());
    363 
    364   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    365                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    366   errors.Clear();
    367   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    368   EXPECT_TRUE(errors.empty());
    369 
    370   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    371                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    372   errors.Clear();
    373   EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
    374   EXPECT_TRUE(errors.empty());
    375 
    376   // Check that values lying outside the accepted range are rejected and yield
    377   // an error message.
    378   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    379                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
    380   errors.Clear();
    381   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    382   EXPECT_FALSE(errors.empty());
    383 
    384   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    385                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
    386   errors.Clear();
    387   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    388   EXPECT_FALSE(errors.empty());
    389 
    390   // Check that an entirely invalid value is rejected and yields an error
    391   // message.
    392   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
    393                  base::Value::CreateStringValue("invalid"), NULL);
    394   errors.Clear();
    395   EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
    396   EXPECT_FALSE(errors.empty());
    397 }
    398 
    399 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) {
    400   PolicyMap policy_map;
    401   PrefValueMap prefs;
    402   scoped_ptr<base::Value> expected;
    403   const base::Value* value;
    404 
    405   // This tests needs to modify an int policy. The exact policy used and its
    406   // semantics outside the test are irrelevant.
    407   IntPercentageToDoublePolicyHandler handler(
    408       kTestPolicy, kTestPref, 0, 10, true);
    409 
    410   // Check that values lying in the accepted range are written to the pref.
    411   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    412                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    413   prefs.Clear();
    414   handler.ApplyPolicySettings(policy_map, &prefs);
    415   expected.reset(base::Value::CreateDoubleValue(0.0));
    416   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    417   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    418 
    419   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    420                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    421   prefs.Clear();
    422   handler.ApplyPolicySettings(policy_map, &prefs);
    423   expected.reset(base::Value::CreateDoubleValue(0.05));
    424   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    425   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    426 
    427   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    428                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    429   prefs.Clear();
    430   handler.ApplyPolicySettings(policy_map, &prefs);
    431   expected.reset(base::Value::CreateDoubleValue(0.1));
    432   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    433   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    434 
    435   // Check that values lying outside the accepted range are clamped and written
    436   // to the pref.
    437   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    438                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
    439   prefs.Clear();
    440   handler.ApplyPolicySettings(policy_map, &prefs);
    441   expected.reset(base::Value::CreateDoubleValue(0.0));
    442   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    443   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    444 
    445   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    446                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
    447   prefs.Clear();
    448   handler.ApplyPolicySettings(policy_map, &prefs);
    449   expected.reset(base::Value::CreateDoubleValue(0.1));
    450   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    451   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    452 }
    453 
    454 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) {
    455   PolicyMap policy_map;
    456   PrefValueMap prefs;
    457   scoped_ptr<base::Value> expected;
    458   const base::Value* value;
    459 
    460   // This tests needs to modify an int policy. The exact policy used and its
    461   // semantics outside the test are irrelevant.
    462   IntPercentageToDoublePolicyHandler handler(
    463       kTestPolicy, kTestPref, 0, 10, true);
    464 
    465   // Check that values lying in the accepted range are written to the pref.
    466   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    467                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
    468   prefs.Clear();
    469   handler.ApplyPolicySettings(policy_map, &prefs);
    470   expected.reset(base::Value::CreateDoubleValue(0.0));
    471   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    472   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    473 
    474   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    475                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
    476   prefs.Clear();
    477   handler.ApplyPolicySettings(policy_map, &prefs);
    478   expected.reset(base::Value::CreateDoubleValue(0.05));
    479   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    480   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    481 
    482   policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
    483                  POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
    484   prefs.Clear();
    485   handler.ApplyPolicySettings(policy_map, &prefs);
    486   expected.reset(base::Value::CreateDoubleValue(0.1));
    487   EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
    488   EXPECT_TRUE(base::Value::Equals(expected.get(), value));
    489 }
    490 
    491 }  // namespace policy
    492