17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/test/base/find_in_page_observer.h"
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
77dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/ui/find_bar/find_tab_helper.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "content/public/test/test_utils.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace ui_test_utils {
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)FindInPageNotificationObserver::FindInPageNotificationObserver(
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    content::WebContents* parent_tab)
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    : active_match_ordinal_(-1),
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      number_of_matches_(0),
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      current_find_request_id_(0),
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      seen_(false),
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      running_(false) {
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FindTabHelper* find_tab_helper =
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      FindTabHelper::FromWebContents(parent_tab);
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  current_find_request_id_ = find_tab_helper->current_find_request_id();
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                 content::Source<content::WebContents>(parent_tab));
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)FindInPageNotificationObserver::~FindInPageNotificationObserver() {}
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void FindInPageNotificationObserver::Wait() {
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (seen_)
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return;
327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  running_ = true;
337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  message_loop_runner_ = new content::MessageLoopRunner;
347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  message_loop_runner_->Run();
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void FindInPageNotificationObserver::Observe(
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    int type,
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const content::NotificationSource& source,
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const content::NotificationDetails& details) {
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) {
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    content::Details<FindNotificationDetails> find_details(details);
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (find_details->request_id() == current_find_request_id_) {
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // We get multiple responses and one of those will contain the ordinal.
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // This message comes to us before the final update is sent.
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (find_details->active_match_ordinal() > -1) {
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        active_match_ordinal_ = find_details->active_match_ordinal();
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        selection_rect_ = find_details->selection_rect();
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      if (find_details->final_update()) {
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        number_of_matches_ = find_details->number_of_matches();
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        seen_ = true;
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        if (running_) {
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          running_ = false;
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)          message_loop_runner_->Quit();
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        }
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      } else {
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        DVLOG(1) << "Ignoring, since we only care about the final message";
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      }
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  } else {
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    NOTREACHED();
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace ui_test_utils
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
68