Home | History | Annotate | Download | only in notifications
      1 // Copyright (c) 2012 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_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
      6 #define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "chrome/browser/notifications/notification.h"
     12 #include "chrome/browser/notifications/notification_object_proxy.h"
     13 #include "chrome/browser/notifications/notification_ui_manager.h"
     14 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
     15 #include "chrome/browser/profiles/profile.h"
     16 #include "ui/gfx/size.h"
     17 
     18 // NotificationDelegate which does nothing, useful for testing when
     19 // the notification events are not important.
     20 class MockNotificationDelegate : public NotificationDelegate {
     21  public:
     22   explicit MockNotificationDelegate(const std::string& id);
     23 
     24   // NotificationDelegate interface.
     25   virtual void Display() OVERRIDE {}
     26   virtual void Error() OVERRIDE {}
     27   virtual void Close(bool by_user) OVERRIDE {}
     28   virtual void Click() OVERRIDE {}
     29   virtual std::string id() const OVERRIDE;
     30   virtual content::WebContents* GetWebContents() const OVERRIDE;
     31 
     32  private:
     33   virtual ~MockNotificationDelegate();
     34 
     35   std::string id_;
     36 
     37   DISALLOW_COPY_AND_ASSIGN(MockNotificationDelegate);
     38 };
     39 
     40 // Mock implementation of Javascript object proxy which logs events that
     41 // would have been fired on it.  Useful for tests where the sequence of
     42 // notification events needs to be verified.
     43 //
     44 // |Logger| class provided in template must implement method
     45 // static void log(string);
     46 template<class Logger>
     47 class LoggingNotificationDelegate : public NotificationDelegate {
     48  public:
     49   explicit LoggingNotificationDelegate(std::string id)
     50       : notification_id_(id) {
     51   }
     52 
     53   // NotificationObjectProxy override
     54   virtual void Display() OVERRIDE {
     55     Logger::log("notification displayed\n");
     56   }
     57   virtual void Error() OVERRIDE {
     58     Logger::log("notification error\n");
     59   }
     60   virtual void Click() OVERRIDE {
     61     Logger::log("notification clicked\n");
     62   }
     63   virtual void ButtonClick(int index) OVERRIDE {
     64     Logger::log("notification button clicked\n");
     65   }
     66   virtual void Close(bool by_user) OVERRIDE {
     67     if (by_user)
     68       Logger::log("notification closed by user\n");
     69     else
     70       Logger::log("notification closed by script\n");
     71   }
     72   virtual std::string id() const OVERRIDE {
     73     return notification_id_;
     74   }
     75   virtual content::WebContents* GetWebContents() const OVERRIDE {
     76     return NULL;
     77   }
     78 
     79  private:
     80   std::string notification_id_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(LoggingNotificationDelegate);
     83 };
     84 
     85 class StubNotificationUIManager : public NotificationUIManager {
     86  public:
     87   explicit StubNotificationUIManager(const GURL& welcome_origin);
     88   virtual ~StubNotificationUIManager();
     89 
     90   // Adds a notification to be displayed. Virtual for unit test override.
     91   virtual void Add(const Notification& notification, Profile* profile) OVERRIDE;
     92   virtual bool Update(const Notification& notification,
     93                       Profile* profile) OVERRIDE;
     94 
     95   // Returns NULL if no notifications match the supplied ID, either currently
     96   // displayed or in the queue.
     97   virtual const Notification* FindById(const std::string& id) const OVERRIDE;
     98 
     99   // Removes any notifications matching the supplied ID, either currently
    100   // displayed or in the queue.  Returns true if anything was removed.
    101   virtual bool CancelById(const std::string& notification_id) OVERRIDE;
    102 
    103   // Adds the notification_id for each outstanding notification to the set
    104   // |notification_ids| (must not be NULL).
    105   virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
    106       Profile* profile,
    107       const GURL& source) OVERRIDE;
    108 
    109   // Removes notifications matching the |source_origin| (which could be an
    110   // extension ID). Returns true if anything was removed.
    111   virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
    112 
    113   // Removes notifications matching |profile|. Returns true if any were removed.
    114   virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
    115 
    116   // Cancels all pending notifications and closes anything currently showing.
    117   // Used when the app is terminating.
    118   virtual void CancelAll() OVERRIDE;
    119 
    120   // Test hook to get the notification so we can check it
    121   const Notification& notification() const { return notification_; }
    122 
    123   // Test hook to check the ID of the last notification cancelled.
    124   std::string& dismissed_id() { return dismissed_id_; }
    125 
    126   size_t added_notifications() const { return added_notifications_; }
    127   bool welcomed() const { return welcomed_; }
    128 
    129  private:
    130   DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager);
    131   Notification notification_;
    132   Profile* profile_;
    133   std::string dismissed_id_;
    134   GURL welcome_origin_;
    135   bool welcomed_;
    136   size_t added_notifications_;
    137 };
    138 
    139 #endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
    140