Index: chrome/browser/prefs/tracked/protected_pref_store_unittest.cc |
diff --git a/chrome/browser/prefs/tracked/protected_pref_store_unittest.cc b/chrome/browser/prefs/tracked/protected_pref_store_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ba4a17bcd000ee887cc0a6248ef7e74e5c0bba32 |
--- /dev/null |
+++ b/chrome/browser/prefs/tracked/protected_pref_store_unittest.cc |
@@ -0,0 +1,99 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/prefs/tracked/protected_pref_store.cc" |
+ |
+#include <set> |
+#include <string> |
+ |
+#include "base/callback.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/prefs/json_pref_store.h" |
+#include "base/prefs/persistent_pref_store.h" |
+#include "base/run_loop.h" |
+#include "base/values.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const char kProtectedFile[] = "protected_prefs"; |
+const char kUnprotectedFile[] = "unprotected_prefs"; |
+ |
+const char kProtectedPref[] = "protected_pref"; |
+const char kUnprotectedPref[] = "unprotected_pref"; |
+ |
+const char kValue1[] = "value1"; |
+const char kValue2[] = "value2"; |
+ |
+} // namespace |
+ |
+class ProtectedPrefStoreTest : public testing::Test { |
+ public: |
+ virtual void SetUp() OVERRIDE { |
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ InitializeStores(); |
+ } |
+ |
+ protected: |
+ void InitializeStores() { |
+ protected_store_ = |
+ new JsonPrefStore(temp_dir_.path().Append(kProtectedFile), |
+ main_message_loop_.message_loop_proxy(), |
+ scoped_ptr<PrefFilter>()); |
+ unprotected_store_ = |
+ new JsonPrefStore(temp_dir_.path().Append(kUnprotectedFile), |
+ main_message_loop_.message_loop_proxy(), |
+ scoped_ptr<PrefFilter>()); |
+ |
+ std::set<std::string> protected_pref_names; |
+ protected_pref_names.insert(kProtectedPref); |
+ |
+ combined_store_ = new ProtectedPrefStore(unprotected_store_, |
+ protected_store_, |
+ protected_pref_names, |
+ base::Closure()); |
+ } |
+ |
+ base::MessageLoop main_message_loop_; |
+ base::ScopedTempDir temp_dir_; |
+ scoped_refptr<PersistentPrefStore> unprotected_store_; |
+ scoped_refptr<PersistentPrefStore> protected_store_; |
+ scoped_refptr<ProtectedPrefStore> combined_store_; |
+}; |
+ |
+TEST_F(ProtectedPrefStoreTest, StoreValues) { |
robertshield
2014/03/25 03:05:12
Can you add a test where either or both of the bac
erikwright (departed)
2014/03/25 20:28:26
Done.
|
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
+ combined_store_->ReadPrefs()); |
+ |
+ combined_store_->SetValue(kProtectedPref, new base::StringValue(kValue1)); |
+ combined_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue2)); |
+ |
+ ASSERT_TRUE(protected_store_->GetValue(kProtectedPref, NULL)); |
+ ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL)); |
+ ASSERT_FALSE(unprotected_store_->GetValue(kProtectedPref, NULL)); |
+ ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); |
+ |
+ ASSERT_TRUE(combined_store_->GetValue(kProtectedPref, NULL)); |
+ ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); |
+ |
+ // Throw away the old instances. |
+ InitializeStores(); |
+ |
+ // Allow serialization to complete. |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
+ combined_store_->ReadPrefs()); |
+ |
+ ASSERT_TRUE(protected_store_->GetValue(kProtectedPref, NULL)); |
+ ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL)); |
+ ASSERT_FALSE(unprotected_store_->GetValue(kProtectedPref, NULL)); |
+ ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); |
+ |
+ ASSERT_TRUE(combined_store_->GetValue(kProtectedPref, NULL)); |
+ ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); |
+} |