Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / chrome / browser / dom_distiller / dom_distiller_viewer_source_browsertest.cc
blob942eddb0cc895b689a9dfb56b73967cb95d590a9
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.
5 #include <string.h>
7 #include "base/command_line.h"
8 #include "base/guid.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/common/chrome_switches.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/dom_distiller/content/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_test_util.h"
25 #include "components/dom_distiller/core/fake_distiller.h"
26 #include "components/dom_distiller/core/fake_distiller_page.h"
27 #include "components/dom_distiller/core/task_tracker.h"
28 #include "components/dom_distiller/core/url_constants.h"
29 #include "components/dom_distiller/core/url_utils.h"
30 #include "components/leveldb_proto/testing/fake_db.h"
31 #include "content/public/browser/render_view_host.h"
32 #include "content/public/browser/url_data_source.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/browser/web_contents_observer.h"
35 #include "content/public/test/browser_test_utils.h"
36 #include "grit/components_strings.h"
37 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gtest/include/gtest/gtest.h"
39 #include "ui/base/l10n/l10n_util.h"
41 namespace dom_distiller {
43 using leveldb_proto::test::FakeDB;
44 using test::FakeDistiller;
45 using test::MockDistillerPage;
46 using test::MockDistillerFactory;
47 using test::MockDistillerPageFactory;
48 using test::util::CreateStoreWithFakeDB;
49 using testing::HasSubstr;
50 using testing::Not;
52 namespace {
54 const char kGetLoadIndicatorClassName[] =
55 "window.domAutomationController.send("
56 "document.getElementById('loadingIndicator').className)";
58 const char kGetContent[] =
59 "window.domAutomationController.send("
60 "document.getElementById('content').innerHTML)";
62 const char kGetBodyClass[] =
63 "window.domAutomationController.send("
64 "document.body.className)";
66 void AddEntry(const ArticleEntry& e, FakeDB<ArticleEntry>::EntryMap* map) {
67 (*map)[e.entry_id()] = e;
70 ArticleEntry CreateEntry(std::string entry_id, std::string page_url) {
71 ArticleEntry entry;
72 entry.set_entry_id(entry_id);
73 if (!page_url.empty()) {
74 ArticleEntryPage* page = entry.add_pages();
75 page->set_url(page_url);
77 return entry;
80 } // namespace
82 class DomDistillerViewerSourceBrowserTest : public InProcessBrowserTest {
83 public:
84 DomDistillerViewerSourceBrowserTest() {}
85 ~DomDistillerViewerSourceBrowserTest() override {}
87 void SetUpOnMainThread() override {
88 database_model_ = new FakeDB<ArticleEntry>::EntryMap;
91 void TearDownOnMainThread() override { delete database_model_; }
93 void SetUpCommandLine(base::CommandLine* command_line) override {
94 command_line->AppendSwitch(switches::kEnableDomDistiller);
97 static scoped_ptr<KeyedService> Build(content::BrowserContext* context) {
98 FakeDB<ArticleEntry>* fake_db = new FakeDB<ArticleEntry>(database_model_);
99 distiller_factory_ = new MockDistillerFactory();
100 MockDistillerPageFactory* distiller_page_factory_ =
101 new MockDistillerPageFactory();
102 scoped_ptr<DomDistillerContextKeyedService> service(
103 new DomDistillerContextKeyedService(
104 scoped_ptr<DomDistillerStoreInterface>(CreateStoreWithFakeDB(
105 fake_db, FakeDB<ArticleEntry>::EntryMap())),
106 scoped_ptr<DistillerFactory>(distiller_factory_),
107 scoped_ptr<DistillerPageFactory>(distiller_page_factory_),
108 scoped_ptr<DistilledPagePrefs>(new DistilledPagePrefs(
109 Profile::FromBrowserContext(context)->GetPrefs()))));
110 fake_db->InitCallback(true);
111 fake_db->LoadCallback(true);
112 if (expect_distillation_) {
113 // There will only be destillation of an article if the database contains
114 // the article.
115 FakeDistiller* distiller = new FakeDistiller(true);
116 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
117 .WillOnce(testing::Return(distiller));
119 if (expect_distiller_page_) {
120 MockDistillerPage* distiller_page = new MockDistillerPage();
121 EXPECT_CALL(*distiller_page_factory_, CreateDistillerPageImpl())
122 .WillOnce(testing::Return(distiller_page));
124 return service.Pass();
127 void ViewSingleDistilledPage(const GURL& url,
128 const std::string& expected_mime_type);
129 // Database entries.
130 static FakeDB<ArticleEntry>::EntryMap* database_model_;
131 static bool expect_distillation_;
132 static bool expect_distiller_page_;
133 static MockDistillerFactory* distiller_factory_;
136 FakeDB<ArticleEntry>::EntryMap*
137 DomDistillerViewerSourceBrowserTest::database_model_;
138 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false;
139 bool DomDistillerViewerSourceBrowserTest::expect_distiller_page_ = false;
140 MockDistillerFactory* DomDistillerViewerSourceBrowserTest::distiller_factory_ =
141 NULL;
143 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
144 // are enabled when the article exists in the database.
145 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
146 NoWebUIBindingsArticleExists) {
147 // Ensure there is one item in the database, which will trigger distillation.
148 const ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1");
149 AddEntry(entry, database_model_);
150 expect_distillation_ = true;
151 expect_distiller_page_ = true;
152 const GURL url = url_utils::GetDistillerViewUrlFromEntryId(
153 kDomDistillerScheme, entry.entry_id());
154 ViewSingleDistilledPage(url, "text/html");
157 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
158 // are enabled when the article is not found.
159 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
160 NoWebUIBindingsArticleNotFound) {
161 // The article does not exist, so assume no distillation will happen.
162 expect_distillation_ = false;
163 expect_distiller_page_ = false;
164 const GURL url = url_utils::GetDistillerViewUrlFromEntryId(
165 kDomDistillerScheme, "DOES_NOT_EXIST");
166 ViewSingleDistilledPage(url, "text/html");
169 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
170 // are enabled when requesting to view an arbitrary URL.
171 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
172 NoWebUIBindingsViewUrl) {
173 // We should expect distillation for any valid URL.
174 expect_distillation_ = true;
175 expect_distiller_page_ = true;
176 GURL view_url("http://www.example.com/1");
177 const GURL url =
178 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, view_url);
179 ViewSingleDistilledPage(url, "text/html");
182 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage(
183 const GURL& url,
184 const std::string& expected_mime_type) {
185 // Ensure the correct factory is used for the DomDistillerService.
186 dom_distiller::DomDistillerServiceFactory::GetInstance()
187 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
189 // Navigate to a URL which the source should respond to.
190 ui_test_utils::NavigateToURL(browser(), url);
192 // Ensure no bindings for the loaded |url|.
193 content::WebContents* contents_after_nav =
194 browser()->tab_strip_model()->GetActiveWebContents();
195 ASSERT_TRUE(contents_after_nav != NULL);
196 EXPECT_EQ(url, contents_after_nav->GetLastCommittedURL());
197 const content::RenderViewHost* render_view_host =
198 contents_after_nav->GetRenderViewHost();
199 EXPECT_EQ(0, render_view_host->GetEnabledBindings());
200 EXPECT_EQ(expected_mime_type, contents_after_nav->GetContentsMimeType());
203 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
204 TestBadUrlErrorPage) {
205 GURL url("chrome-distiller://bad");
207 // Navigate to a distiller URL.
208 ui_test_utils::NavigateToURL(browser(), url);
209 content::WebContents* contents =
210 browser()->tab_strip_model()->GetActiveWebContents();
212 // Wait for the page load to complete as the first page completes the root
213 // document.
214 content::WaitForLoadStop(contents);
216 ASSERT_TRUE(contents != NULL);
217 EXPECT_EQ(url, contents->GetLastCommittedURL());
219 std::string result;
220 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
221 contents, kGetContent , &result));
222 EXPECT_THAT(result, HasSubstr(l10n_util::GetStringUTF8(
223 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT)));
226 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
227 // are enabled when the CSS resource is loaded. This CSS might be bundle with
228 // Chrome or provided by an extension.
229 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
230 NoWebUIBindingsDisplayCSS) {
231 expect_distillation_ = false;
232 expect_distiller_page_ = false;
233 // Navigate to a URL which the source should respond to with CSS.
234 std::string url_without_scheme = std::string("://foobar/") + kViewerCssPath;
235 GURL url(kDomDistillerScheme + url_without_scheme);
236 ViewSingleDistilledPage(url, "text/css");
239 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
240 EmptyURLShouldNotCrash) {
241 // This is a bogus URL, so no distillation will happen.
242 expect_distillation_ = false;
243 expect_distiller_page_ = false;
244 const GURL url(std::string(kDomDistillerScheme) + "://bogus/");
245 ViewSingleDistilledPage(url, "text/html");
248 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
249 InvalidURLShouldNotCrash) {
250 // This is a bogus URL, so no distillation will happen.
251 expect_distillation_ = false;
252 expect_distiller_page_ = false;
253 const GURL url(std::string(kDomDistillerScheme) + "://bogus/foobar");
254 ViewSingleDistilledPage(url, "text/html");
257 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
258 EarlyTemplateLoad) {
259 dom_distiller::DomDistillerServiceFactory::GetInstance()
260 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
262 scoped_refptr<content::MessageLoopRunner> distillation_done_runner =
263 new content::MessageLoopRunner;
265 FakeDistiller* distiller = new FakeDistiller(
266 false,
267 distillation_done_runner->QuitClosure());
268 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
269 .WillOnce(testing::Return(distiller));
271 // Navigate to a URL.
272 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
273 kDomDistillerScheme, GURL("http://urlthatlooksvalid.com")));
274 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
275 chrome::Navigate(&params);
276 distillation_done_runner->Run();
278 content::WebContents* contents =
279 browser()->tab_strip_model()->GetActiveWebContents();
281 // Wait for the page load to complete (should only be template).
282 content::WaitForLoadStop(contents);
283 std::string result;
284 // Loading spinner should be on screen at this point.
285 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
286 contents, kGetLoadIndicatorClassName , &result));
287 EXPECT_EQ("visible", result);
289 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
290 contents, kGetContent , &result));
291 EXPECT_THAT(result, Not(HasSubstr("content")));
293 // Finish distillation and make sure the spinner has been replaced by text.
294 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> >
295 update_pages;
296 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto());
298 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
299 new base::RefCountedData<DistilledPageProto>();
300 page_proto->data.set_url("http://foo.html");
301 page_proto->data.set_html("<div>content</div>");
302 update_pages.push_back(page_proto);
303 *(article->add_pages()) = page_proto->data;
305 ArticleDistillationUpdate update(update_pages, true, false);
306 distiller->RunDistillerUpdateCallback(update);
308 content::WaitForLoadStop(contents);
310 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
311 contents, kGetContent , &result));
312 EXPECT_THAT(result, HasSubstr("content"));
315 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
316 MultiPageArticle) {
317 expect_distillation_ = false;
318 expect_distiller_page_ = true;
319 dom_distiller::DomDistillerServiceFactory::GetInstance()
320 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
322 scoped_refptr<content::MessageLoopRunner> distillation_done_runner =
323 new content::MessageLoopRunner;
325 FakeDistiller* distiller = new FakeDistiller(
326 false,
327 distillation_done_runner->QuitClosure());
328 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
329 .WillOnce(testing::Return(distiller));
331 // Setup observer to inspect the RenderViewHost after committed navigation.
332 content::WebContents* contents =
333 browser()->tab_strip_model()->GetActiveWebContents();
335 // Navigate to a URL and wait for the distiller to flush contents to the page.
336 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
337 kDomDistillerScheme, GURL("http://urlthatlooksvalid.com")));
338 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
339 chrome::Navigate(&params);
340 distillation_done_runner->Run();
342 // Fake a multi-page response from distiller.
344 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> >
345 update_pages;
346 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto());
348 // Flush page 1.
350 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
351 new base::RefCountedData<DistilledPageProto>();
352 page_proto->data.set_url("http://foobar.1.html");
353 page_proto->data.set_html("<div>Page 1 content</div>");
354 update_pages.push_back(page_proto);
355 *(article->add_pages()) = page_proto->data;
357 ArticleDistillationUpdate update(update_pages, true, false);
358 distiller->RunDistillerUpdateCallback(update);
360 // Wait for the page load to complete as the first page completes the root
361 // document.
362 content::WaitForLoadStop(contents);
364 std::string result;
365 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
366 contents, kGetLoadIndicatorClassName , &result));
367 EXPECT_EQ("visible", result);
369 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
370 contents, kGetContent , &result));
371 EXPECT_THAT(result, HasSubstr("Page 1 content"));
372 EXPECT_THAT(result, Not(HasSubstr("Page 2 content")));
375 // Flush page 2.
377 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
378 new base::RefCountedData<DistilledPageProto>();
379 page_proto->data.set_url("http://foobar.2.html");
380 page_proto->data.set_html("<div>Page 2 content</div>");
381 update_pages.push_back(page_proto);
382 *(article->add_pages()) = page_proto->data;
384 ArticleDistillationUpdate update(update_pages, false, false);
385 distiller->RunDistillerUpdateCallback(update);
387 std::string result;
388 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
389 contents, kGetLoadIndicatorClassName , &result));
390 EXPECT_EQ("hidden", result);
392 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
393 contents, kGetContent , &result));
394 EXPECT_THAT(result, HasSubstr("Page 1 content"));
395 EXPECT_THAT(result, HasSubstr("Page 2 content"));
398 // Complete the load.
399 distiller->RunDistillerCallback(article.Pass());
400 base::RunLoop().RunUntilIdle();
402 std::string result;
403 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
404 contents, kGetLoadIndicatorClassName, &result));
405 EXPECT_EQ("hidden", result);
406 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
407 contents, kGetContent , &result));
408 EXPECT_THAT(result, HasSubstr("Page 1 content"));
409 EXPECT_THAT(result, HasSubstr("Page 2 content"));
412 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, PrefChange) {
413 expect_distillation_ = true;
414 expect_distiller_page_ = true;
415 GURL view_url("http://www.example.com/1");
416 content::WebContents* contents =
417 browser()->tab_strip_model()->GetActiveWebContents();
418 const GURL url =
419 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, view_url);
420 ViewSingleDistilledPage(url, "text/html");
421 content::WaitForLoadStop(contents);
422 std::string result;
423 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
424 contents, kGetBodyClass, &result));
425 EXPECT_EQ("light sans-serif", result);
427 DistilledPagePrefs* distilled_page_prefs =
428 DomDistillerServiceFactory::GetForBrowserContext(
429 browser()->profile())->GetDistilledPagePrefs();
431 distilled_page_prefs->SetTheme(DistilledPagePrefs::DARK);
432 base::RunLoop().RunUntilIdle();
433 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
434 contents, kGetBodyClass, &result));
435 EXPECT_EQ("dark sans-serif", result);
437 distilled_page_prefs->SetFontFamily(DistilledPagePrefs::SERIF);
438 base::RunLoop().RunUntilIdle();
439 EXPECT_TRUE(
440 content::ExecuteScriptAndExtractString(contents, kGetBodyClass, &result));
441 EXPECT_EQ("dark serif", result);
444 } // namespace dom_distiller