Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / base / find_in_page_observer.cc
blobc41b81efc23ed7fc96dacd431584821b83be0708
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.
5 #include "chrome/test/base/find_in_page_observer.h"
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"
11 namespace ui_test_utils {
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));
27 FindInPageNotificationObserver::~FindInPageNotificationObserver() {}
29 void FindInPageNotificationObserver::Wait() {
30 if (seen_)
31 return;
32 running_ = true;
33 message_loop_runner_ = new content::MessageLoopRunner;
34 message_loop_runner_->Run();
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();
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();
57 } else {
58 DVLOG(1) << "Ignoring, since we only care about the final message";
61 } else {
62 NOTREACHED();
66 } // namespace ui_test_utils