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