| 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/previews/core/previews_io_data.h" | 5 #include "components/previews/core/previews_io_data.h" |
| 6 | 6 |
| 7 #include <initializer_list> | 7 #include <initializer_list> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/callback.h" | 14 #include "base/callback.h" |
| 15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/metrics/field_trial.h" | 18 #include "base/metrics/field_trial.h" |
| 19 #include "base/metrics/field_trial_params.h" | 19 #include "base/metrics/field_trial_params.h" |
| 20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
| 21 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
| 22 #include "base/test/histogram_tester.h" | 22 #include "base/test/histogram_tester.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "components/previews/core/previews_black_list.h" | 25 #include "components/previews/core/previews_black_list.h" |
| 26 #include "components/previews/core/previews_black_list_item.h" | 26 #include "components/previews/core/previews_black_list_item.h" |
| 27 #include "components/previews/core/previews_experiments.h" |
| 27 #include "components/previews/core/previews_opt_out_store.h" | 28 #include "components/previews/core/previews_opt_out_store.h" |
| 28 #include "components/previews/core/previews_ui_service.h" | 29 #include "components/previews/core/previews_ui_service.h" |
| 29 #include "components/variations/variations_associated_data.h" | 30 #include "components/variations/variations_associated_data.h" |
| 30 #include "net/base/load_flags.h" | 31 #include "net/base/load_flags.h" |
| 31 #include "net/nqe/effective_connection_type.h" | 32 #include "net/nqe/effective_connection_type.h" |
| 32 #include "net/nqe/network_quality_estimator_test_util.h" | 33 #include "net/nqe/network_quality_estimator_test_util.h" |
| 33 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" | 34 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 34 #include "net/url_request/url_request.h" | 35 #include "net/url_request/url_request.h" |
| 35 #include "net/url_request/url_request_test_util.h" | 36 #include "net/url_request/url_request_test_util.h" |
| 36 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 37 #include "url/gurl.h" | 38 #include "url/gurl.h" |
| 38 | 39 |
| 39 namespace previews { | 40 namespace previews { |
| 40 | 41 |
| 41 namespace { | 42 namespace { |
| 42 | 43 |
| 43 // TODO(sclittle): Tests should be testing the actual prod code that checks if | 44 // This method simulates the actual behavior of the passed in callback, which is |
| 44 // the appropriate field trial is enabled for the preview, instead of testing | 45 // validated in other tests. For simplicity, offline, lite page, and server LoFi |
| 45 // this function here. Consider moving that code out of | 46 // use the offline previews check. Client LoFi uses a seperate check to verify |
| 46 // chrome/browser/previews/previews_service.cc and into the previews/ component. | 47 // that types are treated differently. |
| 47 bool IsPreviewFieldTrialEnabled(PreviewsType type) { | 48 bool IsPreviewFieldTrialEnabled(PreviewsType type) { |
| 48 switch (type) { | 49 switch (type) { |
| 49 case PreviewsType::OFFLINE: | 50 case PreviewsType::OFFLINE: |
| 51 case PreviewsType::LITE_PAGE: |
| 50 return params::IsOfflinePreviewsEnabled(); | 52 return params::IsOfflinePreviewsEnabled(); |
| 51 case PreviewsType::CLIENT_LOFI: | 53 case PreviewsType::LOFI: |
| 52 return params::IsClientLoFiEnabled(); | 54 return params::IsClientLoFiEnabled(); |
| 53 case PreviewsType::NONE: | 55 case PreviewsType::NONE: |
| 54 case PreviewsType::LAST: | 56 case PreviewsType::LAST: |
| 55 break; | 57 break; |
| 56 } | 58 } |
| 57 NOTREACHED(); | 59 NOTREACHED(); |
| 58 return false; | 60 return false; |
| 59 } | 61 } |
| 60 | 62 |
| 61 class TestPreviewsIOData : public PreviewsIOData { | 63 class TestPreviewsIOData : public PreviewsIOData { |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 | 268 |
| 267 base::HistogramTester histogram_tester; | 269 base::HistogramTester histogram_tester; |
| 268 EXPECT_FALSE( | 270 EXPECT_FALSE( |
| 269 io_data()->ShouldAllowPreview(*CreateRequest(), PreviewsType::OFFLINE)); | 271 io_data()->ShouldAllowPreview(*CreateRequest(), PreviewsType::OFFLINE)); |
| 270 histogram_tester.ExpectUniqueSample( | 272 histogram_tester.ExpectUniqueSample( |
| 271 "Previews.EligibilityReason.Offline", | 273 "Previews.EligibilityReason.Offline", |
| 272 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), | 274 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), |
| 273 1); | 275 1); |
| 274 } | 276 } |
| 275 | 277 |
| 278 TEST_F(PreviewsIODataTest, TestAllowLitePageWhenNetworkQualityFast) { |
| 279 // LoFi and LitePage check NQE on their own. |
| 280 InitializeUIService(); |
| 281 CreateFieldTrialWithParams("ClientSidePreviews", "Enabled", |
| 282 {{"show_offline_pages", "true"}, |
| 283 {"max_allowed_effective_connection_type", "2G"}}); |
| 284 |
| 285 network_quality_estimator()->set_effective_connection_type( |
| 286 net::EFFECTIVE_CONNECTION_TYPE_3G); |
| 287 |
| 288 base::HistogramTester histogram_tester; |
| 289 EXPECT_TRUE(io_data()->ShouldAllowPreviewAtECT( |
| 290 *CreateRequest(), PreviewsType::LITE_PAGE, |
| 291 net::EFFECTIVE_CONNECTION_TYPE_4G)); |
| 292 histogram_tester.ExpectUniqueSample( |
| 293 "Previews.EligibilityReason.LitePage", |
| 294 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); |
| 295 } |
| 296 |
| 276 TEST_F(PreviewsIODataTest, TestDisallowOfflineWhenNetworkQualityFast) { | 297 TEST_F(PreviewsIODataTest, TestDisallowOfflineWhenNetworkQualityFast) { |
| 277 InitializeUIService(); | 298 InitializeUIService(); |
| 278 CreateFieldTrialWithParams("ClientSidePreviews", "Enabled", | 299 CreateFieldTrialWithParams("ClientSidePreviews", "Enabled", |
| 279 {{"show_offline_pages", "true"}, | 300 {{"show_offline_pages", "true"}, |
| 280 {"max_allowed_effective_connection_type", "2G"}}); | 301 {"max_allowed_effective_connection_type", "2G"}}); |
| 281 | 302 |
| 282 network_quality_estimator()->set_effective_connection_type( | 303 network_quality_estimator()->set_effective_connection_type( |
| 283 net::EFFECTIVE_CONNECTION_TYPE_3G); | 304 net::EFFECTIVE_CONNECTION_TYPE_3G); |
| 284 | 305 |
| 285 base::HistogramTester histogram_tester; | 306 base::HistogramTester histogram_tester; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 299 network_quality_estimator()->set_effective_connection_type( | 320 network_quality_estimator()->set_effective_connection_type( |
| 300 net::EFFECTIVE_CONNECTION_TYPE_2G); | 321 net::EFFECTIVE_CONNECTION_TYPE_2G); |
| 301 | 322 |
| 302 std::unique_ptr<net::URLRequest> request = CreateRequest(); | 323 std::unique_ptr<net::URLRequest> request = CreateRequest(); |
| 303 request->SetLoadFlags(net::LOAD_BYPASS_CACHE); | 324 request->SetLoadFlags(net::LOAD_BYPASS_CACHE); |
| 304 | 325 |
| 305 base::HistogramTester histogram_tester; | 326 base::HistogramTester histogram_tester; |
| 306 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); | 327 EXPECT_FALSE(io_data()->ShouldAllowPreview(*request, PreviewsType::OFFLINE)); |
| 307 histogram_tester.ExpectUniqueSample( | 328 histogram_tester.ExpectUniqueSample( |
| 308 "Previews.EligibilityReason.Offline", | 329 "Previews.EligibilityReason.Offline", |
| 309 static_cast<int>( | 330 static_cast<int>(PreviewsEligibilityReason::RELOAD_DISALLOWED), 1); |
| 310 PreviewsEligibilityReason::RELOAD_DISALLOWED_FOR_OFFLINE), | |
| 311 1); | |
| 312 } | 331 } |
| 313 | 332 |
| 314 TEST_F(PreviewsIODataTest, TestAllowOffline) { | 333 TEST_F(PreviewsIODataTest, TestAllowOffline) { |
| 315 InitializeUIService(); | 334 InitializeUIService(); |
| 316 CreateFieldTrialWithParams("ClientSidePreviews", "Enabled", | 335 CreateFieldTrialWithParams("ClientSidePreviews", "Enabled", |
| 317 {{"show_offline_pages", "true"}, | 336 {{"show_offline_pages", "true"}, |
| 318 {"max_allowed_effective_connection_type", "2G"}}); | 337 {"max_allowed_effective_connection_type", "2G"}}); |
| 319 | 338 |
| 320 network_quality_estimator()->set_effective_connection_type( | 339 network_quality_estimator()->set_effective_connection_type( |
| 321 net::EFFECTIVE_CONNECTION_TYPE_2G); | 340 net::EFFECTIVE_CONNECTION_TYPE_2G); |
| 322 | 341 |
| 323 base::HistogramTester histogram_tester; | 342 base::HistogramTester histogram_tester; |
| 324 EXPECT_TRUE( | 343 EXPECT_TRUE( |
| 325 io_data()->ShouldAllowPreview(*CreateRequest(), PreviewsType::OFFLINE)); | 344 io_data()->ShouldAllowPreview(*CreateRequest(), PreviewsType::OFFLINE)); |
| 326 histogram_tester.ExpectUniqueSample( | 345 histogram_tester.ExpectUniqueSample( |
| 327 "Previews.EligibilityReason.Offline", | 346 "Previews.EligibilityReason.Offline", |
| 328 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); | 347 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); |
| 329 } | 348 } |
| 330 | 349 |
| 331 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedByDefault) { | 350 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedByDefault) { |
| 332 InitializeUIService(); | 351 InitializeUIService(); |
| 333 | 352 |
| 334 base::HistogramTester histogram_tester; | 353 base::HistogramTester histogram_tester; |
| 335 EXPECT_FALSE(io_data()->ShouldAllowPreview(*CreateRequest(), | 354 EXPECT_FALSE(io_data()->ShouldAllowPreviewAtECT( |
| 336 PreviewsType::CLIENT_LOFI)); | 355 *CreateRequest(), PreviewsType::LOFI, |
| 337 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.ClientLoFi", 0); | 356 params::EffectiveConnectionTypeThresholdForClientLoFi())); |
| 357 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.LoFi", 0); |
| 338 } | 358 } |
| 339 | 359 |
| 340 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenFieldTrialDisabled) { | 360 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenFieldTrialDisabled) { |
| 341 InitializeUIService(); | 361 InitializeUIService(); |
| 342 CreateFieldTrialWithParams("PreviewsClientLoFi", "Disabled", {}); | 362 CreateFieldTrialWithParams("PreviewsClientLoFi", "Disabled", {}); |
| 343 | 363 |
| 344 base::HistogramTester histogram_tester; | 364 base::HistogramTester histogram_tester; |
| 345 EXPECT_FALSE(io_data()->ShouldAllowPreview(*CreateRequest(), | 365 EXPECT_FALSE(io_data()->ShouldAllowPreviewAtECT( |
| 346 PreviewsType::CLIENT_LOFI)); | 366 *CreateRequest(), PreviewsType::LOFI, |
| 347 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.ClientLoFi", 0); | 367 params::EffectiveConnectionTypeThresholdForClientLoFi())); |
| 368 histogram_tester.ExpectTotalCount("Previews.EligibilityReason.LoFi", 0); |
| 348 } | 369 } |
| 349 | 370 |
| 350 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenNetworkQualityUnavailable) { | 371 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenNetworkQualityUnavailable) { |
| 351 InitializeUIService(); | 372 InitializeUIService(); |
| 352 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", {}); | 373 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", {}); |
| 353 | 374 |
| 354 network_quality_estimator()->set_effective_connection_type( | 375 network_quality_estimator()->set_effective_connection_type( |
| 355 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); | 376 net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN); |
| 356 | 377 |
| 357 base::HistogramTester histogram_tester; | 378 base::HistogramTester histogram_tester; |
| 358 EXPECT_FALSE(io_data()->ShouldAllowPreview(*CreateRequest(), | 379 EXPECT_FALSE(io_data()->ShouldAllowPreviewAtECT( |
| 359 PreviewsType::CLIENT_LOFI)); | 380 *CreateRequest(), PreviewsType::LOFI, |
| 381 params::EffectiveConnectionTypeThresholdForClientLoFi())); |
| 360 histogram_tester.ExpectUniqueSample( | 382 histogram_tester.ExpectUniqueSample( |
| 361 "Previews.EligibilityReason.ClientLoFi", | 383 "Previews.EligibilityReason.LoFi", |
| 362 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), | 384 static_cast<int>(PreviewsEligibilityReason::NETWORK_QUALITY_UNAVAILABLE), |
| 363 1); | 385 1); |
| 364 } | 386 } |
| 365 | 387 |
| 366 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenNetworkFast) { | 388 TEST_F(PreviewsIODataTest, ClientLoFiDisallowedWhenNetworkFast) { |
| 367 InitializeUIService(); | 389 InitializeUIService(); |
| 368 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", | 390 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", |
| 369 {{"max_allowed_effective_connection_type", "2G"}}); | 391 {{"max_allowed_effective_connection_type", "2G"}}); |
| 370 | 392 |
| 371 network_quality_estimator()->set_effective_connection_type( | 393 network_quality_estimator()->set_effective_connection_type( |
| 372 net::EFFECTIVE_CONNECTION_TYPE_3G); | 394 net::EFFECTIVE_CONNECTION_TYPE_3G); |
| 373 | 395 |
| 374 base::HistogramTester histogram_tester; | 396 base::HistogramTester histogram_tester; |
| 375 EXPECT_FALSE(io_data()->ShouldAllowPreview(*CreateRequest(), | 397 EXPECT_FALSE(io_data()->ShouldAllowPreviewAtECT( |
| 376 PreviewsType::CLIENT_LOFI)); | 398 *CreateRequest(), PreviewsType::LOFI, |
| 399 params::EffectiveConnectionTypeThresholdForClientLoFi())); |
| 377 histogram_tester.ExpectUniqueSample( | 400 histogram_tester.ExpectUniqueSample( |
| 378 "Previews.EligibilityReason.ClientLoFi", | 401 "Previews.EligibilityReason.LoFi", |
| 379 static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); | 402 static_cast<int>(PreviewsEligibilityReason::NETWORK_NOT_SLOW), 1); |
| 380 } | 403 } |
| 381 | 404 |
| 382 TEST_F(PreviewsIODataTest, ClientLoFiAllowed) { | 405 TEST_F(PreviewsIODataTest, ClientLoFiAllowed) { |
| 383 InitializeUIService(); | 406 InitializeUIService(); |
| 384 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", | 407 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", |
| 385 {{"max_allowed_effective_connection_type", "2G"}}); | 408 {{"max_allowed_effective_connection_type", "2G"}}); |
| 386 | 409 |
| 387 network_quality_estimator()->set_effective_connection_type( | 410 network_quality_estimator()->set_effective_connection_type( |
| 388 net::EFFECTIVE_CONNECTION_TYPE_2G); | 411 net::EFFECTIVE_CONNECTION_TYPE_2G); |
| 389 | 412 |
| 390 base::HistogramTester histogram_tester; | 413 base::HistogramTester histogram_tester; |
| 391 EXPECT_TRUE(io_data()->ShouldAllowPreview(*CreateRequest(), | 414 EXPECT_TRUE(io_data()->ShouldAllowPreviewAtECT( |
| 392 PreviewsType::CLIENT_LOFI)); | 415 *CreateRequest(), PreviewsType::LOFI, |
| 416 params::EffectiveConnectionTypeThresholdForClientLoFi())); |
| 393 histogram_tester.ExpectUniqueSample( | 417 histogram_tester.ExpectUniqueSample( |
| 394 "Previews.EligibilityReason.ClientLoFi", | 418 "Previews.EligibilityReason.LoFi", |
| 395 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); | 419 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); |
| 396 } | 420 } |
| 397 | 421 |
| 398 TEST_F(PreviewsIODataTest, ClientLoFiAllowedOnReload) { | 422 TEST_F(PreviewsIODataTest, ClientLoFiAllowedOnReload) { |
| 399 InitializeUIService(); | 423 InitializeUIService(); |
| 400 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", | 424 CreateFieldTrialWithParams("PreviewsClientLoFi", "Enabled", |
| 401 {{"max_allowed_effective_connection_type", "2G"}}); | 425 {{"max_allowed_effective_connection_type", "2G"}}); |
| 402 | 426 |
| 403 network_quality_estimator()->set_effective_connection_type( | 427 network_quality_estimator()->set_effective_connection_type( |
| 404 net::EFFECTIVE_CONNECTION_TYPE_2G); | 428 net::EFFECTIVE_CONNECTION_TYPE_2G); |
| 405 | 429 |
| 406 std::unique_ptr<net::URLRequest> request = CreateRequest(); | 430 std::unique_ptr<net::URLRequest> request = CreateRequest(); |
| 407 request->SetLoadFlags(net::LOAD_BYPASS_CACHE); | 431 request->SetLoadFlags(net::LOAD_BYPASS_CACHE); |
| 408 | 432 |
| 409 base::HistogramTester histogram_tester; | 433 base::HistogramTester histogram_tester; |
| 410 EXPECT_TRUE( | 434 EXPECT_TRUE(io_data()->ShouldAllowPreviewAtECT( |
| 411 io_data()->ShouldAllowPreview(*request, PreviewsType::CLIENT_LOFI)); | 435 *request, PreviewsType::LOFI, |
| 436 params::EffectiveConnectionTypeThresholdForClientLoFi())); |
| 412 histogram_tester.ExpectUniqueSample( | 437 histogram_tester.ExpectUniqueSample( |
| 413 "Previews.EligibilityReason.ClientLoFi", | 438 "Previews.EligibilityReason.LoFi", |
| 414 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); | 439 static_cast<int>(PreviewsEligibilityReason::ALLOWED), 1); |
| 415 } | 440 } |
| 416 | 441 |
| 417 } // namespace | 442 } // namespace |
| 418 | 443 |
| 419 } // namespace previews | 444 } // namespace previews |
| OLD | NEW |