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_CHROMEOS_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_ 6 #define CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_ 7 #pragma once 8 9 #include <set> 10 #include <string> 11 12 #include "base/message_loop.h" 13 #include "base/string_util.h" 14 #include "chrome/browser/chromeos/notifications/balloon_collection_impl.h" 15 #include "chrome/browser/notifications/balloon.h" 16 #include "chrome/browser/notifications/desktop_notification_service.h" 17 #include "chrome/browser/notifications/notification.h" 18 #include "chrome/browser/notifications/notification_test_util.h" 19 #include "chrome/browser/notifications/notification_ui_manager.h" 20 #include "chrome/browser/notifications/notifications_prefs_cache.h" 21 #include "chrome/browser/ui/browser_list.h" 22 #include "chrome/test/testing_pref_service.h" 23 #include "chrome/test/testing_profile.h" 24 #include "content/browser/browser_thread.h" 25 #include "testing/gtest/include/gtest/gtest.h" 26 27 struct DesktopNotificationHostMsg_Show_Params; 28 29 namespace chromeos { 30 31 class DesktopNotificationsTest; 32 typedef LoggingNotificationDelegate<DesktopNotificationsTest> 33 LoggingNotificationProxy; 34 35 // Test version of the balloon collection which counts the number 36 // of notifications that are added to it. 37 class MockBalloonCollection : public BalloonCollectionImpl { 38 public: 39 MockBalloonCollection(); 40 41 // BalloonCollectionImpl overrides 42 virtual void Add(const Notification& notification, 43 Profile* profile); 44 virtual Balloon* MakeBalloon(const Notification& notification, 45 Profile* profile); 46 virtual void OnBalloonClosed(Balloon* source); 47 48 // Number of balloons being shown. balloons()49 std::set<Balloon*>& balloons() { return balloons_; } count()50 int count() const { return balloons_.size(); } 51 52 // Returns the highest y-coordinate of all the balloons in the collection. 53 int UppermostVerticalPosition(); 54 55 private: 56 std::set<Balloon*> balloons_; 57 }; 58 59 class DesktopNotificationsTest : public testing::Test { 60 public: 61 DesktopNotificationsTest(); 62 ~DesktopNotificationsTest(); 63 log(const std::string & message)64 static void log(const std::string& message) { 65 log_output_.append(message); 66 } 67 profile()68 Profile* profile() { return profile_.get(); } 69 70 protected: 71 // testing::Test overrides 72 virtual void SetUp(); 73 virtual void TearDown(); 74 AllowOrigin(const GURL & origin)75 void AllowOrigin(const GURL& origin) { 76 service_->GrantPermission(origin); 77 } 78 DenyOrigin(const GURL & origin)79 void DenyOrigin(const GURL& origin) { 80 service_->DenyPermission(origin); 81 } 82 HasPermission(const GURL & origin)83 int HasPermission(const GURL& origin) { 84 return service_->prefs_cache()->HasPermission(origin); 85 } 86 87 // Constructs a notification parameter structure for use in tests. 88 DesktopNotificationHostMsg_Show_Params StandardTestNotification(); 89 90 // Create a message loop to allow notifications code to post tasks, 91 // and a thread so that notifications code runs on the expected thread. 92 MessageLoopForUI message_loop_; 93 BrowserThread ui_thread_; 94 95 // Mock local state. 96 TestingPrefService local_state_; 97 98 // Test profile. 99 scoped_ptr<TestingProfile> profile_; 100 101 // Mock balloon collection -- owned by the NotificationUIManager 102 MockBalloonCollection* balloon_collection_; 103 104 // Real UI manager. 105 scoped_ptr<NotificationUIManager> ui_manager_; 106 107 // Real DesktopNotificationService 108 scoped_ptr<DesktopNotificationService> service_; 109 110 // Contains the cumulative output of the unit test. 111 static std::string log_output_; 112 }; 113 114 } // namespace chromeos 115 116 #endif // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_ 117