OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "components/dom_distiller/content/distiller_page_web_contents.h" | 10 #include "components/dom_distiller/content/distiller_page_web_contents.h" |
10 #include "components/dom_distiller/content/web_contents_main_frame_observer.h" | 11 #include "components/dom_distiller/content/web_contents_main_frame_observer.h" |
11 #include "components/dom_distiller/core/distiller_page.h" | 12 #include "components/dom_distiller/core/distiller_page.h" |
12 #include "components/dom_distiller/core/proto/distilled_article.pb.h" | 13 #include "components/dom_distiller/core/proto/distilled_article.pb.h" |
13 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 14 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
14 #include "components/dom_distiller/core/viewer.h" | 15 #include "components/dom_distiller/core/viewer.h" |
15 #include "content/public/browser/browser_context.h" | 16 #include "content/public/browser/browser_context.h" |
16 #include "content/public/browser/navigation_controller.h" | 17 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 this)); | 94 this)); |
94 } | 95 } |
95 | 96 |
96 void OnPageDistillationFinished( | 97 void OnPageDistillationFinished( |
97 scoped_ptr<proto::DomDistillerResult> distiller_result, | 98 scoped_ptr<proto::DomDistillerResult> distiller_result, |
98 bool distillation_successful) { | 99 bool distillation_successful) { |
99 distiller_result_ = distiller_result.Pass(); | 100 distiller_result_ = distiller_result.Pass(); |
100 quit_closure_.Run(); | 101 quit_closure_.Run(); |
101 } | 102 } |
102 | 103 |
| 104 void OnJsExecutionDone(base::Closure callback, const base::Value* value) { |
| 105 js_result_.reset(value->DeepCopy()); |
| 106 callback.Run(); |
| 107 } |
| 108 |
103 private: | 109 private: |
104 void AddComponentsResources() { | 110 void AddComponentsResources() { |
105 base::FilePath pak_file; | 111 base::FilePath pak_file; |
106 base::FilePath pak_dir; | 112 base::FilePath pak_dir; |
107 #if defined(OS_ANDROID) | 113 #if defined(OS_ANDROID) |
108 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir)); | 114 CHECK(PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir)); |
109 pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks")); | 115 pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks")); |
110 #else | 116 #else |
111 PathService::Get(base::DIR_MODULE, &pak_dir); | 117 PathService::Get(base::DIR_MODULE, &pak_dir); |
112 #endif // OS_ANDROID | 118 #endif // OS_ANDROID |
113 pak_file = | 119 pak_file = |
114 pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak")); | 120 pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak")); |
115 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | 121 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
116 pak_file, ui::SCALE_FACTOR_NONE); | 122 pak_file, ui::SCALE_FACTOR_NONE); |
117 } | 123 } |
118 | 124 |
119 void SetUpTestServer() { | 125 void SetUpTestServer() { |
120 base::FilePath path; | 126 base::FilePath path; |
121 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 127 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
122 path = path.AppendASCII("components/test/data/dom_distiller"); | 128 embedded_test_server()->ServeFilesFromDirectory( |
123 embedded_test_server()->ServeFilesFromDirectory(path); | 129 path.AppendASCII("components/test/data/dom_distiller")); |
| 130 embedded_test_server()->ServeFilesFromDirectory( |
| 131 path.AppendASCII("components/dom_distiller/core/javascript")); |
124 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 132 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
125 } | 133 } |
126 | 134 |
127 protected: | 135 protected: |
128 void RunUseCurrentWebContentsTest(const std::string& url, | 136 void RunUseCurrentWebContentsTest(const std::string& url, |
129 bool expect_new_web_contents, | 137 bool expect_new_web_contents, |
130 bool setup_main_frame_observer, | 138 bool setup_main_frame_observer, |
131 bool wait_for_document_loaded); | 139 bool wait_for_document_loaded); |
132 | 140 |
133 DistillerPageWebContents* distiller_page_; | 141 DistillerPageWebContents* distiller_page_; |
134 base::Closure quit_closure_; | 142 base::Closure quit_closure_; |
135 scoped_ptr<proto::DomDistillerResult> distiller_result_; | 143 scoped_ptr<proto::DomDistillerResult> distiller_result_; |
| 144 scoped_ptr<base::Value> js_result_; |
136 }; | 145 }; |
137 | 146 |
138 // Use this class to be able to leak the WebContents, which is needed for when | 147 // Use this class to be able to leak the WebContents, which is needed for when |
139 // the current WebContents is used for distillation. | 148 // the current WebContents is used for distillation. |
140 class TestDistillerPageWebContents : public DistillerPageWebContents { | 149 class TestDistillerPageWebContents : public DistillerPageWebContents { |
141 public: | 150 public: |
142 TestDistillerPageWebContents( | 151 TestDistillerPageWebContents( |
143 content::BrowserContext* browser_context, | 152 content::BrowserContext* browser_context, |
144 const gfx::Size& render_view_size, | 153 const gfx::Size& render_view_size, |
145 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle, | 154 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle, |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 | 460 |
452 { // Test missing title for page. | 461 { // Test missing title for page. |
453 scoped_ptr<DistilledPageProto> page_proto(new DistilledPageProto()); | 462 scoped_ptr<DistilledPageProto> page_proto(new DistilledPageProto()); |
454 std::string html = viewer::GetUnsafeArticleTemplateHtml( | 463 std::string html = viewer::GetUnsafeArticleTemplateHtml( |
455 page_proto.get(), DistilledPagePrefs::LIGHT, DistilledPagePrefs::SERIF); | 464 page_proto.get(), DistilledPagePrefs::LIGHT, DistilledPagePrefs::SERIF); |
456 EXPECT_THAT(html, HasSubstr(no_title)); | 465 EXPECT_THAT(html, HasSubstr(no_title)); |
457 EXPECT_THAT(html, Not(HasSubstr(some_title))); | 466 EXPECT_THAT(html, Not(HasSubstr(some_title))); |
458 } | 467 } |
459 } | 468 } |
460 | 469 |
| 470 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, |
| 471 TestPinch) { |
| 472 // Load the test file in content shell and wait until it has fully loaded. |
| 473 content::WebContents* web_contents = shell()->web_contents(); |
| 474 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( |
| 475 web_contents); |
| 476 base::RunLoop url_loaded_runner; |
| 477 WebContentsMainFrameHelper main_frame_loaded(web_contents, |
| 478 url_loaded_runner.QuitClosure(), |
| 479 true); |
| 480 web_contents->GetController().LoadURL( |
| 481 embedded_test_server()->GetURL("/pinch_tester.html"), |
| 482 content::Referrer(), |
| 483 ui::PAGE_TRANSITION_TYPED, |
| 484 std::string()); |
| 485 url_loaded_runner.Run(); |
| 486 |
| 487 // Execute the JS to run the tests, and wait until it has finished. |
| 488 base::RunLoop run_loop; |
| 489 web_contents->GetMainFrame()->ExecuteJavaScript( |
| 490 base::UTF8ToUTF16("(function() {return pinchtest.run();})();"), |
| 491 base::Bind(&DistillerPageWebContentsTest::OnJsExecutionDone, |
| 492 base::Unretained(this), run_loop.QuitClosure())); |
| 493 run_loop.Run(); |
| 494 |
| 495 // Convert to dictionary and parse the results. |
| 496 const base::DictionaryValue* dict; |
| 497 ASSERT_TRUE(js_result_); |
| 498 ASSERT_TRUE(js_result_->GetAsDictionary(&dict)); |
| 499 |
| 500 ASSERT_TRUE(dict->HasKey("success")); |
| 501 bool success; |
| 502 ASSERT_TRUE(dict->GetBoolean("success", &success)); |
| 503 EXPECT_TRUE(success); |
| 504 } |
| 505 |
461 } // namespace dom_distiller | 506 } // namespace dom_distiller |
OLD | NEW |