Commit 5f1264ad authored by Aran Gilman's avatar Aran Gilman Committed by Commit Bot

Create separate test file for UI-related JavaScript on distilled page.

Bug: 1027612
Change-Id: Ia7baa75e8c66f6d5ec255bc6a044640685c83db8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1977380Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Commit-Queue: Aran Gilman <gilmanmh@google.com>
Cr-Commit-Position: refs/heads/master@{#731693}
parent 8b0eaa0c
...@@ -519,7 +519,10 @@ if (!is_ios && !is_fuchsia) { ...@@ -519,7 +519,10 @@ if (!is_ios && !is_fuchsia) {
"autofill/content/renderer/password_form_conversion_utils_browsertest.cc", "autofill/content/renderer/password_form_conversion_utils_browsertest.cc",
"dom_distiller/content/browser/distillable_page_utils_browsertest.cc", "dom_distiller/content/browser/distillable_page_utils_browsertest.cc",
"dom_distiller/content/browser/distiller_page_web_contents_browsertest.cc", "dom_distiller/content/browser/distiller_page_web_contents_browsertest.cc",
"dom_distiller/content/browser/test/distilled_page_js_browsertest.cc",
"dom_distiller/content/browser/test/dom_distiller_js_browsertest.cc", "dom_distiller/content/browser/test/dom_distiller_js_browsertest.cc",
"dom_distiller/content/browser/test/test_util.cc",
"dom_distiller/content/browser/test/test_util.h",
"offline_pages/content/renovations/test/page_renovator_browsertest.cc", "offline_pages/content/renovations/test/page_renovator_browsertest.cc",
"paint_preview/renderer/paint_preview_recorder_browsertest.cc", "paint_preview/renderer/paint_preview_recorder_browsertest.cc",
"security_state/content/content_utils_browsertest.cc", "security_state/content/content_utils_browsertest.cc",
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "base/values.h" #include "base/values.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/dom_distiller/content/browser/distiller_javascript_utils.h" #include "components/dom_distiller/content/browser/distiller_javascript_utils.h"
#include "components/dom_distiller/content/browser/test/test_util.h"
#include "components/dom_distiller/core/distiller_page.h" #include "components/dom_distiller/core/distiller_page.h"
#include "components/dom_distiller/core/proto/distilled_article.pb.h" #include "components/dom_distiller/core/proto/distilled_article.pb.h"
#include "components/dom_distiller/core/proto/distilled_page.pb.h" #include "components/dom_distiller/core/proto/distilled_page.pb.h"
...@@ -83,24 +84,6 @@ namespace dom_distiller { ...@@ -83,24 +84,6 @@ namespace dom_distiller {
const char* kSimpleArticlePath = "/simple_article.html"; const char* kSimpleArticlePath = "/simple_article.html";
const char* kVideoArticlePath = "/video_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::OnceClosure callback, base::Value* out, base::Value result) {
(*out) = std::move(result);
std::move(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 { class DistillerPageWebContentsTest : public ContentBrowserTest {
public: public:
...@@ -110,7 +93,7 @@ class DistillerPageWebContentsTest : public ContentBrowserTest { ...@@ -110,7 +93,7 @@ class DistillerPageWebContentsTest : public ContentBrowserTest {
SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END); SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END);
} }
AddComponentsResources(); AddComponentsResources();
SetUpTestServer(); SetUpTestServer(embedded_test_server());
ContentBrowserTest::SetUpOnMainThread(); ContentBrowserTest::SetUpOnMainThread();
} }
...@@ -131,37 +114,6 @@ class DistillerPageWebContentsTest : public ContentBrowserTest { ...@@ -131,37 +114,6 @@ class DistillerPageWebContentsTest : public ContentBrowserTest {
std::move(quit_closure).Run(); std::move(quit_closure).Run();
} }
private:
void AddComponentsResources() {
base::FilePath pak_file;
base::FilePath pak_dir;
#if defined(OS_ANDROID)
CHECK(base::PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir));
pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks"));
#else
base::PathService::Get(base::DIR_MODULE, &pak_dir);
#endif // OS_ANDROID
pak_file =
pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak"));
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
pak_file, ui::SCALE_FACTOR_NONE);
}
void SetUpTestServer() {
base::FilePath path;
base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
embedded_test_server()->ServeFilesFromDirectory(
path.AppendASCII("components/test/data/dom_distiller"));
embedded_test_server()->ServeFilesFromDirectory(
path.AppendASCII("components/dom_distiller/core/javascript"));
response_ = std::make_unique<net::test_server::ControllableHttpResponse>(
embedded_test_server(), kDistilledPagePath);
ASSERT_TRUE(embedded_test_server()->Start());
}
protected: protected:
void RunUseCurrentWebContentsTest(const std::string& url, void RunUseCurrentWebContentsTest(const std::string& url,
bool expect_new_web_contents, bool expect_new_web_contents,
...@@ -169,8 +121,6 @@ class DistillerPageWebContentsTest : public ContentBrowserTest { ...@@ -169,8 +121,6 @@ class DistillerPageWebContentsTest : public ContentBrowserTest {
DistillerPageWebContents* distiller_page_; DistillerPageWebContents* distiller_page_;
std::unique_ptr<proto::DomDistillerResult> distiller_result_; std::unique_ptr<proto::DomDistillerResult> distiller_result_;
std::unique_ptr<net::test_server::ControllableHttpResponse> response_;
}; };
// Use this class to be able to leak the WebContents, which is needed for when // Use this class to be able to leak the WebContents, which is needed for when
...@@ -534,36 +484,4 @@ IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, ...@@ -534,36 +484,4 @@ IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest,
} }
} }
#if defined(OS_WIN)
#define MAYBE_TestPinch DISABLED_TestPinch
#else
#define MAYBE_TestPinch TestPinch
#endif
IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, MAYBE_TestPinch) {
// Load the test file in content shell and wait until it has fully loaded.
content::WebContents* web_contents = shell()->web_contents();
base::RunLoop url_loaded_runner;
WebContentsMainFrameHelper main_frame_loaded(
web_contents, url_loaded_runner.QuitClosure(), true);
web_contents->GetController().LoadURL(
embedded_test_server()->GetURL(kDistilledPagePath), content::Referrer(),
ui::PAGE_TRANSITION_TYPED, std::string());
const std::string html_template = viewer::GetArticleTemplateHtml(
DistilledPagePrefs::THEME_LIGHT,
DistilledPagePrefs::FONT_FAMILY_SANS_SERIF);
const std::string scripts = R"(
<script src='dom_distiller_viewer.js'></script>
<script src='pinch_tester.js'></script>
)";
response_->WaitForRequest();
response_->Send(net::HTTP_OK, "text/html", html_template + scripts);
response_->Done();
url_loaded_runner.Run();
ExecuteJsScript(web_contents, "pinchtest.run()");
}
} // namespace dom_distiller } // namespace dom_distiller
// Copyright 2019 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.
#include <memory>
#include <string>
#include <vector>
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "build/build_config.h"
#include "components/dom_distiller/content/browser/distiller_javascript_utils.h"
#include "components/dom_distiller/content/browser/test/test_util.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/shell/browser/shell.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/scale_factor.h"
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()
: content::ContentBrowserTest(), distilled_page_(nullptr) {}
~DistilledPageJsTest() override = default;
void SetUpOnMainThread() override {
if (!DistillerJavaScriptWorldIdIsSet()) {
SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END);
}
AddComponentsResources();
distilled_page_ = SetUpTestServerWithDistilledPage(embedded_test_server());
}
void LoadAndExecuteTestScript(const std::string& file,
const std::string& fixture_name) {
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());
}
std::unique_ptr<FakeDistilledPage> distilled_page_;
};
#if defined(OS_WIN)
#define MAYBE_Pinch DISABLED_Pinch
#else
#define MAYBE_Pinch Pinch
#endif
IN_PROC_BROWSER_TEST_F(DistilledPageJsTest, MAYBE_Pinch) {
LoadAndExecuteTestScript("pinch_tester.js", "pinchtest");
}
} // namespace
} // namespace dom_distiller
// Copyright 2019 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.
#include "components/dom_distiller/content/browser/test/test_util.h"
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/strings/strcat.h"
#include "build/build_config.h"
#include "components/dom_distiller/core/viewer.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/scale_factor.h"
namespace dom_distiller {
namespace {
using base::FilePath;
using base::PathService;
using base::StrAppend;
using content::JsReplace;
using content::Referrer;
using content::WaitForLoadStop;
using content::WebContents;
using net::test_server::ControllableHttpResponse;
using net::test_server::EmbeddedTestServer;
using viewer::GetArticleTemplateHtml;
// The path of the distilled page URL relative to the EmbeddedTestServer's base
// directory. This file's contents are generated at test runtime; it is not a
// real file in the repository.
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"));
}
} // namespace
FakeDistilledPage::FakeDistilledPage(EmbeddedTestServer* server)
: response_(
std::make_unique<ControllableHttpResponse>(server,
kDistilledPagePath)) {
CHECK(!server->Started());
// The distilled page HTML does not contain a script element loading this
// file. On a real distilled page, this file is executed by
// DomDistillerRequestViewBase::SendCommonJavaScript(); however, this method
// is impractical to use in testing.
AppendScriptFile("dom_distiller_viewer.js");
}
FakeDistilledPage::~FakeDistilledPage() = default;
void FakeDistilledPage::AppendScriptFile(const std::string& script_file) {
scripts_.push_back(script_file);
}
void FakeDistilledPage::Load(EmbeddedTestServer* server,
WebContents* web_contents) {
web_contents->GetController().LoadURL(server->GetURL(kDistilledPagePath),
Referrer(), ui::PAGE_TRANSITION_TYPED,
std::string());
response_->WaitForRequest();
response_->Send(net::HTTP_OK, "text/html", GetPageHtmlWithScripts());
response_->Done();
ASSERT_TRUE(WaitForLoadStop(web_contents));
}
std::string FakeDistilledPage::GetPageHtmlWithScripts() {
std::string html =
GetArticleTemplateHtml(DistilledPagePrefs::THEME_LIGHT,
DistilledPagePrefs::FONT_FAMILY_SANS_SERIF);
for (const std::string& file : scripts_) {
StrAppend(&html, {JsReplace("<script src=$1></script>", file)});
}
return html;
}
void SetUpTestServer(EmbeddedTestServer* server) {
SetUpTestServerWithoutStarting(server);
ASSERT_TRUE(server->Start());
}
std::unique_ptr<FakeDistilledPage> SetUpTestServerWithDistilledPage(
EmbeddedTestServer* server) {
SetUpTestServerWithoutStarting(server);
auto distilled_page = std::make_unique<FakeDistilledPage>(server);
// CHECKs for server start instead of ASSERTs because ASSERT/EXPECT macros
// only work in functions with a return type of void.
CHECK(server->Start());
return distilled_page;
}
void AddComponentsResources() {
FilePath pak_file;
FilePath pak_dir;
#if defined(OS_ANDROID)
CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir));
pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks"));
#else
PathService::Get(base::DIR_MODULE, &pak_dir);
#endif // OS_ANDROID
pak_file =
pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak"));
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
pak_file, ui::SCALE_FACTOR_NONE);
}
} // namespace dom_distiller
// Copyright 2019 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.
#ifndef COMPONENTS_DOM_DISTILLER_CONTENT_BROWSER_TEST_TEST_UTIL_H_
#define COMPONENTS_DOM_DISTILLER_CONTENT_BROWSER_TEST_TEST_UTIL_H_
#include <memory>
#include <string>
#include <vector>
namespace net {
namespace test_server {
class EmbeddedTestServer;
class ControllableHttpResponse;
} // namespace test_server
} // namespace net
namespace content {
class WebContents;
}
namespace dom_distiller {
// Wrapper for building and loading a fake distilled page for testing.
//
// This object MUST be constructed prior to starting |server|. This is enforced
// via CHECK.
class FakeDistilledPage {
public:
explicit FakeDistilledPage(net::test_server::EmbeddedTestServer* server);
~FakeDistilledPage();
// Adds |script_file| to the list of scripts to load along with the page HTML.
//
// Scripts are loaded by appending script elements with src=|script_file| to
// the end of the HTML template. This function must be called before Load() to
// have an effect.
void AppendScriptFile(const std::string& script_file);
// Generates the distilled page HTML and loads it to |web_contents|.
void Load(net::test_server::EmbeddedTestServer* server,
content::WebContents* web_contents);
// Disallow copy and assign because it's not obvious how these operations
// should handle |response_|.
FakeDistilledPage(const FakeDistilledPage&) = delete;
FakeDistilledPage& operator=(const FakeDistilledPage&) = delete;
private:
// Generates the distilled page's HTML and appends script elements for scripts
// added via AppendScriptFile().
std::string GetPageHtmlWithScripts();
std::unique_ptr<net::test_server::ControllableHttpResponse> response_;
std::vector<const std::string> scripts_;
};
// Starts |server| after initializing it to load files from the following
// directories:
// * components/test/data/dom_distiller
// * components/dom_distiller/core/javascript
void SetUpTestServer(net::test_server::EmbeddedTestServer* server);
// Same as SetUpTestServer(), but allows the server to load a distilled page
// generated at runtime without going through the full distillation process.
std::unique_ptr<FakeDistilledPage> SetUpTestServerWithDistilledPage(
net::test_server::EmbeddedTestServer* server);
// Sets the path to the .pak file to use when loading resources.
void AddComponentsResources();
} // namespace dom_distiller
#endif // COMPONENTS_DOM_DISTILLER_CONTENT_BROWSER_TEST_TEST_UTIL_H_
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