OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/path_service.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
9 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 11 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
10 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
15 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
16 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
17 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" | 19 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" |
18 #include "components/dom_distiller/core/distiller.h" | 20 #include "components/dom_distiller/core/distiller.h" |
19 #include "components/dom_distiller/core/dom_distiller_service.h" | 21 #include "components/dom_distiller/core/dom_distiller_service.h" |
20 #include "components/dom_distiller/core/dom_distiller_store.h" | 22 #include "components/dom_distiller/core/dom_distiller_store.h" |
21 #include "components/dom_distiller/core/dom_distiller_test_util.h" | 23 #include "components/dom_distiller/core/dom_distiller_test_util.h" |
22 #include "components/dom_distiller/core/fake_db.h" | 24 #include "components/dom_distiller/core/fake_db.h" |
23 #include "components/dom_distiller/core/fake_distiller.h" | 25 #include "components/dom_distiller/core/fake_distiller.h" |
26 #include "components/dom_distiller/core/fake_distiller_page.h" | |
24 #include "components/dom_distiller/core/task_tracker.h" | 27 #include "components/dom_distiller/core/task_tracker.h" |
28 #include "components/dom_distiller/core/url_constants.h" | |
25 #include "components/dom_distiller/core/url_utils.h" | 29 #include "components/dom_distiller/core/url_utils.h" |
26 #include "content/public/browser/render_view_host.h" | 30 #include "content/public/browser/render_view_host.h" |
27 #include "content/public/browser/url_data_source.h" | 31 #include "content/public/browser/url_data_source.h" |
28 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
29 #include "content/public/browser/web_contents_observer.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" | |
30 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
31 | 37 |
32 namespace dom_distiller { | 38 namespace dom_distiller { |
33 | 39 |
34 using test::FakeDB; | 40 using test::FakeDB; |
35 using test::FakeDistiller; | 41 using test::FakeDistiller; |
42 using test::MockDistillerPage; | |
36 using test::MockDistillerFactory; | 43 using test::MockDistillerFactory; |
44 using test::MockDistillerPageFactory; | |
37 using test::util::CreateStoreWithFakeDB; | 45 using test::util::CreateStoreWithFakeDB; |
46 using testing::HasSubstr; | |
47 using testing::Not; | |
38 | 48 |
39 namespace { | 49 namespace { |
40 | 50 |
51 const char kGetLoadIndicatorClassName[] = | |
52 "window.domAutomationController.send(" | |
53 "document.getElementById('loadingIndicator').className)"; | |
54 | |
55 const char kGetContent[] = | |
56 "window.domAutomationController.send(" | |
57 "document.getElementById('content').innerHTML)"; | |
58 | |
41 void AddEntry(const ArticleEntry& e, FakeDB::EntryMap* map) { | 59 void AddEntry(const ArticleEntry& e, FakeDB::EntryMap* map) { |
42 (*map)[e.entry_id()] = e; | 60 (*map)[e.entry_id()] = e; |
43 } | 61 } |
44 | 62 |
45 ArticleEntry CreateEntry(std::string entry_id, std::string page_url) { | 63 ArticleEntry CreateEntry(std::string entry_id, std::string page_url) { |
46 ArticleEntry entry; | 64 ArticleEntry entry; |
47 entry.set_entry_id(entry_id); | 65 entry.set_entry_id(entry_id); |
48 if (!page_url.empty()) { | 66 if (!page_url.empty()) { |
49 ArticleEntryPage* page = entry.add_pages(); | 67 ArticleEntryPage* page = entry.add_pages(); |
50 page->set_url(page_url); | 68 page->set_url(page_url); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
115 } | 133 } |
116 | 134 |
117 virtual void CleanUpOnMainThread() OVERRIDE { delete database_model_; } | 135 virtual void CleanUpOnMainThread() OVERRIDE { delete database_model_; } |
118 | 136 |
119 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 137 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
120 command_line->AppendSwitch(switches::kEnableDomDistiller); | 138 command_line->AppendSwitch(switches::kEnableDomDistiller); |
121 } | 139 } |
122 | 140 |
123 static KeyedService* Build(content::BrowserContext* context) { | 141 static KeyedService* Build(content::BrowserContext* context) { |
124 FakeDB* fake_db = new FakeDB(database_model_); | 142 FakeDB* fake_db = new FakeDB(database_model_); |
125 MockDistillerFactory* factory = new MockDistillerFactory(); | 143 distiller_factory_ = new MockDistillerFactory(); |
144 MockDistillerPageFactory* distiller_page_factory_ = | |
145 new MockDistillerPageFactory(); | |
126 DomDistillerContextKeyedService* service = | 146 DomDistillerContextKeyedService* service = |
127 new DomDistillerContextKeyedService( | 147 new DomDistillerContextKeyedService( |
128 scoped_ptr<DomDistillerStoreInterface>( | 148 scoped_ptr<DomDistillerStoreInterface>( |
129 CreateStoreWithFakeDB(fake_db, FakeDB::EntryMap())), | 149 CreateStoreWithFakeDB(fake_db, FakeDB::EntryMap())), |
130 scoped_ptr<DistillerFactory>(factory), | 150 scoped_ptr<DistillerFactory>(distiller_factory_), |
131 scoped_ptr<DistillerPageFactory>()); | 151 scoped_ptr<DistillerPageFactory>(distiller_page_factory_)); |
152 MockDistillerPage* distiller_page = new MockDistillerPage(); | |
153 EXPECT_CALL(*distiller_page_factory_, CreateDistillerPageImpl()) | |
154 .WillOnce(testing::Return(distiller_page)); | |
132 fake_db->InitCallback(true); | 155 fake_db->InitCallback(true); |
133 fake_db->LoadCallback(true); | 156 fake_db->LoadCallback(true); |
134 if (expect_distillation_) { | 157 if (expect_distillation_) { |
135 // There will only be destillation of an article if the database contains | 158 // There will only be destillation of an article if the database contains |
136 // the article. | 159 // the article. |
137 FakeDistiller* distiller = new FakeDistiller(true); | 160 FakeDistiller* distiller = new FakeDistiller(true); |
138 EXPECT_CALL(*factory, CreateDistillerImpl()) | 161 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
139 .WillOnce(testing::Return(distiller)); | 162 .WillOnce(testing::Return(distiller)); |
140 } | 163 } |
141 return service; | 164 return service; |
142 } | 165 } |
143 | 166 |
144 void ViewSingleDistilledPage(const GURL& url); | 167 void ViewSingleDistilledPage(const GURL& url); |
145 | |
146 // Database entries. | 168 // Database entries. |
147 static FakeDB::EntryMap* database_model_; | 169 static FakeDB::EntryMap* database_model_; |
148 static bool expect_distillation_; | 170 static bool expect_distillation_; |
171 static MockDistillerFactory* distiller_factory_; | |
149 }; | 172 }; |
150 | 173 |
151 FakeDB::EntryMap* DomDistillerViewerSourceBrowserTest::database_model_; | 174 FakeDB::EntryMap* DomDistillerViewerSourceBrowserTest::database_model_; |
152 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false; | 175 bool DomDistillerViewerSourceBrowserTest::expect_distillation_ = false; |
176 MockDistillerFactory* DomDistillerViewerSourceBrowserTest::distiller_factory_ = | |
177 NULL; | |
nyquist
2014/05/13 05:01:15
Nit: Indent.
Yaron
2014/05/14 17:42:33
Done.
| |
153 | 178 |
154 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings | 179 // The DomDistillerViewerSource renders untrusted content, so ensure no bindings |
155 // are enabled when the article exists in the database. | 180 // are enabled when the article exists in the database. |
156 // Flakiness: crbug.com/356866 | 181 // Flakiness: crbug.com/356866 |
157 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, | 182 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, |
158 DISABLED_NoWebUIBindingsArticleExists) { | 183 DISABLED_NoWebUIBindingsArticleExists) { |
159 // Ensure there is one item in the database, which will trigger distillation. | 184 // Ensure there is one item in the database, which will trigger distillation. |
160 const ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1"); | 185 const ArticleEntry entry = CreateEntry("DISTILLED", "http://example.com/1"); |
161 AddEntry(entry, database_model_); | 186 AddEntry(entry, database_model_); |
162 expect_distillation_ = true; | 187 expect_distillation_ = true; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 // are enabled when the CSS resource is loaded. This CSS might be bundle with | 244 // are enabled when the CSS resource is loaded. This CSS might be bundle with |
220 // Chrome or provided by an extension. | 245 // Chrome or provided by an extension. |
221 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, | 246 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, |
222 NoWebUIBindingsDisplayCSS) { | 247 NoWebUIBindingsDisplayCSS) { |
223 // Setup observer to inspect the RenderViewHost after committed navigation. | 248 // Setup observer to inspect the RenderViewHost after committed navigation. |
224 content::WebContents* contents = | 249 content::WebContents* contents = |
225 browser()->tab_strip_model()->GetActiveWebContents(); | 250 browser()->tab_strip_model()->GetActiveWebContents(); |
226 LoadSuccessObserver observer(contents); | 251 LoadSuccessObserver observer(contents); |
227 | 252 |
228 // Navigate to a URL which the source should respond to with CSS. | 253 // Navigate to a URL which the source should respond to with CSS. |
229 std::string url_without_scheme = "://foobar/readability.css"; | 254 std::string url_without_scheme = std::string("://foobar/") + kViewerCssPath; |
230 GURL url(chrome::kDomDistillerScheme + url_without_scheme); | 255 GURL url(chrome::kDomDistillerScheme + url_without_scheme); |
231 ui_test_utils::NavigateToURL(browser(), url); | 256 ui_test_utils::NavigateToURL(browser(), url); |
232 | 257 |
233 // A navigation should have succeeded to the correct URL. | 258 // A navigation should have succeeded to the correct URL. |
234 ASSERT_FALSE(observer.load_failed()); | 259 ASSERT_FALSE(observer.load_failed()); |
235 ASSERT_TRUE(observer.finished_load()); | 260 ASSERT_TRUE(observer.finished_load()); |
236 ASSERT_EQ(url, observer.validated_url()); | 261 ASSERT_EQ(url, observer.validated_url()); |
237 // Ensure no bindings. | 262 // Ensure no bindings. |
238 const content::RenderViewHost* render_view_host = observer.render_view_host(); | 263 const content::RenderViewHost* render_view_host = observer.render_view_host(); |
239 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); | 264 ASSERT_EQ(0, render_view_host->GetEnabledBindings()); |
240 // The MIME-type should always be text/css for the CSS resources. | 265 // The MIME-type should always be text/css for the CSS resources. |
241 EXPECT_EQ("text/css", observer.web_contents()->GetContentsMimeType()); | 266 EXPECT_EQ("text/css", observer.web_contents()->GetContentsMimeType()); |
242 } | 267 } |
243 | 268 |
269 | |
270 IN_PROC_BROWSER_TEST_F(DomDistillerViewerSourceBrowserTest, | |
271 MultiPageArticle) { | |
272 expect_distillation_ = false; | |
273 dom_distiller::DomDistillerServiceFactory::GetInstance() | |
274 ->SetTestingFactoryAndUse(browser()->profile(), &Build); | |
275 | |
276 scoped_refptr<content::MessageLoopRunner> distillation_done_runner = | |
277 new content::MessageLoopRunner; | |
278 | |
279 FakeDistiller* distiller = new FakeDistiller(false, distillation_done_runner); | |
280 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) | |
281 .WillOnce(testing::Return(distiller)); | |
282 | |
283 // Setup observer to inspect the RenderViewHost after committed navigation. | |
284 content::WebContents* contents = | |
285 browser()->tab_strip_model()->GetActiveWebContents(); | |
286 LoadSuccessObserver observer(contents); | |
287 | |
288 // Navigate to a URL and wait for the distiller to flush contents to the page. | |
289 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl( | |
290 chrome::kDomDistillerScheme, GURL("http://urlthatlooksvalid.com"))); | |
291 chrome::NavigateParams params(browser(), url, content::PAGE_TRANSITION_TYPED); | |
292 chrome::Navigate(¶ms); | |
293 distillation_done_runner->Run(); | |
294 | |
295 // Fake a multi-page response from distiller. | |
296 | |
297 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> > | |
298 update_pages; | |
299 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto()); | |
300 | |
301 // Flush page 1. | |
302 { | |
303 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto = | |
304 new base::RefCountedData<DistilledPageProto>(); | |
305 page_proto->data.set_url("http://foobar.1.html"); | |
306 page_proto->data.set_html("<div>Page 1 content</div>"); | |
307 update_pages.push_back(page_proto); | |
308 *(article->add_pages()) = page_proto->data; | |
309 | |
310 ArticleDistillationUpdate update(update_pages, true, false); | |
311 distiller->RunDistillerUpdateCallback(update); | |
312 | |
313 // Wait for the page load to complete as the first page completes the root | |
314 // document. | |
315 content::WaitForLoadStop(contents); | |
316 | |
317 std::string result; | |
318 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
319 contents, kGetLoadIndicatorClassName , &result)); | |
320 EXPECT_EQ("visible", result); | |
321 | |
322 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
323 contents, kGetContent , &result)); | |
324 EXPECT_THAT(result, HasSubstr("Page 1 content")); | |
325 EXPECT_THAT(result, Not(HasSubstr("Page 2 content"))); | |
326 } | |
327 | |
328 // Flush page 2. | |
329 { | |
330 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto = | |
331 new base::RefCountedData<DistilledPageProto>(); | |
332 page_proto->data.set_url("http://foobar.2.html"); | |
333 page_proto->data.set_html("<div>Page 2 content</div>"); | |
334 update_pages.push_back(page_proto); | |
335 *(article->add_pages()) = page_proto->data; | |
336 | |
337 ArticleDistillationUpdate update(update_pages, false, false); | |
338 distiller->RunDistillerUpdateCallback(update); | |
339 | |
340 std::string result; | |
341 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
342 contents, kGetLoadIndicatorClassName , &result)); | |
343 EXPECT_EQ("visible", result); | |
344 | |
345 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
346 contents, kGetContent , &result)); | |
347 EXPECT_THAT(result, HasSubstr("Page 1 content")); | |
348 EXPECT_THAT(result, HasSubstr("Page 2 content")); | |
349 } | |
350 | |
351 // Complete the load. | |
352 distiller->RunDistillerCallback(article.Pass()); | |
353 base::RunLoop().RunUntilIdle(); | |
354 | |
355 std::string result; | |
356 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
357 contents, kGetLoadIndicatorClassName, &result)); | |
358 EXPECT_EQ("hidden", result); | |
359 EXPECT_TRUE(content::ExecuteScriptAndExtractString( | |
360 contents, kGetContent , &result)); | |
361 EXPECT_THAT(result, HasSubstr("Page 1 content")); | |
362 EXPECT_THAT(result, HasSubstr("Page 2 content")); | |
363 } | |
364 | |
244 } // namespace dom_distiller | 365 } // namespace dom_distiller |
OLD | NEW |