| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_usage/core/data_use_aggregator.h" | 5 #include "components/data_usage/core/data_use_aggregator.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "components/data_usage/core/data_use.h" | 17 #include "components/data_usage/core/data_use.h" |
| 18 #include "components/data_usage/core/data_use_amortizer.h" |
| 18 #include "components/data_usage/core/data_use_annotator.h" | 19 #include "components/data_usage/core/data_use_annotator.h" |
| 19 #include "net/base/load_timing_info.h" | 20 #include "net/base/load_timing_info.h" |
| 20 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
| 21 #include "net/base/network_delegate_impl.h" | 22 #include "net/base/network_delegate_impl.h" |
| 22 #include "net/socket/socket_test_util.h" | 23 #include "net/socket/socket_test_util.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 namespace data_usage { | 29 namespace data_usage { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 base::TimeTicks GetRequestStart(const net::URLRequest& request) { | 33 base::TimeTicks GetRequestStart(const net::URLRequest& request) { |
| 33 net::LoadTimingInfo load_timing_info; | 34 net::LoadTimingInfo load_timing_info; |
| 34 request.GetLoadTimingInfo(&load_timing_info); | 35 request.GetLoadTimingInfo(&load_timing_info); |
| 35 return load_timing_info.request_start; | 36 return load_timing_info.request_start; |
| 36 } | 37 } |
| 37 | 38 |
| 38 // Test class that can set the network operator's MCCMNC. | 39 // Test class that can set the network operator's MCCMNC. |
| 39 class TestDataUseAggregator : public DataUseAggregator { | 40 class TestDataUseAggregator : public DataUseAggregator { |
| 40 public: | 41 public: |
| 41 TestDataUseAggregator(scoped_ptr<DataUseAnnotator> annotator) | 42 TestDataUseAggregator(scoped_ptr<DataUseAnnotator> annotator, |
| 42 : DataUseAggregator(annotator.Pass()) {} | 43 scoped_ptr<DataUseAmortizer> amortizer) |
| 44 : DataUseAggregator(annotator.Pass(), amortizer.Pass()) {} |
| 43 | 45 |
| 44 ~TestDataUseAggregator() override {} | 46 ~TestDataUseAggregator() override {} |
| 45 | 47 |
| 46 private: | 48 private: |
| 47 friend class TestNetworkChangeNotifier; | 49 friend class TestNetworkChangeNotifier; |
| 48 using DataUseAggregator::OnConnectionTypeChanged; | 50 using DataUseAggregator::OnConnectionTypeChanged; |
| 49 using DataUseAggregator::SetMccMncForTests; | 51 using DataUseAggregator::SetMccMncForTests; |
| 50 }; | 52 }; |
| 51 | 53 |
| 52 // Override NetworkChangeNotifier to simulate connection type changes for tests. | 54 // Override NetworkChangeNotifier to simulate connection type changes for tests. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } | 97 } |
| 96 | 98 |
| 97 void set_tab_id(int32_t tab_id) { tab_id_ = tab_id; } | 99 void set_tab_id(int32_t tab_id) { tab_id_ = tab_id; } |
| 98 | 100 |
| 99 private: | 101 private: |
| 100 int32_t tab_id_; | 102 int32_t tab_id_; |
| 101 | 103 |
| 102 DISALLOW_COPY_AND_ASSIGN(FakeDataUseAnnotator); | 104 DISALLOW_COPY_AND_ASSIGN(FakeDataUseAnnotator); |
| 103 }; | 105 }; |
| 104 | 106 |
| 107 // Test DataUseAmortizer that doubles the bytes of all DataUse objects it sees. |
| 108 class DoublingAmortizer : public DataUseAmortizer { |
| 109 public: |
| 110 DoublingAmortizer() {} |
| 111 ~DoublingAmortizer() override {} |
| 112 |
| 113 void AmortizeDataUse(scoped_ptr<DataUse> data_use, |
| 114 const AmortizationCompleteCallback& callback) override { |
| 115 data_use->tx_bytes *= 2; |
| 116 data_use->rx_bytes *= 2; |
| 117 callback.Run(data_use.Pass()); |
| 118 } |
| 119 |
| 120 void OnExtraBytes(int64_t extra_tx_bytes, int64_t extra_rx_bytes) override {} |
| 121 |
| 122 private: |
| 123 DISALLOW_COPY_AND_ASSIGN(DoublingAmortizer); |
| 124 }; |
| 125 |
| 105 // A network delegate that reports all received and sent network bytes to a | 126 // A network delegate that reports all received and sent network bytes to a |
| 106 // DataUseAggregator. | 127 // DataUseAggregator. |
| 107 class ReportingNetworkDelegate : public net::NetworkDelegateImpl { | 128 class ReportingNetworkDelegate : public net::NetworkDelegateImpl { |
| 108 public: | 129 public: |
| 109 // The simulated context for the data usage of a net::URLRequest. | 130 // The simulated context for the data usage of a net::URLRequest. |
| 110 struct DataUseContext { | 131 struct DataUseContext { |
| 111 DataUseContext() | 132 DataUseContext() |
| 112 : tab_id(-1), | 133 : tab_id(-1), |
| 113 connection_type(net::NetworkChangeNotifier::CONNECTION_UNKNOWN) {} | 134 connection_type(net::NetworkChangeNotifier::CONNECTION_UNKNOWN) {} |
| 114 | 135 |
| 115 DataUseContext(int32_t tab_id, | 136 DataUseContext(int32_t tab_id, |
| 116 net::NetworkChangeNotifier::ConnectionType connection_type, | 137 net::NetworkChangeNotifier::ConnectionType connection_type, |
| 117 const std::string& mcc_mnc) | 138 const std::string& mcc_mnc) |
| 118 : tab_id(tab_id), connection_type(connection_type), mcc_mnc(mcc_mnc) {} | 139 : tab_id(tab_id), connection_type(connection_type), mcc_mnc(mcc_mnc) {} |
| 119 | 140 |
| 120 int32_t tab_id; | 141 int32_t tab_id; |
| 121 net::NetworkChangeNotifier::ConnectionType connection_type; | 142 net::NetworkChangeNotifier::ConnectionType connection_type; |
| 122 std::string mcc_mnc; | 143 std::string mcc_mnc; |
| 123 }; | 144 }; |
| 124 | 145 |
| 125 typedef std::map<const net::URLRequest*, DataUseContext> DataUseContextMap; | 146 typedef std::map<const net::URLRequest*, DataUseContext> DataUseContextMap; |
| 126 | 147 |
| 148 // Constructs a ReportingNetworkDelegate. |fake_data_use_annotator| can be |
| 149 // NULL, indicating that no annotator is in use and no requests should be |
| 150 // annotated with tab IDs. |
| 127 ReportingNetworkDelegate( | 151 ReportingNetworkDelegate( |
| 128 TestDataUseAggregator* data_use_aggregator, | 152 TestDataUseAggregator* data_use_aggregator, |
| 129 FakeDataUseAnnotator* fake_data_use_annotator, | 153 FakeDataUseAnnotator* fake_data_use_annotator, |
| 130 TestNetworkChangeNotifier* test_network_change_notifier) | 154 TestNetworkChangeNotifier* test_network_change_notifier) |
| 131 : data_use_aggregator_(data_use_aggregator), | 155 : data_use_aggregator_(data_use_aggregator), |
| 132 fake_data_use_annotator_(fake_data_use_annotator), | 156 fake_data_use_annotator_(fake_data_use_annotator), |
| 133 test_network_change_notifier_(test_network_change_notifier) {} | 157 test_network_change_notifier_(test_network_change_notifier) {} |
| 134 | 158 |
| 135 ~ReportingNetworkDelegate() override {} | 159 ~ReportingNetworkDelegate() override {} |
| 136 | 160 |
| 137 void set_data_use_context_map(const DataUseContextMap& data_use_context_map) { | 161 void set_data_use_context_map(const DataUseContextMap& data_use_context_map) { |
| 138 data_use_context_map_ = data_use_context_map; | 162 data_use_context_map_ = data_use_context_map; |
| 139 } | 163 } |
| 140 | 164 |
| 141 private: | 165 private: |
| 142 void UpdateDataUseContext(const net::URLRequest& request) { | 166 void UpdateDataUseContext(const net::URLRequest& request) { |
| 143 DataUseContextMap::const_iterator data_use_context_it = | 167 DataUseContextMap::const_iterator data_use_context_it = |
| 144 data_use_context_map_.find(&request); | 168 data_use_context_map_.find(&request); |
| 145 DataUseContext data_use_context = | 169 DataUseContext data_use_context = |
| 146 data_use_context_it == data_use_context_map_.end() | 170 data_use_context_it == data_use_context_map_.end() |
| 147 ? DataUseContext() | 171 ? DataUseContext() |
| 148 : data_use_context_it->second; | 172 : data_use_context_it->second; |
| 149 | 173 |
| 150 fake_data_use_annotator_->set_tab_id(data_use_context.tab_id); | 174 if (fake_data_use_annotator_) |
| 175 fake_data_use_annotator_->set_tab_id(data_use_context.tab_id); |
| 151 | 176 |
| 152 if (test_network_change_notifier_->GetCurrentConnectionType() != | 177 if (test_network_change_notifier_->GetCurrentConnectionType() != |
| 153 data_use_context.connection_type) { | 178 data_use_context.connection_type) { |
| 154 test_network_change_notifier_->SimulateNetworkConnectionChange( | 179 test_network_change_notifier_->SimulateNetworkConnectionChange( |
| 155 data_use_context.connection_type, data_use_context.mcc_mnc); | 180 data_use_context.connection_type, data_use_context.mcc_mnc); |
| 156 } | 181 } |
| 157 } | 182 } |
| 158 | 183 |
| 159 void OnNetworkBytesReceived(net::URLRequest* request, | 184 void OnNetworkBytesReceived(net::URLRequest* request, |
| 160 int64_t bytes_received) override { | 185 int64_t bytes_received) override { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 private: | 229 private: |
| 205 DataUseAggregator* data_use_aggregator_; | 230 DataUseAggregator* data_use_aggregator_; |
| 206 std::vector<DataUse> observed_data_use_; | 231 std::vector<DataUse> observed_data_use_; |
| 207 int on_data_use_called_count_; | 232 int on_data_use_called_count_; |
| 208 | 233 |
| 209 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 234 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 210 }; | 235 }; |
| 211 | 236 |
| 212 class DataUseAggregatorTest : public testing::Test { | 237 class DataUseAggregatorTest : public testing::Test { |
| 213 public: | 238 public: |
| 214 DataUseAggregatorTest() | 239 DataUseAggregatorTest() {} |
| 215 : fake_data_use_annotator_(new FakeDataUseAnnotator()), | 240 ~DataUseAggregatorTest() override {} |
| 216 data_use_aggregator_( | 241 |
| 217 scoped_ptr<DataUseAnnotator>(fake_data_use_annotator_)), | 242 void Initialize(scoped_ptr<FakeDataUseAnnotator> annotator, |
| 218 test_network_change_notifier_(&data_use_aggregator_), | 243 scoped_ptr<DataUseAmortizer> amortizer) { |
| 219 reporting_network_delegate_(&data_use_aggregator_, | 244 // Destroy objects that have dependencies on other objects here in the |
| 220 fake_data_use_annotator_, | 245 // reverse order that they are created. |
| 221 &test_network_change_notifier_), | 246 context_.reset(); |
| 222 context_(true), | 247 reporting_network_delegate_.reset(); |
| 223 test_observer_(&data_use_aggregator_) { | 248 mock_socket_factory_.reset(); |
| 224 context_.set_client_socket_factory(&mock_socket_factory_); | 249 test_network_change_notifier_.reset(); |
| 225 context_.set_network_delegate(&reporting_network_delegate_); | 250 test_observer_.reset(); |
| 226 context_.Init(); | 251 |
| 252 // Initialize testing objects. |
| 253 FakeDataUseAnnotator* fake_data_use_annotator = annotator.get(); |
| 254 data_use_aggregator_.reset( |
| 255 new TestDataUseAggregator(annotator.Pass(), amortizer.Pass())); |
| 256 test_observer_.reset(new TestObserver(data_use_aggregator_.get())); |
| 257 test_network_change_notifier_.reset( |
| 258 new TestNetworkChangeNotifier(data_use_aggregator_.get())); |
| 259 mock_socket_factory_.reset(new net::MockClientSocketFactory()); |
| 260 reporting_network_delegate_.reset(new ReportingNetworkDelegate( |
| 261 data_use_aggregator_.get(), fake_data_use_annotator, |
| 262 test_network_change_notifier_.get())); |
| 263 |
| 264 context_.reset(new net::TestURLRequestContext(true)); |
| 265 context_->set_client_socket_factory(mock_socket_factory_.get()); |
| 266 context_->set_network_delegate(reporting_network_delegate_.get()); |
| 267 context_->Init(); |
| 227 } | 268 } |
| 228 | 269 |
| 229 ~DataUseAggregatorTest() override {} | |
| 230 | |
| 231 scoped_ptr<net::URLRequest> ExecuteRequest( | 270 scoped_ptr<net::URLRequest> ExecuteRequest( |
| 232 const GURL& url, | 271 const GURL& url, |
| 233 const GURL& first_party_for_cookies, | 272 const GURL& first_party_for_cookies, |
| 234 int32_t tab_id, | 273 int32_t tab_id, |
| 235 net::NetworkChangeNotifier::ConnectionType connection_type, | 274 net::NetworkChangeNotifier::ConnectionType connection_type, |
| 236 const std::string& mcc_mnc) { | 275 const std::string& mcc_mnc) { |
| 237 net::MockRead reads[] = { | 276 net::MockRead reads[] = { |
| 238 net::MockRead("HTTP/1.1 200 OK\r\n\r\n"), net::MockRead("hello world"), | 277 net::MockRead("HTTP/1.1 200 OK\r\n\r\n"), net::MockRead("hello world"), |
| 239 net::MockRead(net::SYNCHRONOUS, net::OK), | 278 net::MockRead(net::SYNCHRONOUS, net::OK), |
| 240 }; | 279 }; |
| 241 net::StaticSocketDataProvider socket(reads, arraysize(reads), nullptr, 0); | 280 net::StaticSocketDataProvider socket(reads, arraysize(reads), nullptr, 0); |
| 242 mock_socket_factory_.AddSocketDataProvider(&socket); | 281 mock_socket_factory_->AddSocketDataProvider(&socket); |
| 243 | 282 |
| 244 net::TestDelegate delegate; | 283 net::TestDelegate delegate; |
| 245 scoped_ptr<net::URLRequest> request = | 284 scoped_ptr<net::URLRequest> request = |
| 246 context_.CreateRequest(url, net::IDLE, &delegate); | 285 context_->CreateRequest(url, net::IDLE, &delegate); |
| 247 request->set_first_party_for_cookies(first_party_for_cookies); | 286 request->set_first_party_for_cookies(first_party_for_cookies); |
| 248 | 287 |
| 249 ReportingNetworkDelegate::DataUseContextMap data_use_context_map; | 288 ReportingNetworkDelegate::DataUseContextMap data_use_context_map; |
| 250 data_use_context_map[request.get()] = | 289 data_use_context_map[request.get()] = |
| 251 ReportingNetworkDelegate::DataUseContext(tab_id, connection_type, | 290 ReportingNetworkDelegate::DataUseContext(tab_id, connection_type, |
| 252 mcc_mnc); | 291 mcc_mnc); |
| 253 reporting_network_delegate_.set_data_use_context_map(data_use_context_map); | 292 reporting_network_delegate_->set_data_use_context_map(data_use_context_map); |
| 254 | 293 |
| 255 request->Start(); | 294 request->Start(); |
| 256 loop_.RunUntilIdle(); | 295 loop_.RunUntilIdle(); |
| 257 | 296 |
| 258 return request.Pass(); | 297 return request.Pass(); |
| 259 } | 298 } |
| 260 | 299 |
| 261 ReportingNetworkDelegate* reporting_network_delegate() { | 300 ReportingNetworkDelegate* reporting_network_delegate() { |
| 262 return &reporting_network_delegate_; | 301 return reporting_network_delegate_.get(); |
| 263 } | 302 } |
| 264 | 303 |
| 265 DataUseAggregator* data_use_aggregator() { return &data_use_aggregator_; } | 304 DataUseAggregator* data_use_aggregator() { |
| 305 return data_use_aggregator_.get(); |
| 306 } |
| 266 | 307 |
| 267 net::MockClientSocketFactory* mock_socket_factory() { | 308 net::MockClientSocketFactory* mock_socket_factory() { |
| 268 return &mock_socket_factory_; | 309 return mock_socket_factory_.get(); |
| 269 } | 310 } |
| 270 | 311 |
| 271 net::TestURLRequestContext* context() { return &context_; } | 312 net::TestURLRequestContext* context() { return context_.get(); } |
| 272 | 313 |
| 273 TestObserver* test_observer() { return &test_observer_; } | 314 TestObserver* test_observer() { return test_observer_.get(); } |
| 274 | 315 |
| 275 private: | 316 private: |
| 276 base::MessageLoopForIO loop_; | 317 base::MessageLoopForIO loop_; |
| 277 // Weak, owned by |data_use_aggregator_|. | 318 scoped_ptr<TestDataUseAggregator> data_use_aggregator_; |
| 278 FakeDataUseAnnotator* fake_data_use_annotator_; | 319 scoped_ptr<TestObserver> test_observer_; |
| 279 TestDataUseAggregator data_use_aggregator_; | 320 scoped_ptr<TestNetworkChangeNotifier> test_network_change_notifier_; |
| 280 TestNetworkChangeNotifier test_network_change_notifier_; | 321 scoped_ptr<net::MockClientSocketFactory> mock_socket_factory_; |
| 281 net::MockClientSocketFactory mock_socket_factory_; | 322 scoped_ptr<ReportingNetworkDelegate> reporting_network_delegate_; |
| 282 ReportingNetworkDelegate reporting_network_delegate_; | 323 scoped_ptr<net::TestURLRequestContext> context_; |
| 283 net::TestURLRequestContext context_; | |
| 284 TestObserver test_observer_; | |
| 285 | 324 |
| 286 DISALLOW_COPY_AND_ASSIGN(DataUseAggregatorTest); | 325 DISALLOW_COPY_AND_ASSIGN(DataUseAggregatorTest); |
| 287 }; | 326 }; |
| 288 | 327 |
| 289 TEST_F(DataUseAggregatorTest, ReportDataUse) { | 328 TEST_F(DataUseAggregatorTest, ReportDataUse) { |
| 290 const int32_t kFooTabId = 10; | 329 const struct { |
| 291 const net::NetworkChangeNotifier::ConnectionType kFooConnectionType = | 330 bool use_annotator; |
| 292 net::NetworkChangeNotifier::CONNECTION_2G; | 331 bool use_amortizer; |
| 293 const std::string kFooMccMnc = "foo_mcc_mnc"; | 332 bool expect_tab_ids; |
| 294 scoped_ptr<net::URLRequest> foo_request = | 333 int64_t expected_amortization_multiple; |
| 295 ExecuteRequest(GURL("http://foo.com"), GURL("http://foofirstparty.com"), | 334 } kTestCases[] = { |
| 296 kFooTabId, kFooConnectionType, kFooMccMnc); | 335 {false, false, false, 1}, |
| 336 {false, true, false, 2}, |
| 337 {true, false, true, 1}, |
| 338 {true, true, true, 2}, |
| 339 }; |
| 297 | 340 |
| 298 const int32_t kBarTabId = 20; | 341 for (const auto& test_case : kTestCases) { |
| 299 const net::NetworkChangeNotifier::ConnectionType kBarConnectionType = | 342 scoped_ptr<FakeDataUseAnnotator> annotator( |
| 300 net::NetworkChangeNotifier::CONNECTION_WIFI; | 343 test_case.use_annotator ? new FakeDataUseAnnotator() : nullptr); |
| 301 const std::string kBarMccMnc = "bar_mcc_mnc"; | 344 scoped_ptr<DataUseAmortizer> amortizer( |
| 302 scoped_ptr<net::URLRequest> bar_request = | 345 test_case.use_amortizer ? new DoublingAmortizer() : nullptr); |
| 303 ExecuteRequest(GURL("http://bar.com"), GURL("http://barfirstparty.com"), | |
| 304 kBarTabId, kBarConnectionType, kBarMccMnc); | |
| 305 | 346 |
| 306 auto data_use_it = test_observer()->observed_data_use().begin(); | 347 Initialize(annotator.Pass(), amortizer.Pass()); |
| 307 | 348 |
| 308 // First, the |foo_request| data use should have happened. | 349 const int32_t kFooTabId = 10; |
| 309 int64_t observed_foo_tx_bytes = 0, observed_foo_rx_bytes = 0; | 350 const net::NetworkChangeNotifier::ConnectionType kFooConnectionType = |
| 310 while (data_use_it != test_observer()->observed_data_use().end() && | 351 net::NetworkChangeNotifier::CONNECTION_2G; |
| 311 data_use_it->url == GURL("http://foo.com")) { | 352 const std::string kFooMccMnc = "foo_mcc_mnc"; |
| 312 EXPECT_EQ(GetRequestStart(*foo_request), data_use_it->request_start); | 353 scoped_ptr<net::URLRequest> foo_request = |
| 313 EXPECT_EQ(GURL("http://foofirstparty.com"), | 354 ExecuteRequest(GURL("http://foo.com"), GURL("http://foofirstparty.com"), |
| 314 data_use_it->first_party_for_cookies); | 355 kFooTabId, kFooConnectionType, kFooMccMnc); |
| 315 EXPECT_EQ(kFooTabId, data_use_it->tab_id); | |
| 316 EXPECT_EQ(kFooConnectionType, data_use_it->connection_type); | |
| 317 EXPECT_EQ(kFooMccMnc, data_use_it->mcc_mnc); | |
| 318 | 356 |
| 319 observed_foo_tx_bytes += data_use_it->tx_bytes; | 357 const int32_t kBarTabId = 20; |
| 320 observed_foo_rx_bytes += data_use_it->rx_bytes; | 358 const net::NetworkChangeNotifier::ConnectionType kBarConnectionType = |
| 321 ++data_use_it; | 359 net::NetworkChangeNotifier::CONNECTION_WIFI; |
| 360 const std::string kBarMccMnc = "bar_mcc_mnc"; |
| 361 scoped_ptr<net::URLRequest> bar_request = |
| 362 ExecuteRequest(GURL("http://bar.com"), GURL("http://barfirstparty.com"), |
| 363 kBarTabId, kBarConnectionType, kBarMccMnc); |
| 364 |
| 365 auto data_use_it = test_observer()->observed_data_use().begin(); |
| 366 |
| 367 // First, the |foo_request| data use should have happened. |
| 368 int64_t observed_foo_tx_bytes = 0, observed_foo_rx_bytes = 0; |
| 369 while (data_use_it != test_observer()->observed_data_use().end() && |
| 370 data_use_it->url == GURL("http://foo.com")) { |
| 371 EXPECT_EQ(GetRequestStart(*foo_request), data_use_it->request_start); |
| 372 EXPECT_EQ(GURL("http://foofirstparty.com"), |
| 373 data_use_it->first_party_for_cookies); |
| 374 |
| 375 if (test_case.expect_tab_ids) |
| 376 EXPECT_EQ(kFooTabId, data_use_it->tab_id); |
| 377 else |
| 378 EXPECT_EQ(-1, data_use_it->tab_id); |
| 379 |
| 380 EXPECT_EQ(kFooConnectionType, data_use_it->connection_type); |
| 381 EXPECT_EQ(kFooMccMnc, data_use_it->mcc_mnc); |
| 382 |
| 383 observed_foo_tx_bytes += data_use_it->tx_bytes; |
| 384 observed_foo_rx_bytes += data_use_it->rx_bytes; |
| 385 ++data_use_it; |
| 386 } |
| 387 EXPECT_EQ(foo_request->GetTotalSentBytes() * |
| 388 test_case.expected_amortization_multiple, |
| 389 observed_foo_tx_bytes); |
| 390 EXPECT_EQ(foo_request->GetTotalReceivedBytes() * |
| 391 test_case.expected_amortization_multiple, |
| 392 observed_foo_rx_bytes); |
| 393 |
| 394 // Then, the |bar_request| data use should have happened. |
| 395 int64_t observed_bar_tx_bytes = 0, observed_bar_rx_bytes = 0; |
| 396 while (data_use_it != test_observer()->observed_data_use().end()) { |
| 397 EXPECT_EQ(GURL("http://bar.com"), data_use_it->url); |
| 398 EXPECT_EQ(GetRequestStart(*bar_request), data_use_it->request_start); |
| 399 EXPECT_EQ(GURL("http://barfirstparty.com"), |
| 400 data_use_it->first_party_for_cookies); |
| 401 |
| 402 if (test_case.expect_tab_ids) |
| 403 EXPECT_EQ(kBarTabId, data_use_it->tab_id); |
| 404 else |
| 405 EXPECT_EQ(-1, data_use_it->tab_id); |
| 406 |
| 407 EXPECT_EQ(kBarConnectionType, data_use_it->connection_type); |
| 408 EXPECT_EQ(kBarMccMnc, data_use_it->mcc_mnc); |
| 409 |
| 410 observed_bar_tx_bytes += data_use_it->tx_bytes; |
| 411 observed_bar_rx_bytes += data_use_it->rx_bytes; |
| 412 ++data_use_it; |
| 413 } |
| 414 EXPECT_EQ(bar_request->GetTotalSentBytes() * |
| 415 test_case.expected_amortization_multiple, |
| 416 observed_bar_tx_bytes); |
| 417 EXPECT_EQ(bar_request->GetTotalReceivedBytes() * |
| 418 test_case.expected_amortization_multiple, |
| 419 observed_bar_rx_bytes); |
| 322 } | 420 } |
| 323 EXPECT_EQ(foo_request->GetTotalSentBytes(), observed_foo_tx_bytes); | |
| 324 EXPECT_EQ(foo_request->GetTotalReceivedBytes(), observed_foo_rx_bytes); | |
| 325 | |
| 326 // Then, the |bar_request| data use should have happened. | |
| 327 int64_t observed_bar_tx_bytes = 0, observed_bar_rx_bytes = 0; | |
| 328 while (data_use_it != test_observer()->observed_data_use().end()) { | |
| 329 EXPECT_EQ(GURL("http://bar.com"), data_use_it->url); | |
| 330 EXPECT_EQ(GetRequestStart(*bar_request), data_use_it->request_start); | |
| 331 EXPECT_EQ(GURL("http://barfirstparty.com"), | |
| 332 data_use_it->first_party_for_cookies); | |
| 333 EXPECT_EQ(kBarTabId, data_use_it->tab_id); | |
| 334 EXPECT_EQ(kBarConnectionType, data_use_it->connection_type); | |
| 335 EXPECT_EQ(kBarMccMnc, data_use_it->mcc_mnc); | |
| 336 | |
| 337 observed_bar_tx_bytes += data_use_it->tx_bytes; | |
| 338 observed_bar_rx_bytes += data_use_it->rx_bytes; | |
| 339 ++data_use_it; | |
| 340 } | |
| 341 EXPECT_EQ(bar_request->GetTotalSentBytes(), observed_bar_tx_bytes); | |
| 342 EXPECT_EQ(bar_request->GetTotalReceivedBytes(), observed_bar_rx_bytes); | |
| 343 } | |
| 344 | |
| 345 TEST_F(DataUseAggregatorTest, ReportCombinedDataUse) { | |
| 346 // Set up the |foo_request|. | |
| 347 net::MockRead foo_reads[] = { | |
| 348 net::MockRead(net::SYNCHRONOUS, "HTTP/1.1 200 OK\r\n\r\n"), | |
| 349 net::MockRead(net::SYNCHRONOUS, "hello world"), | |
| 350 net::MockRead(net::SYNCHRONOUS, net::OK), | |
| 351 }; | |
| 352 net::StaticSocketDataProvider foo_socket(foo_reads, arraysize(foo_reads), | |
| 353 nullptr, 0); | |
| 354 mock_socket_factory()->AddSocketDataProvider(&foo_socket); | |
| 355 | |
| 356 net::TestDelegate foo_delegate; | |
| 357 scoped_ptr<net::URLRequest> foo_request = context()->CreateRequest( | |
| 358 GURL("http://foo.com"), net::IDLE, &foo_delegate); | |
| 359 foo_request->set_first_party_for_cookies(GURL("http://foofirstparty.com")); | |
| 360 | |
| 361 // Set up the |bar_request|. | |
| 362 net::MockRead bar_reads[] = { | |
| 363 net::MockRead(net::SYNCHRONOUS, "HTTP/1.1 200 OK\r\n\r\n"), | |
| 364 net::MockRead(net::SYNCHRONOUS, "hello world"), | |
| 365 net::MockRead(net::SYNCHRONOUS, net::OK), | |
| 366 }; | |
| 367 net::StaticSocketDataProvider bar_socket(bar_reads, arraysize(bar_reads), | |
| 368 nullptr, 0); | |
| 369 mock_socket_factory()->AddSocketDataProvider(&bar_socket); | |
| 370 | |
| 371 net::TestDelegate bar_delegate; | |
| 372 scoped_ptr<net::URLRequest> bar_request = context()->CreateRequest( | |
| 373 GURL("http://bar.com"), net::IDLE, &bar_delegate); | |
| 374 bar_request->set_first_party_for_cookies(GURL("http://barfirstparty.com")); | |
| 375 | |
| 376 // Set up the network delegate to assign tab IDs and connection types for each | |
| 377 // request. | |
| 378 const int32_t kFooTabId = 10; | |
| 379 const net::NetworkChangeNotifier::ConnectionType kFooConnectionType = | |
| 380 net::NetworkChangeNotifier::CONNECTION_2G; | |
| 381 const std::string kFooMccMnc = "foo_mcc_mnc"; | |
| 382 const int32_t kBarTabId = 20; | |
| 383 const net::NetworkChangeNotifier::ConnectionType kBarConnectionType = | |
| 384 net::NetworkChangeNotifier::CONNECTION_WIFI; | |
| 385 const std::string kBarMccMnc = "bar_mcc_mnc"; | |
| 386 | |
| 387 ReportingNetworkDelegate::DataUseContextMap data_use_context_map; | |
| 388 data_use_context_map[foo_request.get()] = | |
| 389 ReportingNetworkDelegate::DataUseContext(kFooTabId, kFooConnectionType, | |
| 390 kFooMccMnc); | |
| 391 data_use_context_map[bar_request.get()] = | |
| 392 ReportingNetworkDelegate::DataUseContext(kBarTabId, kBarConnectionType, | |
| 393 kBarMccMnc); | |
| 394 reporting_network_delegate()->set_data_use_context_map(data_use_context_map); | |
| 395 | |
| 396 // Run the requests. | |
| 397 foo_request->Start(); | |
| 398 bar_request->Start(); | |
| 399 base::MessageLoop::current()->RunUntilIdle(); | |
| 400 | |
| 401 // The observer should have been notified once with a DataUse element for each | |
| 402 // request. | |
| 403 EXPECT_EQ(1, test_observer()->on_data_use_called_count()); | |
| 404 EXPECT_EQ(static_cast<size_t>(2), | |
| 405 test_observer()->observed_data_use().size()); | |
| 406 | |
| 407 // All of the |foo_request| DataUse should have been combined into a single | |
| 408 // DataUse element. | |
| 409 const DataUse& foo_data_use = test_observer()->observed_data_use().front(); | |
| 410 EXPECT_EQ(GURL("http://foo.com"), foo_data_use.url); | |
| 411 EXPECT_EQ(GetRequestStart(*foo_request), foo_data_use.request_start); | |
| 412 EXPECT_EQ(GURL("http://foofirstparty.com"), | |
| 413 foo_data_use.first_party_for_cookies); | |
| 414 EXPECT_EQ(kFooTabId, foo_data_use.tab_id); | |
| 415 EXPECT_EQ(kFooConnectionType, foo_data_use.connection_type); | |
| 416 EXPECT_EQ(kFooMccMnc, foo_data_use.mcc_mnc); | |
| 417 EXPECT_EQ(foo_request->GetTotalSentBytes(), foo_data_use.tx_bytes); | |
| 418 EXPECT_EQ(foo_request->GetTotalReceivedBytes(), foo_data_use.rx_bytes); | |
| 419 | |
| 420 // All of the |bar_request| DataUse should have been combined into a single | |
| 421 // DataUse element. | |
| 422 const DataUse& bar_data_use = test_observer()->observed_data_use().back(); | |
| 423 EXPECT_EQ(GURL("http://bar.com"), bar_data_use.url); | |
| 424 EXPECT_EQ(GetRequestStart(*bar_request), bar_data_use.request_start); | |
| 425 EXPECT_EQ(GURL("http://barfirstparty.com"), | |
| 426 bar_data_use.first_party_for_cookies); | |
| 427 EXPECT_EQ(kBarTabId, bar_data_use.tab_id); | |
| 428 EXPECT_EQ(kBarConnectionType, bar_data_use.connection_type); | |
| 429 EXPECT_EQ(kBarMccMnc, bar_data_use.mcc_mnc); | |
| 430 EXPECT_EQ(bar_request->GetTotalSentBytes(), bar_data_use.tx_bytes); | |
| 431 EXPECT_EQ(bar_request->GetTotalReceivedBytes(), bar_data_use.rx_bytes); | |
| 432 } | 421 } |
| 433 | 422 |
| 434 TEST_F(DataUseAggregatorTest, ReportOffTheRecordDataUse) { | 423 TEST_F(DataUseAggregatorTest, ReportOffTheRecordDataUse) { |
| 424 Initialize(scoped_ptr<FakeDataUseAnnotator>(new FakeDataUseAnnotator()), |
| 425 scoped_ptr<DataUseAmortizer>(new DoublingAmortizer())); |
| 426 |
| 435 // Off the record data use should not be reported to observers. | 427 // Off the record data use should not be reported to observers. |
| 436 data_use_aggregator()->ReportOffTheRecordDataUse(1000, 1000); | 428 data_use_aggregator()->ReportOffTheRecordDataUse(1000, 1000); |
| 437 base::MessageLoop::current()->RunUntilIdle(); | 429 base::MessageLoop::current()->RunUntilIdle(); |
| 438 EXPECT_EQ(static_cast<size_t>(0), | 430 EXPECT_EQ(static_cast<size_t>(0), |
| 439 test_observer()->observed_data_use().size()); | 431 test_observer()->observed_data_use().size()); |
| 440 } | 432 } |
| 441 | 433 |
| 442 } // namespace | 434 } // namespace |
| 443 | 435 |
| 444 } // namespace data_usage | 436 } // namespace data_usage |
| OLD | NEW |