desktop_notifications_unittest.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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
8#include <set>
9#include <string>
10
11#include "base/message_loop.h"
12#include "base/string_util.h"
13#include "chrome/browser/browser_list.h"
14#include "chrome/browser/chrome_thread.h"
15#include "chrome/browser/chromeos/notifications/balloon_collection_impl.h"
16#include "chrome/browser/notifications/balloon.h"
17#include "chrome/browser/notifications/desktop_notification_service.h"
18#include "chrome/browser/notifications/notification.h"
19#include "chrome/browser/notifications/notification_object_proxy.h"
20#include "chrome/browser/notifications/notification_test_util.h"
21#include "chrome/browser/notifications/notification_ui_manager.h"
22#include "chrome/browser/notifications/notifications_prefs_cache.h"
23#include "chrome/common/render_messages.h"
24#include "chrome/test/testing_profile.h"
25#include "testing/gtest/include/gtest/gtest.h"
26
27namespace chromeos {
28
29class DesktopNotificationsTest;
30typedef LoggingNotificationProxyBase<DesktopNotificationsTest>
31    LoggingNotificationProxy;
32
33// Test version of the balloon collection which counts the number
34// of notifications that are added to it.
35class MockBalloonCollection : public BalloonCollectionImpl {
36 public:
37  MockBalloonCollection();
38
39  // BalloonCollectionImpl overrides
40  virtual void Add(const Notification& notification,
41                   Profile* profile);
42  virtual bool Remove(const Notification& notification);
43  virtual Balloon* MakeBalloon(const Notification& notification,
44                               Profile* profile);
45  virtual void OnBalloonClosed(Balloon* source);
46
47  // Number of balloons being shown.
48  std::set<Balloon*>& balloons() { return balloons_; }
49  int count() const { return balloons_.size(); }
50
51  // Returns the highest y-coordinate of all the balloons in the collection.
52  int UppermostVerticalPosition();
53
54 private:
55  std::set<Balloon*> balloons_;
56  scoped_refptr<LoggingNotificationProxy> log_proxy_;
57};
58
59class DesktopNotificationsTest : public testing::Test {
60 public:
61  DesktopNotificationsTest();
62  ~DesktopNotificationsTest();
63
64  static void log(const std::string& message) {
65    log_output_.append(message);
66  }
67
68  Profile* profile() { return profile_.get(); }
69
70 protected:
71  // testing::Test overrides
72  virtual void SetUp();
73  virtual void TearDown();
74
75  void AllowOrigin(const GURL& origin) {
76    service_->GrantPermission(origin);
77  }
78
79  void DenyOrigin(const GURL& origin) {
80    service_->DenyPermission(origin);
81  }
82
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  ViewHostMsg_ShowNotification_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  ChromeThread ui_thread_;
94
95  // Test profile.
96  scoped_ptr<TestingProfile> profile_;
97
98  // Mock balloon collection -- owned by the NotificationUIManager
99  MockBalloonCollection* balloon_collection_;
100
101  // Real UI manager.
102  scoped_ptr<NotificationUIManager> ui_manager_;
103
104  // Real DesktopNotificationService
105  scoped_ptr<DesktopNotificationService> service_;
106
107  // Contains the cumulative output of the unit test.
108  static std::string log_output_;
109};
110
111}  // namespace chromeos
112
113#endif  // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_
114