desktop_notifications_unittest.h revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1// Copyright (c) 2010 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/browser_thread.h"
14#include "chrome/browser/notifications/balloon_collection_impl.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/browser/notifications/notification_ui_manager.h"
19#include "chrome/browser/notifications/notifications_prefs_cache.h"
20#include "chrome/common/render_messages.h"
21#include "chrome/test/testing_profile.h"
22#include "testing/gtest/include/gtest/gtest.h"
23
24class DesktopNotificationsTest;
25typedef LoggingNotificationDelegate<DesktopNotificationsTest>
26    LoggingNotificationProxy;
27
28// Test version of the balloon collection which counts the number
29// of notifications that are added to it.
30class MockBalloonCollection : public BalloonCollectionImpl {
31 public:
32  MockBalloonCollection() {}
33
34  // Our mock collection has an area large enough for a fixed number
35  // of balloons.
36  static const int kMockBalloonSpace;
37  int max_balloon_count() const { return kMockBalloonSpace; }
38
39  // BalloonCollectionImpl overrides
40  virtual void Add(const Notification& notification,
41                   Profile* profile);
42  virtual bool HasSpace() const { return count() < kMockBalloonSpace; }
43  virtual Balloon* MakeBalloon(const Notification& notification,
44                               Profile* profile);
45  virtual void DisplayChanged() {}
46  virtual void OnBalloonClosed(Balloon* source);
47  virtual const BalloonCollection::Balloons& GetActiveBalloons() {
48    return balloons_;
49  }
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 private:
63  std::deque<Balloon*> balloons_;
64};
65
66class DesktopNotificationsTest : public testing::Test {
67 public:
68  DesktopNotificationsTest();
69  ~DesktopNotificationsTest();
70
71  static void log(const std::string& message) {
72    log_output_.append(message);
73  }
74
75  Profile* profile() { return profile_.get(); }
76
77 protected:
78  // testing::Test overrides
79  virtual void SetUp();
80  virtual void TearDown();
81
82  void AllowOrigin(const GURL& origin) {
83    service_->GrantPermission(origin);
84  }
85
86  void DenyOrigin(const GURL& origin) {
87    service_->DenyPermission(origin);
88  }
89
90  int HasPermission(const GURL& origin) {
91    return service_->prefs_cache()->HasPermission(origin);
92  }
93
94  // Constructs a notification parameter structure for use in tests.
95  ViewHostMsg_ShowNotification_Params 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  MessageLoopForUI message_loop_;
100  BrowserThread ui_thread_;
101
102  // Test profile.
103  scoped_ptr<TestingProfile> profile_;
104
105  // Mock balloon collection -- owned by the NotificationUIManager
106  MockBalloonCollection* balloon_collection_;
107
108  // Real UI manager.
109  scoped_ptr<NotificationUIManager> ui_manager_;
110
111  // Real DesktopNotificationService
112  scoped_ptr<DesktopNotificationService> service_;
113
114  // Contains the cumulative output of the unit test.
115  static std::string log_output_;
116};
117
118#endif  // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_
119