Commit 3affb50a authored by Aran Gilman's avatar Aran Gilman Committed by Commit Bot

Factor out common code in distiller page browser test.

ExecuteJsScript() is used only once in this CL but will be used in
new tests added by https://crrev.com/c/1922788.

This CL also runs clang-format on pinch_tester.js and modifies it to
return a bool instead of a dict. The latter simplifies verification
of script execution in the C++ portion of the test.

Bug: 1016615
Change-Id: I2c847eb53556a30e991d988fb676c6a3fa5aee8a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1971702
Commit-Queue: Aran Gilman <gilmanmh@google.com>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#726585}
parent c98ab4b5
......@@ -83,6 +83,24 @@ namespace dom_distiller {
const char* kSimpleArticlePath = "/simple_article.html";
const char* kVideoArticlePath = "/video_article.html";
const char* kDistilledPagePath = "/distilled_page.html";
void 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();
ASSERT_EQ(base::Value::Type::BOOLEAN, result.type());
EXPECT_TRUE(result.GetBool());
}
class DistillerPageWebContentsTest : public ContentBrowserTest {
public:
......@@ -112,11 +130,6 @@ class DistillerPageWebContentsTest : public ContentBrowserTest {
quit_closure.Run();
}
void OnJsExecutionDone(base::Closure callback, base::Value value) {
js_result_ = std::move(value);
callback.Run();
}
private:
void AddComponentsResources() {
base::FilePath pak_file;
......@@ -143,7 +156,7 @@ class DistillerPageWebContentsTest : public ContentBrowserTest {
path.AppendASCII("components/dom_distiller/core/javascript"));
response_ = std::make_unique<net::test_server::ControllableHttpResponse>(
embedded_test_server(), "/pinch_tester.html");
embedded_test_server(), kDistilledPagePath);
ASSERT_TRUE(embedded_test_server()->Start());
}
......@@ -155,7 +168,6 @@ class DistillerPageWebContentsTest : public ContentBrowserTest {
DistillerPageWebContents* distiller_page_;
std::unique_ptr<proto::DomDistillerResult> distiller_result_;
base::Value js_result_;
std::unique_ptr<net::test_server::ControllableHttpResponse> response_;
};
......@@ -533,7 +545,7 @@ IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, MAYBE_TestPinch) {
WebContentsMainFrameHelper main_frame_loaded(
web_contents, url_loaded_runner.QuitClosure(), true);
web_contents->GetController().LoadURL(
embedded_test_server()->GetURL("/pinch_tester.html"), content::Referrer(),
embedded_test_server()->GetURL(kDistilledPagePath), content::Referrer(),
ui::PAGE_TRANSITION_TYPED, std::string());
const std::string html_template = viewer::GetArticleTemplateHtml(
......@@ -550,19 +562,7 @@ IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, MAYBE_TestPinch) {
response_->Done();
url_loaded_runner.Run();
// Execute the JS to run the tests, and wait until it has finished.
base::RunLoop run_loop;
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::UTF8ToUTF16("(function() {return pinchtest.run();})();"),
base::BindOnce(&DistillerPageWebContentsTest::OnJsExecutionDone,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
ASSERT_TRUE(js_result_.is_dict());
base::Optional<bool> value = js_result_.FindBoolKey("success");
ASSERT_TRUE(value.has_value());
EXPECT_TRUE(value.value());
ExecuteJsScript(web_contents, "pinchtest.run()");
}
} // namespace dom_distiller
......@@ -7,15 +7,15 @@ const pinchtest = (function() {
function assertTrue(condition, message) {
if (!condition) {
message = message || "Assertion failed";
message = message || 'Assertion failed';
console.trace();
throw new Error(message);
}
}
function assertClose(a, b, message) {
if (Math.abs(a-b) > 1e-5) {
message = message || "Assertion failed";
if (Math.abs(a - b) > 1e-5) {
message = message || 'Assertion failed';
console.log('"', a, '" and "', b, '" are not close.');
console.trace();
throw new Error(message);
......@@ -50,7 +50,7 @@ const pinchtest = (function() {
function assertEqual(a, b, message) {
if (!isEquivalent(a, b)) {
message = message || "Assertion failed";
message = message || 'Assertion failed';
console.log('"', a, '" and "', b, '" are not equal');
console.trace();
throw new Error(message);
......@@ -62,22 +62,17 @@ const pinchtest = (function() {
const points = {};
function lowestID() {
let ans = -1;
for(const key in points) {
for (const key in points) {
ans = Math.max(ans, key);
}
return ans + 1;
}
function changeTouchPoint (key, x, y, offsetX, offsetY) {
const e = {
clientX: x,
clientY: y,
pageX: x,
pageY: y
};
if (typeof(offsetX) === 'number') {
function changeTouchPoint(key, x, y, offsetX, offsetY) {
const e = {clientX: x, clientY: y, pageX: x, pageY: y};
if (typeof (offsetX) === 'number') {
e.clientX += offsetX;
}
if (typeof(offsetY) === 'number') {
if (typeof (offsetY) === 'number') {
e.clientY += offsetY;
}
points[key] = e;
......@@ -92,13 +87,10 @@ const pinchtest = (function() {
},
events: function() {
const arr = [];
for(const key in points) {
for (const key in points) {
arr.push(points[key]);
}
return {
touches: arr,
preventDefault: function(){}
};
return {touches: arr, preventDefault: function() {}};
}
};
});
......@@ -414,7 +406,7 @@ const pinchtest = (function() {
}
return {
run: function(){
run: function() {
testZoomOut();
testZoomIn();
testZoomOutAndPan();
......@@ -427,7 +419,7 @@ const pinchtest = (function() {
testFontScaling();
pincher.reset();
return {success: true};
return true;
}
};
}());
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