1 // Copyright 2014 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.
7 #include "base/command_line.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "components/dom_distiller/content/dom_distiller_viewer_source.h"
18 #include "components/dom_distiller/core/article_entry.h"
19 #include "components/dom_distiller/core/distilled_page_prefs.h"
20 #include "components/dom_distiller/core/distiller.h"
21 #include "components/dom_distiller/core/dom_distiller_service.h"
22 #include "components/dom_distiller/core/dom_distiller_store.h"
23 #include "components/dom_distiller/core/dom_distiller_test_util.h"
24 #include "components/dom_distiller/core/fake_distiller.h"
25 #include "components/dom_distiller/core/fake_distiller_page.h"
26 #include "components/dom_distiller/core/task_tracker.h"
27 #include "components/dom_distiller/core/url_constants.h"
28 #include "components/dom_distiller/core/url_utils.h"
29 #include "components/leveldb_proto/testing/fake_db.h"
30 #include "content/public/browser/render_view_host.h"
31 #include "content/public/browser/url_data_source.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/browser/web_contents_observer.h"
34 #include "content/public/test/browser_test_utils.h"
35 #include "grit/components_strings.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "ui/base/l10n/l10n_util.h"
40 namespace dom_distiller
{
42 using leveldb_proto::test::FakeDB
;
43 using test::FakeDistiller
;
44 using test::MockDistillerPage
;
45 using test::MockDistillerFactory
;
46 using test::MockDistillerPageFactory
;
47 using test::util::CreateStoreWithFakeDB
;
48 using testing::HasSubstr
;
53 const char kGetLoadIndicatorClassName
[] =
54 "window.domAutomationController.send("
55 "document.getElementById('loadingIndicator').className)";
57 const char kGetContent
[] =
58 "window.domAutomationController.send("
59 "document.getElementById('content').innerHTML)";
61 const char kGetBodyClass
[] =
62 "window.domAutomationController.send("
63 "document.body.className)";
65 void AddEntry(const ArticleEntry
& e
, FakeDB
<ArticleEntry
>::EntryMap
* map
) {
66 (*map
)[e
.entry_id()] = e
;
69 ArticleEntry
CreateEntry(std::string entry_id
, std::string page_url
) {
71 entry
.set_entry_id(entry_id
);
72 if (!page_url
.empty()) {
73 ArticleEntryPage
* page
= entry
.add_pages();
74 page
->set_url(page_url
);
81 class DomDistillerViewerSourceBrowserTest
: public InProcessBrowserTest
{
83 DomDistillerViewerSourceBrowserTest() {}
84 ~DomDistillerViewerSourceBrowserTest() override
{}
86 void SetUpOnMainThread() override
{
87 database_model_
= new FakeDB
<ArticleEntry
>::EntryMap
;
90 void TearDownOnMainThread() override
{ delete database_model_
; }
92 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
93 command_line
->AppendSwitch(switches::kEnableDomDistiller
);
96 static KeyedService
* Build(content::BrowserContext
* context
) {
97 FakeDB
<ArticleEntry
>* fake_db
= new FakeDB
<ArticleEntry
>(database_model_
);
98 distiller_factory_
= new MockDistillerFactory();
99 MockDistillerPageFactory
* distiller_page_factory_
=
100 new MockDistillerPageFactory();
101 DomDistillerContextKeyedService
* service
=
102 new DomDistillerContextKeyedService(
103 scoped_ptr
<DomDistillerStoreInterface
>(
104 CreateStoreWithFakeDB(fake_db
,
105 FakeDB
<ArticleEntry
>::EntryMap())),
106 scoped_ptr
<DistillerFactory
>(distiller_factory_
),
107 scoped_ptr
<DistillerPageFactory
>(distiller_page_factory_
),
108 scoped_ptr
<DistilledPagePrefs
>(
109 new DistilledPagePrefs(
110 Profile::FromBrowserContext(
111 context
)->GetPrefs())));
112 fake_db
->InitCallback(true);
113 fake_db
->LoadCallback(true);
114 if (expect_distillation_
) {
115 // There will only be destillation of an article if the database contains
117 FakeDistiller
* distiller
= new FakeDistiller(true);
118 EXPECT_CALL(*distiller_factory_
, CreateDistillerImpl())
119 .WillOnce(testing::Return(distiller
));
121 if (expect_distiller_page_
) {
122 MockDistillerPage
* distiller_page
= new MockDistillerPage();
123 EXPECT_CALL(*distiller_page_factory_
, CreateDistillerPageImpl())
124 .WillOnce(testing::Return(distiller_page
));
129 void ViewSingleDistilledPage(const GURL
& url
,
130 const std::string
& expected_mime_type
);
132 static FakeDB
<ArticleEntry
>::EntryMap
* database_model_
;
133 static bool expect_distillation_
;
134 static bool expect_distiller_page_
;
135 static MockDistillerFactory
* distiller_factory_
;
138 FakeDB
<ArticleEntry
>::EntryMap
*
139 DomDistillerViewerSourceBrowserTest::database_model_
;
140 bool DomDistillerViewerSourceBrowserTest::expect_distillation_
= false;
141 bool DomDistillerViewerSourceBrowserTest::expect_distiller_page_
= false;
142 MockDistillerFactory
* DomDistillerViewerSourceBrowserTest::distiller_factory_
=
145 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
146 // are enabled when the article exists in the database.
147 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
148 NoWebUIBindingsArticleExists
) {
149 // Ensure there is one item in the database, which will trigger distillation.
150 const ArticleEntry entry
= CreateEntry("DISTILLED", "http://example.com/1");
151 AddEntry(entry
, database_model_
);
152 expect_distillation_
= true;
153 expect_distiller_page_
= true;
154 const GURL url
= url_utils::GetDistillerViewUrlFromEntryId(
155 kDomDistillerScheme
, entry
.entry_id());
156 ViewSingleDistilledPage(url
, "text/html");
159 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
160 // are enabled when the article is not found.
161 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
162 NoWebUIBindingsArticleNotFound
) {
163 // The article does not exist, so assume no distillation will happen.
164 expect_distillation_
= false;
165 expect_distiller_page_
= false;
166 const GURL url
= url_utils::GetDistillerViewUrlFromEntryId(
167 kDomDistillerScheme
, "DOES_NOT_EXIST");
168 ViewSingleDistilledPage(url
, "text/html");
171 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
172 // are enabled when requesting to view an arbitrary URL.
173 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
174 NoWebUIBindingsViewUrl
) {
175 // We should expect distillation for any valid URL.
176 expect_distillation_
= true;
177 expect_distiller_page_
= true;
178 GURL
view_url("http://www.example.com/1");
180 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme
, view_url
);
181 ViewSingleDistilledPage(url
, "text/html");
184 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage(
186 const std::string
& expected_mime_type
) {
187 // Ensure the correct factory is used for the DomDistillerService.
188 dom_distiller::DomDistillerServiceFactory::GetInstance()
189 ->SetTestingFactoryAndUse(browser()->profile(), &Build
);
191 // Navigate to a URL which the source should respond to.
192 ui_test_utils::NavigateToURL(browser(), url
);
194 // Ensure no bindings for the loaded |url|.
195 content::WebContents
* contents_after_nav
=
196 browser()->tab_strip_model()->GetActiveWebContents();
197 ASSERT_TRUE(contents_after_nav
!= NULL
);
198 EXPECT_EQ(url
, contents_after_nav
->GetLastCommittedURL());
199 const content::RenderViewHost
* render_view_host
=
200 contents_after_nav
->GetRenderViewHost();
201 EXPECT_EQ(0, render_view_host
->GetEnabledBindings());
202 EXPECT_EQ(expected_mime_type
, contents_after_nav
->GetContentsMimeType());
205 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
206 TestBadUrlErrorPage
) {
207 GURL
url("chrome-distiller://bad");
209 // Navigate to a distiller URL.
210 ui_test_utils::NavigateToURL(browser(), url
);
211 content::WebContents
* contents
=
212 browser()->tab_strip_model()->GetActiveWebContents();
214 // Wait for the page load to complete as the first page completes the root
216 content::WaitForLoadStop(contents
);
218 ASSERT_TRUE(contents
!= NULL
);
219 EXPECT_EQ(url
, contents
->GetLastCommittedURL());
222 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
223 contents
, kGetContent
, &result
));
224 EXPECT_THAT(result
, HasSubstr(l10n_util::GetStringUTF8(
225 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT
)));
228 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
229 // are enabled when the CSS resource is loaded. This CSS might be bundle with
230 // Chrome or provided by an extension.
231 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
232 NoWebUIBindingsDisplayCSS
) {
233 expect_distillation_
= false;
234 expect_distiller_page_
= false;
235 // Navigate to a URL which the source should respond to with CSS.
236 std::string url_without_scheme
= std::string("://foobar/") + kViewerCssPath
;
237 GURL
url(kDomDistillerScheme
+ url_without_scheme
);
238 ViewSingleDistilledPage(url
, "text/css");
241 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
242 EmptyURLShouldNotCrash
) {
243 // This is a bogus URL, so no distillation will happen.
244 expect_distillation_
= false;
245 expect_distiller_page_
= false;
246 const GURL
url(std::string(kDomDistillerScheme
) + "://bogus/");
247 ViewSingleDistilledPage(url
, "text/html");
250 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
251 InvalidURLShouldNotCrash
) {
252 // This is a bogus URL, so no distillation will happen.
253 expect_distillation_
= false;
254 expect_distiller_page_
= false;
255 const GURL
url(std::string(kDomDistillerScheme
) + "://bogus/foobar");
256 ViewSingleDistilledPage(url
, "text/html");
259 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
261 expect_distillation_
= false;
262 expect_distiller_page_
= true;
263 dom_distiller::DomDistillerServiceFactory::GetInstance()
264 ->SetTestingFactoryAndUse(browser()->profile(), &Build
);
266 scoped_refptr
<content::MessageLoopRunner
> distillation_done_runner
=
267 new content::MessageLoopRunner
;
269 FakeDistiller
* distiller
= new FakeDistiller(
271 distillation_done_runner
->QuitClosure());
272 EXPECT_CALL(*distiller_factory_
, CreateDistillerImpl())
273 .WillOnce(testing::Return(distiller
));
275 // Setup observer to inspect the RenderViewHost after committed navigation.
276 content::WebContents
* contents
=
277 browser()->tab_strip_model()->GetActiveWebContents();
279 // Navigate to a URL and wait for the distiller to flush contents to the page.
280 GURL
url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
281 kDomDistillerScheme
, GURL("http://urlthatlooksvalid.com")));
282 chrome::NavigateParams
params(browser(), url
, ui::PAGE_TRANSITION_TYPED
);
283 chrome::Navigate(¶ms
);
284 distillation_done_runner
->Run();
286 // Fake a multi-page response from distiller.
288 std::vector
<scoped_refptr
<ArticleDistillationUpdate::RefCountedPageProto
> >
290 scoped_ptr
<DistilledArticleProto
> article(new DistilledArticleProto());
294 scoped_refptr
<base::RefCountedData
<DistilledPageProto
> > page_proto
=
295 new base::RefCountedData
<DistilledPageProto
>();
296 page_proto
->data
.set_url("http://foobar.1.html");
297 page_proto
->data
.set_html("<div>Page 1 content</div>");
298 update_pages
.push_back(page_proto
);
299 *(article
->add_pages()) = page_proto
->data
;
301 ArticleDistillationUpdate
update(update_pages
, true, false);
302 distiller
->RunDistillerUpdateCallback(update
);
304 // Wait for the page load to complete as the first page completes the root
306 content::WaitForLoadStop(contents
);
309 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
310 contents
, kGetLoadIndicatorClassName
, &result
));
311 EXPECT_EQ("visible", result
);
313 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
314 contents
, kGetContent
, &result
));
315 EXPECT_THAT(result
, HasSubstr("Page 1 content"));
316 EXPECT_THAT(result
, Not(HasSubstr("Page 2 content")));
321 scoped_refptr
<base::RefCountedData
<DistilledPageProto
> > page_proto
=
322 new base::RefCountedData
<DistilledPageProto
>();
323 page_proto
->data
.set_url("http://foobar.2.html");
324 page_proto
->data
.set_html("<div>Page 2 content</div>");
325 update_pages
.push_back(page_proto
);
326 *(article
->add_pages()) = page_proto
->data
;
328 ArticleDistillationUpdate
update(update_pages
, false, false);
329 distiller
->RunDistillerUpdateCallback(update
);
332 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
333 contents
, kGetLoadIndicatorClassName
, &result
));
334 EXPECT_EQ("visible", result
);
336 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
337 contents
, kGetContent
, &result
));
338 EXPECT_THAT(result
, HasSubstr("Page 1 content"));
339 EXPECT_THAT(result
, HasSubstr("Page 2 content"));
342 // Complete the load.
343 distiller
->RunDistillerCallback(article
.Pass());
344 base::RunLoop().RunUntilIdle();
347 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
348 contents
, kGetLoadIndicatorClassName
, &result
));
349 EXPECT_EQ("hidden", result
);
350 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
351 contents
, kGetContent
, &result
));
352 EXPECT_THAT(result
, HasSubstr("Page 1 content"));
353 EXPECT_THAT(result
, HasSubstr("Page 2 content"));
356 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
, PrefChange
) {
357 expect_distillation_
= true;
358 expect_distiller_page_
= true;
359 GURL
view_url("http://www.example.com/1");
360 content::WebContents
* contents
=
361 browser()->tab_strip_model()->GetActiveWebContents();
363 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme
, view_url
);
364 ViewSingleDistilledPage(url
, "text/html");
365 content::WaitForLoadStop(contents
);
367 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
368 contents
, kGetBodyClass
, &result
));
369 EXPECT_EQ("light sans-serif", result
);
371 DistilledPagePrefs
* distilled_page_prefs
=
372 DomDistillerServiceFactory::GetForBrowserContext(
373 browser()->profile())->GetDistilledPagePrefs();
375 distilled_page_prefs
->SetTheme(DistilledPagePrefs::DARK
);
376 base::RunLoop().RunUntilIdle();
377 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
378 contents
, kGetBodyClass
, &result
));
379 EXPECT_EQ("dark sans-serif", result
);
381 distilled_page_prefs
->SetFontFamily(DistilledPagePrefs::SERIF
);
382 base::RunLoop().RunUntilIdle();
384 content::ExecuteScriptAndExtractString(contents
, kGetBodyClass
, &result
));
385 EXPECT_EQ("dark serif", result
);
388 } // namespace dom_distiller