#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 {
class RendererSideContentDecodingBrowserTest
: public ContentBrowserTest,
public ::testing::WithParamInterface<bool> {
public:
RendererSideContentDecodingBrowserTest() {
features_.InitWithFeaturesAndParameters(
{{network::features::kRendererSideContentDecoding,
{{"RendererSideContentDecodingForceMojoFailureForTesting",
ShouldSucceed() ? "false" : "true"}}}},
{});
}
~RendererSideContentDecodingBrowserTest() override = default;
static std::string DescribeParams(
const testing::TestParamInfo<ParamType>& info) {
return info.param ? "ExpectSuccess" : "ExpectFail";
}
protected:
void StartServerAndNavigateToTestPage() {
ASSERT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/hello.html")));
}
bool ShouldSucceed() const { return GetParam(); }
private:
base::test::ScopedFeatureList features_;
};
INSTANTIATE_TEST_SUITE_P(
,
RendererSideContentDecodingBrowserTest,
::testing::Bool(),
&RendererSideContentDecodingBrowserTest::DescribeParams);
IN_PROC_BROWSER_TEST_P(RendererSideContentDecodingBrowserTest,
CompressedScriptLoad) {
StartServerAndNavigateToTestPage();
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);
});
)";
EXPECT_EQ(ShouldSucceed() ? "script executed" : "load error",
EvalJs(shell(), script));
log_watcher.FlushAndStopWatching();
EXPECT_EQ(log_watcher.last_message(),
ShouldSucceed()
? ""
: "Failed to load resource: net::ERR_INSUFFICIENT_RESOURCES");
}
IN_PROC_BROWSER_TEST_P(RendererSideContentDecodingBrowserTest,
CompressedPageNavigation) {
StartServerAndNavigateToTestPage();
std::u16string expected_title(u"page loaded");
TitleWatcher title_watcher(shell()->web_contents(), expected_title);
WebContentsConsoleObserver console_observer(shell()->web_contents());
console_observer.SetPattern("Failed to decode content");
EXPECT_TRUE(ExecJs(shell(), JsReplace("window.location.href = $1",
"./loader/compressed.html.gz")));
if (ShouldSucceed()) {
EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
} else {
ASSERT_TRUE(console_observer.Wait());
}
}
}