[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / chrome / browser / dom_distiller / dom_distiller_viewer_source_browsertest.cc
blob4ad5f69ee8bd2824bbb11d24508dacbfcef783d1
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/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 "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 namespace dom_distiller {
40 using leveldb_proto::test::FakeDB;
41 using test::FakeDistiller;
42 using test::MockDistillerPage;
43 using test::MockDistillerFactory;
44 using test::MockDistillerPageFactory;
45 using test::util::CreateStoreWithFakeDB;
46 using testing::HasSubstr;
47 using testing::Not;
49 namespace {
51 const char kGetLoadIndicatorClassName[] =
52 "window.domAutomationController.send("
53 "document.getElementById('loadingIndicator').className)";
55 const char kGetContent[] =
56 "window.domAutomationController.send("
57 "document.getElementById('content').innerHTML)";
59 const char kGetBodyClass[] =
60 "window.domAutomationController.send("
61 "document.body.className)";
63 void AddEntry(const ArticleEntry& e, FakeDB<ArticleEntry>::EntryMap* map) {
64 (*map)[e.entry_id()] = e;
67 ArticleEntry CreateEntry(std::string entry_id, std::string page_url) {
68 ArticleEntry entry;
69 entry.set_entry_id(entry_id);
70 if (!page_url.empty()) {
71 ArticleEntryPage* page = entry.add_pages();
72 page->set_url(page_url);
74 return entry;
77 } // namespace
79 class DomDistillerViewerSourceBrowserTest : public InProcessBrowserTest {
80 public:
81 DomDistillerViewerSourceBrowserTest() {}
82 ~DomDistillerViewerSourceBrowserTest() override {}
84 void SetUpOnMainThread() override {
85 database_model_ = new FakeDB<ArticleEntry>::EntryMap;
88 void TearDownOnMainThread() override { delete database_model_; }
90 void SetUpCommandLine(base::CommandLine* command_line) override {
91 command_line->AppendSwitch(switches::kEnableDomDistiller);
94 static KeyedService* Build(content::BrowserContext* context) {
95 FakeDB<ArticleEntry>* fake_db = new FakeDB<ArticleEntry>(database_model_);
96 distiller_factory_ = new MockDistillerFactory();
97 MockDistillerPageFactory* distiller_page_factory_ =
98 new MockDistillerPageFactory();
99 DomDistillerContextKeyedService* service =
100 new DomDistillerContextKeyedService(
101 scoped_ptr<DomDistillerStoreInterface>(
102 CreateStoreWithFakeDB(fake_db,
103 FakeDB<ArticleEntry>::EntryMap())),
104 scoped_ptr<DistillerFactory>(distiller_factory_),
105 scoped_ptr<DistillerPageFactory>(distiller_page_factory_),
106 scoped_ptr<DistilledPagePrefs>(
107 new DistilledPagePrefs(
108 Profile::FromBrowserContext(
109 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;
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 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
204 // are enabled when the CSS resource is loaded. This CSS might be bundle with
205 // Chrome or provided by an extension.
206 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
207 NoWebUIBindingsDisplayCSS) {
208 expect_distillation_ = false;
209 expect_distiller_page_ = false;
210 // Navigate to a URL which the source should respond to with CSS.
211 std::string url_without_scheme = std::string("://foobar/") + kViewerCssPath;
212 GURL url(kDomDistillerScheme + url_without_scheme);
213 ViewSingleDistilledPage(url, "text/css");
216 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
217 EmptyURLShouldNotCrash) {
218 // This is a bogus URL, so no distillation will happen.
219 expect_distillation_ = false;
220 expect_distiller_page_ = false;
221 const GURL url(std::string(kDomDistillerScheme) + "://bogus/");
222 ViewSingleDistilledPage(url, "text/html");
225 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
226 InvalidURLShouldNotCrash) {
227 // This is a bogus URL, so no distillation will happen.
228 expect_distillation_ = false;
229 expect_distiller_page_ = false;
230 const GURL url(std::string(kDomDistillerScheme) + "://bogus/foobar");
231 ViewSingleDistilledPage(url, "text/html");
234 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
235 MultiPageArticle) {
236 expect_distillation_ = false;
237 expect_distiller_page_ = true;
238 dom_distiller::DomDistillerServiceFactory::GetInstance()
239 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
241 scoped_refptr<content::MessageLoopRunner> distillation_done_runner =
242 new content::MessageLoopRunner;
244 FakeDistiller* distiller = new FakeDistiller(
245 false,
246 distillation_done_runner->QuitClosure());
247 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
248 .WillOnce(testing::Return(distiller));
250 // Setup observer to inspect the RenderViewHost after committed navigation.
251 content::WebContents* contents =
252 browser()->tab_strip_model()->GetActiveWebContents();
254 // Navigate to a URL and wait for the distiller to flush contents to the page.
255 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
256 kDomDistillerScheme, GURL("http://urlthatlooksvalid.com")));
257 chrome::NavigateParams params(browser(), url, ui::PAGE_TRANSITION_TYPED);
258 chrome::Navigate(&params);
259 distillation_done_runner->Run();
261 // Fake a multi-page response from distiller.
263 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> >
264 update_pages;
265 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto());
267 // Flush page 1.
269 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
270 new base::RefCountedData<DistilledPageProto>();
271 page_proto->data.set_url("http://foobar.1.html");
272 page_proto->data.set_html("<div>Page 1 content</div>");
273 update_pages.push_back(page_proto);
274 *(article->add_pages()) = page_proto->data;
276 ArticleDistillationUpdate update(update_pages, true, false);
277 distiller->RunDistillerUpdateCallback(update);
279 // Wait for the page load to complete as the first page completes the root
280 // document.
281 content::WaitForLoadStop(contents);
283 std::string result;
284 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
285 contents, kGetLoadIndicatorClassName , &result));
286 EXPECT_EQ("visible", result);
288 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
289 contents, kGetContent , &result));
290 EXPECT_THAT(result, HasSubstr("Page 1 content"));
291 EXPECT_THAT(result, Not(HasSubstr("Page 2 content")));
294 // Flush page 2.
296 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
297 new base::RefCountedData<DistilledPageProto>();
298 page_proto->data.set_url("http://foobar.2.html");
299 page_proto->data.set_html("<div>Page 2 content</div>");
300 update_pages.push_back(page_proto);
301 *(article->add_pages()) = page_proto->data;
303 ArticleDistillationUpdate update(update_pages, false, false);
304 distiller->RunDistillerUpdateCallback(update);
306 std::string result;
307 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
308 contents, kGetLoadIndicatorClassName , &result));
309 EXPECT_EQ("visible", result);
311 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
312 contents, kGetContent , &result));
313 EXPECT_THAT(result, HasSubstr("Page 1 content"));
314 EXPECT_THAT(result, HasSubstr("Page 2 content"));
317 // Complete the load.
318 distiller->RunDistillerCallback(article.Pass());
319 base::RunLoop().RunUntilIdle();
321 std::string result;
322 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
323 contents, kGetLoadIndicatorClassName, &result));
324 EXPECT_EQ("hidden", result);
325 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
326 contents, kGetContent , &result));
327 EXPECT_THAT(result, HasSubstr("Page 1 content"));
328 EXPECT_THAT(result, HasSubstr("Page 2 content"));
331 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, PrefChange) {
332 expect_distillation_ = true;
333 expect_distiller_page_ = true;
334 GURL view_url("http://www.example.com/1");
335 content::WebContents* contents =
336 browser()->tab_strip_model()->GetActiveWebContents();
337 const GURL url =
338 url_utils::GetDistillerViewUrlFromUrl(kDomDistillerScheme, view_url);
339 ViewSingleDistilledPage(url, "text/html");
340 content::WaitForLoadStop(contents);
341 std::string result;
342 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
343 contents, kGetBodyClass, &result));
344 EXPECT_EQ("light sans-serif", result);
346 DistilledPagePrefs* distilled_page_prefs =
347 DomDistillerServiceFactory::GetForBrowserContext(
348 browser()->profile())->GetDistilledPagePrefs();
350 distilled_page_prefs->SetTheme(DistilledPagePrefs::DARK);
351 base::RunLoop().RunUntilIdle();
352 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
353 contents, kGetBodyClass, &result));
354 EXPECT_EQ("dark sans-serif", result);
356 distilled_page_prefs->SetFontFamily(DistilledPagePrefs::SERIF);
357 base::RunLoop().RunUntilIdle();
358 EXPECT_TRUE(
359 content::ExecuteScriptAndExtractString(contents, kGetBodyClass, &result));
360 EXPECT_EQ("dark serif", result);
363 } // namespace dom_distiller