Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / login / screens / screen_context_unittest.cc
blobc6a75b164d401030cb55a34303756fbd0776877f
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.
5 #include <algorithm>
6 #include <vector>
8 #include "base/memory/scoped_ptr.h"
9 #include "components/login/screens/screen_context.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace login {
14 class ScreenContextTest : public testing::Test {
15 public:
16 ScreenContextTest() {}
17 ~ScreenContextTest() override {}
19 void SetUp() override { context_.reset(new ScreenContext()); }
21 void TearDown() override {}
23 protected:
24 ScreenContext& context() { return *context_.get(); }
26 private:
27 scoped_ptr<ScreenContext> context_;
30 TEST_F(ScreenContextTest, Simple) {
31 ASSERT_FALSE(context().HasChanges());
33 ASSERT_FALSE(context().HasKey("key0"));
35 bool rv = context().SetBoolean("key0", true);
36 ASSERT_TRUE(rv);
37 ASSERT_TRUE(context().HasKey("key0"));
38 ASSERT_TRUE(context().GetBoolean("key0"));
39 ASSERT_TRUE(context().GetBoolean("key0", false));
40 ASSERT_TRUE(context().HasChanges());
42 rv = context().SetBoolean("key0", true);
43 ASSERT_FALSE(rv);
45 rv = context().SetBoolean("key0", false);
46 ASSERT_TRUE(rv);
47 ASSERT_TRUE(context().HasKey("key0"));
48 ASSERT_FALSE(context().GetBoolean("key0"));
49 ASSERT_FALSE(context().GetBoolean("key0", true));
50 ASSERT_TRUE(context().HasChanges());
52 ASSERT_FALSE(context().HasKey("key1"));
54 ASSERT_EQ(1, context().GetInteger("key1", 1));
55 rv = context().SetInteger("key1", 2);
56 ASSERT_TRUE(rv);
57 ASSERT_TRUE(context().HasKey("key1"));
58 ASSERT_EQ(2, context().GetInteger("key1"));
59 ASSERT_EQ(2, context().GetInteger("key1", 1));
62 TEST_F(ScreenContextTest, Changes) {
63 ASSERT_FALSE(context().HasChanges());
65 bool rv = context().SetInteger("key0", 2);
66 ASSERT_TRUE(rv);
67 ASSERT_EQ(2, context().GetInteger("key0"));
68 ASSERT_TRUE(context().HasChanges());
70 base::DictionaryValue changes;
71 context().GetChangesAndReset(&changes);
72 ASSERT_FALSE(context().HasChanges());
74 ASSERT_EQ(1u, changes.size());
75 int value;
76 rv = changes.GetInteger("key0", &value);
77 ASSERT_TRUE(rv);
78 ASSERT_EQ(2, value);
80 rv = context().SetInteger("key0", 3);
81 ASSERT_TRUE(rv);
82 ASSERT_EQ(3, context().GetInteger("key0", 3));
83 ASSERT_TRUE(context().HasChanges());
85 rv = context().SetInteger("key0", 2);
86 ASSERT_TRUE(rv);
87 ASSERT_TRUE(context().HasChanges());
90 TEST_F(ScreenContextTest, ComplexChanges) {
91 ASSERT_FALSE(context().HasChanges());
93 context().SetString("key0", "value0");
94 context().SetBoolean("key1", true);
95 context().SetDouble("key2", 3.14159);
96 ASSERT_TRUE(context().HasChanges());
98 // Get all changes and verify them.
99 base::DictionaryValue changes;
100 context().GetChangesAndReset(&changes);
101 ASSERT_FALSE(context().HasChanges());
102 ASSERT_EQ(3u, changes.size());
104 std::string string_value;
105 bool bool_value;
106 double double_value;
107 bool rv = changes.GetString("key0", &string_value);
108 ASSERT_TRUE(rv);
109 rv = changes.GetBoolean("key1", &bool_value);
110 ASSERT_TRUE(rv);
111 rv = changes.GetDouble("key2", &double_value);
112 ASSERT_TRUE(rv);
113 ASSERT_EQ("value0", string_value);
114 ASSERT_EQ(true, bool_value);
115 ASSERT_DOUBLE_EQ(3.14159, double_value);
117 context().SetString("key0", "value1");
118 ASSERT_TRUE(context().HasChanges());
120 context().SetString("key0", "value0");
121 ASSERT_TRUE(context().HasChanges());
123 context().GetChangesAndReset(&changes);
124 ASSERT_FALSE(context().HasChanges());
125 ASSERT_EQ(1u, changes.size());
126 rv = changes.GetString("key0", &string_value);
127 ASSERT_TRUE(rv);
128 ASSERT_EQ("value0", string_value);
131 TEST_F(ScreenContextTest, ApplyChanges) {
132 ASSERT_FALSE(context().HasChanges());
134 base::DictionaryValue changes;
135 changes.SetString("key0", "value0");
136 changes.SetInteger("key1", 1);
137 changes.SetBoolean("key2", true);
139 std::vector<std::string> keys;
140 context().ApplyChanges(changes, &keys);
142 ASSERT_EQ(3u, keys.size());
143 std::sort(keys.begin(), keys.end());
144 ASSERT_EQ("key0", keys[0]);
145 ASSERT_EQ("key1", keys[1]);
146 ASSERT_EQ("key2", keys[2]);
148 ASSERT_FALSE(context().HasChanges());
149 ASSERT_EQ("value0", context().GetString("key0"));
150 ASSERT_EQ(1, context().GetInteger("key1"));
151 ASSERT_TRUE(context().GetBoolean("key2"));
154 } // namespace login