1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright 2019 The Chromium Authors
// 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/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_SRC_TEST_DATA_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/node/node_modules/chai"));
server->ServeFilesFromDirectory(
root_dir.AppendASCII("third_party/node/node_modules/mocha"));
}
} // 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");
// Also load test helper scripts.
AppendScriptFile("mocha.js");
AppendScriptFile("test_util.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(
mojom::Theme::kLight, mojom::FontFamily::kSansSerif, std::string(),
/*use_offline_data=*/false);
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 BUILDFLAG(IS_ANDROID)
CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir));
pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks"));
#elif BUILDFLAG(IS_MAC)
PathService::Get(base::DIR_MODULE, &pak_dir);
#else
PathService::Get(base::DIR_ASSETS, &pak_dir);
#endif // BUILDFLAG(IS_ANDROID)
pak_file =
pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak"));
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
pak_file, ui::kScaleFactorNone);
}
} // namespace dom_distiller
|