Chromium Code Reviews| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 | 51 |
| 52 // Overrides the random behavior in DataReductionProxyPingbackClient. If | |
|
tbansal1
2016/07/15 17:44:04
s/random behavior/behavior of random float generat
RyanSturm
2016/07/15 18:16:03
Done.
| |
| 53 // |should_override_random| is true, the typically random value that is | |
| 54 // compared with reporting fraction will deterministically be | |
| 55 // |override_value|. | |
| 52 void OverrideRandom(bool should_override_random, float override_value) { | 56 void OverrideRandom(bool should_override_random, float override_value) { |
| 53 should_override_random_ = should_override_random; | 57 should_override_random_ = should_override_random; |
| 54 override_value_ = override_value; | 58 override_value_ = override_value; |
| 55 } | 59 } |
| 56 | 60 |
| 61 // Sets the time used for the metrics reporting time. | |
| 62 void set_current_time(base::Time current_time) { | |
| 63 current_time_ = current_time; | |
| 64 } | |
| 65 | |
| 57 private: | 66 private: |
| 58 float GenerateRandomFloat() const override { | 67 float GenerateRandomFloat() const override { |
| 59 if (should_override_random_) | 68 if (should_override_random_) |
| 60 return override_value_; | 69 return override_value_; |
| 61 return DataReductionProxyPingbackClient::GenerateRandomFloat(); | 70 return DataReductionProxyPingbackClient::GenerateRandomFloat(); |
| 62 } | 71 } |
| 63 | 72 |
| 73 base::Time CurrentTime() const override { return current_time_; } | |
| 74 | |
| 75 base::Time current_time_; | |
| 64 bool should_override_random_; | 76 bool should_override_random_; |
|
tbansal1
2016/07/15 17:44:04
Try to keep them in order. e.g., randomness relate
RyanSturm
2016/07/15 18:16:03
Done.
| |
| 65 float override_value_; | 77 float override_value_; |
| 66 }; | 78 }; |
| 67 | 79 |
| 68 class DataReductionProxyPingbackClientTest : public testing::Test { | 80 class DataReductionProxyPingbackClientTest : public testing::Test { |
| 69 public: | 81 public: |
| 70 DataReductionProxyPingbackClientTest() | 82 DataReductionProxyPingbackClientTest() |
| 71 : timing_(base::Time::FromJsTime(1500), | 83 : timing_(base::Time::FromJsTime(1500), |
| 72 base::Optional<base::TimeDelta>( | 84 base::Optional<base::TimeDelta>( |
| 73 base::TimeDelta::FromMilliseconds(1600)), | 85 base::TimeDelta::FromMilliseconds(1600)), |
| 74 base::Optional<base::TimeDelta>( | 86 base::Optional<base::TimeDelta>( |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 net::TestURLFetcherFactory factory_; | 123 net::TestURLFetcherFactory factory_; |
| 112 DataReductionProxyPageLoadTiming timing_; | 124 DataReductionProxyPageLoadTiming timing_; |
| 113 base::HistogramTester histogram_tester_; | 125 base::HistogramTester histogram_tester_; |
| 114 }; | 126 }; |
| 115 | 127 |
| 116 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { | 128 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { |
| 117 Init(); | 129 Init(); |
| 118 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 130 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 119 pingback_client()->OverrideRandom(true, 0.5f); | 131 pingback_client()->OverrideRandom(true, 0.5f); |
| 120 pingback_client()->SetPingbackReportingFraction(1.0f); | 132 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 133 base::Time current_time = base::Time::UnixEpoch(); | |
| 134 pingback_client()->set_current_time(current_time); | |
| 121 CreateAndSendPingback(); | 135 CreateAndSendPingback(); |
| 122 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); | 136 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
| 123 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 137 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 124 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); | 138 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 125 RecordPageloadMetricsRequest batched_request; | 139 RecordPageloadMetricsRequest batched_request; |
| 126 batched_request.ParseFromString(test_fetcher->upload_data()); | 140 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 141 EXPECT_EQ(current_time, protobuf_parser::TimestampToTime( | |
| 142 batched_request.metrics_sent_time())); | |
| 127 PageloadMetrics pageload_metrics = batched_request.pageloads(0); | 143 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
| 128 EXPECT_EQ( | 144 EXPECT_EQ( |
| 129 timing().navigation_start, | 145 timing().navigation_start, |
| 130 protobuf_parser::TimestampToTime(pageload_metrics.first_request_time())); | 146 protobuf_parser::TimestampToTime(pageload_metrics.first_request_time())); |
| 131 EXPECT_EQ(timing().response_start.value(), | 147 EXPECT_EQ(timing().response_start.value(), |
| 132 protobuf_parser::DurationToTimeDelta( | 148 protobuf_parser::DurationToTimeDelta( |
| 133 pageload_metrics.time_to_first_byte())); | 149 pageload_metrics.time_to_first_byte())); |
| 134 EXPECT_EQ( | 150 EXPECT_EQ( |
| 135 timing().load_event_start.value(), | 151 timing().load_event_start.value(), |
| 136 protobuf_parser::DurationToTimeDelta(pageload_metrics.page_load_time())); | 152 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); | 252 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 237 EXPECT_TRUE(test_fetcher); | 253 EXPECT_TRUE(test_fetcher); |
| 238 // Simulate a network error. | 254 // Simulate a network error. |
| 239 test_fetcher->set_status(net::URLRequestStatus( | 255 test_fetcher->set_status(net::URLRequestStatus( |
| 240 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS)); | 256 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS)); |
| 241 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 257 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 242 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1); | 258 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1); |
| 243 } | 259 } |
| 244 | 260 |
| 245 } // namespace data_reduction_proxy | 261 } // namespace data_reduction_proxy |
| OLD | NEW |