Commit 9781ff27 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

Revert "Refactor Reader Mode's JavaScript tests to use Mocha."

This reverts commit 7de90169.

Reason for revert: Suspicious to have broken DomDistillerViewerSourceBrowserTest.UISetsPrefs in browser_tests.
https://ci.chromium.org/p/chromium/builders/ci/Linux%20Chromium%20OS%20ASan%20LSan%20Tests%20%281%29/37476


Original change's description:
> 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/+/1992504
> Reviewed-by: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
> Commit-Queue: Aran Gilman <gilmanmh@google.com>
> Cr-Commit-Position: refs/heads/master@{#762551}

TBR=wychen@chromium.org,gilmanmh@google.com

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 1027612
Change-Id: I78b6b82e98c8f47951ab88e2056595de878f1ab1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2166219Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#762720}
parent 7104027b
......@@ -561,7 +561,6 @@ 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,6 +22,22 @@
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()
......@@ -37,13 +53,14 @@ class DistilledPageJsTest : public content::ContentBrowserTest {
distilled_page_ = SetUpTestServerWithDistilledPage(embedded_test_server());
}
void LoadAndExecuteTestScript(const std::string& file) {
void LoadAndExecuteTestScript(const std::string& file,
const std::string& fixture_name) {
distilled_page_->AppendScriptFile(file);
distilled_page_->Load(embedded_test_server(), shell()->web_contents());
bool allTestsPassed;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
shell()->web_contents(), "mocha.run()", &allTestsPassed));
EXPECT_TRUE(allTestsPassed);
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());
}
std::unique_ptr<FakeDistilledPage> distilled_page_;
......@@ -55,7 +72,7 @@ class DistilledPageJsTest : public content::ContentBrowserTest {
#define MAYBE_Pinch Pinch
#endif
IN_PROC_BROWSER_TEST_F(DistilledPageJsTest, MAYBE_Pinch) {
LoadAndExecuteTestScript("pinch_tester.js");
LoadAndExecuteTestScript("pinch_tester.js", "pinchtest");
}
} // namespace
......
......@@ -39,12 +39,10 @@ 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/dom_distiller/core/javascript"));
server->ServeFilesFromDirectory(
root_dir.AppendASCII("components/test/data/dom_distiller"));
server->ServeFilesFromDirectory(root_dir.AppendASCII("third_party/mocha"));
server->ServeFilesFromDirectory(
root_dir.AppendASCII("components/dom_distiller/core/javascript"));
}
} // namespace
......@@ -60,8 +58,6 @@ 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