Home | History | Annotate | Download | only in browser
      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 "base/memory/scoped_ptr.h"
      6 #include "base/prefs/pref_registry_simple.h"
      7 #include "base/prefs/testing_pref_service.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "base/test/test_simple_task_runner.h"
     10 #include "components/data_reduction_proxy/browser/data_reduction_proxy_prefs.h"
     11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs.h"
     12 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 
     15 namespace {
     16 
     17 // TODO Make kNumDaysInHistory accessible from DataReductionProxySettings.
     18 const size_t kNumDaysInHistory = 60;
     19 const int kWriteDelayMinutes = 60;
     20 
     21 int64 GetListPrefInt64Value(
     22     const base::ListValue& list_update, size_t index) {
     23   std::string string_value;
     24   EXPECT_TRUE(list_update.GetString(index, &string_value));
     25 
     26   int64 value = 0;
     27   EXPECT_TRUE(base::StringToInt64(string_value, &value));
     28   return value;
     29 }
     30 
     31 }  // namespace
     32 
     33 namespace data_reduction_proxy {
     34 
     35 class DataReductionProxyStatisticsPrefsTest : public testing::Test {
     36  protected:
     37   DataReductionProxyStatisticsPrefsTest()
     38       : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>(
     39             new base::TestSimpleTaskRunner())) {}
     40 
     41   virtual void SetUp() OVERRIDE {
     42     RegisterPrefs(simple_pref_service_.registry());
     43   }
     44 
     45   // Create daily pref list of |kNumDaysInHistory| zero values.
     46   void CreatePrefList(const char* pref) {
     47     base::ListValue* update = statistics_prefs_->GetList(pref);
     48     update->Clear();
     49     for (size_t i = 0; i < kNumDaysInHistory; ++i) {
     50       update->Insert(0, new base::StringValue(base::Int64ToString(0)));
     51     }
     52   }
     53 
     54   // Verify the pref list values in |pref_service_| are equal to those in
     55   // |simple_pref_service| for |pref|.
     56   void VerifyPrefListWasWritten(const char* pref) {
     57     const base::ListValue* delayed_list = statistics_prefs_->GetList(pref);
     58     const base::ListValue* written_list = simple_pref_service_.GetList(pref);
     59     ASSERT_EQ(delayed_list->GetSize(), written_list->GetSize());
     60     size_t count = delayed_list->GetSize();
     61 
     62     for (size_t i = 0; i < count; ++i) {
     63       EXPECT_EQ(GetListPrefInt64Value(*delayed_list, i),
     64                 GetListPrefInt64Value(*written_list, i));
     65     }
     66   }
     67 
     68   // Verify the pref value in |pref_service_| are equal to that in
     69   // |simple_pref_service|.
     70   void VerifyPrefWasWritten(const char* pref) {
     71     int64 delayed_pref = statistics_prefs_->GetInt64(pref);
     72     int64 written_pref = simple_pref_service_.GetInt64(pref);
     73     EXPECT_EQ(delayed_pref, written_pref);
     74   }
     75 
     76   scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
     77   TestingPrefServiceSimple simple_pref_service_;
     78   scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
     79 };
     80 
     81 TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefsDirect) {
     82   statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs(
     83       &simple_pref_service_,
     84       task_runner_,
     85       base::TimeDelta()));
     86   const int64 kOriginalLength = 150;
     87   const int64 kReceivedLength = 100;
     88 
     89   CreatePrefList(
     90       data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
     91   CreatePrefList(
     92       data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
     93 
     94   statistics_prefs_->SetInt64(
     95       data_reduction_proxy::prefs::kHttpOriginalContentLength, kOriginalLength);
     96   statistics_prefs_->SetInt64(
     97       data_reduction_proxy::prefs::kHttpReceivedContentLength, kReceivedLength);
     98 
     99   base::ListValue* original_daily_content_length_list =
    100       statistics_prefs_->GetList(data_reduction_proxy::prefs::
    101                                  kDailyHttpOriginalContentLength);
    102   base::ListValue* received_daily_content_length_list =
    103       statistics_prefs_->GetList(data_reduction_proxy::prefs::
    104                                  kDailyHttpReceivedContentLength);
    105 
    106   for (size_t i = 0; i < kNumDaysInHistory; ++i) {
    107     original_daily_content_length_list->Set(
    108         i, new base::StringValue(base::Int64ToString(i)));
    109   }
    110 
    111   received_daily_content_length_list->Clear();
    112   for (size_t i = 0; i < kNumDaysInHistory/2; ++i) {
    113     received_daily_content_length_list->Set(
    114         i, new base::StringValue(base::Int64ToString(i)));
    115   }
    116 
    117   VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpOriginalContentLength);
    118   VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpReceivedContentLength);
    119   VerifyPrefListWasWritten(
    120       data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
    121   VerifyPrefListWasWritten(
    122       data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
    123 }
    124 
    125 TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefsDelayed) {
    126   statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs(
    127       &simple_pref_service_,
    128       task_runner_,
    129       base::TimeDelta::FromMinutes(kWriteDelayMinutes)));
    130   statistics_prefs_->Init();
    131 
    132   CreatePrefList(
    133       data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
    134   CreatePrefList(
    135       data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
    136 
    137   const int64 kOriginalLength = 150;
    138   const int64 kReceivedLength = 100;
    139 
    140   statistics_prefs_->SetInt64(
    141       data_reduction_proxy::prefs::kHttpOriginalContentLength, kOriginalLength);
    142   statistics_prefs_->SetInt64(
    143       data_reduction_proxy::prefs::kHttpReceivedContentLength, kReceivedLength);
    144 
    145   base::ListValue* original_daily_content_length_list =
    146       statistics_prefs_->GetList(data_reduction_proxy::prefs::
    147                                  kDailyHttpOriginalContentLength);
    148   base::ListValue* received_daily_content_length_list =
    149       statistics_prefs_->GetList(data_reduction_proxy::prefs::
    150                                  kDailyHttpReceivedContentLength);
    151 
    152   for (size_t i = 0; i < kNumDaysInHistory; ++i) {
    153     original_daily_content_length_list->Set(
    154         i, new base::StringValue(base::Int64ToString(i)));
    155   }
    156 
    157   received_daily_content_length_list->Clear();
    158   for (size_t i = 0; i < kNumDaysInHistory/2; ++i) {
    159     received_daily_content_length_list->Set(
    160         i, new base::StringValue(base::Int64ToString(i)));
    161   }
    162 
    163   task_runner_->RunPendingTasks();
    164 
    165   VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpOriginalContentLength);
    166   VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpReceivedContentLength);
    167   VerifyPrefListWasWritten(
    168       data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
    169   VerifyPrefListWasWritten(
    170       data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
    171 }
    172 
    173 }  // namespace data_reduction_proxy
    174