Home | History | Annotate | Download | only in extensions
      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_BROWSER_EXTENSIONS_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/compiler_specific.h"
     12 #include "chrome/browser/chrome_notification_types.h"
     13 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
     14 #include "chrome/browser/ui/browser.h"
     15 #include "chrome/browser/ui/location_bar/location_bar.h"
     16 #include "content/public/browser/notification_details.h"
     17 #include "content/public/browser/notification_observer.h"
     18 #include "content/public/browser/notification_types.h"
     19 
     20 namespace content {
     21 class WindowedNotificationObserver;
     22 }
     23 
     24 // Test helper class for observing extension-related events.
     25 class ExtensionTestNotificationObserver
     26     : public content::NotificationObserver,
     27       public extensions::ExtensionActionAPI::Observer {
     28  public:
     29   explicit ExtensionTestNotificationObserver(Browser* browser);
     30   virtual ~ExtensionTestNotificationObserver();
     31 
     32   // Wait for the number of visible page actions to change to |count|.
     33   bool WaitForPageActionVisibilityChangeTo(int count);
     34 
     35   // Waits until an extension is installed and loaded. Returns true if an
     36   // install happened before timeout.
     37   bool WaitForExtensionInstall();
     38 
     39   // Wait for an extension install error to be raised. Returns true if an
     40   // error was raised.
     41   bool WaitForExtensionInstallError();
     42 
     43   // Waits until an extension is loaded and all view have loaded.
     44   void WaitForExtensionAndViewLoad();
     45 
     46   // Waits until an extension is loaded.
     47   void WaitForExtensionLoad();
     48 
     49   // Waits for an extension load error. Returns true if the error really
     50   // happened.
     51   bool WaitForExtensionLoadError();
     52 
     53   // Wait for the specified extension to crash. Returns true if it really
     54   // crashed.
     55   bool WaitForExtensionCrash(const std::string& extension_id);
     56 
     57   // Wait for the crx installer to be done. Returns true if it really is done.
     58   bool WaitForCrxInstallerDone();
     59 
     60   // Wait for all extension views to load.
     61   bool WaitForExtensionViewsToLoad();
     62 
     63   // Watch for the given event type from the given source.
     64   // After calling this method, call Wait() to ensure that RunMessageLoop() is
     65   // called appropriately and cleanup is performed.
     66   void Watch(int type, const content::NotificationSource& source);
     67 
     68   // After registering one or more event types with Watch(), call
     69   // this method to run the message loop and perform cleanup.
     70   void Wait();
     71 
     72   const std::string& last_loaded_extension_id() {
     73     return last_loaded_extension_id_;
     74   }
     75   void set_last_loaded_extension_id(
     76       const std::string& last_loaded_extension_id) {
     77     last_loaded_extension_id_ = last_loaded_extension_id;
     78   }
     79 
     80   // content::NotificationObserver
     81   virtual void Observe(int type,
     82                        const content::NotificationSource& source,
     83                        const content::NotificationDetails& details) OVERRIDE;
     84 
     85  private:
     86   class NotificationSet;
     87 
     88   Profile* GetProfile();
     89 
     90   void WaitForNotification(int notification_type);
     91 
     92   // Wait for |condition_| to be met. |notification_set| is the set of
     93   // notifications to wait for and to check |condition| when observing. This
     94   // can be NULL if we are instead waiting for a different observer method, like
     95   // OnPageActionsUpdated().
     96   void WaitForCondition(const base::Callback<bool(void)>& condition,
     97                         NotificationSet* notification_set);
     98 
     99   // Quits the message loop if |condition_| is met.
    100   void MaybeQuit();
    101 
    102   // extensions::ExtensionActionAPI::Observer:
    103   virtual void OnPageActionsUpdated(content::WebContents* contents) OVERRIDE;
    104 
    105   Browser* browser_;
    106   Profile* profile_;
    107 
    108   content::NotificationRegistrar registrar_;
    109   scoped_ptr<content::WindowedNotificationObserver> observer_;
    110 
    111   std::string last_loaded_extension_id_;
    112   int extension_installs_observed_;
    113   int extension_load_errors_observed_;
    114   int crx_installers_done_observed_;
    115 
    116   // The condition for which we are waiting. This should be checked in any
    117   // observing methods that could trigger it.
    118   base::Callback<bool(void)> condition_;
    119 
    120   // The closure to quit the currently-running message loop.
    121   base::Closure quit_closure_;
    122 };
    123 
    124 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
    125