Commit 7de90169 authored by Aran Gilman's avatar Aran Gilman Committed by Commit Bot

Refactor Reader Mode's JavaScript tests to use Mocha.

The tests did not previously use an established test framework, making
tasks like executing certain code before or after every test and testing
asynchronous code more difficult. It should also be easier for
developers familiar with JavaScript testing in general and WebUI testing
in particular to read and write JavaScript tests for DOM Distiller and
Reader Mode.

Follow-up work includes replacing the custom assert methods with Chai,
as well as writing more tests to cover existing functionality.

Bug: 1027612
Change-Id: I39acc923a0424881d2694281d9aa0323c8f9a913
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1992504Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Commit-Queue: Aran Gilman <gilmanmh@google.com>
Cr-Commit-Position: refs/heads/master@{#762551}
parent 70c0a780
......@@ -561,6 +561,7 @@ if (!is_ios && !is_fuchsia) {
"security_state/content/testdata/",
"//content/test/data/",
"//third_party/dom_distiller_js/dist/test/data/",
"//third_party/mocha",
]
deps = [
......
......@@ -22,22 +22,6 @@
namespace dom_distiller {
namespace {
base::Value ExecuteJsScript(content::WebContents* web_contents,
const std::string& script) {
base::Value result;
base::RunLoop run_loop;
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::UTF8ToUTF16(script),
base::BindOnce(
[](base::Closure callback, base::Value* out, base::Value result) {
(*out) = std::move(result);
callback.Run();
},
run_loop.QuitClosure(), &result));
run_loop.Run();
return result;
}
class DistilledPageJsTest : public content::ContentBrowserTest {
protected:
explicit DistilledPageJsTest()
......@@ -53,14 +37,13 @@ class DistilledPageJsTest : public content::ContentBrowserTest {
distilled_page_ = SetUpTestServerWithDistilledPage(embedded_test_server());
}
void LoadAndExecuteTestScript(const std::string& file,
const std::string& fixture_name) {
void LoadAndExecuteTestScript(const std::string& file) {
distilled_page_->AppendScriptFile(file);
distilled_page_->Load(embedded_test_server(), shell()->web_contents());
const base::Value result = ExecuteJsScript(
shell()->web_contents(), base::StrCat({fixture_name, ".run()"}));
ASSERT_EQ(base::Value::Type::BOOLEAN, result.type());
EXPECT_TRUE(result.GetBool());
bool allTestsPassed;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
shell()->web_contents(), "mocha.run()", &allTestsPassed));
EXPECT_TRUE(allTestsPassed);
}
std::unique_ptr<FakeDistilledPage> distilled_page_;
......@@ -72,7 +55,7 @@ class DistilledPageJsTest : public content::ContentBrowserTest {
#define MAYBE_Pinch Pinch
#endif
IN_PROC_BROWSER_TEST_F(DistilledPageJsTest, MAYBE_Pinch) {
LoadAndExecuteTestScript("pinch_tester.js", "pinchtest");
LoadAndExecuteTestScript("pinch_tester.js");
}
} // namespace
......
......@@ -39,10 +39,12 @@ const char* kDistilledPagePath = "/distilled_page.html";
void SetUpTestServerWithoutStarting(EmbeddedTestServer* server) {
FilePath root_dir;
PathService::Get(base::DIR_SOURCE_ROOT, &root_dir);
server->ServeFilesFromDirectory(
root_dir.AppendASCII("components/test/data/dom_distiller"));
server->ServeFilesFromDirectory(
root_dir.AppendASCII("components/dom_distiller/core/javascript"));
server->ServeFilesFromDirectory(
root_dir.AppendASCII("components/test/data/dom_distiller"));
server->ServeFilesFromDirectory(root_dir.AppendASCII("third_party/mocha"));
}
} // namespace
......@@ -58,6 +60,8 @@ FakeDistilledPage::FakeDistilledPage(EmbeddedTestServer* server)
// DomDistillerRequestViewBase::SendCommonJavaScript(); however, this method
// is impractical to use in testing.
AppendScriptFile("dom_distiller_viewer.js");
AppendScriptFile("mocha.js");
AppendScriptFile("test_util.js");
}
FakeDistilledPage::~FakeDistilledPage() = default;
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Based on BrowserTestReporter() in //chrome/test/data/webui/mocha_adapter.js
// TODO(crbug.com/1027612): Look into using that class directly.
function TestReporter(runner) {
let passes = 0;
let failures = 0;
runner.on('pass', function(test) {
passes++;
});
runner.on('fail', function(test, err) {
failures++;
let message = 'Mocha test failed: ' + test.fullTitle() + '\n';
// Remove unhelpful mocha lines from stack trace.
if (err.stack) {
const stack = err.stack.split('\n');
for (let i = 0; i < stack.length; i++) {
if (stack[i].indexOf('mocha.js:') == -1) {
message += stack[i] + '\n';
}
}
} else {
message += err.toString();
}
console.error(message);
});
runner.on('end', function() {
window.domAutomationController.send(failures === 0 && passes > 0);
});
}
mocha.setup({
ui: 'tdd',
reporter: TestReporter,
enableTimeouts: false,
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment