message_center_notifications_browsertest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2013 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#include <string>
6
7#include "base/command_line.h"
8#include "base/message_loop.h"
9#include "base/run_loop.h"
10#include "base/strings/string_number_conversions.h"
11#include "base/strings/string_util.h"
12#include "base/strings/utf_string_conversions.h"
13#include "chrome/browser/browser_process.h"
14#include "chrome/browser/notifications/message_center_notification_manager.h"
15#include "chrome/browser/notifications/notification.h"
16#include "chrome/browser/notifications/notification_ui_manager.h"
17#include "chrome/browser/profiles/profile.h"
18#include "chrome/browser/ui/browser.h"
19#include "chrome/test/base/in_process_browser_test.h"
20#include "ui/message_center/message_center.h"
21#include "ui/message_center/message_center_switches.h"
22#include "ui/message_center/message_center_util.h"
23
24class TestAddObserver : public message_center::MessageCenterObserver {
25 public:
26  TestAddObserver(const std::string& id,
27                  message_center::MessageCenter* message_center)
28      : id_(id), message_center_(message_center) {
29    quit_closure_ = run_loop_.QuitClosure();
30    message_center_->AddObserver(this);
31  }
32
33  virtual ~TestAddObserver() { message_center_->RemoveObserver(this); }
34
35  virtual void OnNotificationAdded(const std::string& id) OVERRIDE {
36    log_ += "_" + id;
37    if (id == id_)
38      base::MessageLoop::current()->PostTask(FROM_HERE, quit_closure_);
39  }
40
41  void Run() { run_loop_.Run(); }
42  const std::string log() const { return log_; }
43
44 private:
45  std::string id_;
46  std::string log_;
47  message_center::MessageCenter* message_center_;
48  base::RunLoop run_loop_;
49  base::Closure quit_closure_;
50};
51
52class MessageCenterNotificationsTest : public InProcessBrowserTest {
53 public:
54  MessageCenterNotificationsTest() {}
55
56  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
57    // This switch enables the new piping of Notifications through Message
58    // Center.
59    command_line->AppendSwitch(
60      message_center::switches::kEnableRichNotifications);
61  }
62
63  MessageCenterNotificationManager* manager() {
64    return static_cast<MessageCenterNotificationManager*>(
65        g_browser_process->notification_ui_manager());
66  }
67
68  message_center::MessageCenter* message_center() {
69    return g_browser_process->message_center();
70  }
71
72  Profile* profile() { return browser()->profile(); }
73
74  class TestDelegate : public NotificationDelegate {
75   public:
76    explicit TestDelegate(const std::string& id) : id_(id) {}
77
78    virtual void Display() OVERRIDE { log_ += "Display_"; }
79    virtual void Error() OVERRIDE { log_ += "Error_"; }
80    virtual void Close(bool by_user) OVERRIDE {
81      log_ += "Close_";
82      log_ += ( by_user ? "by_user_" : "programmatically_");
83    }
84    virtual void Click() OVERRIDE { log_ += "Click_"; }
85    virtual void ButtonClick(int button_index) OVERRIDE {
86      log_ += "ButtonClick_";
87      log_ += base::IntToString(button_index) + "_";
88    }
89    virtual std::string id() const OVERRIDE { return id_; }
90    virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
91      return NULL;
92    }
93
94    const std::string& log() { return log_; }
95
96   private:
97    virtual ~TestDelegate() {}
98    std::string id_;
99    std::string log_;
100
101    DISALLOW_COPY_AND_ASSIGN(TestDelegate);
102  };
103
104  Notification CreateTestNotification(const std::string& id,
105                                      TestDelegate** delegate = NULL) {
106    TestDelegate* new_delegate = new TestDelegate(id);
107    if (delegate) {
108      *delegate = new_delegate;
109      new_delegate->AddRef();
110    }
111
112    return Notification(GURL("chrome-test://testing/"),
113                        GURL(),
114                        ASCIIToUTF16("title"),
115                        ASCIIToUTF16("message"),
116                        WebKit::WebTextDirectionDefault,
117                        UTF8ToUTF16("chrome-test://testing/"),
118                        UTF8ToUTF16("REPLACE-ME"),
119                        new_delegate);
120  }
121
122  Notification CreateRichTestNotification(const std::string& id,
123                                          TestDelegate** delegate = NULL) {
124    TestDelegate* new_delegate = new TestDelegate(id);
125    if (delegate) {
126      *delegate = new_delegate;
127      new_delegate->AddRef();
128    }
129
130    message_center::RichNotificationData data;
131
132    return Notification(message_center::NOTIFICATION_TYPE_BASE_FORMAT,
133                        GURL("chrome-test://testing/"),
134                        ASCIIToUTF16("title"),
135                        ASCIIToUTF16("message"),
136                        gfx::Image(),
137                        WebKit::WebTextDirectionDefault,
138                        UTF8ToUTF16("chrome-test://testing/"),
139                        UTF8ToUTF16("REPLACE-ME"),
140                        data,
141                        new_delegate);
142  }
143};
144
145// TODO(rsesek): Implement Message Center on Mac and get these tests passing
146// for real. http://crbug.com/179904
147#if !defined(OS_MACOSX)
148
149IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, RetrieveBaseParts) {
150  // Make sure comamnd-line switch has an effect.
151  EXPECT_EQ(NotificationUIManager::DelegatesToMessageCenter(),
152            message_center::IsRichNotificationEnabled());
153  EXPECT_TRUE(manager());
154  EXPECT_TRUE(message_center());
155}
156
157// MessaceCenter-specific test.
158#if defined(RUN_MESSAGE_CENTER_TESTS)
159#define MAYBE_BasicAddCancel BasicAddCancel
160#else
161#define MAYBE_BasicAddCancel DISABLED_BasicAddCancel
162#endif
163
164IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, MAYBE_BasicAddCancel) {
165  EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
166  manager()->Add(CreateTestNotification("hey"), profile());
167  EXPECT_EQ(1u, message_center()->NotificationCount());
168  manager()->CancelById("hey");
169  EXPECT_EQ(0u, message_center()->NotificationCount());
170}
171
172// MessaceCenter-specific test.
173#if defined(RUN_MESSAGE_CENTER_TESTS)
174#define MAYBE_BasicDelegate BasicDelegate
175#else
176#define MAYBE_BasicDelegate DISABLED_BasicDelegate
177#endif
178
179IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, MAYBE_BasicDelegate) {
180  EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
181  TestDelegate* delegate;
182  manager()->Add(CreateTestNotification("hey", &delegate), profile());
183  // Verify that delegate accumulated correct log of events.
184  EXPECT_EQ("Display_", delegate->log());
185  manager()->CancelById("hey");
186  // Verify that delegate accumulated correct log of events.
187  EXPECT_EQ("Display_Close_programmatically_", delegate->log());
188  delegate->Release();
189}
190
191// MessaceCenter-specific test.
192#if defined(RUN_MESSAGE_CENTER_TESTS)
193#define MAYBE_ButtonClickedDelegate ButtonClickedDelegate
194#else
195#define MAYBE_ButtonClickedDelegate DISABLED_ButtonClickedDelegate
196#endif
197
198IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
199                       MAYBE_ButtonClickedDelegate) {
200  EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
201  TestDelegate* delegate;
202  manager()->Add(CreateTestNotification("n", &delegate), profile());
203  message_center()->ClickOnNotificationButton("n", 1);
204  // Verify that delegate accumulated correct log of events.
205  EXPECT_EQ("Display_ButtonClick_1_", delegate->log());
206  delegate->Release();
207}
208
209// MessaceCenter-specific test.
210#if defined(RUN_MESSAGE_CENTER_TESTS)
211#define MAYBE_UpdateExistingNotification UpdateExistingNotification
212#else
213#define MAYBE_UpdateExistingNotification DISABLED_UpdateExistingNotification
214#endif
215
216IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
217                       MAYBE_UpdateExistingNotification) {
218  EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
219  TestDelegate* delegate;
220  manager()->Add(CreateTestNotification("n", &delegate), profile());
221  TestDelegate* delegate2;
222  manager()->Add(CreateRichTestNotification("n", &delegate2), profile());
223
224  manager()->CancelById("n");
225  EXPECT_EQ("Display_Close_programmatically_", delegate->log());
226  EXPECT_EQ("Close_programmatically_", delegate2->log());
227
228  delegate->Release();
229  delegate2->Release();
230}
231
232// MessaceCenter-specific test.
233#if defined(RUN_MESSAGE_CENTER_TESTS)
234#define MAYBE_QueueWhenCenterVisible QueueWhenCenterVisible
235#else
236#define MAYBE_QueueWhenCenterVisible DISABLED_QueueWhenCenterVisible
237#endif
238
239IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest,
240                       MAYBE_QueueWhenCenterVisible) {
241  EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter());
242  TestAddObserver observer("n2", message_center());
243
244  TestDelegate* delegate;
245  TestDelegate* delegate2;
246
247  manager()->Add(CreateTestNotification("n", &delegate), profile());
248  message_center()->SetMessageCenterVisible(true);
249  manager()->Add(CreateTestNotification("n2", &delegate2), profile());
250
251  EXPECT_EQ("_n", observer.log());
252
253  message_center()->SetMessageCenterVisible(false);
254  observer.Run();
255
256  EXPECT_EQ("_n_n2", observer.log());
257
258  delegate->Release();
259  delegate2->Release();
260}
261
262#endif  // !defined(OS_MACOSX)
263