Chromium Code Reviews| Index: chrome/browser/page_load_metrics/experiments/delay_navigation_throttle_unittest.cc |
| diff --git a/chrome/browser/page_load_metrics/experiments/delay_navigation_throttle_unittest.cc b/chrome/browser/page_load_metrics/experiments/delay_navigation_throttle_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..17837a86f6d8ba833814e999f5bb73d88246c432 |
| --- /dev/null |
| +++ b/chrome/browser/page_load_metrics/experiments/delay_navigation_throttle_unittest.cc |
| @@ -0,0 +1,184 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/page_load_metrics/experiments/delay_navigation_throttle.h" |
| + |
| +#include <map> |
| +#include <string> |
| +#include <tuple> |
| +#include <utility> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/test/scoped_feature_list.h" |
| +#include "base/test/test_mock_time_task_runner.h" |
| +#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| +#include "components/variations/variations_associated_data.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "net/http/http_util.h" |
| + |
| +class DelayNavigationThrottleTest : public ChromeRenderViewHostTestHarness {}; |
| + |
| +TEST_F(DelayNavigationThrottleTest, BasicDelay) { |
| + const char kBasicResponseHeaders[] = "HTTP/1.1 200 OK"; |
| + base::TimeDelta navigation_delay = base::TimeDelta::FromSeconds(1); |
|
Charlie Harrison
2017/03/23 17:28:39
include base/time/time.h
Bryan McQuade
2017/03/24 00:57:29
done
|
| + GURL url("http://www.example.com/"); |
|
Charlie Harrison
2017/03/23 17:28:40
include url/gurl.h
Bryan McQuade
2017/03/24 00:57:29
done
|
| + |
| + scoped_refptr<base::TestMockTimeTaskRunner> mock_time_task_runner( |
|
Charlie Harrison
2017/03/23 17:28:39
include base/memory/ref_counted.h
Bryan McQuade
2017/03/24 00:57:28
done
|
| + new base::TestMockTimeTaskRunner()); |
| + std::unique_ptr<content::NavigationHandle> test_handle = |
|
Charlie Harrison
2017/03/23 17:28:39
include <memory>
Bryan McQuade
2017/03/24 00:57:29
done
|
| + content::NavigationHandle::CreateNavigationHandleForTesting(url, |
| + main_rfh()); |
| + test_handle->RegisterThrottleForTesting( |
| + base::MakeUnique<DelayNavigationThrottle>( |
| + test_handle.get(), mock_time_task_runner, navigation_delay)); |
| + |
| + EXPECT_FALSE(mock_time_task_runner->HasPendingTask()); |
|
Charlie Harrison
2017/03/23 17:28:39
include gtest
Bryan McQuade
2017/03/24 00:57:29
done
|
| + EXPECT_EQ(content::NavigationThrottle::DEFER, |
|
Charlie Harrison
2017/03/23 17:28:40
include navigation_throttle
Bryan McQuade
2017/03/24 00:57:29
done
|
| + test_handle->CallWillStartRequestForTesting( |
| + false /* is_post */, content::Referrer(), |
| + false /* has_user_gesture */, ui::PAGE_TRANSITION_LINK, |
| + false /* is_external_protocol */)); |
| + |
| + // There may be other throttles that DEFER and post async tasks to the UI |
| + // thread. Allow them to run to completion, so our throttle is guaranteed to |
| + // have a chance to run. |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_TRUE(mock_time_task_runner->HasPendingTask()); |
| + EXPECT_FALSE(test_handle->HasCommitted()); |
| + |
| + mock_time_task_runner->FastForwardBy(navigation_delay); |
| + EXPECT_FALSE(mock_time_task_runner->HasPendingTask()); |
| + |
| + // Run any remaining async tasks, to make sure all other deferred throttles |
| + // can complete. |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Verify that the WillSendRequest portion of the navigation has completed, |
| + // and NavigationHandle::WillProcessResponse and the commit portion of the |
| + // navigation lifetime can now be invoked. |
| + EXPECT_EQ(content::NavigationThrottle::PROCEED, |
| + test_handle->CallWillProcessResponseForTesting( |
| + main_rfh(), |
| + net::HttpUtil::AssembleRawHeaders( |
| + kBasicResponseHeaders, strlen(kBasicResponseHeaders)))); |
| + test_handle->CallDidCommitNavigationForTesting(url); |
| + EXPECT_TRUE(test_handle->HasCommitted()); |
| +} |
| + |
| +enum ExpectInstantiationResult { |
| + EXPECT_INSTANTIATION, |
| + EXPECT_NO_INSTANTIATION |
| +}; |
| + |
| +class DelayNavigationThrottleInstantiationTest |
| + : public ChromeRenderViewHostTestHarness, |
| + public testing::WithParamInterface< |
| + std::tuple<ExpectInstantiationResult, |
| + base::FeatureList::OverrideState, |
| + base::TimeDelta, |
| + double, |
| + std::string>> { |
|
Charlie Harrison
2017/03/23 17:28:39
nit: Can you make the double a TimeDelta and the s
Bryan McQuade
2017/03/23 17:32:47
i can address the other comments, but the double i
Charlie Harrison
2017/03/23 17:36:55
Ha, sorry. Misread it. double is fine.
Can you ma
Bryan McQuade
2017/03/24 00:57:29
switched to a gurl.
|
| + public: |
| + DelayNavigationThrottleInstantiationTest() |
| + : field_trial_list_(nullptr /* entropy_provider */) {} |
| + |
| + void SetUp() override { |
| + ChromeRenderViewHostTestHarness::SetUp(); |
| + |
| + std::tie(expected_instantiation_result_, feature_state_, delay_, |
| + probability_, url_) = GetParam(); |
| + |
| + std::map<std::string, std::string> variation_params( |
| + {{DelayNavigationThrottle::kParamDelayNavigationDurationMillis, |
| + base::IntToString(delay_.InMilliseconds())}, |
| + {DelayNavigationThrottle::kParamDelayNavigationProbability, |
| + base::DoubleToString(probability_)}}); |
| + InitializeScopedFeatureList(feature_state_, variation_params); |
| + } |
| + |
| + void TearDown() override { |
| + ChromeRenderViewHostTestHarness::TearDown(); |
| + variations::testing::ClearAllVariationParams(); |
| + } |
| + |
| + void InitializeScopedFeatureList( |
| + base::FeatureList::OverrideState feature_state, |
| + const std::map<std::string, std::string>& variation_params) { |
| + static const char kTestFieldTrialName[] = "TestTrial"; |
| + static const char kTestExperimentGroupName[] = "TestGroup"; |
| + |
| + EXPECT_TRUE(variations::AssociateVariationParams( |
| + kTestFieldTrialName, kTestExperimentGroupName, variation_params)); |
| + |
| + base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial( |
| + kTestFieldTrialName, kTestExperimentGroupName); |
| + |
| + std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
| + feature_list->RegisterFieldTrialOverride(kDelayNavigationFeature.name, |
| + feature_state, field_trial); |
| + |
| + // Since we are adding a scoped feature list after browser start, copy over |
| + // the existing feature list to prevent inconsistency. |
| + base::FeatureList* existing_feature_list = base::FeatureList::GetInstance(); |
| + if (existing_feature_list) { |
| + std::string enabled_features; |
| + std::string disabled_features; |
| + base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, |
| + &disabled_features); |
| + feature_list->InitializeFromCommandLine(enabled_features, |
| + disabled_features); |
| + } |
| + |
| + scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); |
| + } |
| + |
| + base::FieldTrialList field_trial_list_; |
| + base::test::ScopedFeatureList scoped_feature_list_; |
| + |
| + // Fields filled in via GetParam(): |
| + base::FeatureList::OverrideState feature_state_; |
| + base::TimeDelta delay_; |
| + double probability_; |
| + std::string url_; |
| + ExpectInstantiationResult expected_instantiation_result_; |
| +}; |
|
Charlie Harrison
2017/03/23 17:28:39
DISALLOW_COPY_AND_ASSIGN and include base/macros
Bryan McQuade
2017/03/24 00:57:29
done
|
| + |
| +TEST_P(DelayNavigationThrottleInstantiationTest, Instantiate) { |
| + std::unique_ptr<content::NavigationHandle> test_handle = |
| + content::NavigationHandle::CreateNavigationHandleForTesting(GURL(url_), |
| + main_rfh()); |
| + std::unique_ptr<content::NavigationThrottle> throttle = |
| + DelayNavigationThrottle::MaybeCreateThrottleFor(test_handle.get()); |
| + const bool expect_instantiation = |
| + expected_instantiation_result_ == EXPECT_INSTANTIATION; |
| + EXPECT_EQ(expect_instantiation, throttle != nullptr); |
| +} |
| + |
| +INSTANTIATE_TEST_CASE_P( |
| + InstantiateThrottle, |
| + DelayNavigationThrottleInstantiationTest, |
| + ::testing::Values( |
| + std::make_tuple(EXPECT_NO_INSTANTIATION, |
| + base::FeatureList::OVERRIDE_DISABLE_FEATURE, |
| + base::TimeDelta::FromMilliseconds(10), |
| + 1.0, |
| + "http://www.example.com/"), |
| + std::make_tuple(EXPECT_NO_INSTANTIATION, |
| + base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| + base::TimeDelta::FromMilliseconds(10), |
| + 1.0, |
| + "chrome://version"), |
| + std::make_tuple(EXPECT_NO_INSTANTIATION, |
| + base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| + base::TimeDelta::FromMilliseconds(10), |
| + 0.0, |
| + "http://www.example.com/"), |
| + std::make_tuple(EXPECT_INSTANTIATION, |
| + base::FeatureList::OVERRIDE_ENABLE_FEATURE, |
| + base::TimeDelta::FromMilliseconds(10), |
| + 1.0, |
| + "http://www.example.com/"))); |