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/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.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/browser/distiller_javascript_utils.h"
18 #include "components/dom_distiller/content/browser/dom_distiller_viewer_source.h"
19 #include "components/dom_distiller/core/article_entry.h"
20 #include "components/dom_distiller/core/distilled_page_prefs.h"
21 #include "components/dom_distiller/core/distiller.h"
22 #include "components/dom_distiller/core/dom_distiller_service.h"
23 #include "components/dom_distiller/core/dom_distiller_store.h"
24 #include "components/dom_distiller/core/dom_distiller_switches.h"
25 #include "components/dom_distiller/core/dom_distiller_test_util.h"
26 #include "components/dom_distiller/core/fake_distiller.h"
27 #include "components/dom_distiller/core/fake_distiller_page.h"
28 #include "components/dom_distiller/core/task_tracker.h"
29 #include "components/dom_distiller/core/url_constants.h"
30 #include "components/dom_distiller/core/url_utils.h"
31 #include "components/leveldb_proto/testing/fake_db.h"
32 #include "content/public/browser/render_view_host.h"
33 #include "content/public/browser/url_data_source.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/browser/web_contents_observer.h"
36 #include "content/public/common/isolated_world_ids.h"
37 #include "content/public/test/browser_test_utils.h"
38 #include "grit/components_strings.h"
39 #include "testing/gmock/include/gmock/gmock.h"
40 #include "testing/gtest/include/gtest/gtest.h"
41 #include "ui/base/l10n/l10n_util.h"
43 namespace dom_distiller
{
45 using leveldb_proto::test::FakeDB
;
46 using test::FakeDistiller
;
47 using test::MockDistillerPage
;
48 using test::MockDistillerFactory
;
49 using test::MockDistillerPageFactory
;
50 using test::util::CreateStoreWithFakeDB
;
51 using testing::HasSubstr
;
56 const char kGetLoadIndicatorClassName
[] =
57 "window.domAutomationController.send("
58 "document.getElementById('loadingIndicator').className)";
60 const char kGetContent
[] =
61 "window.domAutomationController.send("
62 "document.getElementById('content').innerHTML)";
64 const char kGetBodyClass
[] =
65 "window.domAutomationController.send("
66 "document.body.className)";
68 void AddEntry(const ArticleEntry
& e
, FakeDB
<ArticleEntry
>::EntryMap
* map
) {
69 (*map
)[e
.entry_id()] = e
;
72 ArticleEntry
CreateEntry(std::string entry_id
, std::string page_url
) {
74 entry
.set_entry_id(entry_id
);
75 if (!page_url
.empty()) {
76 ArticleEntryPage
* page
= entry
.add_pages();
77 page
->set_url(page_url
);
84 class DomDistillerViewerSourceBrowserTest
: public InProcessBrowserTest
{
86 DomDistillerViewerSourceBrowserTest() {}
87 ~DomDistillerViewerSourceBrowserTest() override
{}
89 void SetUpOnMainThread() override
{
90 if (!DistillerJavaScriptWorldIdIsSet()) {
91 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END
);
93 database_model_
= new FakeDB
<ArticleEntry
>::EntryMap
;
96 void TearDownOnMainThread() override
{ delete database_model_
; }
98 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
99 command_line
->AppendSwitch(switches::kEnableDomDistiller
);
102 static scoped_ptr
<KeyedService
> Build(content::BrowserContext
* context
) {
103 FakeDB
<ArticleEntry
>* fake_db
= new FakeDB
<ArticleEntry
>(database_model_
);
104 distiller_factory_
= new MockDistillerFactory();
105 MockDistillerPageFactory
* distiller_page_factory_
=
106 new MockDistillerPageFactory();
107 scoped_ptr
<DomDistillerContextKeyedService
> service(
108 new DomDistillerContextKeyedService(
109 scoped_ptr
<DomDistillerStoreInterface
>(CreateStoreWithFakeDB(
110 fake_db
, FakeDB
<ArticleEntry
>::EntryMap())),
111 scoped_ptr
<DistillerFactory
>(distiller_factory_
),
112 scoped_ptr
<DistillerPageFactory
>(distiller_page_factory_
),
113 scoped_ptr
<DistilledPagePrefs
>(new DistilledPagePrefs(
114 Profile::FromBrowserContext(context
)->GetPrefs()))));
115 fake_db
->InitCallback(true);
116 fake_db
->LoadCallback(true);
117 if (expect_distillation_
) {
118 // There will only be destillation of an article if the database contains
120 FakeDistiller
* distiller
= new FakeDistiller(true);
121 EXPECT_CALL(*distiller_factory_
, CreateDistillerImpl())
122 .WillOnce(testing::Return(distiller
));
124 if (expect_distiller_page_
) {
125 MockDistillerPage
* distiller_page
= new MockDistillerPage();
126 EXPECT_CALL(*distiller_page_factory_
, CreateDistillerPageImpl())
127 .WillOnce(testing::Return(distiller_page
));
129 return service
.Pass();
132 void ViewSingleDistilledPage(const GURL
& url
,
133 const std::string
& expected_mime_type
);
135 static FakeDB
<ArticleEntry
>::EntryMap
* database_model_
;
136 static bool expect_distillation_
;
137 static bool expect_distiller_page_
;
138 static MockDistillerFactory
* distiller_factory_
;
141 FakeDB
<ArticleEntry
>::EntryMap
*
142 DomDistillerViewerSourceBrowserTest::database_model_
;
143 bool DomDistillerViewerSourceBrowserTest::expect_distillation_
= false;
144 bool DomDistillerViewerSourceBrowserTest::expect_distiller_page_
= false;
145 MockDistillerFactory
* DomDistillerViewerSourceBrowserTest::distiller_factory_
=
148 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
149 // are enabled when the article exists in the database.
150 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
151 NoWebUIBindingsArticleExists
) {
152 // Ensure there is one item in the database, which will trigger distillation.
153 const ArticleEntry entry
= CreateEntry("DISTILLED", "http://example.com/1");
154 AddEntry(entry
, database_model_
);
155 expect_distillation_
= true;
156 expect_distiller_page_
= true;
157 const GURL url
= url_utils::GetDistillerViewUrlFromEntryId(
158 kDomDistillerScheme
, entry
.entry_id());
159 ViewSingleDistilledPage(url
, "text/html");
162 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
163 // are enabled when the article is not found.
164 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
165 NoWebUIBindingsArticleNotFound
) {
166 // The article does not exist, so assume no distillation will happen.
167 expect_distillation_
= false;
168 expect_distiller_page_
= false;
169 const GURL url
= url_utils::GetDistillerViewUrlFromEntryId(
170 kDomDistillerScheme
, "DOES_NOT_EXIST");
171 ViewSingleDistilledPage(url
, "text/html");
174 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
175 // are enabled when requesting to view an arbitrary URL.
176 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
177 NoWebUIBindingsViewUrl
) {
178 // We should expect distillation for any valid URL.
179 expect_distillation_
= true;
180 expect_distiller_page_
= true;
181 GURL
view_url("http://www.example.com/1");
183 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme
, view_url
);
184 ViewSingleDistilledPage(url
, "text/html");
187 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage(
189 const std::string
& expected_mime_type
) {
190 // Ensure the correct factory is used for the DomDistillerService.
191 dom_distiller::DomDistillerServiceFactory::GetInstance()
192 ->SetTestingFactoryAndUse(browser()->profile(), &Build
);
194 // Navigate to a URL which the source should respond to.
195 ui_test_utils::NavigateToURL(browser(), url
);
197 // Ensure no bindings for the loaded |url|.
198 content::WebContents
* contents_after_nav
=
199 browser()->tab_strip_model()->GetActiveWebContents();
200 ASSERT_TRUE(contents_after_nav
!= NULL
);
201 EXPECT_EQ(url
, contents_after_nav
->GetLastCommittedURL());
202 const content::RenderViewHost
* render_view_host
=
203 contents_after_nav
->GetRenderViewHost();
204 EXPECT_EQ(0, render_view_host
->GetEnabledBindings());
205 EXPECT_EQ(expected_mime_type
, contents_after_nav
->GetContentsMimeType());
208 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
209 TestBadUrlErrorPage
) {
210 GURL
url("chrome-distiller://bad");
212 // Navigate to a distiller URL.
213 ui_test_utils::NavigateToURL(browser(), url
);
214 content::WebContents
* contents
=
215 browser()->tab_strip_model()->GetActiveWebContents();
217 // Wait for the page load to complete as the first page completes the root
219 content::WaitForLoadStop(contents
);
221 ASSERT_TRUE(contents
!= NULL
);
222 EXPECT_EQ(url
, contents
->GetLastCommittedURL());
225 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
226 contents
, kGetContent
, &result
));
227 EXPECT_THAT(result
, HasSubstr(l10n_util::GetStringUTF8(
228 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT
)));
231 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
232 // are enabled when the CSS resource is loaded. This CSS might be bundle with
233 // Chrome or provided by an extension.
234 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
235 NoWebUIBindingsDisplayCSS
) {
236 expect_distillation_
= false;
237 expect_distiller_page_
= false;
238 // Navigate to a URL which the source should respond to with CSS.
239 std::string url_without_scheme
= std::string("://foobar/") + kViewerCssPath
;
240 GURL
url(kDomDistillerScheme
+ url_without_scheme
);
241 ViewSingleDistilledPage(url
, "text/css");
244 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
245 EmptyURLShouldNotCrash
) {
246 // This is a bogus URL, so no distillation will happen.
247 expect_distillation_
= false;
248 expect_distiller_page_
= false;
249 const GURL
url(std::string(kDomDistillerScheme
) + "://bogus/");
250 ViewSingleDistilledPage(url
, "text/html");
253 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
254 InvalidURLShouldNotCrash
) {
255 // This is a bogus URL, so no distillation will happen.
256 expect_distillation_
= false;
257 expect_distiller_page_
= false;
258 const GURL
url(std::string(kDomDistillerScheme
) + "://bogus/foobar");
259 ViewSingleDistilledPage(url
, "text/html");
262 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
264 dom_distiller::DomDistillerServiceFactory::GetInstance()
265 ->SetTestingFactoryAndUse(browser()->profile(), &Build
);
267 scoped_refptr
<content::MessageLoopRunner
> distillation_done_runner
=
268 new content::MessageLoopRunner
;
270 FakeDistiller
* distiller
= new FakeDistiller(
272 distillation_done_runner
->QuitClosure());
273 EXPECT_CALL(*distiller_factory_
, CreateDistillerImpl())
274 .WillOnce(testing::Return(distiller
));
276 // Navigate to a URL.
277 GURL
url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
278 kDomDistillerScheme
, GURL("http://urlthatlooksvalid.com")));
279 chrome::NavigateParams
params(browser(), url
, ui::PAGE_TRANSITION_TYPED
);
280 chrome::Navigate(¶ms
);
281 distillation_done_runner
->Run();
283 content::WebContents
* contents
=
284 browser()->tab_strip_model()->GetActiveWebContents();
286 // Wait for the page load to complete (should only be template).
287 content::WaitForLoadStop(contents
);
289 // Loading spinner should be on screen at this point.
290 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
291 contents
, kGetLoadIndicatorClassName
, &result
));
292 EXPECT_EQ("visible", result
);
294 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
295 contents
, kGetContent
, &result
));
296 EXPECT_THAT(result
, Not(HasSubstr("content")));
298 // Finish distillation and make sure the spinner has been replaced by text.
299 std::vector
<scoped_refptr
<ArticleDistillationUpdate::RefCountedPageProto
> >
301 scoped_ptr
<DistilledArticleProto
> article(new DistilledArticleProto());
303 scoped_refptr
<base::RefCountedData
<DistilledPageProto
> > page_proto
=
304 new base::RefCountedData
<DistilledPageProto
>();
305 page_proto
->data
.set_url("http://foo.html");
306 page_proto
->data
.set_html("<div>content</div>");
307 update_pages
.push_back(page_proto
);
308 *(article
->add_pages()) = page_proto
->data
;
310 ArticleDistillationUpdate
update(update_pages
, true, false);
311 distiller
->RunDistillerUpdateCallback(update
);
313 content::WaitForLoadStop(contents
);
315 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
316 contents
, kGetContent
, &result
));
317 EXPECT_THAT(result
, HasSubstr("content"));
320 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
,
322 expect_distillation_
= false;
323 expect_distiller_page_
= true;
324 dom_distiller::DomDistillerServiceFactory::GetInstance()
325 ->SetTestingFactoryAndUse(browser()->profile(), &Build
);
327 scoped_refptr
<content::MessageLoopRunner
> distillation_done_runner
=
328 new content::MessageLoopRunner
;
330 FakeDistiller
* distiller
= new FakeDistiller(
332 distillation_done_runner
->QuitClosure());
333 EXPECT_CALL(*distiller_factory_
, CreateDistillerImpl())
334 .WillOnce(testing::Return(distiller
));
336 // Setup observer to inspect the RenderViewHost after committed navigation.
337 content::WebContents
* contents
=
338 browser()->tab_strip_model()->GetActiveWebContents();
340 // Navigate to a URL and wait for the distiller to flush contents to the page.
341 GURL
url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
342 kDomDistillerScheme
, GURL("http://urlthatlooksvalid.com")));
343 chrome::NavigateParams
params(browser(), url
, ui::PAGE_TRANSITION_TYPED
);
344 chrome::Navigate(¶ms
);
345 distillation_done_runner
->Run();
347 // Fake a multi-page response from distiller.
349 std::vector
<scoped_refptr
<ArticleDistillationUpdate::RefCountedPageProto
> >
351 scoped_ptr
<DistilledArticleProto
> article(new DistilledArticleProto());
355 scoped_refptr
<base::RefCountedData
<DistilledPageProto
> > page_proto
=
356 new base::RefCountedData
<DistilledPageProto
>();
357 page_proto
->data
.set_url("http://foobar.1.html");
358 page_proto
->data
.set_html("<div>Page 1 content</div>");
359 update_pages
.push_back(page_proto
);
360 *(article
->add_pages()) = page_proto
->data
;
362 ArticleDistillationUpdate
update(update_pages
, true, false);
363 distiller
->RunDistillerUpdateCallback(update
);
365 // Wait for the page load to complete as the first page completes the root
367 content::WaitForLoadStop(contents
);
370 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
371 contents
, kGetLoadIndicatorClassName
, &result
));
372 EXPECT_EQ("visible", result
);
374 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
375 contents
, kGetContent
, &result
));
376 EXPECT_THAT(result
, HasSubstr("Page 1 content"));
377 EXPECT_THAT(result
, Not(HasSubstr("Page 2 content")));
382 scoped_refptr
<base::RefCountedData
<DistilledPageProto
> > page_proto
=
383 new base::RefCountedData
<DistilledPageProto
>();
384 page_proto
->data
.set_url("http://foobar.2.html");
385 page_proto
->data
.set_html("<div>Page 2 content</div>");
386 update_pages
.push_back(page_proto
);
387 *(article
->add_pages()) = page_proto
->data
;
389 ArticleDistillationUpdate
update(update_pages
, false, false);
390 distiller
->RunDistillerUpdateCallback(update
);
393 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
394 contents
, kGetLoadIndicatorClassName
, &result
));
395 EXPECT_EQ("hidden", result
);
397 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
398 contents
, kGetContent
, &result
));
399 EXPECT_THAT(result
, HasSubstr("Page 1 content"));
400 EXPECT_THAT(result
, HasSubstr("Page 2 content"));
403 // Complete the load.
404 distiller
->RunDistillerCallback(article
.Pass());
405 base::RunLoop().RunUntilIdle();
408 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
409 contents
, kGetLoadIndicatorClassName
, &result
));
410 EXPECT_EQ("hidden", result
);
411 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
412 contents
, kGetContent
, &result
));
413 EXPECT_THAT(result
, HasSubstr("Page 1 content"));
414 EXPECT_THAT(result
, HasSubstr("Page 2 content"));
417 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest
, PrefChange
) {
418 expect_distillation_
= true;
419 expect_distiller_page_
= true;
420 GURL
view_url("http://www.example.com/1");
421 content::WebContents
* contents
=
422 browser()->tab_strip_model()->GetActiveWebContents();
424 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme
, view_url
);
425 ViewSingleDistilledPage(url
, "text/html");
426 content::WaitForLoadStop(contents
);
428 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
429 contents
, kGetBodyClass
, &result
));
430 EXPECT_EQ("light sans-serif", result
);
432 DistilledPagePrefs
* distilled_page_prefs
=
433 DomDistillerServiceFactory::GetForBrowserContext(
434 browser()->profile())->GetDistilledPagePrefs();
436 distilled_page_prefs
->SetTheme(DistilledPagePrefs::DARK
);
437 base::RunLoop().RunUntilIdle();
438 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
439 contents
, kGetBodyClass
, &result
));
440 EXPECT_EQ("dark sans-serif", result
);
442 distilled_page_prefs
->SetFontFamily(DistilledPagePrefs::SERIF
);
443 base::RunLoop().RunUntilIdle();
445 content::ExecuteScriptAndExtractString(contents
, kGetBodyClass
, &result
));
446 EXPECT_EQ("dark serif", result
);
449 } // namespace dom_distiller