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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 this)); | 54 this)); |
54 } | 55 } |
55 | 56 |
56 void OnPageDistillationFinished( | 57 void OnPageDistillationFinished( |
57 scoped_ptr<proto::DomDistillerResult> distiller_result, | 58 scoped_ptr<proto::DomDistillerResult> distiller_result, |
58 bool distillation_successful) { | 59 bool distillation_successful) { |
59 distiller_result_ = distiller_result.Pass(); | 60 distiller_result_ = distiller_result.Pass(); |
60 quit_closure_.Run(); | 61 quit_closure_.Run(); |
61 } | 62 } |
62 | 63 |
64 void OnJsExecutionDone(const base::Value* value) { | |
65 js_result_ = value->DeepCopy(); | |
66 callback_.Run(); | |
67 } | |
68 | |
63 private: | 69 private: |
64 void AddComponentsResources() { | 70 void AddComponentsResources() { |
65 base::FilePath pak_file; | 71 base::FilePath pak_file; |
66 base::FilePath pak_dir; | 72 base::FilePath pak_dir; |
67 PathService::Get(base::DIR_MODULE, &pak_dir); | 73 PathService::Get(base::DIR_MODULE, &pak_dir); |
68 pak_file = | 74 pak_file = |
69 pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak")); | 75 pak_dir.Append(FILE_PATH_LITERAL("components_tests_resources.pak")); |
70 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( | 76 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath( |
71 pak_file, ui::SCALE_FACTOR_NONE); | 77 pak_file, ui::SCALE_FACTOR_NONE); |
72 } | 78 } |
73 | 79 |
74 void SetUpTestServer() { | 80 void SetUpTestServer() { |
75 base::FilePath path; | 81 base::FilePath path; |
76 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 82 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
77 path = path.AppendASCII("components/test/data/dom_distiller"); | 83 path = path.AppendASCII("components/test/data/dom_distiller"); |
78 embedded_test_server()->ServeFilesFromDirectory(path); | 84 embedded_test_server()->ServeFilesFromDirectory(path); |
79 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 85 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
80 } | 86 } |
81 | 87 |
82 protected: | 88 protected: |
83 void RunUseCurrentWebContentsTest(const std::string& url, | 89 void RunUseCurrentWebContentsTest(const std::string& url, |
84 bool expect_new_web_contents, | 90 bool expect_new_web_contents, |
85 bool setup_main_frame_observer, | 91 bool setup_main_frame_observer, |
86 bool wait_for_document_loaded); | 92 bool wait_for_document_loaded); |
87 | 93 |
88 DistillerPageWebContents* distiller_page_; | 94 DistillerPageWebContents* distiller_page_; |
89 base::Closure quit_closure_; | 95 base::Closure quit_closure_; |
90 scoped_ptr<proto::DomDistillerResult> distiller_result_; | 96 scoped_ptr<proto::DomDistillerResult> distiller_result_; |
97 base::Closure callback_; | |
98 const base::Value* js_result_; | |
91 }; | 99 }; |
92 | 100 |
93 // Use this class to be able to leak the WebContents, which is needed for when | 101 // Use this class to be able to leak the WebContents, which is needed for when |
94 // the current WebContents is used for distillation. | 102 // the current WebContents is used for distillation. |
95 class TestDistillerPageWebContents : public DistillerPageWebContents { | 103 class TestDistillerPageWebContents : public DistillerPageWebContents { |
96 public: | 104 public: |
97 TestDistillerPageWebContents( | 105 TestDistillerPageWebContents( |
98 content::BrowserContext* browser_context, | 106 content::BrowserContext* browser_context, |
99 const gfx::Size& render_view_size, | 107 const gfx::Size& render_view_size, |
100 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle, | 108 scoped_ptr<SourcePageHandleWebContents> optional_web_contents_handle, |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
512 page_proto->set_title(some_title); | 520 page_proto->set_title(some_title); |
513 std::string html = viewer::GetUnsafePartialArticleHtml(page_proto.get(), | 521 std::string html = viewer::GetUnsafePartialArticleHtml(page_proto.get(), |
514 DistilledPagePrefs::LIGHT, DistilledPagePrefs::SERIF); | 522 DistilledPagePrefs::LIGHT, DistilledPagePrefs::SERIF); |
515 EXPECT_THAT(html, HasSubstr(some_title)); | 523 EXPECT_THAT(html, HasSubstr(some_title)); |
516 EXPECT_THAT(html, HasSubstr(no_content)); | 524 EXPECT_THAT(html, HasSubstr(no_content)); |
517 EXPECT_THAT(html, Not(HasSubstr(no_title))); | 525 EXPECT_THAT(html, Not(HasSubstr(no_title))); |
518 EXPECT_THAT(html, Not(HasSubstr(some_content))); | 526 EXPECT_THAT(html, Not(HasSubstr(some_content))); |
519 } | 527 } |
520 } | 528 } |
521 | 529 |
530 IN_PROC_BROWSER_TEST_F(DistillerPageWebContentsTest, | |
531 TestPinch) { | |
532 // Load the test file in content shell and wait until it has fully loaded. | |
533 content::WebContents* web_contents = shell()->web_contents(); | |
534 dom_distiller::WebContentsMainFrameObserver::CreateForWebContents( | |
535 web_contents); | |
536 base::RunLoop url_loaded_runner; | |
537 WebContentsMainFrameHelper main_frame_loaded(web_contents, | |
538 url_loaded_runner.QuitClosure(), | |
539 true); | |
540 web_contents->GetController().LoadURL( | |
541 embedded_test_server()->GetURL("pinch_tester.html"), | |
542 content::Referrer(), | |
543 ui::PAGE_TRANSITION_TYPED, | |
544 std::string()); | |
545 url_loaded_runner.Run(); | |
546 | |
547 // Execute the JS to run the tests, and wait until it has finished. | |
548 base::RunLoop run_loop; | |
549 callback_ = run_loop.QuitClosure(); | |
jdduke (slow)
2015/03/23 15:42:06
To avoid confusion with the existing quit_closure_
wychen
2015/03/24 22:52:08
Done.
| |
550 web_contents->GetMainFrame()->ExecuteJavaScript( | |
551 base::UTF8ToUTF16("(function() {return pinchtest.run();})();"), | |
552 base::Bind(&DistillerPageWebContentsTest::OnJsExecutionDone, | |
553 base::Unretained(this))); | |
554 run_loop.Run(); | |
555 | |
556 // Convert to dictionary and parse the results. | |
557 const base::DictionaryValue* dict; | |
558 js_result_->GetAsDictionary(&dict); | |
559 ASSERT_TRUE(js_result_->GetAsDictionary(&dict)); | |
560 | |
561 ASSERT_TRUE(dict->HasKey("success")); | |
562 bool success; | |
563 ASSERT_TRUE(dict->GetBoolean("success", &success)); | |
564 EXPECT_TRUE(success); | |
565 } | |
566 | |
522 } // namespace dom_distiller | 567 } // namespace dom_distiller |
OLD | NEW |