OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/ref_counted.h" | 6 #include "base/ref_counted.h" |
7 #include "chrome/browser/browser.h" | 7 #include "chrome/browser/browser.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/chromeos/notifications/balloon_collection_impl.h" | 9 #include "chrome/browser/chromeos/notifications/balloon_collection_impl.h" |
10 #include "chrome/browser/chromeos/notifications/notification_panel.h" | 10 #include "chrome/browser/chromeos/notifications/notification_panel.h" |
11 #include "chrome/browser/chromeos/notifications/system_notification_factory.h" | 11 #include "chrome/browser/chromeos/notifications/system_notification_factory.h" |
12 #include "chrome/browser/notifications/notification_delegate.h" | 12 #include "chrome/browser/notifications/notification_delegate.h" |
13 #include "chrome/browser/notifications/notification_ui_manager.h" | 13 #include "chrome/browser/notifications/notification_ui_manager.h" |
14 #include "chrome/test/in_process_browser_test.h" | 14 #include "chrome/test/in_process_browser_test.h" |
15 #include "chrome/test/ui_test_utils.h" | 15 #include "chrome/test/ui_test_utils.h" |
16 | 16 |
17 namespace chromeos { | 17 namespace chromeos { |
18 | 18 |
19 class SystemNotificationDelegate : public NotificationDelegate { | 19 class MockNotificationDelegate : public NotificationDelegate { |
20 public: | 20 public: |
21 explicit SystemNotificationDelegate(const std::string id) : id_(id) {} | 21 explicit MockNotificationDelegate(const std::string& id) : id_(id) {} |
22 | 22 |
23 virtual void Display() {} | 23 virtual void Display() {} |
24 virtual void Error() {} | 24 virtual void Error() {} |
25 virtual void Close(bool by_user) {} | 25 virtual void Close(bool by_user) {} |
26 virtual std::string id() const { return id_; } | 26 virtual std::string id() const { return id_; } |
27 | 27 |
28 private: | 28 private: |
29 std::string id_; | 29 std::string id_; |
30 | 30 |
31 DISALLOW_COPY_AND_ASSIGN(SystemNotificationDelegate); | 31 DISALLOW_COPY_AND_ASSIGN(MockNotificationDelegate); |
32 }; | 32 }; |
33 | 33 |
34 class NotificationTest : public InProcessBrowserTest { | 34 class NotificationTest : public InProcessBrowserTest { |
35 public: | 35 public: |
36 NotificationTest() { | 36 NotificationTest() { |
37 } | 37 } |
38 | 38 |
39 protected: | 39 protected: |
40 BalloonCollectionImpl* GetBalloonCollectionImpl() { | 40 BalloonCollectionImpl* GetBalloonCollectionImpl() { |
41 return static_cast<BalloonCollectionImpl*>( | 41 return static_cast<BalloonCollectionImpl*>( |
42 g_browser_process->notification_ui_manager()->balloon_collection()); | 42 g_browser_process->notification_ui_manager()->balloon_collection()); |
43 } | 43 } |
44 | 44 |
45 NotificationPanel* GetNotificationPanel() { | 45 NotificationPanel* GetNotificationPanel() { |
46 return static_cast<NotificationPanel*>( | 46 return static_cast<NotificationPanel*>( |
47 GetBalloonCollectionImpl()->notification_ui()); | 47 GetBalloonCollectionImpl()->notification_ui()); |
48 } | 48 } |
| 49 |
| 50 Notification NewMockNotification(const std::string& id) { |
| 51 return NewMockNotification(new MockNotificationDelegate(id)); |
| 52 } |
| 53 |
| 54 Notification NewMockNotification(NotificationDelegate* delegate) { |
| 55 return SystemNotificationFactory::Create( |
| 56 GURL(), ASCIIToUTF16(""), ASCIIToUTF16(""), |
| 57 delegate); |
| 58 } |
| 59 |
| 60 void RunAllPending() { |
| 61 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 62 ui_test_utils::RunMessageLoop(); |
| 63 } |
49 }; | 64 }; |
50 | 65 |
| 66 IN_PROC_BROWSER_TEST_F(NotificationTest, TestBasic) { |
| 67 BalloonCollectionImpl* collection = GetBalloonCollectionImpl(); |
| 68 NotificationPanel* panel = GetNotificationPanel(); |
| 69 NotificationPanelTester* tester = panel->GetTester(); |
| 70 |
| 71 // Using system notification as regular notification. |
| 72 collection->Add(NewMockNotification("1"), browser()->profile()); |
| 73 |
| 74 EXPECT_EQ(1, tester->GetNewNotificationCount()); |
| 75 EXPECT_EQ(1, tester->GetNotificationCount()); |
| 76 EXPECT_EQ(0, tester->GetStickyNotificationCount()); |
| 77 EXPECT_EQ(NotificationPanel::STICKY_AND_NEW, tester->state()); |
| 78 |
| 79 collection->Add(NewMockNotification("2"), browser()->profile()); |
| 80 |
| 81 EXPECT_EQ(2, tester->GetNewNotificationCount()); |
| 82 EXPECT_EQ(2, tester->GetNotificationCount()); |
| 83 EXPECT_EQ(NotificationPanel::STICKY_AND_NEW, tester->state()); |
| 84 |
| 85 collection->Remove(NewMockNotification("1")); |
| 86 RunAllPending(); |
| 87 |
| 88 EXPECT_EQ(1, tester->GetNewNotificationCount()); |
| 89 EXPECT_EQ(1, tester->GetNotificationCount()); |
| 90 EXPECT_EQ(NotificationPanel::STICKY_AND_NEW, tester->state()); |
| 91 |
| 92 collection->Remove(NewMockNotification("2")); |
| 93 RunAllPending(); |
| 94 EXPECT_EQ(0, tester->GetNewNotificationCount()); |
| 95 EXPECT_EQ(0, tester->GetNotificationCount()); |
| 96 EXPECT_EQ(NotificationPanel::CLOSED, tester->state()); |
| 97 } |
| 98 |
| 99 IN_PROC_BROWSER_TEST_F(NotificationTest, TestMouseMotion) { |
| 100 BalloonCollectionImpl* collection = GetBalloonCollectionImpl(); |
| 101 NotificationPanel* panel = GetNotificationPanel(); |
| 102 NotificationPanelTester* tester = panel->GetTester(); |
| 103 |
| 104 // Using system notification as regular notification. |
| 105 collection->Add(NewMockNotification("1"), browser()->profile()); |
| 106 collection->Add(NewMockNotification("2"), browser()->profile()); |
| 107 |
| 108 EXPECT_EQ(NotificationPanel::STICKY_AND_NEW, tester->state()); |
| 109 |
| 110 panel->OnMouseMotion(); |
| 111 EXPECT_EQ(NotificationPanel::KEEP_SIZE, tester->state()); |
| 112 |
| 113 collection->Remove(NewMockNotification("1")); |
| 114 RunAllPending(); |
| 115 |
| 116 EXPECT_EQ(1, tester->GetNewNotificationCount()); |
| 117 EXPECT_EQ(1, tester->GetNotificationCount()); |
| 118 EXPECT_EQ(NotificationPanel::KEEP_SIZE, tester->state()); |
| 119 |
| 120 collection->Remove(NewMockNotification("2")); |
| 121 RunAllPending(); |
| 122 EXPECT_EQ(0, tester->GetNotificationCount()); |
| 123 EXPECT_EQ(NotificationPanel::CLOSED, tester->state()); |
| 124 |
| 125 collection->Add(NewMockNotification("3"), browser()->profile()); |
| 126 EXPECT_EQ(NotificationPanel::STICKY_AND_NEW, tester->state()); |
| 127 collection->Remove(NewMockNotification("3")); |
| 128 |
| 129 RunAllPending(); |
| 130 EXPECT_EQ(0, tester->GetNotificationCount()); |
| 131 EXPECT_EQ(NotificationPanel::CLOSED, tester->state()); |
| 132 } |
| 133 |
51 IN_PROC_BROWSER_TEST_F(NotificationTest, TestSystemNotification) { | 134 IN_PROC_BROWSER_TEST_F(NotificationTest, TestSystemNotification) { |
52 BalloonCollectionImpl* collection = GetBalloonCollectionImpl(); | 135 BalloonCollectionImpl* collection = GetBalloonCollectionImpl(); |
53 NotificationPanel* panel = GetNotificationPanel(); | 136 NotificationPanel* panel = GetNotificationPanel(); |
54 scoped_refptr<SystemNotificationDelegate> delegate( | 137 scoped_refptr<MockNotificationDelegate> delegate( |
55 new SystemNotificationDelegate("power")); | 138 new MockNotificationDelegate("power")); |
| 139 NotificationPanelTester* tester = panel->GetTester(); |
56 | 140 |
57 Notification notify = SystemNotificationFactory::Create( | 141 Notification notify = NewMockNotification(delegate.get()); |
58 GURL(), ASCIIToUTF16("Title"), ASCIIToUTF16("test"), delegate.get()); | |
59 collection->AddSystemNotification(notify, browser()->profile(), true, false); | 142 collection->AddSystemNotification(notify, browser()->profile(), true, false); |
60 | 143 |
61 EXPECT_EQ(1, panel->GetNewNotificationCount()); | 144 EXPECT_EQ(1, tester->GetNewNotificationCount()); |
62 EXPECT_EQ(1, panel->GetStickyNotificationCount()); | 145 EXPECT_EQ(1, tester->GetStickyNotificationCount()); |
63 | 146 |
64 Notification update = SystemNotificationFactory::Create( | 147 Notification update = SystemNotificationFactory::Create( |
65 GURL(), ASCIIToUTF16("Title"), ASCIIToUTF16("updated"), delegate.get()); | 148 GURL(), ASCIIToUTF16("Title"), ASCIIToUTF16("updated"), delegate.get()); |
66 collection->UpdateNotification(update); | 149 collection->UpdateNotification(update); |
67 | 150 |
68 EXPECT_EQ(1, panel->GetStickyNotificationCount()); | 151 EXPECT_EQ(1, tester->GetStickyNotificationCount()); |
69 | 152 |
70 // Dismiss the notification. | 153 // Dismiss the notification. |
71 // TODO(oshima): Consider updating API to Remove(NotificationDelegate) | 154 // TODO(oshima): Consider updating API to Remove(NotificationDelegate) |
72 // or Remove(std::string id); | 155 // or Remove(std::string id); |
73 collection->Remove(Notification(GURL(), GURL(), | 156 collection->Remove(Notification(GURL(), GURL(), |
74 std::wstring(), delegate.get())); | 157 std::wstring(), delegate.get())); |
| 158 RunAllPending(); |
75 | 159 |
76 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 160 EXPECT_EQ(0, tester->GetStickyNotificationCount()); |
77 ui_test_utils::RunMessageLoop(); | 161 EXPECT_EQ(0, tester->GetNewNotificationCount()); |
78 | |
79 EXPECT_EQ(0, panel->GetStickyNotificationCount()); | |
80 EXPECT_EQ(0, panel->GetNewNotificationCount()); | |
81 // TODO(oshima): check content, etc.. | 162 // TODO(oshima): check content, etc.. |
82 } | 163 } |
| 164 |
83 } // namespace chromeos | 165 } // namespace chromeos |
OLD | NEW |