OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
9 #include "base/prefs/json_pref_store.h" | 9 #include "base/prefs/json_pref_store.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, | 89 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, |
90 const FilePath& output_file, | 90 const FilePath& output_file, |
91 const FilePath& golden_output_file) { | 91 const FilePath& golden_output_file) { |
92 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; | 92 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
93 const char kMaxTabs[] = "tabs.max_tabs"; | 93 const char kMaxTabs[] = "tabs.max_tabs"; |
94 const char kLongIntPref[] = "long_int.pref"; | 94 const char kLongIntPref[] = "long_int.pref"; |
95 | 95 |
96 std::string cnn("http://www.cnn.com"); | 96 std::string cnn("http://www.cnn.com"); |
97 | 97 |
98 const Value* actual; | 98 const Value* actual; |
99 EXPECT_EQ(PrefStore::READ_OK, | 99 EXPECT_TRUE(pref_store->GetValue(prefs::kHomePage, &actual)); |
100 pref_store->GetValue(prefs::kHomePage, &actual)); | |
101 std::string string_value; | 100 std::string string_value; |
102 EXPECT_TRUE(actual->GetAsString(&string_value)); | 101 EXPECT_TRUE(actual->GetAsString(&string_value)); |
103 EXPECT_EQ(cnn, string_value); | 102 EXPECT_EQ(cnn, string_value); |
104 | 103 |
105 const char kSomeDirectory[] = "some_directory"; | 104 const char kSomeDirectory[] = "some_directory"; |
106 | 105 |
107 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); | 106 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
108 FilePath::StringType path; | 107 FilePath::StringType path; |
109 EXPECT_TRUE(actual->GetAsString(&path)); | 108 EXPECT_TRUE(actual->GetAsString(&path)); |
110 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); | 109 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
111 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); | 110 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
112 | 111 |
113 pref_store->SetValue(kSomeDirectory, | 112 pref_store->SetValue(kSomeDirectory, |
114 Value::CreateStringValue(some_path.value())); | 113 Value::CreateStringValue(some_path.value())); |
115 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); | 114 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
116 EXPECT_TRUE(actual->GetAsString(&path)); | 115 EXPECT_TRUE(actual->GetAsString(&path)); |
117 EXPECT_EQ(some_path.value(), path); | 116 EXPECT_EQ(some_path.value(), path); |
118 | 117 |
119 // Test reading some other data types from sub-dictionaries. | 118 // Test reading some other data types from sub-dictionaries. |
120 EXPECT_EQ(PrefStore::READ_OK, | 119 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
121 pref_store->GetValue(kNewWindowsInTabs, &actual)); | |
122 bool boolean = false; | 120 bool boolean = false; |
123 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 121 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
124 EXPECT_TRUE(boolean); | 122 EXPECT_TRUE(boolean); |
125 | 123 |
126 pref_store->SetValue(kNewWindowsInTabs, | 124 pref_store->SetValue(kNewWindowsInTabs, |
127 Value::CreateBooleanValue(false)); | 125 Value::CreateBooleanValue(false)); |
128 EXPECT_EQ(PrefStore::READ_OK, | 126 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
129 pref_store->GetValue(kNewWindowsInTabs, &actual)); | |
130 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 127 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
131 EXPECT_FALSE(boolean); | 128 EXPECT_FALSE(boolean); |
132 | 129 |
133 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); | 130 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
134 int integer = 0; | 131 int integer = 0; |
135 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 132 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
136 EXPECT_EQ(20, integer); | 133 EXPECT_EQ(20, integer); |
137 pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10)); | 134 pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10)); |
138 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); | 135 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
139 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 136 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
140 EXPECT_EQ(10, integer); | 137 EXPECT_EQ(10, integer); |
141 | 138 |
142 pref_store->SetValue(kLongIntPref, | 139 pref_store->SetValue(kLongIntPref, |
143 Value::CreateStringValue( | 140 Value::CreateStringValue( |
144 base::Int64ToString(214748364842LL))); | 141 base::Int64ToString(214748364842LL))); |
145 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual)); | 142 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
146 EXPECT_TRUE(actual->GetAsString(&string_value)); | 143 EXPECT_TRUE(actual->GetAsString(&string_value)); |
147 int64 value; | 144 int64 value; |
148 base::StringToInt64(string_value, &value); | 145 base::StringToInt64(string_value, &value); |
149 EXPECT_EQ(214748364842LL, value); | 146 EXPECT_EQ(214748364842LL, value); |
150 | 147 |
151 // Serialize and compare to expected output. | 148 // Serialize and compare to expected output. |
152 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 149 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
153 pref_store->CommitPendingWrite(); | 150 pref_store->CommitPendingWrite(); |
154 MessageLoop::current()->RunUntilIdle(); | 151 MessageLoop::current()->RunUntilIdle(); |
155 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 152 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // Write to file. | 285 // Write to file. |
289 pref_store->CommitPendingWrite(); | 286 pref_store->CommitPendingWrite(); |
290 MessageLoop::current()->RunUntilIdle(); | 287 MessageLoop::current()->RunUntilIdle(); |
291 | 288 |
292 // Compare to expected output. | 289 // Compare to expected output. |
293 FilePath golden_output_file = | 290 FilePath golden_output_file = |
294 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 291 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
295 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 292 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
296 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 293 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
297 } | 294 } |
OLD | NEW |