| 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/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
| 15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace base { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 const char kHomePage[] = "homepage"; | 24 const char kHomePage[] = "homepage"; |
| 24 | 25 |
| 25 class MockPrefStoreObserver : public PrefStore::Observer { | 26 class MockPrefStoreObserver : public PrefStore::Observer { |
| 26 public: | 27 public: |
| 27 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); | 28 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 28 MOCK_METHOD1(OnInitializationCompleted, void (bool)); | 29 MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 29 }; | 30 }; |
| 30 | 31 |
| 31 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | 32 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 32 public: | 33 public: |
| 33 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); | 34 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 } // namespace | 37 } // namespace |
| 37 | 38 |
| 38 class JsonPrefStoreTest : public testing::Test { | 39 class JsonPrefStoreTest : public testing::Test { |
| 39 protected: | 40 protected: |
| 40 virtual void SetUp() OVERRIDE { | 41 virtual void SetUp() OVERRIDE { |
| 41 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 42 | 43 |
| 43 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir_)); | 44 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir_)); |
| 44 data_dir_ = data_dir_.AppendASCII("base"); | 45 data_dir_ = data_dir_.AppendASCII("base"); |
| 45 data_dir_ = data_dir_.AppendASCII("prefs"); | 46 data_dir_ = data_dir_.AppendASCII("prefs"); |
| 46 data_dir_ = data_dir_.AppendASCII("test"); | 47 data_dir_ = data_dir_.AppendASCII("test"); |
| 47 data_dir_ = data_dir_.AppendASCII("data"); | 48 data_dir_ = data_dir_.AppendASCII("data"); |
| 48 data_dir_ = data_dir_.AppendASCII("pref_service"); | 49 data_dir_ = data_dir_.AppendASCII("pref_service"); |
| 49 LOG(WARNING) << data_dir_.value(); | 50 LOG(WARNING) << data_dir_.value().c_str(); |
| 50 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 51 ASSERT_TRUE(file_util::PathExists(data_dir_)); |
| 51 } | 52 } |
| 52 | 53 |
| 53 // The path to temporary directory used to contain the test operations. | 54 // The path to temporary directory used to contain the test operations. |
| 54 base::ScopedTempDir temp_dir_; | 55 base::ScopedTempDir temp_dir_; |
| 55 // The path to the directory where the test data is stored. | 56 // The path to the directory where the test data is stored. |
| 56 FilePath data_dir_; | 57 FilePath data_dir_; |
| 57 // A message loop that we can use as the file thread message loop. | 58 // A message loop that we can use as the file thread message loop. |
| 58 MessageLoop message_loop_; | 59 MessageLoop message_loop_; |
| 59 }; | 60 }; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 EXPECT_EQ(cnn, string_value); | 109 EXPECT_EQ(cnn, string_value); |
| 109 | 110 |
| 110 const char kSomeDirectory[] = "some_directory"; | 111 const char kSomeDirectory[] = "some_directory"; |
| 111 | 112 |
| 112 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 113 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
| 113 FilePath::StringType path; | 114 FilePath::StringType path; |
| 114 EXPECT_TRUE(actual->GetAsString(&path)); | 115 EXPECT_TRUE(actual->GetAsString(&path)); |
| 115 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); | 116 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
| 116 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); | 117 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
| 117 | 118 |
| 118 pref_store->SetValue(kSomeDirectory, | 119 pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value())); |
| 119 Value::CreateStringValue(some_path.value())); | |
| 120 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 120 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
| 121 EXPECT_TRUE(actual->GetAsString(&path)); | 121 EXPECT_TRUE(actual->GetAsString(&path)); |
| 122 EXPECT_EQ(some_path.value(), path); | 122 EXPECT_EQ(some_path.value(), path); |
| 123 | 123 |
| 124 // Test reading some other data types from sub-dictionaries. | 124 // Test reading some other data types from sub-dictionaries. |
| 125 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 125 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
| 126 bool boolean = false; | 126 bool boolean = false; |
| 127 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 127 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
| 128 EXPECT_TRUE(boolean); | 128 EXPECT_TRUE(boolean); |
| 129 | 129 |
| 130 pref_store->SetValue(kNewWindowsInTabs, | 130 pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false)); |
| 131 Value::CreateBooleanValue(false)); | |
| 132 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 131 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
| 133 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 132 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
| 134 EXPECT_FALSE(boolean); | 133 EXPECT_FALSE(boolean); |
| 135 | 134 |
| 136 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 135 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
| 137 int integer = 0; | 136 int integer = 0; |
| 138 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 137 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
| 139 EXPECT_EQ(20, integer); | 138 EXPECT_EQ(20, integer); |
| 140 pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10)); | 139 pref_store->SetValue(kMaxTabs, new FundamentalValue(10)); |
| 141 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 140 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
| 142 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 141 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
| 143 EXPECT_EQ(10, integer); | 142 EXPECT_EQ(10, integer); |
| 144 | 143 |
| 145 pref_store->SetValue(kLongIntPref, | 144 pref_store->SetValue(kLongIntPref, |
| 146 Value::CreateStringValue( | 145 new StringValue(base::Int64ToString(214748364842LL))); |
| 147 base::Int64ToString(214748364842LL))); | |
| 148 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); | 146 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
| 149 EXPECT_TRUE(actual->GetAsString(&string_value)); | 147 EXPECT_TRUE(actual->GetAsString(&string_value)); |
| 150 int64 value; | 148 int64 value; |
| 151 base::StringToInt64(string_value, &value); | 149 base::StringToInt64(string_value, &value); |
| 152 EXPECT_EQ(214748364842LL, value); | 150 EXPECT_EQ(214748364842LL, value); |
| 153 | 151 |
| 154 // Serialize and compare to expected output. | 152 // Serialize and compare to expected output. |
| 155 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 153 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 156 pref_store->CommitPendingWrite(); | 154 pref_store->CommitPendingWrite(); |
| 157 MessageLoop::current()->RunUntilIdle(); | 155 MessageLoop::current()->RunUntilIdle(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // Write to file. | 289 // Write to file. |
| 292 pref_store->CommitPendingWrite(); | 290 pref_store->CommitPendingWrite(); |
| 293 MessageLoop::current()->RunUntilIdle(); | 291 MessageLoop::current()->RunUntilIdle(); |
| 294 | 292 |
| 295 // Compare to expected output. | 293 // Compare to expected output. |
| 296 FilePath golden_output_file = | 294 FilePath golden_output_file = |
| 297 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 295 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 298 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 296 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 299 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 297 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 300 } | 298 } |
| 299 |
| 300 } // namespace base |
| OLD | NEW |