1 // Copyright (c) 2011 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 #pragma once 8 9 #include <deque> 10 #include <string> 11 12 #include "base/message_loop.h" 13 #include "chrome/browser/notifications/balloon_collection_impl.h" 14 #include "chrome/browser/notifications/desktop_notification_service.h" 15 #include "chrome/browser/notifications/notification.h" 16 #include "chrome/browser/notifications/notification_test_util.h" 17 #include "chrome/browser/notifications/notification_ui_manager.h" 18 #include "chrome/browser/notifications/notifications_prefs_cache.h" 19 #include "chrome/test/testing_pref_service.h" 20 #include "chrome/test/testing_profile.h" 21 #include "content/browser/browser_thread.h" 22 #include "testing/gtest/include/gtest/gtest.h" 23 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); 43 virtual bool HasSpace() const; 44 virtual Balloon* MakeBalloon(const Notification& notification, 45 Profile* profile); 46 virtual void DisplayChanged() {} 47 virtual void OnBalloonClosed(Balloon* source); 48 virtual const BalloonCollection::Balloons& GetActiveBalloons(); 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(); 84 virtual void TearDown(); 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 int HasPermission(const GURL& origin) { 95 return service_->prefs_cache()->HasPermission(origin); 96 } 97 98 // Constructs a notification parameter structure for use in tests. 99 DesktopNotificationHostMsg_Show_Params StandardTestNotification(); 100 101 // Create a message loop to allow notifications code to post tasks, 102 // and a thread so that notifications code runs on the expected thread. 103 MessageLoopForUI message_loop_; 104 BrowserThread ui_thread_; 105 106 // Local state mock. 107 TestingPrefService local_state_; 108 109 // Test profile. 110 scoped_ptr<TestingProfile> profile_; 111 112 // Mock balloon collection -- owned by the NotificationUIManager 113 MockBalloonCollection* balloon_collection_; 114 115 // Real UI manager. 116 scoped_ptr<NotificationUIManager> ui_manager_; 117 118 // Real DesktopNotificationService 119 scoped_ptr<DesktopNotificationService> service_; 120 121 // Contains the cumulative output of the unit test. 122 static std::string log_output_; 123 }; 124 125 #endif // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_ 126