OLD | NEW |
---|---|
(Empty) | |
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/string_number_conversions.h" | |
9 #include "base/string_util.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/notifications/message_center_notification_manager.h" | |
13 #include "chrome/browser/notifications/notification.h" | |
14 #include "chrome/browser/notifications/notification_ui_manager.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/ui/browser.h" | |
17 #include "chrome/common/chrome_switches.h" | |
18 #include "chrome/test/base/in_process_browser_test.h" | |
19 #include "ui/message_center/message_center.h" | |
20 | |
21 class MessageCenterNotificationsTest : public InProcessBrowserTest { | |
22 public: | |
23 MessageCenterNotificationsTest() {} | |
24 | |
25 virtual void SetUpCommandLine(CommandLine* command_line) { | |
26 // This switch enables the new piping of Notifications through Message | |
27 // Center. | |
28 command_line->AppendSwitch(switches::kEnableRichNotifications); | |
29 } | |
30 | |
31 MessageCenterNotificationManager* manager() { | |
32 return static_cast<MessageCenterNotificationManager*>( | |
33 g_browser_process->notification_ui_manager()); | |
34 } | |
35 | |
36 message_center::MessageCenter* message_center() { | |
37 return g_browser_process->message_center(); | |
38 } | |
39 | |
40 Profile* profile() { return browser()->profile(); } | |
41 | |
42 class TestDelegate : public NotificationDelegate { | |
43 public: | |
44 explicit TestDelegate(const std::string& id) : id_(id) {} | |
45 | |
46 void Display() OVERRIDE { log_ += "Display_"; } | |
47 void Error() OVERRIDE { log_ += "Error_"; } | |
48 void Close(bool by_user) OVERRIDE { | |
49 log_ += "Close_"; | |
50 log_ += ( by_user ? "by_user_" : "programmatically_"); | |
51 } | |
52 void Click() OVERRIDE { log_ += "Click_"; } | |
53 void ButtonClick(int button_index) OVERRIDE { | |
54 log_ += "ButtonClick_"; | |
55 log_ += base::IntToString(button_index) + "_"; | |
56 } | |
57 std::string id() const OVERRIDE { return id_; } | |
58 content::RenderViewHost* GetRenderViewHost() const OVERRIDE { return NULL; } | |
59 | |
60 const std::string& log() { return log_; } | |
stevenjb
2013/01/24 23:59:54
blank line
Dmitry Titov
2013/01/25 00:47:45
Done.
| |
61 private: | |
62 std::string id_; | |
63 std::string log_; | |
64 }; | |
65 | |
66 Notification CreateTestNotification(const std::string& id, | |
67 TestDelegate** delegate = NULL) { | |
68 TestDelegate* new_delegate = new TestDelegate(id); | |
69 if (delegate) { | |
70 *delegate = new_delegate; | |
71 new_delegate->AddRef(); | |
72 } | |
73 | |
74 return Notification(GURL(), | |
75 GURL(), | |
76 ASCIIToUTF16("title"), | |
77 ASCIIToUTF16("message"), | |
78 WebKit::WebTextDirectionDefault, | |
79 EmptyString16(), | |
80 EmptyString16(), | |
81 new_delegate); | |
82 } | |
83 }; | |
84 | |
85 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, RetrieveBaseParts) { | |
86 // Make sure comamnd-line switch has an effect. | |
87 EXPECT_TRUE(NotificationUIManager::DelegatesToMessageCenter()); | |
88 EXPECT_TRUE(manager()); | |
89 EXPECT_TRUE(message_center()); | |
90 } | |
91 | |
92 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, BasicAddCancel) { | |
93 manager()->Add(CreateTestNotification("hey"), profile()); | |
94 EXPECT_EQ(1u, message_center()->NotificationCount()); | |
95 manager()->CancelById("hey"); | |
96 EXPECT_EQ(0u, message_center()->NotificationCount()); | |
97 } | |
98 | |
99 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, BasicDelegate) { | |
100 TestDelegate* delegate; | |
101 manager()->Add(CreateTestNotification("hey", &delegate), profile()); | |
102 // Verify that delegate accumulated correct log of events. | |
103 EXPECT_EQ("Display_", delegate->log()); | |
104 manager()->CancelById("hey"); | |
105 // Verify that delegate accumulated correct log of events. | |
106 EXPECT_EQ("Display_Close_programmatically_", delegate->log()); | |
107 delegate->Release(); | |
108 } | |
109 | |
110 IN_PROC_BROWSER_TEST_F(MessageCenterNotificationsTest, ButtonClickedDelegate) { | |
111 TestDelegate* delegate; | |
112 manager()->Add(CreateTestNotification("n", &delegate), profile()); | |
113 message_center()->OnButtonClicked("n", 1); | |
114 // Verify that delegate accumulated correct log of events. | |
115 EXPECT_EQ("Display_ButtonClick_1_", delegate->log()); | |
116 delegate->Release(); | |
117 } | |
118 | |
OLD | NEW |