paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 1 | // Copyright 2017 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. |
| 4 | |
| 5 | #include "base/command_line.h" |
| 6 | #include "base/strings/utf_string_conversions.h" |
| 7 | #include "chrome/browser/pdf/pdf_extension_test_util.h" |
| 8 | #include "chrome/browser/ui/browser.h" |
| 9 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 | #include "chrome/test/base/in_process_browser_test.h" |
| 11 | #include "chrome/test/base/ui_test_utils.h" |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 12 | #include "content/public/browser/browser_context.h" |
| 13 | #include "content/public/browser/browser_plugin_guest_manager.h" |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 14 | #include "content/public/common/content_switches.h" |
| 15 | #include "content/public/test/browser_test_utils.h" |
| 16 | #include "content/public/test/find_test_utils.h" |
| 17 | #include "content/public/test/test_navigation_observer.h" |
| 18 | #include "net/dns/mock_host_resolver.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 19 | #include "third_party/blink/public/web/web_find_options.h" |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 20 | |
| 21 | namespace content { |
| 22 | |
| 23 | class ChromeFindRequestManagerTest : public InProcessBrowserTest { |
| 24 | public: |
| 25 | ChromeFindRequestManagerTest() |
| 26 | : normal_delegate_(nullptr), |
| 27 | last_request_id_(0) {} |
| 28 | ~ChromeFindRequestManagerTest() override {} |
| 29 | |
| 30 | void SetUpOnMainThread() override { |
| 31 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 32 | embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data"); |
| 33 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 34 | |
| 35 | // Swap the WebContents's delegate for our test delegate. |
| 36 | normal_delegate_ = contents()->GetDelegate(); |
| 37 | contents()->SetDelegate(&test_delegate_); |
| 38 | } |
| 39 | |
| 40 | void TearDownOnMainThread() override { |
| 41 | // Swap the WebContents's delegate back to its usual delegate. |
| 42 | contents()->SetDelegate(normal_delegate_); |
| 43 | } |
| 44 | |
| 45 | protected: |
| 46 | // Navigates to |url| and waits for it to finish loading. |
| 47 | void LoadAndWait(const std::string& url) { |
| 48 | TestNavigationObserver navigation_observer(contents()); |
| 49 | ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 50 | browser(), embedded_test_server()->GetURL("a.com", url), 1); |
| 51 | ASSERT_TRUE(navigation_observer.last_navigation_succeeded()); |
| 52 | } |
| 53 | |
| 54 | void Find(const std::string& search_text, |
| 55 | const blink::WebFindOptions& options) { |
| 56 | delegate()->UpdateLastRequest(++last_request_id_); |
| 57 | contents()->Find(last_request_id_, |
| 58 | base::UTF8ToUTF16(search_text), |
| 59 | options); |
| 60 | } |
| 61 | |
| 62 | WebContents* contents() const { |
| 63 | return browser()->tab_strip_model()->GetActiveWebContents(); |
| 64 | } |
| 65 | |
| 66 | FindTestWebContentsDelegate* delegate() const { |
| 67 | return static_cast<FindTestWebContentsDelegate*>(contents()->GetDelegate()); |
| 68 | } |
| 69 | |
| 70 | int last_request_id() const { |
| 71 | return last_request_id_; |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | FindTestWebContentsDelegate test_delegate_; |
| 76 | WebContentsDelegate* normal_delegate_; |
| 77 | |
| 78 | // The ID of the last find request requested. |
| 79 | int last_request_id_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(ChromeFindRequestManagerTest); |
| 82 | }; |
| 83 | |
| 84 | |
| 85 | // Tests searching in a full-page PDF. |
| 86 | IN_PROC_BROWSER_TEST_F(ChromeFindRequestManagerTest, FindInPDF) { |
| 87 | LoadAndWait("/find_in_pdf_page.pdf"); |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 88 | ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(contents())); |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 89 | |
| 90 | blink::WebFindOptions options; |
| 91 | Find("result", options); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 92 | options.find_next = true; |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 93 | Find("result", options); |
| 94 | Find("result", options); |
| 95 | delegate()->WaitForFinalReply(); |
| 96 | |
| 97 | FindResults results = delegate()->GetFindResults(); |
| 98 | EXPECT_EQ(last_request_id(), results.request_id); |
| 99 | EXPECT_EQ(5, results.number_of_matches); |
| 100 | EXPECT_EQ(3, results.active_match_ordinal); |
| 101 | } |
| 102 | |
paulmeyer | feafc2d | 2017-04-25 21:46:40 | [diff] [blame] | 103 | // Tests searching in a page with embedded PDFs. Note that this test, the |
| 104 | // FindInPDF test, and the find tests in web_view_browsertest.cc ensure that |
| 105 | // find-in-page works across GuestViews. |
| 106 | // |
| 107 | // TODO(paulmeyer): Note that this is left disabled for now since |
| 108 | // EnsurePDFHasLoaded() currently does not work for embedded PDFs. This will be |
| 109 | // fixed and enabled in a subsequent patch. |
| 110 | IN_PROC_BROWSER_TEST_F(ChromeFindRequestManagerTest, |
| 111 | DISABLED_FindInEmbeddedPDFs) { |
| 112 | LoadAndWait("/find_in_embedded_pdf_page.html"); |
| 113 | ASSERT_TRUE(pdf_extension_test_util::EnsurePDFHasLoaded(contents())); |
| 114 | |
| 115 | blink::WebFindOptions options; |
| 116 | Find("result", options); |
| 117 | options.find_next = true; |
| 118 | options.forward = false; |
| 119 | Find("result", options); |
| 120 | Find("result", options); |
| 121 | Find("result", options); |
| 122 | delegate()->WaitForFinalReply(); |
| 123 | |
| 124 | FindResults results = delegate()->GetFindResults(); |
| 125 | EXPECT_EQ(last_request_id(), results.request_id); |
| 126 | EXPECT_EQ(13, results.number_of_matches); |
| 127 | EXPECT_EQ(11, results.active_match_ordinal); |
| 128 | } |
| 129 | |
paulmeyer | da992f5 | 2017-01-27 17:11:28 | [diff] [blame] | 130 | } // namespace content |