Home | History | Annotate | Download | only in screens
      1 // Copyright (c) 2013 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 <algorithm>
      6 #include <vector>
      7 
      8 #include "base/memory/scoped_ptr.h"
      9 #include "chrome/browser/chromeos/login/screens/screen_context.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace chromeos {
     13 
     14 class ScreenContextTest : public testing::Test {
     15  public:
     16   ScreenContextTest() {}
     17   virtual ~ScreenContextTest() {}
     18 
     19   virtual void SetUp() {
     20     context_.reset(new ScreenContext());
     21   }
     22 
     23   virtual void TearDown() {
     24   }
     25 
     26  protected:
     27   ScreenContext& context() { return *context_.get(); }
     28 
     29  private:
     30   scoped_ptr<ScreenContext> context_;
     31 };
     32 
     33 TEST_F(ScreenContextTest, Simple) {
     34   ASSERT_FALSE(context().HasChanges());
     35 
     36   ASSERT_FALSE(context().HasKey("key0"));
     37 
     38   bool rv = context().SetBoolean("key0", true);
     39   ASSERT_TRUE(rv);
     40   ASSERT_TRUE(context().HasKey("key0"));
     41   ASSERT_TRUE(context().GetBoolean("key0"));
     42   ASSERT_TRUE(context().GetBoolean("key0", false));
     43   ASSERT_TRUE(context().HasChanges());
     44 
     45   rv = context().SetBoolean("key0", true);
     46   ASSERT_FALSE(rv);
     47 
     48   rv = context().SetBoolean("key0", false);
     49   ASSERT_TRUE(rv);
     50   ASSERT_TRUE(context().HasKey("key0"));
     51   ASSERT_FALSE(context().GetBoolean("key0"));
     52   ASSERT_FALSE(context().GetBoolean("key0", true));
     53   ASSERT_TRUE(context().HasChanges());
     54 
     55   ASSERT_FALSE(context().HasKey("key1"));
     56 
     57   ASSERT_EQ(1, context().GetInteger("key1", 1));
     58   rv = context().SetInteger("key1", 2);
     59   ASSERT_TRUE(rv);
     60   ASSERT_TRUE(context().HasKey("key1"));
     61   ASSERT_EQ(2, context().GetInteger("key1"));
     62   ASSERT_EQ(2, context().GetInteger("key1", 1));
     63 }
     64 
     65 TEST_F(ScreenContextTest, Changes) {
     66   ASSERT_FALSE(context().HasChanges());
     67 
     68   bool rv = context().SetInteger("key0", 2);
     69   ASSERT_TRUE(rv);
     70   ASSERT_EQ(2, context().GetInteger("key0"));
     71   ASSERT_TRUE(context().HasChanges());
     72 
     73   base::DictionaryValue changes;
     74   context().GetChangesAndReset(&changes);
     75   ASSERT_FALSE(context().HasChanges());
     76 
     77   ASSERT_EQ(1u, changes.size());
     78   int value;
     79   rv = changes.GetInteger("key0", &value);
     80   ASSERT_TRUE(rv);
     81   ASSERT_EQ(2, value);
     82 
     83   rv = context().SetInteger("key0", 3);
     84   ASSERT_TRUE(rv);
     85   ASSERT_EQ(3, context().GetInteger("key0", 3));
     86   ASSERT_TRUE(context().HasChanges());
     87 
     88   rv = context().SetInteger("key0", 2);
     89   ASSERT_TRUE(rv);
     90   ASSERT_TRUE(context().HasChanges());
     91 }
     92 
     93 TEST_F(ScreenContextTest, ComplexChanges) {
     94   ASSERT_FALSE(context().HasChanges());
     95 
     96   context().SetString("key0", "value0");
     97   context().SetBoolean("key1", true);
     98   context().SetDouble("key2", 3.14159);
     99   ASSERT_TRUE(context().HasChanges());
    100 
    101   // Get all changes and verify them.
    102   base::DictionaryValue changes;
    103   context().GetChangesAndReset(&changes);
    104   ASSERT_FALSE(context().HasChanges());
    105   ASSERT_EQ(3u, changes.size());
    106 
    107   std::string string_value;
    108   bool bool_value;
    109   double double_value;
    110   bool rv = changes.GetString("key0", &string_value);
    111   ASSERT_TRUE(rv);
    112   rv = changes.GetBoolean("key1", &bool_value);
    113   ASSERT_TRUE(rv);
    114   rv = changes.GetDouble("key2", &double_value);
    115   ASSERT_TRUE(rv);
    116   ASSERT_EQ("value0", string_value);
    117   ASSERT_EQ(true, bool_value);
    118   ASSERT_DOUBLE_EQ(3.14159, double_value);
    119 
    120   context().SetString("key0", "value1");
    121   ASSERT_TRUE(context().HasChanges());
    122 
    123   context().SetString("key0", "value0");
    124   ASSERT_TRUE(context().HasChanges());
    125 
    126   context().GetChangesAndReset(&changes);
    127   ASSERT_FALSE(context().HasChanges());
    128   ASSERT_EQ(1u, changes.size());
    129   rv = changes.GetString("key0", &string_value);
    130   ASSERT_TRUE(rv);
    131   ASSERT_EQ("value0", string_value);
    132 }
    133 
    134 TEST_F(ScreenContextTest, ApplyChanges) {
    135   ASSERT_FALSE(context().HasChanges());
    136 
    137   base::DictionaryValue changes;
    138   changes.SetString("key0", "value0");
    139   changes.SetInteger("key1", 1);
    140   changes.SetBoolean("key2", true);
    141 
    142   std::vector<std::string> keys;
    143   context().ApplyChanges(changes, &keys);
    144 
    145   ASSERT_EQ(3u, keys.size());
    146   std::sort(keys.begin(), keys.end());
    147   ASSERT_EQ("key0", keys[0]);
    148   ASSERT_EQ("key1", keys[1]);
    149   ASSERT_EQ("key2", keys[2]);
    150 
    151   ASSERT_FALSE(context().HasChanges());
    152   ASSERT_EQ("value0", context().GetString("key0"));
    153   ASSERT_EQ(1, context().GetInteger("key1"));
    154   ASSERT_TRUE(context().GetBoolean("key2"));
    155 }
    156 
    157 }  // namespace chromeos
    158