• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CHROME_TEST_BASE_FIND_IN_PAGE_OBSERVER_H_
6 #define CHROME_TEST_BASE_FIND_IN_PAGE_OBSERVER_H_
7 
8 #include "base/memory/ref_counted.h"
9 #include "content/public/browser/notification_observer.h"
10 #include "content/public/browser/notification_registrar.h"
11 #include "ui/gfx/rect.h"
12 
13 namespace content {
14 class MessageLoopRunner;
15 class WebContents;
16 }
17 
18 namespace ui_test_utils {
19 
20 // FindInPageNotificationObserver allows blocking UI thread until find results
21 // are available. Typical usage:
22 // FindInPageWchar();
23 // FindInPageNotificationObserver observer(tab);
24 // observer.Wait();
25 
26 // Always construct FindInPageNotificationObserver AFTER initiating the search.
27 // It captures the current search ID in constructor and waits for it only.
28 class FindInPageNotificationObserver : public content::NotificationObserver {
29  public:
30   explicit FindInPageNotificationObserver(content::WebContents* parent_tab);
31   virtual ~FindInPageNotificationObserver();
32 
33   void Wait();
34 
active_match_ordinal()35   int active_match_ordinal() const { return active_match_ordinal_; }
number_of_matches()36   int number_of_matches() const { return number_of_matches_; }
selection_rect()37   gfx::Rect selection_rect() const { return selection_rect_; }
38 
39  private:
40   virtual void Observe(int type,
41                        const content::NotificationSource& source,
42                        const content::NotificationDetails& details) OVERRIDE;
43 
44   content::NotificationRegistrar registrar_;
45   // We will at some point (before final update) be notified of the ordinal and
46   // we need to preserve it so we can send it later.
47   int active_match_ordinal_;
48   int number_of_matches_;
49   gfx::Rect selection_rect_;
50   // The id of the current find request, obtained from WebContents. Allows us
51   // to monitor when the search completes.
52   int current_find_request_id_;
53   scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
54 
55   bool seen_; // true after transition to expected state has been seen
56   bool running_; // indicates whether message loop is running
57 
58   DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
59 };
60 
61 }  // namespace ui_test_utils
62 
63 #endif  // CHROME_TEST_BASE_FIND_IN_PAGE_OBSERVER_H_
64