Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: chrome/browser/page_load_metrics/experiments/delay_navigation_throttle_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698