desktop_notifications_unittest.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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/test/testing_profile.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23class DesktopNotificationsTest;
24typedef LoggingNotificationDelegate<DesktopNotificationsTest>
25    LoggingNotificationProxy;
26
27// Test version of the balloon collection which counts the number
28// of notifications that are added to it.
29class MockBalloonCollection : public BalloonCollectionImpl {
30 public:
31  MockBalloonCollection();
32  virtual ~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  // Returns the bounding box.
63  gfx::Rect GetBalloonsBoundingBox() {
64    return BalloonCollectionImpl::GetBalloonsBoundingBox();
65  }
66
67 private:
68  std::deque<Balloon*> balloons_;
69};
70
71class 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();
85  virtual void TearDown();
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  int HasPermission(const GURL& origin) {
96    return service_->prefs_cache()->HasPermission(origin);
97  }
98
99  // Constructs a notification parameter structure for use in tests.
100  ViewHostMsg_ShowNotification_Params StandardTestNotification();
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  MessageLoopForUI message_loop_;
105  BrowserThread ui_thread_;
106
107  // Test profile.
108  scoped_ptr<TestingProfile> profile_;
109
110  // Mock balloon collection -- owned by the NotificationUIManager
111  MockBalloonCollection* balloon_collection_;
112
113  // Real UI manager.
114  scoped_ptr<NotificationUIManager> ui_manager_;
115
116  // Real DesktopNotificationService
117  scoped_ptr<DesktopNotificationService> service_;
118
119  // Contains the cumulative output of the unit test.
120  static std::string log_output_;
121};
122
123#endif  // CHROME_BROWSER_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_
124