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

Unified Diff: chrome/browser/prefs/pref_hash_filter_unittest.cc

Issue 114223002: Multi-strategy based tracking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: UNKNOWN_VALUE when going from atomic to split hash for an existing tracked pref Created 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/prefs/pref_hash_filter_unittest.cc
diff --git a/chrome/browser/prefs/pref_hash_filter_unittest.cc b/chrome/browser/prefs/pref_hash_filter_unittest.cc
index d26fe705e56b6dbcd4a4cdbf7cb64f726fe24fce..c59cbddda83de87384c7b2add47492b8faa566b7 100644
--- a/chrome/browser/prefs/pref_hash_filter_unittest.cc
+++ b/chrome/browser/prefs/pref_hash_filter_unittest.cc
@@ -17,11 +17,25 @@
#include "chrome/browser/prefs/pref_hash_store.h"
#include "testing/gtest/include/gtest/gtest.h"
+namespace {
+
+const char kAtomicPref[] = "atomic_pref";
+const char kSplitPref[] = "split_pref";
+
+const PrefHashFilter::TrackedPreference kTestTrackedPrefs[] = {
+ { 0, kAtomicPref, PrefHashFilter::TRACKING_STRATEGY_ATOMIC },
+ { 1, kSplitPref, PrefHashFilter::TRACKING_STRATEGY_SPLIT },
+ { 2, "other_value", PrefHashFilter::TRACKING_STRATEGY_ATOMIC },
+};
+
+} // namespace
+
// A PrefHashStore that allows simulation of CheckValue results and captures
// checked and stored values.
class MockPrefHashStore : public PrefHashStore {
public:
- static const char kNullPlaceholder[];
+ typedef std::pair<const base::Value*, PrefHashFilter::PrefTrackingStrategy>
+ ValueStrategyPair;
MockPrefHashStore() {}
@@ -29,34 +43,38 @@ class MockPrefHashStore : public PrefHashStore {
void SetCheckResult(const std::string& path,
PrefHashStore::ValueState result);
- // Returns paths that have been passed to |CheckValue|.
- const std::set<std::string>& checked_paths() const {
- return checked_paths_;
+ // Returns the number of paths checked.
+ size_t checked_paths_count() const {
+ return checked_values_.size();
}
- // Returns paths that have been passed to |StoreHash|.
- const std::set<std::string>& stored_paths() const {
- return stored_paths_;
+ // Returns the number of paths stored.
+ size_t stored_paths_count() const {
+ return stored_values_.size();
}
- // Returns the value that was passed to |CheckValue| for |path|, rendered to a
- // string. If the checked value was NULL, the string is kNullPlaceholder.
- std::string checked_value(const std::string& path) const {
- std::map<std::string, std::string>::iterator value =
+ // Returns the value and strategy that was passed to |Check(Split)?Hash| for
+ // |path|, rendered to a string.
+ ValueStrategyPair checked_value(const std::string& path) const {
+ std::map<std::string, ValueStrategyPair>::iterator value =
checked_values_.find(path);
if (value != checked_values_.end())
return value->second;
- return std::string("No checked value.");
+ return std::make_pair(
+ base::Value::CreateStringValue("No checked value."),
+ static_cast<PrefHashFilter::PrefTrackingStrategy>(-1));
}
- // Returns the value that was passed to |StoreHash| for |path|, rendered to a
- // string. If the stored value was NULL, the string is kNullPlaceholder.
- std::string stored_value(const std::string& path) const {
- std::map<std::string, std::string>::const_iterator value =
+ // Returns the value that was passed to |Store(Split)?Hash| for |path|,
+ // rendered to a string.
+ ValueStrategyPair stored_value(const std::string& path) const {
+ std::map<std::string, ValueStrategyPair>::const_iterator value =
stored_values_.find(path);
if (value != stored_values_.end())
return value->second;
- return std::string("No stored value.");
+ return std::make_pair(
+ base::Value::CreateStringValue("No stored value."),
+ static_cast<PrefHashFilter::PrefTrackingStrategy>(-1));
}
// PrefHashStore implementation.
@@ -64,19 +82,33 @@ class MockPrefHashStore : public PrefHashStore {
const std::string& path, const base::Value* value) const OVERRIDE;
virtual void StoreHash(const std::string& path,
const base::Value* new_value) OVERRIDE;
+ virtual PrefHashStore::ValueState CheckSplitValue(
+ const std::string& path,
+ const base::DictionaryValue* initial_split_value,
+ std::vector<std::string>* invalid_keys) const OVERRIDE;
+ virtual void StoreSplitHash(
+ const std::string& path,
+ const base::DictionaryValue* split_value) OVERRIDE;
private:
+ // Records a call to this mock's CheckValue/CheckSplitValue methods.
+ PrefHashStore::ValueState RecordCheckValue(
+ const std::string& path,
+ const base::Value* value,
+ PrefHashFilter::PrefTrackingStrategy strategy) const;
+
+ // Records a call to this mock's StoreHash/StoreSplitHash methods.
+ void RecordStoreHash(const std::string& path,
+ const base::Value* new_value,
+ PrefHashFilter::PrefTrackingStrategy strategy);
+
std::map<std::string, PrefHashStore::ValueState> check_results_;
- mutable std::set<std::string> checked_paths_;
- std::set<std::string> stored_paths_;
- mutable std::map<std::string, std::string> checked_values_;
- std::map<std::string, std::string> stored_values_;
+ mutable std::map<std::string, ValueStrategyPair> checked_values_;
+ std::map<std::string, ValueStrategyPair> stored_values_;
DISALLOW_COPY_AND_ASSIGN(MockPrefHashStore);
};
-const char MockPrefHashStore::kNullPlaceholder[] = "NULL";
-
void MockPrefHashStore::SetCheckResult(
const std::string& path, PrefHashStore::ValueState result) {
check_results_.insert(std::make_pair(path, result));
@@ -84,12 +116,37 @@ void MockPrefHashStore::SetCheckResult(
PrefHashStore::ValueState MockPrefHashStore::CheckValue(
const std::string& path, const base::Value* value) const {
- EXPECT_TRUE(checked_paths_.insert(path).second);
- std::string as_string = kNullPlaceholder;
- if (value)
- EXPECT_TRUE(value->GetAsString(&as_string));
+ return RecordCheckValue(path, value,
+ PrefHashFilter::TRACKING_STRATEGY_ATOMIC);
+}
+
+void MockPrefHashStore::StoreHash(const std::string& path,
+ const base::Value* new_value) {
+ RecordStoreHash(path, new_value, PrefHashFilter::TRACKING_STRATEGY_ATOMIC);
+}
+
+PrefHashStore::ValueState MockPrefHashStore::CheckSplitValue(
+ const std::string& path,
+ const base::DictionaryValue* initial_split_value,
+ std::vector<std::string>* invalid_keys) const {
+ return RecordCheckValue(path, initial_split_value,
+ PrefHashFilter::TRACKING_STRATEGY_SPLIT);
+}
- checked_values_.insert(std::make_pair(path, as_string));
+void MockPrefHashStore::StoreSplitHash(const std::string& path,
+ const base::DictionaryValue* new_value) {
+ RecordStoreHash(path, new_value, PrefHashFilter::TRACKING_STRATEGY_SPLIT);
+}
+
+PrefHashStore::ValueState MockPrefHashStore::RecordCheckValue(
+ const std::string& path,
+ const base::Value* value,
+ PrefHashFilter::PrefTrackingStrategy strategy) const {
+ // Record that |path| was checked and validate that it wasn't previously
+ // checked.
+ EXPECT_TRUE(checked_values_.insert(std::make_pair(
+ path, std::make_pair(value, strategy))).
+ second);
std::map<std::string, PrefHashStore::ValueState>::const_iterator result =
check_results_.find(path);
if (result != check_results_.end())
@@ -97,13 +154,12 @@ PrefHashStore::ValueState MockPrefHashStore::CheckValue(
return PrefHashStore::UNCHANGED;
}
-void MockPrefHashStore::StoreHash(const std::string& path,
- const base::Value* new_value) {
- std::string as_string = kNullPlaceholder;
- if (new_value)
- EXPECT_TRUE(new_value->GetAsString(&as_string));
- stored_paths_.insert(path);
- stored_values_.insert(std::make_pair(path, as_string));
+void MockPrefHashStore::RecordStoreHash(
+ const std::string& path,
+ const base::Value* new_value,
+ PrefHashFilter::PrefTrackingStrategy strategy) {
+ EXPECT_TRUE(stored_values_.insert(std::make_pair(
+ path, std::make_pair(new_value, strategy))).second);
}
// Creates a PrefHashFilter that uses a MockPrefHashStore. The
@@ -116,7 +172,8 @@ scoped_ptr<PrefHashFilter> CreatePrefHashFilter(
if (mock_pref_hash_store)
*mock_pref_hash_store = temp_mock_pref_hash_store.get();
return scoped_ptr<PrefHashFilter>(
- new PrefHashFilter(temp_mock_pref_hash_store.PassAs<PrefHashStore>()));
+ new PrefHashFilter(temp_mock_pref_hash_store.PassAs<PrefHashStore>(),
+ kTestTrackedPrefs, arraysize(kTestTrackedPrefs)));
}
class PrefHashFilterTest : public testing::Test {
@@ -124,20 +181,11 @@ class PrefHashFilterTest : public testing::Test {
PrefHashFilterTest() : mock_pref_hash_store_(NULL) {}
virtual void SetUp() OVERRIDE {
- // Capture the name of one tracked pref.
- MockPrefHashStore* temp_hash_store = NULL;
- scoped_ptr<PrefHashFilter> temp_filter =
- CreatePrefHashFilter(&temp_hash_store);
- temp_filter->FilterOnLoad(&pref_store_contents_);
- ASSERT_FALSE(temp_hash_store->checked_paths().empty());
- tracked_path_ = *temp_hash_store->checked_paths().begin();
-
// Construct a PrefHashFilter and MockPrefHashStore for the test.
pref_hash_filter_ = CreatePrefHashFilter(&mock_pref_hash_store_);
}
protected:
- std::string tracked_path_;
MockPrefHashStore* mock_pref_hash_store_;
base::DictionaryValue pref_store_contents_;
scoped_ptr<PrefHashFilter> pref_hash_filter_;
@@ -147,80 +195,171 @@ class PrefHashFilterTest : public testing::Test {
TEST_F(PrefHashFilterTest, EmptyAndUnchanged) {
pref_hash_filter_->FilterOnLoad(&pref_store_contents_);
- // More than 0 paths checked.
- ASSERT_LT(0u, mock_pref_hash_store_->checked_paths().size());
+ // All paths checked.
+ ASSERT_EQ(arraysize(kTestTrackedPrefs),
+ mock_pref_hash_store_->checked_paths_count());
// No paths stored, since they all return |UNCHANGED|.
- ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths().size());
+ ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
// Since there was nothing in |pref_store_contents_| the checked value should
- // have been NULL.
- ASSERT_EQ(MockPrefHashStore::kNullPlaceholder,
- mock_pref_hash_store_->checked_value(tracked_path_));
+ // have been NULL for all tracked preferences.
+ for (int i = 0; i < arraysize(kTestTrackedPrefs); ++i) {
+ ASSERT_EQ(NULL, mock_pref_hash_store_->checked_value(
+ kTestTrackedPrefs[i].name).first);
+ }
}
-TEST_F(PrefHashFilterTest, FilterUpdate) {
+TEST_F(PrefHashFilterTest, FilterAtomicPrefUpdate) {
base::StringValue string_value("string value");
- pref_hash_filter_->FilterUpdate(tracked_path_, &string_value);
+ pref_hash_filter_->FilterUpdate(kAtomicPref, &string_value);
// One path should be stored.
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths().size());
- ASSERT_EQ(tracked_path_, *mock_pref_hash_store_->stored_paths().begin());
- ASSERT_EQ("string value", mock_pref_hash_store_->stored_value(tracked_path_));
+ ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count());
+ MockPrefHashStore::ValueStrategyPair stored_value =
+ mock_pref_hash_store_->stored_value(kAtomicPref);
+ ASSERT_EQ(&string_value, stored_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC, stored_value.second);
}
-TEST_F(PrefHashFilterTest, EmptyAndUnknown){
- mock_pref_hash_store_->SetCheckResult(tracked_path_,
+TEST_F(PrefHashFilterTest, FilterSplitPrefUpdate) {
+ base::DictionaryValue dict_value;
+ dict_value.SetString("a", "foo");
+ dict_value.SetInteger("b", 1234);
+ pref_hash_filter_->FilterUpdate(kSplitPref, &dict_value);
+ // One path should be stored.
+ ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths_count());
+ MockPrefHashStore::ValueStrategyPair stored_value =
+ mock_pref_hash_store_->stored_value(kSplitPref);
+ ASSERT_EQ(&dict_value, stored_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT, stored_value.second);
+}
+
+TEST_F(PrefHashFilterTest, FilterUntrackedPrefUpdate) {
+ base::StringValue untracked_value("some value");
+ pref_hash_filter_->FilterUpdate("untracked", &untracked_value);
+ // No paths should be stored.
+ ASSERT_EQ(0u, mock_pref_hash_store_->stored_paths_count());
+}
+
+TEST_F(PrefHashFilterTest, EmptyAndUnknown) {
+ mock_pref_hash_store_->SetCheckResult(kAtomicPref,
+ PrefHashStore::UNKNOWN_VALUE);
+ mock_pref_hash_store_->SetCheckResult(kSplitPref,
PrefHashStore::UNKNOWN_VALUE);
pref_hash_filter_->FilterOnLoad(&pref_store_contents_);
- ASSERT_LT(0u, mock_pref_hash_store_->checked_paths().size());
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths().size());
- ASSERT_EQ(tracked_path_, *mock_pref_hash_store_->stored_paths().begin());
- ASSERT_EQ(MockPrefHashStore::kNullPlaceholder,
- mock_pref_hash_store_->stored_value(tracked_path_));
+ ASSERT_EQ(arraysize(kTestTrackedPrefs),
+ mock_pref_hash_store_->checked_paths_count());
+ ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
+
+ MockPrefHashStore::ValueStrategyPair stored_atomic_value =
+ mock_pref_hash_store_->stored_value(kAtomicPref);
+ ASSERT_EQ(NULL, stored_atomic_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
+ stored_atomic_value.second);
+
+ MockPrefHashStore::ValueStrategyPair stored_split_value =
+ mock_pref_hash_store_->stored_value(kSplitPref);
+ ASSERT_EQ(NULL, stored_split_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT,
+ stored_split_value.second);
}
TEST_F(PrefHashFilterTest, InitialValueUnknown) {
- pref_store_contents_.Set(tracked_path_,
- new base::StringValue("string value"));
-
- mock_pref_hash_store_->SetCheckResult(tracked_path_,
+ // Ownership of these values is transfered to |pref_store_contents_|.
+ base::StringValue* string_value = new base::StringValue("string value");
+ base::DictionaryValue* dict_value = new base::DictionaryValue;
+ dict_value->SetString("a", "foo");
+ dict_value->SetInteger("b", 1234);
+ pref_store_contents_.Set(kAtomicPref, string_value);
+ pref_store_contents_.Set(kSplitPref, dict_value);
+
+ mock_pref_hash_store_->SetCheckResult(kAtomicPref,
+ PrefHashStore::UNKNOWN_VALUE);
+ mock_pref_hash_store_->SetCheckResult(kSplitPref,
PrefHashStore::UNKNOWN_VALUE);
pref_hash_filter_->FilterOnLoad(&pref_store_contents_);
- ASSERT_LT(0u, mock_pref_hash_store_->checked_paths().size());
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths().size());
- ASSERT_EQ(tracked_path_, *mock_pref_hash_store_->stored_paths().begin());
- ASSERT_EQ("string value", mock_pref_hash_store_->stored_value(tracked_path_));
+ ASSERT_EQ(arraysize(kTestTrackedPrefs),
+ mock_pref_hash_store_->checked_paths_count());
+ ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
+
+ MockPrefHashStore::ValueStrategyPair stored_atomic_value =
+ mock_pref_hash_store_->stored_value(kAtomicPref);
+ ASSERT_EQ(string_value, stored_atomic_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
+ stored_atomic_value.second);
+
+ MockPrefHashStore::ValueStrategyPair stored_split_value =
+ mock_pref_hash_store_->stored_value(kSplitPref);
+ ASSERT_EQ(dict_value, stored_split_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT,
+ stored_split_value.second);
}
TEST_F(PrefHashFilterTest, InitialValueChanged) {
- pref_store_contents_.Set(tracked_path_,
- new base::StringValue("string value"));
-
- mock_pref_hash_store_->SetCheckResult(tracked_path_,
- PrefHashStore::CHANGED);
+ // Ownership of these values is transfered to |pref_store_contents_|.
+ base::StringValue* string_value = new base::StringValue("string value");
+ base::DictionaryValue* dict_value = new base::DictionaryValue;
+ dict_value->SetString("a", "foo");
+ dict_value->SetInteger("b", 1234);
+ pref_store_contents_.Set(kAtomicPref, string_value);
+ pref_store_contents_.Set(kSplitPref, dict_value);
+
+ mock_pref_hash_store_->SetCheckResult(kAtomicPref, PrefHashStore::CHANGED);
+ mock_pref_hash_store_->SetCheckResult(kSplitPref, PrefHashStore::CHANGED);
pref_hash_filter_->FilterOnLoad(&pref_store_contents_);
- ASSERT_LT(0u, mock_pref_hash_store_->checked_paths().size());
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths().size());
- ASSERT_EQ(tracked_path_, *mock_pref_hash_store_->stored_paths().begin());
- ASSERT_EQ("string value", mock_pref_hash_store_->stored_value(tracked_path_));
+ ASSERT_EQ(arraysize(kTestTrackedPrefs),
+ mock_pref_hash_store_->checked_paths_count());
+ ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
+
+ MockPrefHashStore::ValueStrategyPair stored_atomic_value =
+ mock_pref_hash_store_->stored_value(kAtomicPref);
+ ASSERT_EQ(string_value, stored_atomic_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
+ stored_atomic_value.second);
+
+ MockPrefHashStore::ValueStrategyPair stored_split_value =
+ mock_pref_hash_store_->stored_value(kSplitPref);
+ ASSERT_EQ(dict_value, stored_split_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT,
+ stored_split_value.second);
}
TEST_F(PrefHashFilterTest, EmptyCleared) {
- mock_pref_hash_store_->SetCheckResult(tracked_path_,
- PrefHashStore::CLEARED);
+ mock_pref_hash_store_->SetCheckResult(kAtomicPref, PrefHashStore::CLEARED);
+ mock_pref_hash_store_->SetCheckResult(kSplitPref, PrefHashStore::CLEARED);
pref_hash_filter_->FilterOnLoad(&pref_store_contents_);
- ASSERT_LT(0u, mock_pref_hash_store_->checked_paths().size());
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths().size());
- ASSERT_EQ(tracked_path_, *mock_pref_hash_store_->stored_paths().begin());
- ASSERT_EQ(MockPrefHashStore::kNullPlaceholder,
- mock_pref_hash_store_->stored_value(tracked_path_));
+ ASSERT_EQ(arraysize(kTestTrackedPrefs),
+ mock_pref_hash_store_->checked_paths_count());
+ ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
+
+ MockPrefHashStore::ValueStrategyPair stored_atomic_value =
+ mock_pref_hash_store_->stored_value(kAtomicPref);
+ ASSERT_EQ(NULL, stored_atomic_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
+ stored_atomic_value.second);
+
+ MockPrefHashStore::ValueStrategyPair stored_split_value =
+ mock_pref_hash_store_->stored_value(kSplitPref);
+ ASSERT_EQ(NULL, stored_split_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT,
+ stored_split_value.second);
}
TEST_F(PrefHashFilterTest, EmptyMigrated) {
- mock_pref_hash_store_->SetCheckResult(tracked_path_,
- PrefHashStore::MIGRATED);
+ mock_pref_hash_store_->SetCheckResult(kAtomicPref, PrefHashStore::MIGRATED);
+ mock_pref_hash_store_->SetCheckResult(kSplitPref, PrefHashStore::MIGRATED);
pref_hash_filter_->FilterOnLoad(&pref_store_contents_);
- ASSERT_LT(0u, mock_pref_hash_store_->checked_paths().size());
- ASSERT_EQ(1u, mock_pref_hash_store_->stored_paths().size());
- ASSERT_EQ(tracked_path_, *mock_pref_hash_store_->stored_paths().begin());
- ASSERT_EQ(MockPrefHashStore::kNullPlaceholder,
- mock_pref_hash_store_->stored_value(tracked_path_));
+ ASSERT_EQ(arraysize(kTestTrackedPrefs),
+ mock_pref_hash_store_->checked_paths_count());
+ ASSERT_EQ(2u, mock_pref_hash_store_->stored_paths_count());
+
+ MockPrefHashStore::ValueStrategyPair stored_atomic_value =
+ mock_pref_hash_store_->stored_value(kAtomicPref);
+ ASSERT_EQ(NULL, stored_atomic_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_ATOMIC,
+ stored_atomic_value.second);
+
+ MockPrefHashStore::ValueStrategyPair stored_split_value =
+ mock_pref_hash_store_->stored_value(kSplitPref);
+ ASSERT_EQ(NULL, stored_split_value.first);
+ ASSERT_EQ(PrefHashFilter::TRACKING_STRATEGY_SPLIT,
+ stored_split_value.second);
}

Powered by Google App Engine
This is Rietveld 408576698