Chromium Code Reviews| Index: components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs_unittest.cc |
| diff --git a/components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs_unittest.cc b/components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a540a603fd2aa9c9e3d12d47bf47354f6b96ae37 |
| --- /dev/null |
| +++ b/components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs_unittest.cc |
| @@ -0,0 +1,147 @@ |
| +// 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 "base/prefs/pref_registry_simple.h" |
| +#include "base/prefs/testing_pref_service.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/test/test_simple_task_runner.h" |
| +#include "components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs.h" |
| +#include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +const size_t kNumDaysInHistory = 60; |
|
bengr
2014/08/27 04:53:26
Add TODO to make such constants accessible by othe
megjablon
2014/08/28 20:44:08
Done.
|
| + |
| +int64 GetListPrefInt64Value( |
| + const base::ListValue& list_update, size_t index) { |
| + std::string string_value; |
| + EXPECT_TRUE(list_update.GetString(index, &string_value)); |
| + |
| + int64 value = 0; |
| + EXPECT_TRUE(base::StringToInt64(string_value, &value)); |
| + return value; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace data_reduction_proxy { |
| +class DataReductionProxyStatisticsPrefsTest : public testing::Test { |
| + protected: |
| + DataReductionProxyStatisticsPrefsTest() |
| + : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>( |
| + new base::TestSimpleTaskRunner())), |
| + pref_service_(new DataReductionProxyStatisticsPrefs( |
| + &simple_pref_service_, |
| + task_runner_, |
| + commit_delay_)) {} |
| + |
| + virtual void SetUp() OVERRIDE { |
| + PrefRegistrySimple* registry = simple_pref_service_.registry(); |
| + registry->RegisterInt64Pref( |
| + data_reduction_proxy::prefs::kHttpReceivedContentLength, 0); |
| + registry->RegisterInt64Pref( |
| + data_reduction_proxy::prefs::kHttpOriginalContentLength, 32); |
| + |
| + registry->RegisterListPref(data_reduction_proxy::prefs:: |
| + kDailyHttpOriginalContentLength); |
| + registry->RegisterListPref(data_reduction_proxy::prefs:: |
| + kDailyHttpReceivedContentLength); |
| + |
| +#if !(defined(OS_ANDROID) || defined(OS_IOS)) |
|
bengr
2014/08/27 04:53:26
Why is this here?
megjablon
2014/08/28 20:44:08
We only add the prefs to the pref map when we're n
|
| + pref_service_->InitInt64Pref(data_reduction_proxy::prefs:: |
| + kHttpReceivedContentLength); |
| + pref_service_->InitInt64Pref(data_reduction_proxy::prefs:: |
| + kHttpOriginalContentLength); |
| + pref_service_->InitListPref(data_reduction_proxy::prefs:: |
| + kDailyHttpOriginalContentLength); |
| + pref_service_->InitListPref(data_reduction_proxy::prefs:: |
| + kDailyHttpReceivedContentLength); |
| +#endif |
| + |
| + CreatePrefList( |
| + data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); |
| + CreatePrefList( |
| + data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); |
| + } |
| + |
| + // Create daily pref list of |kNumDaysInHistory| zero values. |
| + void CreatePrefList(const char* pref) { |
| + base::ListValue* update = pref_service_->GetList(pref); |
| + update->Clear(); |
| + for (size_t i = 0; i < kNumDaysInHistory; ++i) { |
| + update->Insert(0, new base::StringValue(base::Int64ToString(0))); |
| + } |
| + } |
| + |
| + // Verify the pref list values in |pref_service_| are equal to those in |
| + // |simple_pref_service| for |pref|. |
| + void VerifyPrefListWasWritten(const char* pref) { |
| + const base::ListValue* delayed_list = pref_service_->GetList(pref); |
| + const base::ListValue* written_list = simple_pref_service_.GetList(pref); |
| + ASSERT_EQ(delayed_list->GetSize(), written_list->GetSize()); |
| + size_t count = delayed_list->GetSize(); |
| + |
| + for (size_t i = 0; i < count; ++i) { |
| + EXPECT_EQ( |
| + GetListPrefInt64Value(*delayed_list, i), |
|
bengr
2014/08/27 04:53:26
Can this be moved up a line?
megjablon
2014/08/28 20:44:08
Done.
|
| + GetListPrefInt64Value(*written_list, i)); |
| + } |
| + } |
| + |
| + // Verify the pref value in |pref_service_| are equal to that in |
| + // |simple_pref_service|. |
| + void VerifyPrefWasWritten(const char* pref) { |
| + int64 delayed_pref = pref_service_->GetInt64(pref); |
| + int64 written_pref = simple_pref_service_.GetInt64(pref); |
| + EXPECT_EQ(delayed_pref, written_pref); |
| + } |
| + |
| +#if defined(OS_ANDROID) || defined(OS_IOS) |
| + base::TimeDelta commit_delay_ = base::TimeDelta(); |
| +#else |
| + base::TimeDelta commit_delay_ = base::TimeDelta::FromMinutes(60); |
| +#endif |
| + scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| + TestingPrefServiceSimple simple_pref_service_; |
| + DataReductionProxyStatisticsPrefs* pref_service_; |
| +}; |
| + |
| +TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefs) { |
| + const int64 kOriginalLength = 150; |
| + const int64 kReceivedLength = 100; |
| + |
| + pref_service_->SetInt64( |
| + data_reduction_proxy::prefs::kHttpOriginalContentLength, kOriginalLength); |
| + pref_service_->SetInt64( |
| + data_reduction_proxy::prefs::kHttpReceivedContentLength, kReceivedLength); |
| + |
| + base::ListValue* original_daily_content_length_list = pref_service_->GetList( |
| + data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); |
| + base::ListValue* received_daily_content_length_list = pref_service_->GetList( |
| + data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); |
| + |
| + for (size_t i = 0; i < kNumDaysInHistory; ++i) { |
| + original_daily_content_length_list->Set( |
| + i, new base::StringValue(base::Int64ToString(i))); |
| + } |
| + |
| + received_daily_content_length_list->Clear(); |
| + for (size_t i = 0; i < kNumDaysInHistory/2; ++i) { |
| + received_daily_content_length_list->Set( |
| + i, new base::StringValue(base::Int64ToString(i))); |
| + } |
| +#if !(defined(OS_ANDROID) || defined(OS_IOS)) |
| + task_runner_->RunPendingTasks(); |
| +#endif |
| + |
| + VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpOriginalContentLength); |
| + VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpReceivedContentLength); |
| + VerifyPrefListWasWritten( |
| + data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); |
| + VerifyPrefListWasWritten( |
| + data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); |
| +} |
| + |
| +} // namespace data_reduction_proxy |