File: dev_ui_loader_throttle_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (405 lines) | stat: -rw-r--r-- 15,378 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/dev_ui/android/dev_ui_loader_throttle.h"

#include <deque>
#include <memory>
#include <utility>

#include "chrome/android/modules/dev_ui/provider/dev_ui_module_provider.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/mock_navigation_throttle_registry.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "url/gurl.h"

namespace dev_ui {

namespace {

const char* const kNonDevUiUrls[] = {
    "https://example.com",
    "https://example.com/path?query#frag",
    "chrome://credits",
    "chrome://credits/path?query#frag",
};

const char* const kDevUiUrls[] = {
    "chrome://bluetooth-internals",
    "chrome://bluetooth-internals/path?query#frag",
};

/******** MockDevUiModuleProvider ********/

// Mock DevUiModuleProvider that overrides the main module install / load
// functions, and provides SimulateAsyncInstall().
class MockDevUiModuleProvider : public DevUiModuleProvider {
 public:
  MockDevUiModuleProvider() = default;
  ~MockDevUiModuleProvider() override = default;

  // DevUiModuleProvider:
  bool ModuleInstalled() override { return is_installed_; }

  // DevUiModuleProvider:
  void InstallModule(base::OnceCallback<void(bool)> on_complete) override {
    on_complete_queue_.emplace_back(std::move(on_complete));
  }

  // DevUiModuleProvider:
  void EnsureLoaded() override { is_loaded_ = true; }

  void Reset() {
    is_installed_ = false;
    is_loaded_ = false;
    on_complete_queue_.clear();
  }

  // Simulate DevUI DFN install, and dispatches all on-complete callbacks.
  void SimulateAsyncInstall(bool install_succeeds) {
    is_installed_ = is_installed_ || install_succeeds;
    while (!on_complete_queue_.empty()) {
      std::move(on_complete_queue_.front()).Run(install_succeeds);
      on_complete_queue_.pop_front();
    }
  }

  // Direct access of fake installation states.
  void SetIsInstalled(bool is_installed) { is_installed_ = is_installed; }
  bool GetIsInstalled() const { return is_installed_; }
  void SetIsLoaded(bool is_loaded) { is_loaded_ = is_loaded; }
  bool GetIsLoaded() const { return is_loaded_; }

 private:
  bool is_installed_ = false;
  bool is_loaded_ = false;
  std::deque<base::OnceCallback<void(bool)>> on_complete_queue_;
};

/******** TestDevUiLoaderThrottle ********/

// DevUiLoaderThrottle that overrides and captures Resume() and
// CancelDeferredNavigation(). This is needed because:
// * In actual usage, both functions can lead to throttle deletion, which is
//   problematic since the tests own the throttle instance.
// * We want to record results for verification.
// Note that this is instantiated directly, instead of using
// DevUiLoaderThrottle::MaybeCreateAndAdd().
class TestDevUiLoaderThrottle : public DevUiLoaderThrottle {
 public:
  explicit TestDevUiLoaderThrottle(
      content::NavigationThrottleRegistry& registry)
      : DevUiLoaderThrottle(registry), cancel_result(LAST) {}
  ~TestDevUiLoaderThrottle() override = default;
  TestDevUiLoaderThrottle(const TestDevUiLoaderThrottle&) = delete;
  const TestDevUiLoaderThrottle& operator=(const TestDevUiLoaderThrottle&) =
      delete;

  // content::NavigationThrottle:
  void Resume() override { called_resume = true; }

  // content::NavigationThrottle:
  void CancelDeferredNavigation(
      content::NavigationThrottle::ThrottleCheckResult result) override {
    called_cancel = true;
    cancel_result = std::move(result);
  }

  bool called_resume = false;
  bool called_cancel = false;
  content::NavigationThrottle::ThrottleCheckResult cancel_result;
};

/******** DevUiLoaderThrottleTest ********/

class DevUiLoaderThrottleTest : public ChromeRenderViewHostTestHarness {
 public:
  DevUiLoaderThrottleTest() = default;
  ~DevUiLoaderThrottleTest() override = default;
  DevUiLoaderThrottleTest(const DevUiLoaderThrottleTest&) = delete;
  const DevUiLoaderThrottleTest& operator=(const DevUiLoaderThrottleTest&) =
      delete;

  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();
    DevUiModuleProvider::SetTestInstance(&mock_provider_);
  }

  void TearDown() override {
    mock_provider_.Reset();
    DevUiModuleProvider::SetTestInstance(nullptr);
    ChromeRenderViewHostTestHarness::TearDown();
  }

 protected:
  MockDevUiModuleProvider mock_provider_;
};

class DevUiLoaderThrottleFencedFrameTest : public DevUiLoaderThrottleTest {
 public:
  DevUiLoaderThrottleFencedFrameTest() {
    scoped_feature_list_.InitAndEnableFeatureWithParameters(
        blink::features::kFencedFrames, {{"implementation_type", "mparch"}});
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

}  // namespace

TEST_F(DevUiLoaderThrottleTest, ShouldInstallDevUiDfm) {
  auto ShouldInstallDevUiDfm = DevUiLoaderThrottle::ShouldInstallDevUiDfm;
  for (const char* url_string : kNonDevUiUrls) {
    EXPECT_FALSE(ShouldInstallDevUiDfm(GURL(url_string)));
  }
  for (const char* url_string : kDevUiUrls) {
    EXPECT_TRUE(ShouldInstallDevUiDfm(GURL(url_string)));
  }
}

// Test to ensure that pages that are purposefully left in the base module are
// not accidentally moved to the DevUI DFM.
TEST_F(DevUiLoaderThrottleTest, PreventAccidentalInclusion) {
  auto ShouldInstallDevUiDfm = DevUiLoaderThrottle::ShouldInstallDevUiDfm;

  // Useful to have the catalog always.
  EXPECT_FALSE(ShouldInstallDevUiDfm(GURL("chrome://chrome-urls")));
  EXPECT_FALSE(ShouldInstallDevUiDfm(GURL("chrome://about")));
  // Inclusion in base module is mandatory.
  EXPECT_FALSE(ShouldInstallDevUiDfm(GURL("chrome://credits")));
  // Well-loved game, and shown when there's no internet (cannot install DFM).
  EXPECT_FALSE(ShouldInstallDevUiDfm(GURL("chrome://dino")));
  // chrome://flags has relatively high usage.
  EXPECT_FALSE(ShouldInstallDevUiDfm(GURL("chrome://flags")));
  // Useful for filing bugs.
  EXPECT_FALSE(ShouldInstallDevUiDfm(GURL("chrome://version")));
  // Used by Android WebView.
  EXPECT_FALSE(ShouldInstallDevUiDfm(GURL("chrome://safe-browsing")));
}

TEST_F(DevUiLoaderThrottleTest, MaybeCreateAndAdd) {
  bool is_installed = false;
  auto creates_throttle = [&](const std::string& url_string) -> bool {
    mock_provider_.Reset();
    mock_provider_.SetIsInstalled(is_installed);
    content::MockNavigationHandle handle(GURL(url_string), main_rfh());
    content::MockNavigationThrottleRegistry registry(
        &handle,
        content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
    DevUiLoaderThrottle::MaybeCreateAndAdd(registry);
    return !registry.throttles().empty();
  };

  // Case 1: DevUI DFM is not installed.
  is_installed = false;
  // Case 1a: Non DevUI URLs: Throttle not created.
  for (const char* url_string : kNonDevUiUrls) {
    EXPECT_FALSE(creates_throttle(url_string));
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
  }
  // Case 1b: DevUI URLs: Throttle created, but no load takes place.
  for (const char* url_string : kDevUiUrls) {
    EXPECT_TRUE(creates_throttle(url_string));
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
  }

  // Case 2: DevUI DFM is installed.
  is_installed = true;
  // Case 2a: Non DevUI URLs: Throttle not created.
  for (const char* url_string : kNonDevUiUrls) {
    EXPECT_FALSE(creates_throttle(url_string));
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
  }
  // Case 2b: DevUI URLs: Throttle not created, but load takes place.
  for (const char* url_string : kDevUiUrls) {
    EXPECT_FALSE(creates_throttle(url_string));
    EXPECT_TRUE(mock_provider_.GetIsLoaded());
  }
}

TEST_F(DevUiLoaderThrottleFencedFrameTest, MaybeCreateAndAdd) {
  bool is_installed = false;
  content::RenderFrameHost* render_frame_host = nullptr;
  auto creates_throttle = [&](const std::string& url_string) -> bool {
    mock_provider_.Reset();
    mock_provider_.SetIsInstalled(is_installed);
    content::MockNavigationHandle handle(GURL(url_string), render_frame_host);
    content::MockNavigationThrottleRegistry registry(
        &handle,
        content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
    DevUiLoaderThrottle::MaybeCreateAndAdd(registry);
    return !registry.throttles().empty();
  };

  content::RenderFrameHostTester::For(main_rfh())
      ->InitializeRenderFrameIfNeeded();
  render_frame_host =
      content::RenderFrameHostTester::For(main_rfh())->AppendFencedFrame();
  EXPECT_TRUE(render_frame_host);

  // In any case, throttles should not be created in fenced frames.
  is_installed = false;
  for (const char* url_string : kNonDevUiUrls) {
    EXPECT_FALSE(creates_throttle(url_string));
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
  }
  for (const char* url_string : kDevUiUrls) {
    EXPECT_FALSE(creates_throttle(url_string));
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
  }
  is_installed = true;
  for (const char* url_string : kNonDevUiUrls) {
    EXPECT_FALSE(creates_throttle(url_string));
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
  }
  for (const char* url_string : kDevUiUrls) {
    EXPECT_FALSE(creates_throttle(url_string));
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
  }
}

TEST_F(DevUiLoaderThrottleTest, InstallSuccess) {
  for (const char* url_string : kDevUiUrls) {
    mock_provider_.Reset();
    content::MockNavigationHandle handle(GURL(url_string), main_rfh());
    content::MockNavigationThrottleRegistry registry(
        &handle,
        content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
    auto throttle = std::make_unique<TestDevUiLoaderThrottle>(registry);
    EXPECT_FALSE(throttle->called_resume);
    EXPECT_FALSE(throttle->called_cancel);
    EXPECT_FALSE(mock_provider_.GetIsInstalled());
    EXPECT_FALSE(mock_provider_.GetIsLoaded());

    // Simulate request start.
    EXPECT_EQ(content::NavigationThrottle::DEFER, throttle->WillStartRequest());
    EXPECT_FALSE(throttle->called_resume);
    EXPECT_FALSE(throttle->called_cancel);
    EXPECT_FALSE(mock_provider_.GetIsInstalled());
    EXPECT_FALSE(mock_provider_.GetIsLoaded());

    // Simulate actual install (success)
    mock_provider_.SimulateAsyncInstall(true);  // !
    EXPECT_TRUE(throttle->called_resume);
    EXPECT_FALSE(throttle->called_cancel);
    EXPECT_TRUE(mock_provider_.GetIsInstalled());
    EXPECT_TRUE(mock_provider_.GetIsLoaded());
  }
}

TEST_F(DevUiLoaderThrottleTest, InstallQueued) {
  mock_provider_.Reset();
  // Page 1 request.
  content::MockNavigationHandle handle1(GURL(kDevUiUrls[0]), main_rfh());
  content::MockNavigationThrottleRegistry registry1(
      &handle1,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle1 = std::make_unique<TestDevUiLoaderThrottle>(registry1);
  // Page 2 request.
  content::MockNavigationHandle handle2(GURL(kDevUiUrls[1]), main_rfh());
  content::MockNavigationThrottleRegistry registry2(
      &handle2,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle2 = std::make_unique<TestDevUiLoaderThrottle>(registry2);
  EXPECT_FALSE(mock_provider_.GetIsInstalled());
  EXPECT_FALSE(mock_provider_.GetIsLoaded());

  // Simulate request start of page 1. This triggers install.
  EXPECT_EQ(content::NavigationThrottle::DEFER, throttle1->WillStartRequest());

  // Simulate request start of page 2. This does not trigger install. However,
  // the on-complete callback is queued, and will be called.
  EXPECT_EQ(content::NavigationThrottle::DEFER, throttle2->WillStartRequest());

  // Simulate actual install.
  mock_provider_.SimulateAsyncInstall(true);  // !
  EXPECT_TRUE(throttle1->called_resume);
  EXPECT_FALSE(throttle1->called_cancel);
  EXPECT_TRUE(throttle2->called_resume);
  EXPECT_FALSE(throttle2->called_cancel);
  EXPECT_TRUE(mock_provider_.GetIsInstalled());
  EXPECT_TRUE(mock_provider_.GetIsLoaded());
}

TEST_F(DevUiLoaderThrottleTest, InstallRedundant) {
  mock_provider_.Reset();
  // Page 1 request.
  content::MockNavigationHandle handle1(GURL(kDevUiUrls[0]), main_rfh());
  content::MockNavigationThrottleRegistry registry1(
      &handle1,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle1 = std::make_unique<TestDevUiLoaderThrottle>(registry1);
  // Page 2 request.
  content::MockNavigationHandle handle2(GURL(kDevUiUrls[1]), main_rfh());
  content::MockNavigationThrottleRegistry registry2(
      &handle2,
      content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
  auto throttle2 = std::make_unique<TestDevUiLoaderThrottle>(registry2);
  EXPECT_FALSE(mock_provider_.GetIsInstalled());
  EXPECT_FALSE(mock_provider_.GetIsLoaded());

  // Simulate request start of page 1.
  EXPECT_EQ(content::NavigationThrottle::DEFER, throttle1->WillStartRequest());

  // Simulate actual install.
  mock_provider_.SimulateAsyncInstall(true);  // !
  EXPECT_TRUE(throttle1->called_resume);
  EXPECT_FALSE(throttle1->called_cancel);
  EXPECT_TRUE(mock_provider_.GetIsInstalled());
  EXPECT_TRUE(mock_provider_.GetIsLoaded());

  // Simulate request start of page 2. The request was made before install
  // completes, but request start occurs after install completes. In this case,
  // simply proceed.
  EXPECT_EQ(content::NavigationThrottle::PROCEED,
            throttle2->WillStartRequest());
  EXPECT_FALSE(throttle2->called_resume);
  EXPECT_FALSE(throttle2->called_cancel);
  EXPECT_TRUE(mock_provider_.GetIsInstalled());
  EXPECT_TRUE(mock_provider_.GetIsLoaded());
}

TEST_F(DevUiLoaderThrottleTest, InstallFailure) {
  for (const char* url_string : kDevUiUrls) {
    mock_provider_.Reset();
    content::MockNavigationHandle handle(GURL(url_string), main_rfh());
    content::MockNavigationThrottleRegistry registry(
        &handle,
        content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
    auto throttle = std::make_unique<TestDevUiLoaderThrottle>(registry);
    EXPECT_FALSE(throttle->called_resume);
    EXPECT_FALSE(throttle->called_cancel);
    EXPECT_FALSE(mock_provider_.GetIsInstalled());
    EXPECT_FALSE(mock_provider_.GetIsLoaded());

    // Simulate request start.
    EXPECT_EQ(content::NavigationThrottle::DEFER, throttle->WillStartRequest());
    EXPECT_FALSE(throttle->called_resume);
    EXPECT_FALSE(throttle->called_cancel);
    EXPECT_FALSE(mock_provider_.GetIsInstalled());
    EXPECT_FALSE(mock_provider_.GetIsLoaded());

    // Simulate actual install (failure)
    mock_provider_.SimulateAsyncInstall(false);  // !
    EXPECT_FALSE(throttle->called_resume);
    EXPECT_TRUE(throttle->called_cancel);
    EXPECT_FALSE(mock_provider_.GetIsInstalled());
    EXPECT_FALSE(mock_provider_.GetIsLoaded());
    EXPECT_EQ(content::NavigationThrottle::CANCEL,
              throttle->cancel_result.action());
    EXPECT_EQ(net::ERR_CONNECTION_FAILED,
              throttle->cancel_result.net_error_code());
    EXPECT_TRUE(throttle->cancel_result.error_page_content().has_value());
    std::string page_content =
        throttle->cancel_result.error_page_content().value();
    EXPECT_NE(std::string::npos, page_content.find("<!DOCTYPE html"));
  }
}

}  // namespace dev_ui