15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <set>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/notifications/notification.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/notifications/notification_object_proxy.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/notifications/notification_ui_manager.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/size.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NotificationDelegate which does nothing, useful for testing when
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the notification events are not important.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MockNotificationDelegate : public NotificationDelegate {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit MockNotificationDelegate(const std::string& id);
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NotificationDelegate interface.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Display() OVERRIDE {}
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Error() OVERRIDE {}
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Close(bool by_user) OVERRIDE {}
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Click() OVERRIDE {}
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string id() const OVERRIDE;
300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual content::WebContents* GetWebContents() const OVERRIDE;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~MockNotificationDelegate();
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string id_;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(MockNotificationDelegate);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Mock implementation of Javascript object proxy which logs events that
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// would have been fired on it.  Useful for tests where the sequence of
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// notification events needs to be verified.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |Logger| class provided in template must implement method
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static void log(string);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<class Logger>
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LoggingNotificationDelegate : public NotificationDelegate {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit LoggingNotificationDelegate(std::string id)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : notification_id_(id) {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NotificationObjectProxy override
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Display() OVERRIDE {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Logger::log("notification displayed\n");
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Error() OVERRIDE {
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Logger::log("notification error\n");
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Click() OVERRIDE {
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Logger::log("notification clicked\n");
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void ButtonClick(int index) OVERRIDE {
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Logger::log("notification button clicked\n");
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Close(bool by_user) OVERRIDE {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (by_user)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Logger::log("notification closed by user\n");
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      Logger::log("notification closed by script\n");
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual std::string id() const OVERRIDE {
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return notification_id_;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual content::WebContents* GetWebContents() const OVERRIDE {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string notification_id_;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(LoggingNotificationDelegate);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class StubNotificationUIManager : public NotificationUIManager {
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit StubNotificationUIManager(const GURL& welcome_origin);
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~StubNotificationUIManager();
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds a notification to be displayed. Virtual for unit test override.
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Add(const Notification& notification, Profile* profile) OVERRIDE;
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool Update(const Notification& notification,
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      Profile* profile) OVERRIDE;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns NULL if no notifications match the supplied ID, either currently
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // displayed or in the queue.
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual const Notification* FindById(const std::string& id) const OVERRIDE;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Removes any notifications matching the supplied ID, either currently
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // displayed or in the queue.  Returns true if anything was removed.
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool CancelById(const std::string& notification_id) OVERRIDE;
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds the notification_id for each outstanding notification to the set
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |notification_ids| (must not be NULL).
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual std::set<std::string> GetAllIdsByProfileAndSourceOrigin(
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      Profile* profile,
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const GURL& source) OVERRIDE;
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Removes notifications matching the |source_origin| (which could be an
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // extension ID). Returns true if anything was removed.
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE;
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Removes notifications matching |profile|. Returns true if any were removed.
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool CancelAllByProfile(Profile* profile) OVERRIDE;
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Cancels all pending notifications and closes anything currently showing.
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Used when the app is terminating.
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void CancelAll() OVERRIDE;
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Test hook to get the notification so we can check it
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const Notification& notification() const { return notification_; }
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Test hook to check the ID of the last notification cancelled.
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string& dismissed_id() { return dismissed_id_; }
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t added_notifications() const { return added_notifications_; }
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool welcomed() const { return welcomed_; }
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager);
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Notification notification_;
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Profile* profile_;
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string dismissed_id_;
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  GURL welcome_origin_;
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool welcomed_;
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t added_notifications_;
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATION_TEST_UTIL_H_
140