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; |
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( |
| 280 false, |
| 281 distillation_done_runner->QuitClosure()); |
| 282 EXPECT_CALL(*distiller_factory_, CreateDistillerImpl()) |
| 283 .WillOnce(testing::Return(distiller)); |
| 284 |
| 285 // Setup observer to inspect the RenderViewHost after committed navigation. |
| 286 content::WebContents* contents = |
| 287 browser()->tab_strip_model()->GetActiveWebContents(); |
| 288 LoadSuccessObserver observer(contents); |
| 289 |
| 290 // Navigate to a URL and wait for the distiller to flush contents to the page. |
| 291 GURL url(dom_distiller::url_utils::GetDistillerViewUrlFromUrl( |
| 292 chrome::kDomDistillerScheme, GURL("http://urlthatlooksvalid.com"))); |
| 293 chrome::NavigateParams params(browser(), url, content::PAGE_TRANSITION_TYPED); |
| 294 chrome::Navigate(¶ms); |
| 295 distillation_done_runner->Run(); |
| 296 |
| 297 // Fake a multi-page response from distiller. |
| 298 |
| 299 std::vector<scoped_refptr<ArticleDistillationUpdate::RefCountedPageProto> > |
| 300 update_pages; |
| 301 scoped_ptr<DistilledArticleProto> article(new DistilledArticleProto()); |
| 302 |
| 303 // Flush page 1. |
| 304 { |
| 305 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto = |
| 306 new base::RefCountedData<DistilledPageProto>(); |
| 307 page_proto->data.set_url("http://foobar.1.html"); |
| 308 page_proto->data.set_html("<div>Page 1 content</div>"); |
| 309 update_pages.push_back(page_proto); |
| 310 *(article->add_pages()) = page_proto->data; |
| 311 |
| 312 ArticleDistillationUpdate update(update_pages, true, false); |
| 313 distiller->RunDistillerUpdateCallback(update); |
| 314 |
| 315 // Wait for the page load to complete as the first page completes the root |
| 316 // document. |
| 317 content::WaitForLoadStop(contents); |
| 318 |
| 319 std::string result; |
| 320 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 321 contents, kGetLoadIndicatorClassName , &result)); |
| 322 EXPECT_EQ("visible", result); |
| 323 |
| 324 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 325 contents, kGetContent , &result)); |
| 326 EXPECT_THAT(result, HasSubstr("Page 1 content")); |
| 327 EXPECT_THAT(result, Not(HasSubstr("Page 2 content"))); |
| 328 } |
| 329 |
| 330 // Flush page 2. |
| 331 { |
| 332 scoped_refptr<base::RefCountedData<DistilledPageProto> > page_proto = |
| 333 new base::RefCountedData<DistilledPageProto>(); |
| 334 page_proto->data.set_url("http://foobar.2.html"); |
| 335 page_proto->data.set_html("<div>Page 2 content</div>"); |
| 336 update_pages.push_back(page_proto); |
| 337 *(article->add_pages()) = page_proto->data; |
| 338 |
| 339 ArticleDistillationUpdate update(update_pages, false, false); |
| 340 distiller->RunDistillerUpdateCallback(update); |
| 341 |
| 342 std::string result; |
| 343 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 344 contents, kGetLoadIndicatorClassName , &result)); |
| 345 EXPECT_EQ("visible", result); |
| 346 |
| 347 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 348 contents, kGetContent , &result)); |
| 349 EXPECT_THAT(result, HasSubstr("Page 1 content")); |
| 350 EXPECT_THAT(result, HasSubstr("Page 2 content")); |
| 351 } |
| 352 |
| 353 // Complete the load. |
| 354 distiller->RunDistillerCallback(article.Pass()); |
| 355 base::RunLoop().RunUntilIdle(); |
| 356 |
| 357 std::string result; |
| 358 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 359 contents, kGetLoadIndicatorClassName, &result)); |
| 360 EXPECT_EQ("hidden", result); |
| 361 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 362 contents, kGetContent , &result)); |
| 363 EXPECT_THAT(result, HasSubstr("Page 1 content")); |
| 364 EXPECT_THAT(result, HasSubstr("Page 2 content")); |
| 365 } |
| 366 |
244 } // namespace dom_distiller | 367 } // namespace dom_distiller |
OLD | NEW |