| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 // Controls whether a pingback is sent or not. | 42 // Controls whether a pingback is sent or not. |
| 43 class TestDataReductionProxyPingbackClient | 43 class TestDataReductionProxyPingbackClient |
| 44 : public DataReductionProxyPingbackClient { | 44 : public DataReductionProxyPingbackClient { |
| 45 public: | 45 public: |
| 46 TestDataReductionProxyPingbackClient( | 46 TestDataReductionProxyPingbackClient( |
| 47 net::URLRequestContextGetter* url_request_context_getter) | 47 net::URLRequestContextGetter* url_request_context_getter) |
| 48 : DataReductionProxyPingbackClient(url_request_context_getter), | 48 : DataReductionProxyPingbackClient(url_request_context_getter), |
| 49 should_override_random_(false), | 49 should_override_random_(false), |
| 50 override_value_(0.0f) {} | 50 override_value_(0.0f), |
| 51 current_time_(base::Time::Now()) {} |
| 51 | 52 |
| 53 // Overrides the bahvior of the random float generator in |
| 54 // DataReductionProxyPingbackClient. |
| 55 // If |should_override_random| is true, the typically random value that is |
| 56 // compared with reporting fraction will deterministically be |
| 57 // |override_value|. |
| 52 void OverrideRandom(bool should_override_random, float override_value) { | 58 void OverrideRandom(bool should_override_random, float override_value) { |
| 53 should_override_random_ = should_override_random; | 59 should_override_random_ = should_override_random; |
| 54 override_value_ = override_value; | 60 override_value_ = override_value; |
| 55 } | 61 } |
| 56 | 62 |
| 63 // Sets the time used for the metrics reporting time. |
| 64 void set_current_time(base::Time current_time) { |
| 65 current_time_ = current_time; |
| 66 } |
| 67 |
| 57 private: | 68 private: |
| 58 float GenerateRandomFloat() const override { | 69 float GenerateRandomFloat() const override { |
| 59 if (should_override_random_) | 70 if (should_override_random_) |
| 60 return override_value_; | 71 return override_value_; |
| 61 return DataReductionProxyPingbackClient::GenerateRandomFloat(); | 72 return DataReductionProxyPingbackClient::GenerateRandomFloat(); |
| 62 } | 73 } |
| 63 | 74 |
| 75 base::Time CurrentTime() const override { return current_time_; } |
| 76 |
| 64 bool should_override_random_; | 77 bool should_override_random_; |
| 65 float override_value_; | 78 float override_value_; |
| 79 base::Time current_time_; |
| 66 }; | 80 }; |
| 67 | 81 |
| 68 class DataReductionProxyPingbackClientTest : public testing::Test { | 82 class DataReductionProxyPingbackClientTest : public testing::Test { |
| 69 public: | 83 public: |
| 70 DataReductionProxyPingbackClientTest() | 84 DataReductionProxyPingbackClientTest() |
| 71 : timing_(base::Time::FromJsTime(1500), | 85 : timing_(base::Time::FromJsTime(1500), |
| 72 base::Optional<base::TimeDelta>( | 86 base::Optional<base::TimeDelta>( |
| 73 base::TimeDelta::FromMilliseconds(1600)), | 87 base::TimeDelta::FromMilliseconds(1600)), |
| 74 base::Optional<base::TimeDelta>( | 88 base::Optional<base::TimeDelta>( |
| 75 base::TimeDelta::FromMilliseconds(1700)), | 89 base::TimeDelta::FromMilliseconds(1700)), |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 net::TestURLFetcherFactory factory_; | 125 net::TestURLFetcherFactory factory_; |
| 112 DataReductionProxyPageLoadTiming timing_; | 126 DataReductionProxyPageLoadTiming timing_; |
| 113 base::HistogramTester histogram_tester_; | 127 base::HistogramTester histogram_tester_; |
| 114 }; | 128 }; |
| 115 | 129 |
| 116 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { | 130 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { |
| 117 Init(); | 131 Init(); |
| 118 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 132 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 119 pingback_client()->OverrideRandom(true, 0.5f); | 133 pingback_client()->OverrideRandom(true, 0.5f); |
| 120 pingback_client()->SetPingbackReportingFraction(1.0f); | 134 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 135 base::Time current_time = base::Time::UnixEpoch(); |
| 136 pingback_client()->set_current_time(current_time); |
| 121 CreateAndSendPingback(); | 137 CreateAndSendPingback(); |
| 122 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); | 138 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
| 123 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 139 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 124 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); | 140 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 125 RecordPageloadMetricsRequest batched_request; | 141 RecordPageloadMetricsRequest batched_request; |
| 126 batched_request.ParseFromString(test_fetcher->upload_data()); | 142 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 143 EXPECT_EQ(current_time, protobuf_parser::TimestampToTime( |
| 144 batched_request.metrics_sent_time())); |
| 127 PageloadMetrics pageload_metrics = batched_request.pageloads(0); | 145 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
| 128 EXPECT_EQ( | 146 EXPECT_EQ( |
| 129 timing().navigation_start, | 147 timing().navigation_start, |
| 130 protobuf_parser::TimestampToTime(pageload_metrics.first_request_time())); | 148 protobuf_parser::TimestampToTime(pageload_metrics.first_request_time())); |
| 131 EXPECT_EQ(timing().response_start.value(), | 149 EXPECT_EQ(timing().response_start.value(), |
| 132 protobuf_parser::DurationToTimeDelta( | 150 protobuf_parser::DurationToTimeDelta( |
| 133 pageload_metrics.time_to_first_byte())); | 151 pageload_metrics.time_to_first_byte())); |
| 134 EXPECT_EQ( | 152 EXPECT_EQ( |
| 135 timing().load_event_start.value(), | 153 timing().load_event_start.value(), |
| 136 protobuf_parser::DurationToTimeDelta(pageload_metrics.page_load_time())); | 154 protobuf_parser::DurationToTimeDelta(pageload_metrics.page_load_time())); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 254 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 237 EXPECT_TRUE(test_fetcher); | 255 EXPECT_TRUE(test_fetcher); |
| 238 // Simulate a network error. | 256 // Simulate a network error. |
| 239 test_fetcher->set_status(net::URLRequestStatus( | 257 test_fetcher->set_status(net::URLRequestStatus( |
| 240 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS)); | 258 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS)); |
| 241 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 259 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 242 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1); | 260 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1); |
| 243 } | 261 } |
| 244 | 262 |
| 245 } // namespace data_reduction_proxy | 263 } // namespace data_reduction_proxy |
| OLD | NEW |