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" |
| 12 | #include "content/public/common/content_switches.h" |
| 13 | #include "content/public/test/browser_test_utils.h" |
| 14 | #include "content/public/test/find_test_utils.h" |
| 15 | #include "content/public/test/test_navigation_observer.h" |
| 16 | #include "net/dns/mock_host_resolver.h" |
| 17 | #include "third_party/WebKit/public/web/WebFindOptions.h" |
| 18 | |
| 19 | namespace content { |
| 20 | |
| 21 | class ChromeFindRequestManagerTest : public InProcessBrowserTest { |
| 22 | public: |
| 23 | ChromeFindRequestManagerTest() |
| 24 | : normal_delegate_(nullptr), |
| 25 | last_request_id_(0) {} |
| 26 | ~ChromeFindRequestManagerTest() override {} |
| 27 | |
| 28 | void SetUpOnMainThread() override { |
| 29 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 30 | embedded_test_server()->ServeFilesFromSourceDirectory("content/test/data"); |
| 31 | ASSERT_TRUE(embedded_test_server()->Start()); |
| 32 | |
| 33 | // Swap the WebContents's delegate for our test delegate. |
| 34 | normal_delegate_ = contents()->GetDelegate(); |
| 35 | contents()->SetDelegate(&test_delegate_); |
| 36 | } |
| 37 | |
| 38 | void TearDownOnMainThread() override { |
| 39 | // Swap the WebContents's delegate back to its usual delegate. |
| 40 | contents()->SetDelegate(normal_delegate_); |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | // Navigates to |url| and waits for it to finish loading. |
| 45 | void LoadAndWait(const std::string& url) { |
| 46 | TestNavigationObserver navigation_observer(contents()); |
| 47 | ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
| 48 | browser(), embedded_test_server()->GetURL("a.com", url), 1); |
| 49 | ASSERT_TRUE(navigation_observer.last_navigation_succeeded()); |
| 50 | } |
| 51 | |
| 52 | void Find(const std::string& search_text, |
| 53 | const blink::WebFindOptions& options) { |
| 54 | delegate()->UpdateLastRequest(++last_request_id_); |
| 55 | contents()->Find(last_request_id_, |
| 56 | base::UTF8ToUTF16(search_text), |
| 57 | options); |
| 58 | } |
| 59 | |
| 60 | WebContents* contents() const { |
| 61 | return browser()->tab_strip_model()->GetActiveWebContents(); |
| 62 | } |
| 63 | |
| 64 | FindTestWebContentsDelegate* delegate() const { |
| 65 | return static_cast<FindTestWebContentsDelegate*>(contents()->GetDelegate()); |
| 66 | } |
| 67 | |
| 68 | int last_request_id() const { |
| 69 | return last_request_id_; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | FindTestWebContentsDelegate test_delegate_; |
| 74 | WebContentsDelegate* normal_delegate_; |
| 75 | |
| 76 | // The ID of the last find request requested. |
| 77 | int last_request_id_; |
| 78 | |
| 79 | DISALLOW_COPY_AND_ASSIGN(ChromeFindRequestManagerTest); |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | // Tests searching in a full-page PDF. |
| 84 | IN_PROC_BROWSER_TEST_F(ChromeFindRequestManagerTest, FindInPDF) { |
| 85 | LoadAndWait("/find_in_pdf_page.pdf"); |
| 86 | pdf_extension_test_util::EnsurePDFHasLoaded(contents()); |
| 87 | |
| 88 | blink::WebFindOptions options; |
| 89 | Find("result", options); |
| 90 | options.findNext = true; |
| 91 | Find("result", options); |
| 92 | Find("result", options); |
| 93 | delegate()->WaitForFinalReply(); |
| 94 | |
| 95 | FindResults results = delegate()->GetFindResults(); |
| 96 | EXPECT_EQ(last_request_id(), results.request_id); |
| 97 | EXPECT_EQ(5, results.number_of_matches); |
| 98 | EXPECT_EQ(3, results.active_match_ordinal); |
| 99 | } |
| 100 | |
| 101 | } // namespace content |