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
3 // found in the LICENSE file.
4
5 #include "chrome/test/base/find_in_page_observer.h"
6
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
9 #include "content/public/test/test_utils.h"
10
11 namespace ui_test_utils {
12
FindInPageNotificationObserver(content::WebContents * parent_tab)13 FindInPageNotificationObserver::FindInPageNotificationObserver(
14 content::WebContents* parent_tab)
15 : active_match_ordinal_(-1),
16 number_of_matches_(0),
17 current_find_request_id_(0),
18 seen_(false),
19 running_(false) {
20 FindTabHelper* find_tab_helper =
21 FindTabHelper::FromWebContents(parent_tab);
22 current_find_request_id_ = find_tab_helper->current_find_request_id();
23 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
24 content::Source<content::WebContents>(parent_tab));
25 }
26
~FindInPageNotificationObserver()27 FindInPageNotificationObserver::~FindInPageNotificationObserver() {}
28
Wait()29 void FindInPageNotificationObserver::Wait() {
30 if (seen_)
31 return;
32 running_ = true;
33 message_loop_runner_ = new content::MessageLoopRunner;
34 message_loop_runner_->Run();
35 }
36
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)37 void FindInPageNotificationObserver::Observe(
38 int type,
39 const content::NotificationSource& source,
40 const content::NotificationDetails& details) {
41 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) {
42 content::Details<FindNotificationDetails> find_details(details);
43 if (find_details->request_id() == current_find_request_id_) {
44 // We get multiple responses and one of those will contain the ordinal.
45 // This message comes to us before the final update is sent.
46 if (find_details->active_match_ordinal() > -1) {
47 active_match_ordinal_ = find_details->active_match_ordinal();
48 selection_rect_ = find_details->selection_rect();
49 }
50 if (find_details->final_update()) {
51 number_of_matches_ = find_details->number_of_matches();
52 seen_ = true;
53 if (running_) {
54 running_ = false;
55 message_loop_runner_->Quit();
56 }
57 } else {
58 DVLOG(1) << "Ignoring, since we only care about the final message";
59 }
60 }
61 } else {
62 NOTREACHED();
63 }
64 }
65
66 } // namespace ui_test_utils
67
68