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.
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"
14 class ScreenContextTest
: public testing::Test
{
16 ScreenContextTest() {}
17 virtual ~ScreenContextTest() {}
19 virtual void SetUp() {
20 context_
.reset(new ScreenContext());
23 virtual void TearDown() {
27 ScreenContext
& context() { return *context_
.get(); }
30 scoped_ptr
<ScreenContext
> context_
;
33 TEST_F(ScreenContextTest
, Simple
) {
34 ASSERT_FALSE(context().HasChanges());
36 ASSERT_FALSE(context().HasKey("key0"));
38 bool rv
= context().SetBoolean("key0", true);
40 ASSERT_TRUE(context().HasKey("key0"));
41 ASSERT_TRUE(context().GetBoolean("key0"));
42 ASSERT_TRUE(context().GetBoolean("key0", false));
43 ASSERT_TRUE(context().HasChanges());
45 rv
= context().SetBoolean("key0", true);
48 rv
= context().SetBoolean("key0", false);
50 ASSERT_TRUE(context().HasKey("key0"));
51 ASSERT_FALSE(context().GetBoolean("key0"));
52 ASSERT_FALSE(context().GetBoolean("key0", true));
53 ASSERT_TRUE(context().HasChanges());
55 ASSERT_FALSE(context().HasKey("key1"));
57 ASSERT_EQ(1, context().GetInteger("key1", 1));
58 rv
= context().SetInteger("key1", 2);
60 ASSERT_TRUE(context().HasKey("key1"));
61 ASSERT_EQ(2, context().GetInteger("key1"));
62 ASSERT_EQ(2, context().GetInteger("key1", 1));
65 TEST_F(ScreenContextTest
, Changes
) {
66 ASSERT_FALSE(context().HasChanges());
68 bool rv
= context().SetInteger("key0", 2);
70 ASSERT_EQ(2, context().GetInteger("key0"));
71 ASSERT_TRUE(context().HasChanges());
73 base::DictionaryValue changes
;
74 context().GetChangesAndReset(&changes
);
75 ASSERT_FALSE(context().HasChanges());
77 ASSERT_EQ(1u, changes
.size());
79 rv
= changes
.GetInteger("key0", &value
);
83 rv
= context().SetInteger("key0", 3);
85 ASSERT_EQ(3, context().GetInteger("key0", 3));
86 ASSERT_TRUE(context().HasChanges());
88 rv
= context().SetInteger("key0", 2);
90 ASSERT_TRUE(context().HasChanges());
93 TEST_F(ScreenContextTest
, ComplexChanges
) {
94 ASSERT_FALSE(context().HasChanges());
96 context().SetString("key0", "value0");
97 context().SetBoolean("key1", true);
98 context().SetDouble("key2", 3.14159);
99 ASSERT_TRUE(context().HasChanges());
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());
107 std::string string_value
;
110 bool rv
= changes
.GetString("key0", &string_value
);
112 rv
= changes
.GetBoolean("key1", &bool_value
);
114 rv
= changes
.GetDouble("key2", &double_value
);
116 ASSERT_EQ("value0", string_value
);
117 ASSERT_EQ(true, bool_value
);
118 ASSERT_DOUBLE_EQ(3.14159, double_value
);
120 context().SetString("key0", "value1");
121 ASSERT_TRUE(context().HasChanges());
123 context().SetString("key0", "value0");
124 ASSERT_TRUE(context().HasChanges());
126 context().GetChangesAndReset(&changes
);
127 ASSERT_FALSE(context().HasChanges());
128 ASSERT_EQ(1u, changes
.size());
129 rv
= changes
.GetString("key0", &string_value
);
131 ASSERT_EQ("value0", string_value
);
134 TEST_F(ScreenContextTest
, ApplyChanges
) {
135 ASSERT_FALSE(context().HasChanges());
137 base::DictionaryValue changes
;
138 changes
.SetString("key0", "value0");
139 changes
.SetInteger("key1", 1);
140 changes
.SetBoolean("key2", true);
142 std::vector
<std::string
> keys
;
143 context().ApplyChanges(changes
, &keys
);
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]);
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"));
157 } // namespace chromeos