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