1 // Copyright (c) 2006-2008 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.
5 #include "base/message_loop.h"
6 #include "chrome/browser/browser.h"
7 #include "chrome/browser/find_notification_details.h"
8 #include "chrome/browser/renderer_host/render_view_host.h"
9 #include "chrome/browser/tab_contents/tab_contents.h"
10 #include "chrome/browser/tab_contents/web_contents.h"
11 #include "chrome/browser/tab_contents/web_contents_view.h"
12 #include "chrome/browser/views/find_bar_win.h"
13 #include "chrome/common/notification_service.h"
14 #include "chrome/test/in_process_browser_test.h"
15 #include "chrome/test/ui_test_utils.h"
17 const std::wstring kFramePage
= L
"files/find_in_page/frames.html";
18 const std::wstring kFrameData
= L
"files/find_in_page/framedata_general.html";
19 const std::wstring kUserSelectPage
= L
"files/find_in_page/user-select.html";
20 const std::wstring kCrashPage
= L
"files/find_in_page/crash_1341577.html";
21 const std::wstring kTooFewMatchesPage
= L
"files/find_in_page/bug_1155639.html";
23 class FindInPageNotificationObserver
: public NotificationObserver
{
25 explicit FindInPageNotificationObserver(TabContents
* parent_tab
)
26 : parent_tab_(parent_tab
),
27 active_match_ordinal_(-1),
28 number_of_matches_(0) {
29 registrar_
.Add(this, NotificationType::FIND_RESULT_AVAILABLE
,
30 Source
<TabContents
>(parent_tab_
));
31 ui_test_utils::RunMessageLoop();
34 int active_match_ordinal() const { return active_match_ordinal_
; }
36 int number_of_matches() const { return number_of_matches_
; }
38 virtual void Observe(NotificationType type
, const NotificationSource
& source
,
39 const NotificationDetails
& details
) {
40 if (type
== NotificationType::FIND_RESULT_AVAILABLE
) {
41 Details
<FindNotificationDetails
> find_details(details
);
42 if (find_details
->request_id() == kFindInPageRequestId
) {
43 // We get multiple responses and one of those will contain the ordinal.
44 // This message comes to us before the final update is sent.
45 if (find_details
->active_match_ordinal() > -1)
46 active_match_ordinal_
= find_details
->active_match_ordinal();
47 if (find_details
->final_update()) {
48 number_of_matches_
= find_details
->number_of_matches();
49 MessageLoopForUI::current()->Quit();
51 DLOG(INFO
) << "Ignoring, since we only care about the final message";
59 // The Find mechanism is over asynchronous IPC, so a search is kicked off and
60 // we wait for notification to find out what the results are. As the user is
61 // typing, new search requests can be issued and the Request ID helps us make
62 // sense of whether this is the current request or an old one. The unit tests,
63 // however, which uses this constant issues only one search at a time, so we
64 // don't need a rolling id to identify each search. But, we still need to
65 // specify one, so we just use a fixed one - its value does not matter.
66 static const int kFindInPageRequestId
;
69 NotificationRegistrar registrar_
;
70 TabContents
* parent_tab_
;
71 // We will at some point (before final update) be notified of the ordinal and
72 // we need to preserve it so we can send it later.
73 int active_match_ordinal_
;
74 int number_of_matches_
;
77 typedef enum FindInPageDirection
{ BACK
= 0, FWD
= 1 };
78 typedef enum FindInPageCase
{ IGNORE_CASE
= 0, CASE_SENSITIVE
= 1 };
80 class FindInPageControllerTest
: public InProcessBrowserTest
{
82 FindInPageControllerTest() {}
85 int FindInPage(const std::wstring
& search_string
,
86 FindInPageDirection forward
,
87 FindInPageCase match_case
,
89 WebContents
* web_contents
=
90 browser()->GetSelectedTabContents()->AsWebContents();
92 web_contents
->set_current_find_request_id(
93 FindInPageNotificationObserver::kFindInPageRequestId
);
94 web_contents
->render_view_host()->StartFinding(
95 FindInPageNotificationObserver::kFindInPageRequestId
,
96 search_string
, forward
== FWD
, match_case
== CASE_SENSITIVE
,
98 return FindInPageNotificationObserver(web_contents
).number_of_matches();
104 // This test loads a page with frames and starts FindInPage requests
105 IN_PROC_BROWSER_TEST_F(FindInPageControllerTest
, FindInPageFrames
) {
106 HTTPTestServer
* server
= StartHTTPServer();
108 // First we navigate to our frames page.
109 GURL url
= server
->TestServerPageW(kFramePage
);
110 ui_test_utils::NavigateToURL(browser(), url
);
112 // Try incremental search (mimicking user typing in).
113 EXPECT_EQ(18, FindInPage(L
"g", FWD
, IGNORE_CASE
, false));
114 EXPECT_EQ(11, FindInPage(L
"go", FWD
, IGNORE_CASE
, false));
115 EXPECT_EQ(04, FindInPage(L
"goo", FWD
, IGNORE_CASE
, false));
116 EXPECT_EQ(03, FindInPage(L
"goog", FWD
, IGNORE_CASE
, false));
117 EXPECT_EQ(02, FindInPage(L
"googl", FWD
, IGNORE_CASE
, false));
118 EXPECT_EQ(01, FindInPage(L
"google", FWD
, IGNORE_CASE
, false));
119 EXPECT_EQ(00, FindInPage(L
"google!", FWD
, IGNORE_CASE
, false));
121 // Negative test (no matches should be found).
122 EXPECT_EQ(0, FindInPage(L
"Non-existing string", FWD
, IGNORE_CASE
, false));
124 // 'horse' only exists in the three right frames.
125 EXPECT_EQ(3, FindInPage(L
"horse", FWD
, IGNORE_CASE
, false));
127 // 'cat' only exists in the first frame.
128 EXPECT_EQ(1, FindInPage(L
"cat", FWD
, IGNORE_CASE
, false));
130 // Try searching again, should still come up with 1 match.
131 EXPECT_EQ(1, FindInPage(L
"cat", FWD
, IGNORE_CASE
, false));
133 // Try searching backwards, ignoring case, should still come up with 1 match.
134 EXPECT_EQ(1, FindInPage(L
"CAT", BACK
, IGNORE_CASE
, false));
136 // Try case sensitive, should NOT find it.
137 EXPECT_EQ(0, FindInPage(L
"CAT", FWD
, CASE_SENSITIVE
, false));
139 // Try again case sensitive, but this time with right case.
140 EXPECT_EQ(1, FindInPage(L
"dog", FWD
, CASE_SENSITIVE
, false));
142 // Try non-Latin characters ('Hreggvidur' with 'eth' for 'd' in left frame).
143 EXPECT_EQ(1, FindInPage(L
"Hreggvi\u00F0ur", FWD
, IGNORE_CASE
, false));
144 EXPECT_EQ(1, FindInPage(L
"Hreggvi\u00F0ur", FWD
, CASE_SENSITIVE
, false));
145 EXPECT_EQ(0, FindInPage(L
"hreggvi\u00F0ur", FWD
, CASE_SENSITIVE
, false));