Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(566)

Side by Side Diff: chrome/common/json_pref_store_unittest.cc

Issue 5646003: Sanitize PrefStore interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix PrefService mock construction in PrefServiceTest to include command line store. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/json_pref_store.cc ('k') | chrome/common/persistent_pref_store.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/message_loop.h" 6 #include "base/message_loop.h"
7 #include "base/message_loop_proxy.h" 7 #include "base/message_loop_proxy.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/scoped_ptr.h" 9 #include "base/scoped_ptr.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 // A message loop that we can use as the file thread message loop. 47 // A message loop that we can use as the file thread message loop.
48 MessageLoop message_loop_; 48 MessageLoop message_loop_;
49 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 49 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
50 }; 50 };
51 51
52 // Test fallback behavior for a nonexistent file. 52 // Test fallback behavior for a nonexistent file.
53 TEST_F(JsonPrefStoreTest, NonExistentFile) { 53 TEST_F(JsonPrefStoreTest, NonExistentFile) {
54 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); 54 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
55 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); 55 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
56 JsonPrefStore pref_store(bogus_input_file, message_loop_proxy_.get()); 56 JsonPrefStore pref_store(bogus_input_file, message_loop_proxy_.get());
57 EXPECT_EQ(PrefStore::PREF_READ_ERROR_NO_FILE, pref_store.ReadPrefs()); 57 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
58 pref_store.ReadPrefs());
58 EXPECT_FALSE(pref_store.ReadOnly()); 59 EXPECT_FALSE(pref_store.ReadOnly());
59 EXPECT_TRUE(pref_store.prefs()->empty());
60 } 60 }
61 61
62 // Test fallback behavior for an invalid file. 62 // Test fallback behavior for an invalid file.
63 TEST_F(JsonPrefStoreTest, InvalidFile) { 63 TEST_F(JsonPrefStoreTest, InvalidFile) {
64 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); 64 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json");
65 FilePath invalid_file = test_dir_.AppendASCII("invalid.json"); 65 FilePath invalid_file = test_dir_.AppendASCII("invalid.json");
66 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); 66 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file));
67 JsonPrefStore pref_store(invalid_file, message_loop_proxy_.get()); 67 JsonPrefStore pref_store(invalid_file, message_loop_proxy_.get());
68 EXPECT_EQ(PrefStore::PREF_READ_ERROR_JSON_PARSE, pref_store.ReadPrefs()); 68 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
69 pref_store.ReadPrefs());
69 EXPECT_FALSE(pref_store.ReadOnly()); 70 EXPECT_FALSE(pref_store.ReadOnly());
70 EXPECT_TRUE(pref_store.prefs()->empty());
71 71
72 // The file should have been moved aside. 72 // The file should have been moved aside.
73 EXPECT_FALSE(file_util::PathExists(invalid_file)); 73 EXPECT_FALSE(file_util::PathExists(invalid_file));
74 FilePath moved_aside = test_dir_.AppendASCII("invalid.bad"); 74 FilePath moved_aside = test_dir_.AppendASCII("invalid.bad");
75 EXPECT_TRUE(file_util::PathExists(moved_aside)); 75 EXPECT_TRUE(file_util::PathExists(moved_aside));
76 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, 76 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original,
77 moved_aside)); 77 moved_aside));
78 } 78 }
79 79
80 TEST_F(JsonPrefStoreTest, Basic) { 80 TEST_F(JsonPrefStoreTest, Basic) {
81 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), 81 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
82 test_dir_.AppendASCII("write.json"))); 82 test_dir_.AppendASCII("write.json")));
83 83
84 // Test that the persistent value can be loaded. 84 // Test that the persistent value can be loaded.
85 FilePath input_file = test_dir_.AppendASCII("write.json"); 85 FilePath input_file = test_dir_.AppendASCII("write.json");
86 ASSERT_TRUE(file_util::PathExists(input_file)); 86 ASSERT_TRUE(file_util::PathExists(input_file));
87 JsonPrefStore pref_store(input_file, message_loop_proxy_.get()); 87 JsonPrefStore pref_store(input_file, message_loop_proxy_.get());
88 ASSERT_EQ(PrefStore::PREF_READ_ERROR_NONE, pref_store.ReadPrefs()); 88 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store.ReadPrefs());
89 ASSERT_FALSE(pref_store.ReadOnly()); 89 ASSERT_FALSE(pref_store.ReadOnly());
90 DictionaryValue* prefs = pref_store.prefs();
91 90
92 // The JSON file looks like this: 91 // The JSON file looks like this:
93 // { 92 // {
94 // "homepage": "http://www.cnn.com", 93 // "homepage": "http://www.cnn.com",
95 // "some_directory": "/usr/local/", 94 // "some_directory": "/usr/local/",
96 // "tabs": { 95 // "tabs": {
97 // "new_windows_in_tabs": true, 96 // "new_windows_in_tabs": true,
98 // "max_tabs": 20 97 // "max_tabs": 20
99 // } 98 // }
100 // } 99 // }
101 100
102 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; 101 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
103 const char kMaxTabs[] = "tabs.max_tabs"; 102 const char kMaxTabs[] = "tabs.max_tabs";
104 const char kLongIntPref[] = "long_int.pref"; 103 const char kLongIntPref[] = "long_int.pref";
105 104
106 std::string cnn("http://www.cnn.com"); 105 std::string cnn("http://www.cnn.com");
107 106
107 Value* actual;
108 EXPECT_EQ(PrefStore::READ_OK,
109 pref_store.GetValue(prefs::kHomePage, &actual));
108 std::string string_value; 110 std::string string_value;
109 EXPECT_TRUE(prefs->GetString(prefs::kHomePage, &string_value)); 111 EXPECT_TRUE(actual->GetAsString(&string_value));
110 EXPECT_EQ(cnn, string_value); 112 EXPECT_EQ(cnn, string_value);
111 113
112 const char kSomeDirectory[] = "some_directory"; 114 const char kSomeDirectory[] = "some_directory";
113 115
116 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kSomeDirectory, &actual));
114 FilePath::StringType path; 117 FilePath::StringType path;
115 EXPECT_TRUE(prefs->GetString(kSomeDirectory, &path)); 118 EXPECT_TRUE(actual->GetAsString(&path));
116 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); 119 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path);
117 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); 120 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/"));
118 prefs->SetString(kSomeDirectory, some_path.value()); 121
119 EXPECT_TRUE(prefs->GetString(kSomeDirectory, &path)); 122 pref_store.SetValue(kSomeDirectory,
123 Value::CreateStringValue(some_path.value()));
124 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kSomeDirectory, &actual));
125 EXPECT_TRUE(actual->GetAsString(&path));
120 EXPECT_EQ(some_path.value(), path); 126 EXPECT_EQ(some_path.value(), path);
121 127
122 // Test reading some other data types from sub-dictionaries. 128 // Test reading some other data types from sub-dictionaries.
123 bool boolean; 129 EXPECT_EQ(PrefStore::READ_OK,
124 EXPECT_TRUE(prefs->GetBoolean(kNewWindowsInTabs, &boolean)); 130 pref_store.GetValue(kNewWindowsInTabs, &actual));
131 bool boolean = false;
132 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
125 EXPECT_TRUE(boolean); 133 EXPECT_TRUE(boolean);
126 134
127 prefs->SetBoolean(kNewWindowsInTabs, false); 135 pref_store.SetValue(kNewWindowsInTabs,
128 EXPECT_TRUE(prefs->GetBoolean(kNewWindowsInTabs, &boolean)); 136 Value::CreateBooleanValue(false));
137 EXPECT_EQ(PrefStore::READ_OK,
138 pref_store.GetValue(kNewWindowsInTabs, &actual));
139 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
129 EXPECT_FALSE(boolean); 140 EXPECT_FALSE(boolean);
130 141
131 int integer; 142 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kMaxTabs, &actual));
132 EXPECT_TRUE(prefs->GetInteger(kMaxTabs, &integer)); 143 int integer = 0;
144 EXPECT_TRUE(actual->GetAsInteger(&integer));
133 EXPECT_EQ(20, integer); 145 EXPECT_EQ(20, integer);
134 prefs->SetInteger(kMaxTabs, 10); 146 pref_store.SetValue(kMaxTabs, Value::CreateIntegerValue(10));
135 EXPECT_TRUE(prefs->GetInteger(kMaxTabs, &integer)); 147 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kMaxTabs, &actual));
148 EXPECT_TRUE(actual->GetAsInteger(&integer));
136 EXPECT_EQ(10, integer); 149 EXPECT_EQ(10, integer);
137 150
138 prefs->SetString(kLongIntPref, base::Int64ToString(214748364842LL)); 151 pref_store.SetValue(kLongIntPref,
139 EXPECT_TRUE(prefs->GetString(kLongIntPref, &string_value)); 152 Value::CreateStringValue(
153 base::Int64ToString(214748364842LL)));
154 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kLongIntPref, &actual));
155 EXPECT_TRUE(actual->GetAsString(&string_value));
140 int64 value; 156 int64 value;
141 base::StringToInt64(string_value, &value); 157 base::StringToInt64(string_value, &value);
142 EXPECT_EQ(214748364842LL, value); 158 EXPECT_EQ(214748364842LL, value);
143 159
144 // Serialize and compare to expected output. 160 // Serialize and compare to expected output.
145 FilePath output_file = input_file; 161 FilePath output_file = input_file;
146 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); 162 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json");
147 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 163 ASSERT_TRUE(file_util::PathExists(golden_output_file));
148 ASSERT_TRUE(pref_store.WritePrefs()); 164 ASSERT_TRUE(pref_store.WritePrefs());
149 MessageLoop::current()->RunAllPending(); 165 MessageLoop::current()->RunAllPending();
150 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); 166 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
151 ASSERT_TRUE(file_util::Delete(output_file, false)); 167 ASSERT_TRUE(file_util::Delete(output_file, false));
152 } 168 }
OLDNEW
« no previous file with comments | « chrome/common/json_pref_store.cc ('k') | chrome/common/persistent_pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698