Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/prefs/tracked/protected_pref_store.cc" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "base/prefs/json_pref_store.h" | |
| 16 #include "base/prefs/persistent_pref_store.h" | |
| 17 #include "base/run_loop.h" | |
| 18 #include "base/values.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char kProtectedFile[] = "protected_prefs"; | |
| 24 const char kUnprotectedFile[] = "unprotected_prefs"; | |
| 25 | |
| 26 const char kProtectedPref[] = "protected_pref"; | |
| 27 const char kUnprotectedPref[] = "unprotected_pref"; | |
| 28 | |
| 29 const char kValue1[] = "value1"; | |
| 30 const char kValue2[] = "value2"; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 class ProtectedPrefStoreTest : public testing::Test { | |
| 35 public: | |
| 36 virtual void SetUp() OVERRIDE { | |
| 37 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 38 InitializeStores(); | |
| 39 } | |
| 40 | |
| 41 protected: | |
| 42 void InitializeStores() { | |
| 43 protected_store_ = | |
| 44 new JsonPrefStore(temp_dir_.path().Append(kProtectedFile), | |
| 45 main_message_loop_.message_loop_proxy(), | |
| 46 scoped_ptr<PrefFilter>()); | |
| 47 unprotected_store_ = | |
| 48 new JsonPrefStore(temp_dir_.path().Append(kUnprotectedFile), | |
| 49 main_message_loop_.message_loop_proxy(), | |
| 50 scoped_ptr<PrefFilter>()); | |
| 51 | |
| 52 std::set<std::string> protected_pref_names; | |
| 53 protected_pref_names.insert(kProtectedPref); | |
| 54 | |
| 55 combined_store_ = new ProtectedPrefStore(unprotected_store_, | |
| 56 protected_store_, | |
| 57 protected_pref_names, | |
| 58 base::Closure()); | |
| 59 } | |
| 60 | |
| 61 base::MessageLoop main_message_loop_; | |
| 62 base::ScopedTempDir temp_dir_; | |
| 63 scoped_refptr<PersistentPrefStore> unprotected_store_; | |
| 64 scoped_refptr<PersistentPrefStore> protected_store_; | |
| 65 scoped_refptr<ProtectedPrefStore> combined_store_; | |
| 66 }; | |
| 67 | |
| 68 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.
| |
| 69 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | |
| 70 combined_store_->ReadPrefs()); | |
| 71 | |
| 72 combined_store_->SetValue(kProtectedPref, new base::StringValue(kValue1)); | |
| 73 combined_store_->SetValue(kUnprotectedPref, new base::StringValue(kValue2)); | |
| 74 | |
| 75 ASSERT_TRUE(protected_store_->GetValue(kProtectedPref, NULL)); | |
| 76 ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 77 ASSERT_FALSE(unprotected_store_->GetValue(kProtectedPref, NULL)); | |
| 78 ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 79 | |
| 80 ASSERT_TRUE(combined_store_->GetValue(kProtectedPref, NULL)); | |
| 81 ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); | |
| 82 | |
| 83 // Throw away the old instances. | |
| 84 InitializeStores(); | |
| 85 | |
| 86 // Allow serialization to complete. | |
| 87 base::RunLoop().RunUntilIdle(); | |
| 88 | |
| 89 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | |
| 90 combined_store_->ReadPrefs()); | |
| 91 | |
| 92 ASSERT_TRUE(protected_store_->GetValue(kProtectedPref, NULL)); | |
| 93 ASSERT_FALSE(protected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 94 ASSERT_FALSE(unprotected_store_->GetValue(kProtectedPref, NULL)); | |
| 95 ASSERT_TRUE(unprotected_store_->GetValue(kUnprotectedPref, NULL)); | |
| 96 | |
| 97 ASSERT_TRUE(combined_store_->GetValue(kProtectedPref, NULL)); | |
| 98 ASSERT_TRUE(combined_store_->GetValue(kUnprotectedPref, NULL)); | |
| 99 } | |
| OLD | NEW |