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_DESKTOP_NOTIFICATIONS_UNITTEST_H_
      6 #define CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_
      7 
      8 #include <deque>
      9 #include <string>
     10 
     11 #include "base/message_loop/message_loop.h"
     12 #include "base/prefs/testing_pref_service.h"
     13 #include "chrome/browser/notifications/balloon_collection_impl.h"
     14 #include "chrome/browser/notifications/balloon_notification_ui_manager.h"
     15 #include "chrome/browser/notifications/desktop_notification_service.h"
     16 #include "chrome/browser/notifications/notification.h"
     17 #include "chrome/browser/notifications/notification_test_util.h"
     18 #include "chrome/test/base/testing_browser_process.h"
     19 #include "chrome/test/base/testing_profile.h"
     20 #include "content/public/test/render_view_test.h"
     21 #include "content/public/test/test_browser_thread.h"
     22 #include "testing/gtest/include/gtest/gtest.h"
     23 
     24 class ActiveDesktopMonitor;
     25 class DesktopNotificationsTest;
     26 typedef LoggingNotificationDelegate<DesktopNotificationsTest>
     27     LoggingNotificationProxy;
     28 
     29 // Test version of the balloon collection which counts the number
     30 // of notifications that are added to it.
     31 class MockBalloonCollection : public BalloonCollectionImpl {
     32  public:
     33   MockBalloonCollection();
     34   virtual ~MockBalloonCollection();
     35 
     36   // Our mock collection has an area large enough for a fixed number
     37   // of balloons.
     38   static const int kMockBalloonSpace;
     39   int max_balloon_count() const { return kMockBalloonSpace; }
     40 
     41   // BalloonCollectionImpl overrides
     42   virtual void Add(const Notification& notification,
     43                    Profile* profile) OVERRIDE;
     44   virtual bool HasSpace() const OVERRIDE;
     45   virtual Balloon* MakeBalloon(const Notification& notification,
     46                                Profile* profile) OVERRIDE;
     47   virtual void DisplayChanged() OVERRIDE {}
     48   virtual void OnBalloonClosed(Balloon* source) OVERRIDE;
     49   virtual const BalloonCollection::Balloons& GetActiveBalloons() OVERRIDE;
     50 
     51   // Number of balloons being shown.
     52   std::deque<Balloon*>& balloons() { return balloons_; }
     53   int count() const { return balloons_.size(); }
     54 
     55   // Returns the highest y-coordinate of all the balloons in the collection.
     56   int UppermostVerticalPosition();
     57 
     58   // Returns the height bounds of a balloon.
     59   int MinHeight() { return Layout::min_balloon_height(); }
     60   int MaxHeight() { return Layout::max_balloon_height(); }
     61 
     62   // Returns the bounding box.
     63   gfx::Rect GetBalloonsBoundingBox() {
     64     return BalloonCollectionImpl::GetBalloonsBoundingBox();
     65   }
     66 
     67  private:
     68   std::deque<Balloon*> balloons_;
     69 };
     70 
     71 class DesktopNotificationsTest : public testing::Test {
     72  public:
     73   DesktopNotificationsTest();
     74   virtual ~DesktopNotificationsTest();
     75 
     76   static void log(const std::string& message) {
     77     log_output_.append(message);
     78   }
     79 
     80   Profile* profile() { return profile_.get(); }
     81 
     82  protected:
     83   // testing::Test overrides
     84   virtual void SetUp() OVERRIDE;
     85   virtual void TearDown() OVERRIDE;
     86 
     87   void AllowOrigin(const GURL& origin) {
     88     service_->GrantPermission(origin);
     89   }
     90 
     91   void DenyOrigin(const GURL& origin) {
     92     service_->DenyPermission(origin);
     93   }
     94 
     95   // Constructs a notification parameter structure for use in tests.
     96   content::ShowDesktopNotificationHostMsgParams StandardTestNotification();
     97 
     98   // Must be first member. Because we're running a unit test in browser_tests
     99   // we need to handle TestingBrowserProcess initialization ourselves.
    100   TestingBrowserProcessInitializer initializer_;
    101 
    102   // Create a message loop to allow notifications code to post tasks,
    103   // and a thread so that notifications code runs on the expected thread.
    104   base::MessageLoopForUI message_loop_;
    105   content::TestBrowserThread ui_thread_;
    106 
    107   // Local state mock.
    108   TestingPrefServiceSimple local_state_;
    109 
    110   // Test profile.
    111   scoped_ptr<TestingProfile> profile_;
    112 
    113   // Mock balloon collection -- owned by the NotificationUIManager
    114   MockBalloonCollection* balloon_collection_;
    115 
    116   // Real UI manager.
    117   scoped_ptr<BalloonNotificationUIManager> ui_manager_;
    118 
    119   // Real DesktopNotificationService
    120   scoped_ptr<DesktopNotificationService> service_;
    121 
    122   // Contains the cumulative output of the unit test.
    123   static std::string log_output_;
    124 };
    125 
    126 #endif  // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_
    127