desktop_notifications_unittest.h revision dc0f95d653279beabeb9817299e2902918ba123e
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/browser_list.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_test_util.h"
20#include "chrome/browser/notifications/notification_ui_manager.h"
21#include "chrome/browser/notifications/notifications_prefs_cache.h"
22#include "chrome/common/render_messages.h"
23#include "chrome/test/testing_pref_service.h"
24#include "chrome/test/testing_profile.h"
25#include "content/browser/browser_thread.h"
26#include "testing/gtest/include/gtest/gtest.h"
27
28namespace chromeos {
29
30class DesktopNotificationsTest;
31typedef LoggingNotificationDelegate<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 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};
57
58class DesktopNotificationsTest : public testing::Test {
59 public:
60  DesktopNotificationsTest();
61  ~DesktopNotificationsTest();
62
63  static void log(const std::string& message) {
64    log_output_.append(message);
65  }
66
67  Profile* profile() { return profile_.get(); }
68
69 protected:
70  // testing::Test overrides
71  virtual void SetUp();
72  virtual void TearDown();
73
74  void AllowOrigin(const GURL& origin) {
75    service_->GrantPermission(origin);
76  }
77
78  void DenyOrigin(const GURL& origin) {
79    service_->DenyPermission(origin);
80  }
81
82  int HasPermission(const GURL& origin) {
83    return service_->prefs_cache()->HasPermission(origin);
84  }
85
86  // Constructs a notification parameter structure for use in tests.
87  ViewHostMsg_ShowNotification_Params StandardTestNotification();
88
89  // Create a message loop to allow notifications code to post tasks,
90  // and a thread so that notifications code runs on the expected thread.
91  MessageLoopForUI message_loop_;
92  BrowserThread ui_thread_;
93
94  // Mock local state.
95  TestingPrefService local_state_;
96
97  // Test profile.
98  scoped_ptr<TestingProfile> profile_;
99
100  // Mock balloon collection -- owned by the NotificationUIManager
101  MockBalloonCollection* balloon_collection_;
102
103  // Real UI manager.
104  scoped_ptr<NotificationUIManager> ui_manager_;
105
106  // Real DesktopNotificationService
107  scoped_ptr<DesktopNotificationService> service_;
108
109  // Contains the cumulative output of the unit test.
110  static std::string log_output_;
111};
112
113}  // namespace chromeos
114
115#endif  // CHROME_BROWSER_CHROMEOS_NOTIFICATIONS_DESKTOP_NOTIFICATIONS_UNITTEST_H_
116