OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/loader/delay_navigation_throttle.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "base/run_loop.h" | |
9 #include "base/test/scoped_feature_list.h" | |
10 #include "base/test/scoped_mock_time_message_loop_task_runner.h" | |
11 #include "base/test/test_mock_time_task_runner.h" | |
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
13 #include "components/variations/variations_associated_data.h" | |
14 #include "content/public/browser/navigation_handle.h" | |
15 #include "net/http/http_util.h" | |
16 | |
17 class ScopedDelayNavigationFeatureParams { | |
Charlie Harrison
2017/03/22 15:30:12
optional: Since this is a fairly simple feature, a
Bryan McQuade
2017/03/22 20:50:54
Sure, I ported to a parameterized harness. I kept
| |
18 public: | |
19 ScopedDelayNavigationFeatureParams( | |
20 base::FeatureList::OverrideState feature_state, | |
21 const std::map<std::string, std::string>& variation_params) { | |
22 static const char kTestFieldTrialName[] = "TestTrial"; | |
23 static const char kTestExperimentGroupName[] = "TestGroup"; | |
24 | |
25 EXPECT_TRUE(variations::AssociateVariationParams( | |
26 kTestFieldTrialName, kTestExperimentGroupName, variation_params)); | |
27 | |
28 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial( | |
29 kTestFieldTrialName, kTestExperimentGroupName); | |
30 | |
31 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
32 feature_list->RegisterFieldTrialOverride(kDelayNavigationFeature.name, | |
33 feature_state, field_trial); | |
34 | |
35 // Since we are adding a scoped feature list after browser start, copy over | |
36 // the existing feature list to prevent inconsistency. | |
37 base::FeatureList* existing_feature_list = base::FeatureList::GetInstance(); | |
38 if (existing_feature_list) { | |
39 std::string enabled_features; | |
40 std::string disabled_features; | |
41 base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, | |
42 &disabled_features); | |
43 feature_list->InitializeFromCommandLine(enabled_features, | |
44 disabled_features); | |
45 } | |
46 | |
47 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); | |
48 } | |
49 | |
50 ~ScopedDelayNavigationFeatureParams() { | |
51 variations::testing::ClearAllVariationParams(); | |
52 } | |
53 | |
54 private: | |
55 base::test::ScopedFeatureList scoped_feature_list_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(ScopedDelayNavigationFeatureParams); | |
58 }; | |
59 | |
60 class DelayNavigationThrottleTest : public ChromeRenderViewHostTestHarness {}; | |
61 | |
62 TEST_F(DelayNavigationThrottleTest, BasicDelay) { | |
63 const char kBasicResponseHeaders[] = "HTTP/1.1 200 OK"; | |
64 base::TimeDelta navigation_delay = base::TimeDelta::FromSeconds(1); | |
65 GURL url("http://www.example.com/"); | |
66 | |
67 scoped_refptr<base::TestMockTimeTaskRunner> mock_time_task_runner( | |
68 new base::TestMockTimeTaskRunner()); | |
69 std::unique_ptr<content::NavigationHandle> test_handle = | |
70 content::NavigationHandle::CreateNavigationHandleForTesting(url, | |
71 main_rfh()); | |
72 test_handle->RegisterThrottleForTesting( | |
73 base::MakeUnique<DelayNavigationThrottle>( | |
74 test_handle.get(), mock_time_task_runner, navigation_delay)); | |
75 | |
76 EXPECT_FALSE(mock_time_task_runner->HasPendingTask()); | |
77 EXPECT_EQ(content::NavigationThrottle::DEFER, | |
78 test_handle->CallWillStartRequestForTesting( | |
79 false /* is_post */, content::Referrer(), | |
80 false /* has_user_gesture */, ui::PAGE_TRANSITION_LINK, | |
81 false /* is_external_protocol */)); | |
82 | |
83 // There may be other throttles that DEFER and post async tasks to the UI | |
84 // thread. Allow them to run to completion, so our throttle is guaranteed to | |
85 // have a chance to run. | |
86 base::RunLoop().RunUntilIdle(); | |
87 | |
88 EXPECT_TRUE(mock_time_task_runner->HasPendingTask()); | |
89 EXPECT_FALSE(test_handle->HasCommitted()); | |
90 | |
91 mock_time_task_runner->FastForwardBy(navigation_delay); | |
92 EXPECT_FALSE(mock_time_task_runner->HasPendingTask()); | |
93 | |
94 // Run any remaining async tasks, to make sure all other deferred throttles | |
95 // can complete. | |
96 base::RunLoop().RunUntilIdle(); | |
97 | |
98 // Verify that the WillSendRequest portion of the navigation has completed, | |
99 // and NavigationHandle::WillProcessResponse and the commit portion of the | |
100 // navigation lifetime can now be invoked. | |
101 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
102 test_handle->CallWillProcessResponseForTesting( | |
103 main_rfh(), | |
104 net::HttpUtil::AssembleRawHeaders( | |
105 kBasicResponseHeaders, strlen(kBasicResponseHeaders)))); | |
106 test_handle->CallDidCommitNavigationForTesting(url); | |
107 EXPECT_TRUE(test_handle->HasCommitted()); | |
108 } | |
109 | |
110 TEST_F(DelayNavigationThrottleTest, NoThrottleWhenFeatureDisabled) { | |
111 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); | |
112 ScopedDelayNavigationFeatureParams params( | |
113 base::FeatureList::OVERRIDE_DISABLE_FEATURE, | |
114 std::map<std::string, std::string>()); | |
115 GURL url("http://www.example.com/"); | |
116 std::unique_ptr<content::NavigationHandle> test_handle = | |
117 content::NavigationHandle::CreateNavigationHandleForTesting(url, | |
118 main_rfh()); | |
119 EXPECT_EQ(nullptr, | |
120 DelayNavigationThrottle::MaybeCreateThrottleFor(test_handle.get())); | |
121 } | |
122 | |
123 TEST_F(DelayNavigationThrottleTest, CreateThrottleWhenFeatureEnabled) { | |
124 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); | |
125 ScopedDelayNavigationFeatureParams params( | |
126 base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
127 {{DelayNavigationThrottle::kParamDelayNavigationDurationMillis, "10"}, | |
128 {DelayNavigationThrottle::kParamDelayNavigationProbability, "1"}}); | |
129 GURL url("http://www.example.com/"); | |
130 std::unique_ptr<content::NavigationHandle> test_handle = | |
131 content::NavigationHandle::CreateNavigationHandleForTesting(url, | |
132 main_rfh()); | |
133 EXPECT_NE(nullptr, | |
134 DelayNavigationThrottle::MaybeCreateThrottleFor(test_handle.get())); | |
135 } | |
136 | |
137 TEST_F(DelayNavigationThrottleTest, NoThrottleForNonHttpOrHttpsURL) { | |
138 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); | |
139 ScopedDelayNavigationFeatureParams params( | |
140 base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
141 {{DelayNavigationThrottle::kParamDelayNavigationDurationMillis, "10"}, | |
142 {DelayNavigationThrottle::kParamDelayNavigationProbability, "1"}}); | |
143 GURL url("chrome://version"); | |
144 std::unique_ptr<content::NavigationHandle> test_handle = | |
145 content::NavigationHandle::CreateNavigationHandleForTesting(url, | |
146 main_rfh()); | |
147 EXPECT_EQ(nullptr, | |
148 DelayNavigationThrottle::MaybeCreateThrottleFor(test_handle.get())); | |
149 } | |
150 | |
151 TEST_F(DelayNavigationThrottleTest, NoThrottleWhenProbabilityZero) { | |
152 base::FieldTrialList field_trial_list(nullptr /* entropy_provider */); | |
153 ScopedDelayNavigationFeatureParams params( | |
154 base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
155 {{DelayNavigationThrottle::kParamDelayNavigationDurationMillis, "10"}, | |
156 {DelayNavigationThrottle::kParamDelayNavigationProbability, "0"}}); | |
157 GURL url("http://www.example.com/"); | |
158 std::unique_ptr<content::NavigationHandle> test_handle = | |
159 content::NavigationHandle::CreateNavigationHandleForTesting(url, | |
160 main_rfh()); | |
161 EXPECT_EQ(nullptr, | |
162 DelayNavigationThrottle::MaybeCreateThrottleFor(test_handle.get())); | |
163 } | |
OLD | NEW |