File: renderer_side_content_decoding_browsertests.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 5,384 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Browser tests for the RendererSideContentDecoding feature, specifically
// testing scenarios involving successful decoding and simulated failures during
// data pipe creation.

#include <string_view>

#include "base/test/scoped_feature_list.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "services/network/public/cpp/features.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

// Test fixture for RendererSideContentDecoding browser tests.
// Parameterized to run tests in two modes:
// - ExpectSuccess (true): Normal operation, Mojo data pipe creation succeeds.
// - ExpectFail (false): Simulates Mojo data pipe creation failure using the
//   `RendererSideContentDecodingForceMojoFailureForTesting` feature parameter.
class RendererSideContentDecodingBrowserTest
    : public ContentBrowserTest,
      public ::testing::WithParamInterface<bool> {
 public:
  RendererSideContentDecodingBrowserTest() {
    // Enable the main feature and optionally the failure simulation parameter
    // based on the test parameter (`GetParam()`).
    features_.InitWithFeaturesAndParameters(
        {{network::features::kRendererSideContentDecoding,
          {{"RendererSideContentDecodingForceMojoFailureForTesting",
            ShouldSucceed() ? "false" : "true"}}}},
        {});
  }
  ~RendererSideContentDecodingBrowserTest() override = default;

  // Provides human-readable names for the test parameter values.
  static std::string DescribeParams(
      const testing::TestParamInfo<ParamType>& info) {
    return info.param ? "ExpectSuccess" : "ExpectFail";
  }

 protected:
  // Starts the embedded test server and navigates the shell to a simple page.
  void StartServerAndNavigateToTestPage() {
    ASSERT_TRUE(embedded_test_server()->Start());
    EXPECT_TRUE(
        NavigateToURL(shell(), embedded_test_server()->GetURL("/hello.html")));
  }

  // Returns true if the current test parameter expects success (no simulated
  // failure), false otherwise.
  bool ShouldSucceed() const { return GetParam(); }

 private:
  base::test::ScopedFeatureList features_;
};

INSTANTIATE_TEST_SUITE_P(
    /* No prefix */,
    RendererSideContentDecodingBrowserTest,
    // Run with both true (ExpectSuccess) and false (ExpectFail).
    ::testing::Bool(),
    &RendererSideContentDecodingBrowserTest::DescribeParams);

// Tests loading a compressed Javascript file (.js.gz).
// In ExpectSuccess mode, the script should execute.
// In ExpectFail mode, the script load should fail, triggering the 'error'
// event, and a specific error message should be logged to the console.
IN_PROC_BROWSER_TEST_P(RendererSideContentDecodingBrowserTest,
                       CompressedScriptLoad) {
  StartServerAndNavigateToTestPage();
  // Watch for console errors related to resource loading.
  DevToolsInspectorLogWatcher log_watcher(shell()->web_contents());
  const std::string_view script = R"(
    new Promise(resolve => {
        window.resolveTest = resolve;
        const script = document.createElement('script');
        script.src = './loader/compressed.js.gz';
        script.addEventListener('error', () => {resolve('load error')});
        document.body.appendChild(script);
      });
  )";
  // compressed.js.gz contains: window.resolveTest('script executed');
  EXPECT_EQ(ShouldSucceed() ? "script executed" : "load error",
            EvalJs(shell(), script));

  log_watcher.FlushAndStopWatching();
  // Verify the console message in case of failure.
  EXPECT_EQ(log_watcher.last_message(),
            ShouldSucceed()
                ? ""
                : "Failed to load resource: net::ERR_INSUFFICIENT_RESOURCES");
}

// Tests navigating to a compressed HTML file (.html.gz).
// In ExpectSuccess mode, the navigation should complete, and the page title
// should be set correctly.
// In ExpectFail mode, the navigation should fail, and a specific error message
// ("Failed to decode content") should be logged to the console by
// RenderFrameImpl.
IN_PROC_BROWSER_TEST_P(RendererSideContentDecodingBrowserTest,
                       CompressedPageNavigation) {
  StartServerAndNavigateToTestPage();
  // compressed.html.gz contains: <title>page loaded</title>
  std::u16string expected_title(u"page loaded");
  TitleWatcher title_watcher(shell()->web_contents(), expected_title);

  // Watch for the specific console error message logged on navigation failure.
  WebContentsConsoleObserver console_observer(shell()->web_contents());
  console_observer.SetPattern("Failed to decode content");

  // Initiate navigation to the compressed HTML file.
  EXPECT_TRUE(ExecJs(shell(), JsReplace("window.location.href = $1",
                                        "./loader/compressed.html.gz")));

  if (ShouldSucceed()) {
    // Wait for the title change to confirm successful navigation and decoding.
    EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
  } else {
    // Wait for the expected console error message.
    ASSERT_TRUE(console_observer.Wait());
  }
}

}  // namespace content