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/page_load_metrics/experiments/delay_navigation_throttle .h" | |
6 | |
7 #include <map> | |
8 #include <string> | |
9 #include <tuple> | |
10 #include <utility> | |
11 | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/test/scoped_feature_list.h" | |
16 #include "base/test/test_mock_time_task_runner.h" | |
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
18 #include "components/variations/variations_associated_data.h" | |
19 #include "content/public/browser/navigation_handle.h" | |
20 #include "net/http/http_util.h" | |
21 | |
22 class DelayNavigationThrottleTest : public ChromeRenderViewHostTestHarness {}; | |
23 | |
24 TEST_F(DelayNavigationThrottleTest, BasicDelay) { | |
25 const char kBasicResponseHeaders[] = "HTTP/1.1 200 OK"; | |
26 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
| |
27 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
| |
28 | |
29 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
| |
30 new base::TestMockTimeTaskRunner()); | |
31 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
| |
32 content::NavigationHandle::CreateNavigationHandleForTesting(url, | |
33 main_rfh()); | |
34 test_handle->RegisterThrottleForTesting( | |
35 base::MakeUnique<DelayNavigationThrottle>( | |
36 test_handle.get(), mock_time_task_runner, navigation_delay)); | |
37 | |
38 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
| |
39 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
| |
40 test_handle->CallWillStartRequestForTesting( | |
41 false /* is_post */, content::Referrer(), | |
42 false /* has_user_gesture */, ui::PAGE_TRANSITION_LINK, | |
43 false /* is_external_protocol */)); | |
44 | |
45 // There may be other throttles that DEFER and post async tasks to the UI | |
46 // thread. Allow them to run to completion, so our throttle is guaranteed to | |
47 // have a chance to run. | |
48 base::RunLoop().RunUntilIdle(); | |
49 | |
50 EXPECT_TRUE(mock_time_task_runner->HasPendingTask()); | |
51 EXPECT_FALSE(test_handle->HasCommitted()); | |
52 | |
53 mock_time_task_runner->FastForwardBy(navigation_delay); | |
54 EXPECT_FALSE(mock_time_task_runner->HasPendingTask()); | |
55 | |
56 // Run any remaining async tasks, to make sure all other deferred throttles | |
57 // can complete. | |
58 base::RunLoop().RunUntilIdle(); | |
59 | |
60 // Verify that the WillSendRequest portion of the navigation has completed, | |
61 // and NavigationHandle::WillProcessResponse and the commit portion of the | |
62 // navigation lifetime can now be invoked. | |
63 EXPECT_EQ(content::NavigationThrottle::PROCEED, | |
64 test_handle->CallWillProcessResponseForTesting( | |
65 main_rfh(), | |
66 net::HttpUtil::AssembleRawHeaders( | |
67 kBasicResponseHeaders, strlen(kBasicResponseHeaders)))); | |
68 test_handle->CallDidCommitNavigationForTesting(url); | |
69 EXPECT_TRUE(test_handle->HasCommitted()); | |
70 } | |
71 | |
72 enum ExpectInstantiationResult { | |
73 EXPECT_INSTANTIATION, | |
74 EXPECT_NO_INSTANTIATION | |
75 }; | |
76 | |
77 class DelayNavigationThrottleInstantiationTest | |
78 : public ChromeRenderViewHostTestHarness, | |
79 public testing::WithParamInterface< | |
80 std::tuple<ExpectInstantiationResult, | |
81 base::FeatureList::OverrideState, | |
82 base::TimeDelta, | |
83 double, | |
84 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.
| |
85 public: | |
86 DelayNavigationThrottleInstantiationTest() | |
87 : field_trial_list_(nullptr /* entropy_provider */) {} | |
88 | |
89 void SetUp() override { | |
90 ChromeRenderViewHostTestHarness::SetUp(); | |
91 | |
92 std::tie(expected_instantiation_result_, feature_state_, delay_, | |
93 probability_, url_) = GetParam(); | |
94 | |
95 std::map<std::string, std::string> variation_params( | |
96 {{DelayNavigationThrottle::kParamDelayNavigationDurationMillis, | |
97 base::IntToString(delay_.InMilliseconds())}, | |
98 {DelayNavigationThrottle::kParamDelayNavigationProbability, | |
99 base::DoubleToString(probability_)}}); | |
100 InitializeScopedFeatureList(feature_state_, variation_params); | |
101 } | |
102 | |
103 void TearDown() override { | |
104 ChromeRenderViewHostTestHarness::TearDown(); | |
105 variations::testing::ClearAllVariationParams(); | |
106 } | |
107 | |
108 void InitializeScopedFeatureList( | |
109 base::FeatureList::OverrideState feature_state, | |
110 const std::map<std::string, std::string>& variation_params) { | |
111 static const char kTestFieldTrialName[] = "TestTrial"; | |
112 static const char kTestExperimentGroupName[] = "TestGroup"; | |
113 | |
114 EXPECT_TRUE(variations::AssociateVariationParams( | |
115 kTestFieldTrialName, kTestExperimentGroupName, variation_params)); | |
116 | |
117 base::FieldTrial* field_trial = base::FieldTrialList::CreateFieldTrial( | |
118 kTestFieldTrialName, kTestExperimentGroupName); | |
119 | |
120 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
121 feature_list->RegisterFieldTrialOverride(kDelayNavigationFeature.name, | |
122 feature_state, field_trial); | |
123 | |
124 // Since we are adding a scoped feature list after browser start, copy over | |
125 // the existing feature list to prevent inconsistency. | |
126 base::FeatureList* existing_feature_list = base::FeatureList::GetInstance(); | |
127 if (existing_feature_list) { | |
128 std::string enabled_features; | |
129 std::string disabled_features; | |
130 base::FeatureList::GetInstance()->GetFeatureOverrides(&enabled_features, | |
131 &disabled_features); | |
132 feature_list->InitializeFromCommandLine(enabled_features, | |
133 disabled_features); | |
134 } | |
135 | |
136 scoped_feature_list_.InitWithFeatureList(std::move(feature_list)); | |
137 } | |
138 | |
139 base::FieldTrialList field_trial_list_; | |
140 base::test::ScopedFeatureList scoped_feature_list_; | |
141 | |
142 // Fields filled in via GetParam(): | |
143 base::FeatureList::OverrideState feature_state_; | |
144 base::TimeDelta delay_; | |
145 double probability_; | |
146 std::string url_; | |
147 ExpectInstantiationResult expected_instantiation_result_; | |
148 }; | |
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
| |
149 | |
150 TEST_P(DelayNavigationThrottleInstantiationTest, Instantiate) { | |
151 std::unique_ptr<content::NavigationHandle> test_handle = | |
152 content::NavigationHandle::CreateNavigationHandleForTesting(GURL(url_), | |
153 main_rfh()); | |
154 std::unique_ptr<content::NavigationThrottle> throttle = | |
155 DelayNavigationThrottle::MaybeCreateThrottleFor(test_handle.get()); | |
156 const bool expect_instantiation = | |
157 expected_instantiation_result_ == EXPECT_INSTANTIATION; | |
158 EXPECT_EQ(expect_instantiation, throttle != nullptr); | |
159 } | |
160 | |
161 INSTANTIATE_TEST_CASE_P( | |
162 InstantiateThrottle, | |
163 DelayNavigationThrottleInstantiationTest, | |
164 ::testing::Values( | |
165 std::make_tuple(EXPECT_NO_INSTANTIATION, | |
166 base::FeatureList::OVERRIDE_DISABLE_FEATURE, | |
167 base::TimeDelta::FromMilliseconds(10), | |
168 1.0, | |
169 "http://www.example.com/"), | |
170 std::make_tuple(EXPECT_NO_INSTANTIATION, | |
171 base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
172 base::TimeDelta::FromMilliseconds(10), | |
173 1.0, | |
174 "chrome://version"), | |
175 std::make_tuple(EXPECT_NO_INSTANTIATION, | |
176 base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
177 base::TimeDelta::FromMilliseconds(10), | |
178 0.0, | |
179 "http://www.example.com/"), | |
180 std::make_tuple(EXPECT_INSTANTIATION, | |
181 base::FeatureList::OVERRIDE_ENABLE_FEATURE, | |
182 base::TimeDelta::FromMilliseconds(10), | |
183 1.0, | |
184 "http://www.example.com/"))); | |
OLD | NEW |