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

Unified Diff: chrome/browser/prefs/tracked/protected_pref_store_unittest.cc

Issue 205813002: Separate storage for protected preferences into Protected Preferences file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pp4_profile_pref_store
Patch Set: Pull out some changes into other CLs. Created 6 years, 9 months 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 side-by-side diff with in-line comments
Download patch
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..4f3ca2456c8365cadfb270a8f3aecd57d8b5378d
--- /dev/null
+++ b/chrome/browser/prefs/tracked/protected_pref_store_unittest.cc
@@ -0,0 +1,254 @@
+// 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"
erikwright (departed) 2014/03/26 21:08:12 Ha. Can't believe I wrote it, not too surprised y'
+
+#include <set>
+#include <string>
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/prefs/persistent_pref_store.h"
+#include "base/prefs/pref_store_observer_mock.h"
+#include "base/prefs/testing_pref_store.h"
+#include "base/values.h"
+#include "chrome/browser/prefs/tracked/protected_pref_store.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const char kProtectedPref[] = "protected_pref";
+const char kUnprotectedPref[] = "unprotected_pref";
+
+const char kValue1[] = "value1";
+const char kValue2[] = "value2";
+
+class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
+ public:
+ struct Data {
+ bool invoked;
+ PersistentPrefStore::PrefReadError read_error;
+ };
+
+ explicit MockReadErrorDelegate(Data* data) : data_(data) {
+ DCHECK(data_);
+ EXPECT_FALSE(data_->invoked);
+ }
+
+ // PersistentPrefStore::ReadErrorDelegate implementation
+ virtual void OnError(PersistentPrefStore::PrefReadError read_error) OVERRIDE {
+ EXPECT_FALSE(data_->invoked);
+ data_->invoked = true;
+ data_->read_error = read_error;
+ }
+
+ private:
+ Data* data_;
+};
+
+} // namespace
+
+class ProtectedPrefStoreTest : public testing::Test {
+ public:
+ ProtectedPrefStoreTest()
+ : initialization_callback_invoked_(false),
+ read_error_delegate_data_(
+ {false, PersistentPrefStore::PREF_READ_ERROR_NONE}),
+ read_error_delegate_(
+ new MockReadErrorDelegate(&read_error_delegate_data_)) {}
+
+ virtual void SetUp() OVERRIDE {
+ protected_store_ = new TestingPrefStore;
+ unprotected_store_ = new TestingPrefStore;
+
+ std::set<std::string> protected_pref_names;
+ protected_pref_names.insert(kProtectedPref);
+
+ combined_store_ = new ProtectedPrefStore(
+ unprotected_store_,
+ protected_store_,
+ protected_pref_names,
+ base::Bind(&ProtectedPrefStoreTest::InitializationCallback,
+ base::Unretained(this)));
+
+ combined_store_->AddObserver(&observer_);
+ }
+
+ virtual void TearDown() OVERRIDE {
+ combined_store_->RemoveObserver(&observer_);
+ }
+
+ protected:
+ scoped_ptr<PersistentPrefStore::ReadErrorDelegate> GetReadErrorDelegate() {
+ EXPECT_TRUE(read_error_delegate_);
+ return read_error_delegate_
+ .PassAs<PersistentPrefStore::ReadErrorDelegate>();
+ }
+
+ PrefStoreObserverMock observer_;
+ bool initialization_callback_invoked_;
+
+ scoped_refptr<TestingPrefStore> unprotected_store_;
+ scoped_refptr<TestingPrefStore> protected_store_;
+ scoped_refptr<ProtectedPrefStore> combined_store_;
+
+ MockReadErrorDelegate::Data read_error_delegate_data_;
+
+ private:
+ void InitializationCallback() {
+ EXPECT_FALSE(observer_.initialized);
+ EXPECT_FALSE(initialization_callback_invoked_);
+ initialization_callback_invoked_ = true;
+ }
+
+ scoped_ptr<MockReadErrorDelegate> read_error_delegate_;
+};
+
+TEST_F(ProtectedPrefStoreTest, StoreValues) {
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
+ combined_store_->ReadPrefs());
+
+ // Properly stores new values.
+ 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));
+
+ ASSERT_FALSE(protected_store_->committed());
+ ASSERT_FALSE(unprotected_store_->committed());
+
+ combined_store_->CommitPendingWrite();
+
+ ASSERT_TRUE(protected_store_->committed());
+ ASSERT_TRUE(unprotected_store_->committed());
+}
+
+TEST_F(ProtectedPrefStoreTest, ReadValues) {
+ protected_store_->SetValue(kProtectedPref, new base::StringValue(kValue1));
+ unprotected_store_->SetValue(kUnprotectedPref,
+ new base::StringValue(kValue2));
+
+ // Works properly with values that are already there.
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
+ combined_store_->ReadPrefs());
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
+ combined_store_->GetReadError());
+
+ 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));
+}
+
+TEST_F(ProtectedPrefStoreTest, PreviouslyProtected) {
+ protected_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue1));
+ combined_store_->ReadPrefs();
+ // It will read from the protected store.
+ ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL));
+ ASSERT_TRUE(protected_store_->GetValue(kUnprotectedPref, NULL));
+ ASSERT_FALSE(unprotected_store_->GetValue(kUnprotectedPref, NULL));
+
+ // But when we update the value...
+ combined_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue2));
+ // ...it will be migrated.
+ ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL));
+ ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL));
+ ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL));
+}
+
+TEST_F(ProtectedPrefStoreTest, Observer) {
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
+ combined_store_->ReadPrefs());
+ EXPECT_TRUE(initialization_callback_invoked_);
+ EXPECT_TRUE(observer_.initialized);
+ EXPECT_TRUE(observer_.initialization_success);
+ EXPECT_TRUE(observer_.changed_keys.empty());
+ combined_store_->SetValue(kProtectedPref, new base::StringValue(kValue1));
+ EXPECT_THAT(observer_.changed_keys, testing::ElementsAre(kProtectedPref));
+ observer_.changed_keys.clear();
+ combined_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue2));
+ EXPECT_THAT(observer_.changed_keys, testing::ElementsAre(kUnprotectedPref));
+}
+
+TEST_F(ProtectedPrefStoreTest, ProtectedPrefReadError) {
+ protected_store_->set_read_error(
+ PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->ReadPrefs());
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->GetReadError());
+}
+
+TEST_F(ProtectedPrefStoreTest, UnprotectedPrefReadError) {
+ unprotected_store_->set_read_error(
+ PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->ReadPrefs());
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->GetReadError());
+}
+
+TEST_F(ProtectedPrefStoreTest, BothPrefReadError) {
+ unprotected_store_->set_read_error(
+ PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
+ protected_store_->set_read_error(
+ PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->ReadPrefs());
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->GetReadError());
+}
+
+TEST_F(ProtectedPrefStoreTest, BothPrefReadErrorAsync) {
+ unprotected_store_->set_read_error(
+ PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
+ protected_store_->set_read_error(
+ PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
+
+ protected_store_->SetBlockAsyncRead(true);
+
+ EXPECT_FALSE(read_error_delegate_data_.invoked);
+
+ combined_store_->ReadPrefsAsync(GetReadErrorDelegate().release());
+
+ EXPECT_FALSE(read_error_delegate_data_.invoked);
+
+ protected_store_->SetBlockAsyncRead(false);
+
+ EXPECT_TRUE(read_error_delegate_data_.invoked);
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->GetReadError());
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ combined_store_->GetReadError());
+}
+
+TEST_F(ProtectedPrefStoreTest, IsInitializationComplete) {
+ EXPECT_FALSE(combined_store_->IsInitializationComplete());
+ combined_store_->ReadPrefs();
+ EXPECT_TRUE(combined_store_->IsInitializationComplete());
+}
+
+TEST_F(ProtectedPrefStoreTest, IsInitializationCompleteAsync) {
+ protected_store_->SetBlockAsyncRead(true);
+ unprotected_store_->SetBlockAsyncRead(true);
+ EXPECT_FALSE(combined_store_->IsInitializationComplete());
+ combined_store_->ReadPrefsAsync(NULL);
+ EXPECT_FALSE(combined_store_->IsInitializationComplete());
+ protected_store_->SetBlockAsyncRead(false);
+ EXPECT_FALSE(combined_store_->IsInitializationComplete());
+ unprotected_store_->SetBlockAsyncRead(false);
+ EXPECT_TRUE(combined_store_->IsInitializationComplete());
+}

Powered by Google App Engine
This is Rietveld 408576698