1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/weak_ptr.h"
6 #include "base/path_service.h"
7 #include "base/run_loop.h"
8 #include "base/values.h"
9 #include "components/dom_distiller/content/distiller_page_web_contents.h"
10 #include "components/dom_distiller/core/distiller_page.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/shell/browser/shell.h"
14 #include "grit/component_resources.h"
15 #include "net/test/embedded_test_server/embedded_test_server.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "ui/base/resource/resource_bundle.h"
19 using content::ContentBrowserTest
;
20 using testing::ContainsRegex
;
21 using testing::HasSubstr
;
24 namespace dom_distiller
{
26 class DistillerPageWebContentsTest
: public ContentBrowserTest
{
28 // ContentBrowserTest:
29 virtual void SetUpOnMainThread() OVERRIDE
{
30 AddComponentsResources();
32 ContentBrowserTest::SetUpOnMainThread();
35 void DistillPage(const base::Closure
& quit_closure
, const std::string
& url
) {
36 quit_closure_
= quit_closure
;
37 distiller_page_
->DistillPage(
38 embedded_test_server()->GetURL(url
),
39 base::Bind(&DistillerPageWebContentsTest::OnPageDistillationFinished
,
43 void OnPageDistillationFinished(scoped_ptr
<DistilledPageInfo
> distilled_page
,
44 bool distillation_successful
) {
45 page_info_
= distilled_page
.Pass();
50 void AddComponentsResources() {
51 base::FilePath pak_file
;
52 base::FilePath pak_dir
;
53 PathService::Get(base::DIR_MODULE
, &pak_dir
);
54 pak_file
= pak_dir
.Append(FILE_PATH_LITERAL("components_resources.pak"));
55 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
56 pak_file
, ui::SCALE_FACTOR_NONE
);
59 void SetUpTestServer() {
61 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
62 path
= path
.AppendASCII("components/test/data/dom_distiller");
63 embedded_test_server()->ServeFilesFromDirectory(path
);
64 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
68 DistillerPageWebContents
* distiller_page_
;
69 base::Closure quit_closure_
;
70 scoped_ptr
<DistilledPageInfo
> page_info_
;
73 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest
, BasicDistillationWorks
) {
74 DistillerPageWebContents
distiller_page(
75 shell()->web_contents()->GetBrowserContext());
76 distiller_page_
= &distiller_page
;
78 base::RunLoop run_loop
;
79 DistillPage(run_loop
.QuitClosure(), "/simple_article.html");
82 EXPECT_EQ("Test Page Title", page_info_
.get()->title
);
83 EXPECT_THAT(page_info_
.get()->html
, HasSubstr("Lorem ipsum"));
84 EXPECT_THAT(page_info_
.get()->html
, Not(HasSubstr("questionable content")));
85 EXPECT_EQ("", page_info_
.get()->next_page_url
);
86 EXPECT_EQ("", page_info_
.get()->prev_page_url
);
89 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest
, HandlesRelativeLinks
) {
90 DistillerPageWebContents
distiller_page(
91 shell()->web_contents()->GetBrowserContext());
92 distiller_page_
= &distiller_page
;
94 base::RunLoop run_loop
;
95 DistillPage(run_loop
.QuitClosure(), "/simple_article.html");
98 // A relative link should've been updated.
99 EXPECT_THAT(page_info_
.get()->html
,
100 ContainsRegex("href=\"http://127.0.0.1:.*/relativelink.html\""));
101 EXPECT_THAT(page_info_
.get()->html
,
102 HasSubstr("href=\"http://www.google.com/absolutelink.html\""));
105 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest
, HandlesRelativeImages
) {
106 DistillerPageWebContents
distiller_page(
107 shell()->web_contents()->GetBrowserContext());
108 distiller_page_
= &distiller_page
;
110 base::RunLoop run_loop
;
111 DistillPage(run_loop
.QuitClosure(), "/simple_article.html");
114 // A relative link should've been updated.
115 EXPECT_THAT(page_info_
.get()->html
,
116 ContainsRegex("src=\"http://127.0.0.1:.*/relativeimage.png\""));
117 EXPECT_THAT(page_info_
.get()->html
,
118 HasSubstr("src=\"http://www.google.com/absoluteimage.png\""));
121 } // namespace dom_distiller