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 "components/login/screens/screen_context.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class ScreenContextTest
: public testing::Test
{
16 ScreenContextTest() {}
17 ~ScreenContextTest() override
{}
19 void SetUp() override
{ context_
.reset(new ScreenContext()); }
21 void TearDown() override
{}
24 ScreenContext
& context() { return *context_
.get(); }
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);
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);
45 rv
= context().SetBoolean("key0", false);
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);
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);
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());
76 rv
= changes
.GetInteger("key0", &value
);
80 rv
= context().SetInteger("key0", 3);
82 ASSERT_EQ(3, context().GetInteger("key0", 3));
83 ASSERT_TRUE(context().HasChanges());
85 rv
= context().SetInteger("key0", 2);
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
;
107 bool rv
= changes
.GetString("key0", &string_value
);
109 rv
= changes
.GetBoolean("key1", &bool_value
);
111 rv
= changes
.GetDouble("key2", &double_value
);
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
);
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"));