OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "base/bind.h" | |
6 #include "base/memory/ref_counted.h" | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/string16.h" | |
10 #include "base/string_util.h" | |
11 #include "base/stringprintf.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/chromeos/notifications/system_notification_factory.h" | |
15 #include "chrome/browser/notifications/notification_test_util.h" | |
16 #include "chrome/browser/notifications/notification_ui_manager.h" | |
17 #include "chrome/browser/ui/browser.h" | |
18 #include "chrome/common/chrome_notification_types.h" | |
19 #include "chrome/test/base/in_process_browser_test.h" | |
20 #include "chrome/test/base/ui_test_utils.h" | |
21 #include "content/public/browser/notification_service.h" | |
22 #include "ui/base/x/x11_util.h" | |
23 | |
24 #if defined(USE_AURA) | |
25 #include "chrome/browser/chromeos/notifications/balloon_collection_impl_aura.h" | |
26 #include "ui/aura/root_window.h" | |
27 #else | |
28 #include "chrome/browser/chromeos/notifications/balloon_collection_impl.h" | |
29 #endif | |
30 | |
31 #if defined(USE_AURA) | |
32 typedef class chromeos::BalloonCollectionImplAura BalloonCollectionImplType; | |
33 #else | |
34 typedef class chromeos::BalloonCollectionImpl BalloonCollectionImplType; | |
35 #endif | |
36 | |
37 namespace chromeos { | |
38 | |
39 class SystemNotificationTest : public InProcessBrowserTest { | |
40 public: | |
41 SystemNotificationTest() {} | |
42 | |
43 void HandleWebUIMessage(const ListValue* value) { | |
44 MessageLoop::current()->Quit(); | |
45 } | |
46 | |
47 protected: | |
48 virtual void SetUp() { | |
49 InProcessBrowserTest::SetUp(); | |
50 } | |
51 | |
52 BalloonCollectionImplType* GetBalloonCollectionImpl() { | |
53 return static_cast<BalloonCollectionImplType*>( | |
54 g_browser_process->notification_ui_manager()->balloon_collection()); | |
55 } | |
56 | |
57 Notification NewMockNotification(const std::string& id) { | |
58 return NewMockNotification(new MockNotificationDelegate(id)); | |
59 } | |
60 | |
61 Notification NewMockNotification(NotificationDelegate* delegate) { | |
62 std::string text = delegate->id(); | |
63 return SystemNotificationFactory::Create( | |
64 GURL(), ASCIIToUTF16(text.c_str()), ASCIIToUTF16(text.c_str()), | |
65 delegate); | |
66 } | |
67 | |
68 private: | |
69 DISALLOW_COPY_AND_ASSIGN(SystemNotificationTest); | |
70 }; | |
71 | |
72 IN_PROC_BROWSER_TEST_F(SystemNotificationTest, TestSystemNotification) { | |
73 BalloonCollectionImplType* collection = GetBalloonCollectionImpl(); | |
74 scoped_refptr<MockNotificationDelegate> delegate( | |
75 new MockNotificationDelegate("power")); | |
76 | |
77 EXPECT_EQ(0, collection->count()); | |
78 | |
79 Notification notify = NewMockNotification(delegate.get()); | |
80 collection->AddSystemNotification(notify, browser()->profile(), true); | |
81 | |
82 EXPECT_EQ(1, collection->count()); | |
83 | |
84 Notification update = SystemNotificationFactory::Create( | |
85 GURL(), ASCIIToUTF16("Title"), ASCIIToUTF16("updated"), delegate.get()); | |
86 collection->UpdateNotification(update); | |
87 | |
88 EXPECT_EQ(1, collection->count()); | |
89 | |
90 Notification update_and_show = SystemNotificationFactory::Create( | |
91 GURL(), ASCIIToUTF16("Title"), ASCIIToUTF16("updated and shown"), | |
92 delegate.get()); | |
93 collection->UpdateAndShowNotification(update_and_show); | |
94 | |
95 EXPECT_EQ(1, collection->count()); | |
96 | |
97 // Dismiss the notification. | |
98 collection->RemoveById(delegate->id()); | |
99 ui_test_utils::RunAllPendingInMessageLoop(); | |
100 | |
101 EXPECT_EQ(0, collection->count()); | |
102 } | |
103 | |
104 IN_PROC_BROWSER_TEST_F(SystemNotificationTest, TestAddWebUIMessageCallback) { | |
105 BalloonCollectionImplType* collection = GetBalloonCollectionImpl(); | |
106 Profile* profile = browser()->profile(); | |
107 | |
108 collection->AddSystemNotification( | |
109 NewMockNotification("1"), profile, false); | |
110 | |
111 EXPECT_TRUE(collection->AddWebUIMessageCallback( | |
112 NewMockNotification("1"), | |
113 "test", | |
114 base::Bind(&SystemNotificationTest::HandleWebUIMessage, | |
115 base::Unretained(static_cast<SystemNotificationTest*>(this))))); | |
116 | |
117 // Adding callback for the same message twice should fail. | |
118 EXPECT_FALSE(collection->AddWebUIMessageCallback( | |
119 NewMockNotification("1"), | |
120 "test", | |
121 base::Bind(&SystemNotificationTest::HandleWebUIMessage, | |
122 base::Unretained(static_cast<SystemNotificationTest*>(this))))); | |
123 | |
124 // Adding callback to nonexistent notification should fail. | |
125 EXPECT_FALSE(collection->AddWebUIMessageCallback( | |
126 NewMockNotification("2"), | |
127 "test1", | |
128 base::Bind(&SystemNotificationTest::HandleWebUIMessage, | |
129 base::Unretained(static_cast<SystemNotificationTest*>(this))))); | |
130 } | |
131 | |
132 // Occasional crash: http://crbug.com/96461 | |
133 IN_PROC_BROWSER_TEST_F(SystemNotificationTest, TestWebUIMessageCallback) { | |
134 BalloonCollectionImplType* collection = GetBalloonCollectionImpl(); | |
135 Profile* profile = browser()->profile(); | |
136 // A notification that sends 'test' WebUI message back to chrome. | |
137 const GURL content_url( | |
138 "data:text/html;charset=utf-8," | |
139 "<html><script>function send() { chrome.send('test', ['']); }</script>" | |
140 "<body onload='send()'></body></html>"); | |
141 collection->AddSystemNotification( | |
142 Notification(GURL(), content_url, string16(), string16(), | |
143 new MockNotificationDelegate("1")), | |
144 profile, | |
145 false); | |
146 EXPECT_TRUE(collection->AddWebUIMessageCallback( | |
147 NewMockNotification("1"), | |
148 "test", | |
149 base::Bind(&SystemNotificationTest::HandleWebUIMessage, | |
150 base::Unretained(static_cast<SystemNotificationTest*>(this))))); | |
151 MessageLoop::current()->Run(); | |
152 } | |
153 | |
154 } // namespace chromeos | |
OLD | NEW |