Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / chrome / browser / dom_distiller / dom_distiller_viewer_source_browsertest.cc
blob83ac18e9fe7b8ef2ce1c656445f8eefda5efa634
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/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;
52 using testing::Not;
54 namespace {
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 const unsigned kDarkToolbarThemeColor = 0xFF1A1A1A;
70 void AddEntry(const ArticleEntry& e, FakeDB<ArticleEntry>::EntryMap* map) {
71 (*map)[e.entry_id()] = e;
74 ArticleEntry CreateEntry(std::string entry_id, std::string page_url) {
75 ArticleEntry entry;
76 entry.set_entry_id(entry_id);
77 if (!page_url.empty()) {
78 ArticleEntryPage* page = entry.add_pages();
79 page->set_url(page_url);
81 return entry;
84 } // namespace
86 class DomDistillerViewerSourceBrowserTest : public InProcessBrowserTest {
87 public:
88 DomDistillerViewerSourceBrowserTest() {}
89 ~DomDistillerViewerSourceBrowserTest() override {}
91 void SetUpOnMainThread() override {
92 if (!DistillerJavaScriptWorldIdIsSet()) {
93 SetDistillerJavaScriptWorldId(content::ISOLATED_WORLD_ID_CONTENT_END);
95 database_model_ = new FakeDB<ArticleEntry>::EntryMap;
98 void TearDownOnMainThread() override { delete database_model_; }
100 void SetUpCommandLine(base::CommandLine* command_line) override {
101 command_line->AppendSwitch(switches::kEnableDomDistiller);
104 static scoped_ptr<KeyedService> Build(content::BrowserContext* context) {
105 FakeDB<ArticleEntry>* fake_db = new FakeDB<ArticleEntry>(database_model_);
106 distiller_factory_ = new MockDistillerFactory();
107 MockDistillerPageFactory* distiller_page_factory_ =
108 new MockDistillerPageFactory();
109 scoped_ptr<DomDistillerContextKeyedService> service(
110 new DomDistillerContextKeyedService(
111 scoped_ptr<DomDistillerStoreInterface>(CreateStoreWithFakeDB(
112 fake_db, FakeDB<ArticleEntry>::EntryMap())),
113 scoped_ptr<DistillerFactory>(distiller_factory_),
114 scoped_ptr<DistillerPageFactory>(distiller_page_factory_),
115 scoped_ptr<DistilledPagePrefs>(new DistilledPagePrefs(
116 Profile::FromBrowserContext(context)->GetPrefs()))));
117 fake_db->InitCallback(true);
118 fake_db->LoadCallback(true);
119 if (expect_distillation_) {
120 // There will only be destillation of an article if the database contains
121 // the article.
122 FakeDistiller* distiller = new FakeDistiller(true);
123 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
124 .WillOnce(testing::Return(distiller));
126 if (expect_distiller_page_) {
127 MockDistillerPage* distiller_page = new MockDistillerPage();
128 EXPECT_CALL(*distiller_page_factory_, CreateDistillerPageImpl())
129 .WillOnce(testing::Return(distiller_page));
131 return service.Pass();
134 void ViewSingleDistilledPage(const GURL& url,
135 const std::string& expected_mime_type);
136 // Database entries.
137 static FakeDB<ArticleEntry>::EntryMap* database_model_;
138 static bool expect_distillation_;
139 static bool expect_distiller_page_;
140 static MockDistillerFactory* distiller_factory_;
143 FakeDB<ArticleEntry>::EntryMap*
144 DomDistillerViewerSourceBrowserTest::database_model_;
145 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false;
146 bool DomDistillerViewerSourceBrowserTest::expect_distiller_page_ = false;
147 MockDistillerFactory* DomDistillerViewerSourceBrowserTest::distiller_factory_ =
148 NULL;
150 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
151 // are enabled when the article exists in the database.
152 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
153 NoWebUIBindingsArticleExists) {
154 // Ensure there is one item in the database, which will trigger distillation.
155 const ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1");
156 AddEntry(entry, database_model_);
157 expect_distillation_ = true;
158 expect_distiller_page_ = true;
159 const GURL url = url_utils::GetDistillerViewUrlFromEntryId(
160 kDomDistillerScheme, entry.entry_id());
161 ViewSingleDistilledPage(url, "text/html");
164 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
165 // are enabled when the article is not found.
166 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
167 NoWebUIBindingsArticleNotFound) {
168 // The article does not exist, so assume no distillation will happen.
169 expect_distillation_ = false;
170 expect_distiller_page_ = false;
171 const GURL url = url_utils::GetDistillerViewUrlFromEntryId(
172 kDomDistillerScheme, "DOES_NOT_EXIST");
173 ViewSingleDistilledPage(url, "text/html");
176 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
177 // are enabled when requesting to view an arbitrary URL.
178 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
179 NoWebUIBindingsViewUrl) {
180 // We should expect distillation for any valid URL.
181 expect_distillation_ = true;
182 expect_distiller_page_ = true;
183 GURL view_url("http://www.example.com/1");
184 const GURL url =
185 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, view_url);
186 ViewSingleDistilledPage(url, "text/html");
189 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage(
190 const GURL& url,
191 const std::string& expected_mime_type) {
192 // Ensure the correct factory is used for the DomDistillerService.
193 dom_distiller::DomDistillerServiceFactory::GetInstance()
194 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
196 // Navigate to a URL which the source should respond to.
197 ui_test_utils::NavigateToURL(browser(), url);
199 // Ensure no bindings for the loaded |url|.
200 content::WebContents* contents_after_nav =
201 browser()->tab_strip_model()->GetActiveWebContents();
202 ASSERT_TRUE(contents_after_nav != NULL);
203 EXPECT_EQ(url, contents_after_nav->GetLastCommittedURL());
204 const content::RenderViewHost* render_view_host =
205 contents_after_nav->GetRenderViewHost();
206 EXPECT_EQ(0, render_view_host->GetEnabledBindings());
207 EXPECT_EQ(expected_mime_type, contents_after_nav->GetContentsMimeType());
210 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
211 TestBadUrlErrorPage) {
212 GURL url("chrome-distiller://bad");
214 // Navigate to a distiller URL.
215 ui_test_utils::NavigateToURL(browser(), url);
216 content::WebContents* contents =
217 browser()->tab_strip_model()->GetActiveWebContents();
219 // Wait for the page load to complete as the first page completes the root
220 // document.
221 content::WaitForLoadStop(contents);
223 ASSERT_TRUE(contents != NULL);
224 EXPECT_EQ(url, contents->GetLastCommittedURL());
226 std::string result;
227 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
228 contents, kGetContent , &result));
229 EXPECT_THAT(result, HasSubstr(l10n_util::GetStringUTF8(
230 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT)));
233 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
234 // are enabled when the CSS resource is loaded. This CSS might be bundle with
235 // Chrome or provided by an extension.
236 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
237 NoWebUIBindingsDisplayCSS) {
238 expect_distillation_ = false;
239 expect_distiller_page_ = false;
240 // Navigate to a URL which the source should respond to with CSS.
241 std::string url_without_scheme = std::string("://foobar/") + kViewerCssPath;
242 GURL url(kDomDistillerScheme + url_without_scheme);
243 ViewSingleDistilledPage(url, "text/css");
246 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
247 EmptyURLShouldNotCrash) {
248 // This is a bogus URL, so no distillation will happen.
249 expect_distillation_ = false;
250 expect_distiller_page_ = false;
251 const GURL url(std::string(kDomDistillerScheme) + "://bogus/");
252 ViewSingleDistilledPage(url, "text/html");
255 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
256 InvalidURLShouldNotCrash) {
257 // This is a bogus URL, so no distillation will happen.
258 expect_distillation_ = false;
259 expect_distiller_page_ = false;
260 const GURL url(std::string(kDomDistillerScheme) + "://bogus/foobar");
261 ViewSingleDistilledPage(url, "text/html");
264 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
265 EarlyTemplateLoad) {
266 dom_distiller::DomDistillerServiceFactory::GetInstance()
267 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
269 scoped_refptr<content::MessageLoopRunner> distillation_done_runner =
270 new content::MessageLoopRunner;
272 FakeDistiller* distiller = new FakeDistiller(
273 false,
274 distillation_done_runner->QuitClosure());
275 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
276 .WillOnce(testing::Return(distiller));
278 // Navigate to a URL.
279 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
280 kDomDistillerScheme, GURL("http://urlthatlooksvalid.com")));
281 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
282 chrome::Navigate(&params);
283 distillation_done_runner->Run();
285 content::WebContents* contents =
286 browser()->tab_strip_model()->GetActiveWebContents();
288 // Wait for the page load to complete (should only be template).
289 content::WaitForLoadStop(contents);
290 std::string result;
291 // Loading spinner should be on screen at this point.
292 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
293 contents, kGetLoadIndicatorClassName , &result));
294 EXPECT_EQ("visible", result);
296 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
297 contents, kGetContent , &result));
298 EXPECT_THAT(result, Not(HasSubstr("content")));
300 // Finish distillation and make sure the spinner has been replaced by text.
301 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> >
302 update_pages;
303 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto());
305 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
306 new base::RefCountedData<DistilledPageProto>();
307 page_proto->data.set_url("http://foo.html");
308 page_proto->data.set_html("<div>content</div>");
309 update_pages.push_back(page_proto);
310 *(article->add_pages()) = page_proto->data;
312 ArticleDistillationUpdate update(update_pages, true, false);
313 distiller->RunDistillerUpdateCallback(update);
315 content::WaitForLoadStop(contents);
317 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
318 contents, kGetContent , &result));
319 EXPECT_THAT(result, HasSubstr("content"));
322 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
323 MultiPageArticle) {
324 expect_distillation_ = false;
325 expect_distiller_page_ = true;
326 dom_distiller::DomDistillerServiceFactory::GetInstance()
327 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
329 scoped_refptr<content::MessageLoopRunner> distillation_done_runner =
330 new content::MessageLoopRunner;
332 FakeDistiller* distiller = new FakeDistiller(
333 false,
334 distillation_done_runner->QuitClosure());
335 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
336 .WillOnce(testing::Return(distiller));
338 // Setup observer to inspect the RenderViewHost after committed navigation.
339 content::WebContents* contents =
340 browser()->tab_strip_model()->GetActiveWebContents();
342 // Navigate to a URL and wait for the distiller to flush contents to the page.
343 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
344 kDomDistillerScheme, GURL("http://urlthatlooksvalid.com")));
345 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
346 chrome::Navigate(&params);
347 distillation_done_runner->Run();
349 // Fake a multi-page response from distiller.
351 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> >
352 update_pages;
353 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto());
355 // Flush page 1.
357 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
358 new base::RefCountedData<DistilledPageProto>();
359 page_proto->data.set_url("http://foobar.1.html");
360 page_proto->data.set_html("<div>Page 1 content</div>");
361 update_pages.push_back(page_proto);
362 *(article->add_pages()) = page_proto->data;
364 ArticleDistillationUpdate update(update_pages, true, false);
365 distiller->RunDistillerUpdateCallback(update);
367 // Wait for the page load to complete as the first page completes the root
368 // document.
369 content::WaitForLoadStop(contents);
371 std::string result;
372 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
373 contents, kGetLoadIndicatorClassName , &result));
374 EXPECT_EQ("visible", result);
376 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
377 contents, kGetContent , &result));
378 EXPECT_THAT(result, HasSubstr("Page 1 content"));
379 EXPECT_THAT(result, Not(HasSubstr("Page 2 content")));
382 // Flush page 2.
384 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
385 new base::RefCountedData<DistilledPageProto>();
386 page_proto->data.set_url("http://foobar.2.html");
387 page_proto->data.set_html("<div>Page 2 content</div>");
388 update_pages.push_back(page_proto);
389 *(article->add_pages()) = page_proto->data;
391 ArticleDistillationUpdate update(update_pages, false, false);
392 distiller->RunDistillerUpdateCallback(update);
394 std::string result;
395 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
396 contents, kGetLoadIndicatorClassName , &result));
397 EXPECT_EQ("hidden", result);
399 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
400 contents, kGetContent , &result));
401 EXPECT_THAT(result, HasSubstr("Page 1 content"));
402 EXPECT_THAT(result, HasSubstr("Page 2 content"));
405 // Complete the load.
406 distiller->RunDistillerCallback(article.Pass());
407 base::RunLoop().RunUntilIdle();
409 std::string result;
410 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
411 contents, kGetLoadIndicatorClassName, &result));
412 EXPECT_EQ("hidden", result);
413 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
414 contents, kGetContent , &result));
415 EXPECT_THAT(result, HasSubstr("Page 1 content"));
416 EXPECT_THAT(result, HasSubstr("Page 2 content"));
419 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, PrefChange) {
420 expect_distillation_ = true;
421 expect_distiller_page_ = true;
422 GURL view_url("http://www.example.com/1");
423 content::WebContents* contents =
424 browser()->tab_strip_model()->GetActiveWebContents();
425 const GURL url =
426 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, view_url);
427 ViewSingleDistilledPage(url, "text/html");
428 content::WaitForLoadStop(contents);
429 std::string result;
430 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
431 contents, kGetBodyClass, &result));
432 EXPECT_EQ("light sans-serif", result);
434 DistilledPagePrefs* distilled_page_prefs =
435 DomDistillerServiceFactory::GetForBrowserContext(
436 browser()->profile())->GetDistilledPagePrefs();
438 distilled_page_prefs->SetTheme(DistilledPagePrefs::DARK);
439 base::RunLoop().RunUntilIdle();
440 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
441 contents, kGetBodyClass, &result));
442 EXPECT_EQ("dark sans-serif", result);
444 // Verify that the theme color for the tab is updated as well.
445 EXPECT_EQ(kDarkToolbarThemeColor, contents->GetThemeColor());
447 distilled_page_prefs->SetFontFamily(DistilledPagePrefs::SERIF);
448 base::RunLoop().RunUntilIdle();
449 EXPECT_TRUE(
450 content::ExecuteScriptAndExtractString(contents, kGetBodyClass, &result));
451 EXPECT_EQ("dark serif", result);
454 } // namespace dom_distiller