[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / dom_distiller / dom_distiller_viewer_source_browsertest.cc
blob37f4093ab9c17385f7505511e1cdb8e9257b69f8
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/path_service.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/common/url_constants.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "components/dom_distiller/content/dom_distiller_viewer_source.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_db.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 "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 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 void AddEntry(const ArticleEntry& e, FakeDB::EntryMap* map) {
60 (*map)[e.entry_id()] = e;
63 ArticleEntry CreateEntry(std::string entry_id, std::string page_url) {
64 ArticleEntry entry;
65 entry.set_entry_id(entry_id);
66 if (!page_url.empty()) {
67 ArticleEntryPage* page = entry.add_pages();
68 page->set_url(page_url);
70 return entry;
73 } // namespace
75 class DomDistillerViewerSourceBrowserTest : public InProcessBrowserTest {
76 public:
77 DomDistillerViewerSourceBrowserTest() {}
78 virtual ~DomDistillerViewerSourceBrowserTest() {}
80 virtual void SetUpOnMainThread() OVERRIDE {
81 database_model_ = new FakeDB::EntryMap;
84 virtual void CleanUpOnMainThread() OVERRIDE { delete database_model_; }
86 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
87 command_line->AppendSwitch(switches::kEnableDomDistiller);
90 static KeyedService* Build(content::BrowserContext* context) {
91 FakeDB* fake_db = new FakeDB(database_model_);
92 distiller_factory_ = new MockDistillerFactory();
93 MockDistillerPageFactory* distiller_page_factory_ =
94 new MockDistillerPageFactory();
95 DomDistillerContextKeyedService* service =
96 new DomDistillerContextKeyedService(
97 scoped_ptr<DomDistillerStoreInterface>(
98 CreateStoreWithFakeDB(fake_db, FakeDB::EntryMap())),
99 scoped_ptr<DistillerFactory>(distiller_factory_),
100 scoped_ptr<DistillerPageFactory>(distiller_page_factory_));
101 fake_db->InitCallback(true);
102 fake_db->LoadCallback(true);
103 if (expect_distillation_) {
104 // There will only be destillation of an article if the database contains
105 // the article.
106 FakeDistiller* distiller = new FakeDistiller(true);
107 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
108 .WillOnce(testing::Return(distiller));
110 if (expect_distiller_page_) {
111 MockDistillerPage* distiller_page = new MockDistillerPage();
112 EXPECT_CALL(*distiller_page_factory_, CreateDistillerPageImpl())
113 .WillOnce(testing::Return(distiller_page));
115 return service;
118 void ViewSingleDistilledPage(const GURL& url,
119 const std::string& expected_mime_type);
120 // Database entries.
121 static FakeDB::EntryMap* database_model_;
122 static bool expect_distillation_;
123 static bool expect_distiller_page_;
124 static MockDistillerFactory* distiller_factory_;
127 FakeDB::EntryMap* DomDistillerViewerSourceBrowserTest::database_model_;
128 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false;
129 bool DomDistillerViewerSourceBrowserTest::expect_distiller_page_ = false;
130 MockDistillerFactory* DomDistillerViewerSourceBrowserTest::distiller_factory_ =
131 NULL;
133 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
134 // are enabled when the article exists in the database.
135 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
136 NoWebUIBindingsArticleExists) {
137 // Ensure there is one item in the database, which will trigger distillation.
138 const ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1");
139 AddEntry(entry, database_model_);
140 expect_distillation_ = true;
141 expect_distiller_page_ = true;
142 const GURL url = url_utils::GetDistillerViewUrlFromEntryId(
143 chrome::kDomDistillerScheme, entry.entry_id());
144 ViewSingleDistilledPage(url, "text/html");
147 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
148 // are enabled when the article is not found.
149 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
150 NoWebUIBindingsArticleNotFound) {
151 // The article does not exist, so assume no distillation will happen.
152 expect_distillation_ = false;
153 expect_distiller_page_ = false;
154 const GURL url = url_utils::GetDistillerViewUrlFromEntryId(
155 chrome::kDomDistillerScheme, "DOES_NOT_EXIST");
156 ViewSingleDistilledPage(url, "text/html");
159 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
160 // are enabled when requesting to view an arbitrary URL.
161 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
162 NoWebUIBindingsViewUrl) {
163 // We should expect distillation for any valid URL.
164 expect_distillation_ = true;
165 expect_distiller_page_ = true;
166 GURL view_url("http://www.example.com/1");
167 const GURL url = url_utils::GetDistillerViewUrlFromUrl(
168 chrome::kDomDistillerScheme, view_url);
169 ViewSingleDistilledPage(url, "text/html");
172 void DomDistillerViewerSourceBrowserTest::ViewSingleDistilledPage(
173 const GURL& url,
174 const std::string& expected_mime_type) {
175 // Ensure the correct factory is used for the DomDistillerService.
176 dom_distiller::DomDistillerServiceFactory::GetInstance()
177 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
179 // Navigate to a URL which the source should respond to.
180 ui_test_utils::NavigateToURL(browser(), url);
182 // Ensure no bindings for the loaded |url|.
183 content::WebContents* contents_after_nav =
184 browser()->tab_strip_model()->GetActiveWebContents();
185 ASSERT_TRUE(contents_after_nav != NULL);
186 EXPECT_EQ(url, contents_after_nav->GetLastCommittedURL());
187 const content::RenderViewHost* render_view_host =
188 contents_after_nav->GetRenderViewHost();
189 EXPECT_EQ(0, render_view_host->GetEnabledBindings());
190 EXPECT_EQ(expected_mime_type, contents_after_nav->GetContentsMimeType());
193 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings
194 // are enabled when the CSS resource is loaded. This CSS might be bundle with
195 // Chrome or provided by an extension.
196 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
197 NoWebUIBindingsDisplayCSS) {
198 expect_distillation_ = false;
199 expect_distiller_page_ = false;
200 // Navigate to a URL which the source should respond to with CSS.
201 std::string url_without_scheme = std::string("://foobar/") + kViewerCssPath;
202 GURL url(chrome::kDomDistillerScheme + url_without_scheme);
203 ViewSingleDistilledPage(url, "text/css");
206 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
207 EmptyURLShouldNotCrash) {
208 // This is a bogus URL, so no distillation will happen.
209 expect_distillation_ = false;
210 expect_distiller_page_ = false;
211 const GURL url(std::string(chrome::kDomDistillerScheme) + "://bogus/");
212 ViewSingleDistilledPage(url, "text/html");
215 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
216 InvalidURLShouldNotCrash) {
217 // This is a bogus URL, so no distillation will happen.
218 expect_distillation_ = false;
219 expect_distiller_page_ = false;
220 const GURL url(std::string(chrome::kDomDistillerScheme) + "://bogus/foobar");
221 ViewSingleDistilledPage(url, "text/html");
224 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest,
225 MultiPageArticle) {
226 expect_distillation_ = false;
227 expect_distiller_page_ = true;
228 dom_distiller::DomDistillerServiceFactory::GetInstance()
229 ->SetTestingFactoryAndUse(browser()->profile(), &Build);
231 scoped_refptr<content::MessageLoopRunner> distillation_done_runner =
232 new content::MessageLoopRunner;
234 FakeDistiller* distiller = new FakeDistiller(
235 false,
236 distillation_done_runner->QuitClosure());
237 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
238 .WillOnce(testing::Return(distiller));
240 // Setup observer to inspect the RenderViewHost after committed navigation.
241 content::WebContents* contents =
242 browser()->tab_strip_model()->GetActiveWebContents();
244 // Navigate to a URL and wait for the distiller to flush contents to the page.
245 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
246 chrome::kDomDistillerScheme, GURL("http://urlthatlooksvalid.com")));
247 chrome::NavigateParams params(browser(), url, content::PAGE_TRANSITION_TYPED);
248 chrome::Navigate(&params);
249 distillation_done_runner->Run();
251 // Fake a multi-page response from distiller.
253 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> >
254 update_pages;
255 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto());
257 // Flush page 1.
259 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
260 new base::RefCountedData<DistilledPageProto>();
261 page_proto->data.set_url("http://foobar.1.html");
262 page_proto->data.set_html("<div>Page 1 content</div>");
263 update_pages.push_back(page_proto);
264 *(article->add_pages()) = page_proto->data;
266 ArticleDistillationUpdate update(update_pages, true, false);
267 distiller->RunDistillerUpdateCallback(update);
269 // Wait for the page load to complete as the first page completes the root
270 // document.
271 content::WaitForLoadStop(contents);
273 std::string result;
274 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
275 contents, kGetLoadIndicatorClassName , &result));
276 EXPECT_EQ("visible", result);
278 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
279 contents, kGetContent , &result));
280 EXPECT_THAT(result, HasSubstr("Page 1 content"));
281 EXPECT_THAT(result, Not(HasSubstr("Page 2 content")));
284 // Flush page 2.
286 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto =
287 new base::RefCountedData<DistilledPageProto>();
288 page_proto->data.set_url("http://foobar.2.html");
289 page_proto->data.set_html("<div>Page 2 content</div>");
290 update_pages.push_back(page_proto);
291 *(article->add_pages()) = page_proto->data;
293 ArticleDistillationUpdate update(update_pages, false, false);
294 distiller->RunDistillerUpdateCallback(update);
296 std::string result;
297 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
298 contents, kGetLoadIndicatorClassName , &result));
299 EXPECT_EQ("visible", result);
301 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
302 contents, kGetContent , &result));
303 EXPECT_THAT(result, HasSubstr("Page 1 content"));
304 EXPECT_THAT(result, HasSubstr("Page 2 content"));
307 // Complete the load.
308 distiller->RunDistillerCallback(article.Pass());
309 base::RunLoop().RunUntilIdle();
311 std::string result;
312 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
313 contents, kGetLoadIndicatorClassName, &result));
314 EXPECT_EQ("hidden", result);
315 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
316 contents, kGetContent , &result));
317 EXPECT_THAT(result, HasSubstr("Page 1 content"));
318 EXPECT_THAT(result, HasSubstr("Page 2 content"));
321 } // namespace dom_distiller