OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 "ui/arc/notification/arc_custom_notification_view.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/strings/utf_string_conversions.h" | |
10 #include "components/exo/notification_surface.h" | |
11 #include "ui/arc/notification/arc_notification_delegate.h" | |
12 #include "ui/arc/notification/arc_notification_item.h" | |
13 #include "ui/message_center/notification.h" | |
14 #include "ui/message_center/views/custom_notification_view.h" | |
15 #include "ui/message_center/views/message_center_controller.h" | |
16 #include "ui/message_center/views/message_view_factory.h" | |
17 #include "ui/views/controls/button/image_button.h" | |
18 #include "ui/views/test/views_test_base.h" | |
19 | |
20 namespace arc { | |
21 | |
22 namespace { | |
23 | |
24 class MockNotificationSurface : public exo::NotificationSurface { | |
hidehiko
2017/03/02 15:38:16
Optional: Mock implies gmock in Chromium, IIUC.
Ho
| |
25 public: | |
26 MockNotificationSurface(const std::string& notification_id, | |
27 aura::Window* window) | |
28 : notification_id_(notification_id), window_(window) {} | |
29 | |
30 gfx::Size GetSize() const override { return gfx::Size(100, 200); } | |
31 | |
32 void AttachWindow(views::NativeViewHost* nvh) override {} | |
33 | |
34 aura::Window* window() override { return window_; } | |
35 | |
36 const std::string& notification_id() const override { | |
37 return notification_id_; | |
38 } | |
39 | |
40 private: | |
41 std::string notification_id_; | |
42 aura::Window* window_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(MockNotificationSurface); | |
45 }; | |
46 | |
47 } // anonymous namespace | |
48 | |
49 class MockArcNotificationItem : public ArcNotificationItem { | |
hidehiko
2017/03/02 15:38:16
Moving to anonymous namespace, too?
Also how abou
| |
50 public: | |
51 MockArcNotificationItem(const std::string& notification_key) | |
52 : notification_key_(notification_key), weak_factory_(this) {} | |
53 | |
54 void UpdateWithArcNotificationData( | |
55 mojom::ArcNotificationDataPtr data) override {} | |
hidehiko
2017/03/02 15:38:16
s/{}/= default;/
| |
56 | |
57 void OnClosedFromAndroid() override {} | |
58 | |
59 void Close(bool by_user) override {} | |
60 | |
61 void CloseFromCloseButton() override { count_close_from_close_button_++; } | |
62 size_t count_close_from_close_button() { | |
63 return count_close_from_close_button_; | |
64 } | |
65 | |
66 void OpenSettings() override {} | |
67 | |
68 void AddObserver(Observer* observer) override {} | |
69 void RemoveObserver(Observer* observer) override {} | |
70 | |
71 void IncrementWindowRefCount() override {} | |
72 void DecrementWindowRefCount() override {} | |
73 bool pinned() const override { return false; } | |
74 const gfx::ImageSkia& snapshot() const override { return snapshot_; } | |
75 | |
76 const std::string& notification_key() const override { | |
77 return notification_key_; | |
78 } | |
79 | |
80 base::WeakPtr<MockArcNotificationItem> GetWeakPtr() { | |
81 return weak_factory_.GetWeakPtr(); | |
82 } | |
83 | |
84 private: | |
85 std::string notification_key_; | |
86 gfx::ImageSkia snapshot_; | |
87 size_t count_close_from_close_button_ = 0; | |
88 | |
89 base::WeakPtrFactory<MockArcNotificationItem> weak_factory_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(MockArcNotificationItem); | |
92 }; | |
93 | |
94 class TestMessageCenterController | |
95 : public message_center::MessageCenterController { | |
96 public: | |
97 TestMessageCenterController() {} | |
hidehiko
2017/03/02 15:38:16
s/{}/= default;/
| |
98 | |
99 // MessageCenterController | |
100 void ClickOnNotification(const std::string& notification_id) override { | |
101 // For this test, this method should not be invoked. | |
102 NOTREACHED(); | |
103 } | |
104 | |
105 void RemoveNotification(const std::string& notification_id, | |
106 bool by_user) override { | |
107 removed_ids_.insert(notification_id); | |
108 } | |
109 | |
110 std::unique_ptr<ui::MenuModel> CreateMenuModel( | |
111 const message_center::NotifierId& notifier_id, | |
112 const base::string16& display_source) override { | |
113 // For this test, this method should not be invoked. | |
114 NOTREACHED(); | |
115 return nullptr; | |
116 } | |
117 | |
118 bool HasClickedListener(const std::string& notification_id) override { | |
119 return false; | |
120 } | |
121 | |
122 void ClickOnNotificationButton(const std::string& notification_id, | |
123 int button_index) override { | |
124 // For this test, this method should not be invoked. | |
125 NOTREACHED(); | |
126 } | |
127 | |
128 void ClickOnSettingsButton(const std::string& notification_id) override { | |
129 // For this test, this method should not be invoked. | |
130 NOTREACHED(); | |
131 } | |
132 | |
133 void UpdateNotificationSize(const std::string& notification_id) override {} | |
134 | |
135 bool IsRemoved(const std::string& notification_id) const { | |
136 return (removed_ids_.find(notification_id) != removed_ids_.end()); | |
137 } | |
138 | |
139 private: | |
140 std::set<std::string> removed_ids_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController); | |
143 }; | |
144 | |
145 class DummyEvent : public ui::Event { | |
146 public: | |
147 DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {} | |
148 ~DummyEvent() override {} | |
hidehiko
2017/03/02 15:38:16
s/{}/= default;/
Or, you may be able to drop it si
| |
149 }; | |
150 | |
151 class ArcCustomNotificationViewTest : public views::ViewsTestBase { | |
152 public: | |
153 ArcCustomNotificationViewTest() {} | |
hidehiko
2017/03/02 15:38:16
ditto.
| |
154 ~ArcCustomNotificationViewTest() override {} | |
155 | |
156 void SetUp() override { | |
157 std::string notification_id("notification id"); | |
hidehiko
2017/03/02 15:38:16
Optional: in Chromium, std::string foo = "string";
| |
158 | |
159 views::ViewsTestBase::SetUp(); | |
hidehiko
2017/03/02 15:38:16
Could you do this at beginning of SetUp()?
| |
160 notification_item_.reset(new MockArcNotificationItem(notification_id)); | |
hidehiko
2017/03/02 15:38:16
Optional: I recommend to use notification_item_ =
| |
161 | |
162 notification_delegate_ = | |
163 new ArcNotificationDelegate(notification_item_->GetWeakPtr()); | |
164 | |
165 notification_.reset(new message_center::Notification( | |
166 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id, | |
167 base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message"), gfx::Image(), | |
168 base::UTF8ToUTF16("display source"), GURL(), | |
169 message_center::NotifierId(message_center::NotifierId::APPLICATION, | |
170 "extension_id"), | |
171 message_center::RichNotificationData(), notification_delegate_.get())); | |
172 | |
173 notification_view_.reset( | |
174 static_cast<message_center::CustomNotificationView*>( | |
175 message_center::MessageViewFactory::Create(controller(), | |
176 *notification_, true))); | |
177 notification_view_->set_owned_by_client(); | |
178 | |
179 views::Widget::InitParams init_params( | |
180 CreateParams(views::Widget::InitParams::TYPE_POPUP)); | |
181 views::Widget* widget = new views::Widget(); | |
hidehiko
2017/03/02 15:38:16
Looks leak?
| |
182 widget->Init(init_params); | |
183 | |
184 surface_ = | |
185 new MockNotificationSurface(notification_id, widget->GetNativeWindow()); | |
186 GetArcNotificationView()->OnNotificationSurfaceAdded(surface_); | |
187 | |
188 widget->SetContentsView(notification_view_.get()); | |
189 widget->SetSize(notification_view_->GetPreferredSize()); | |
190 } | |
191 | |
192 void TearDown() override { | |
193 GetArcNotificationView()->OnNotificationSurfaceRemoved(surface_); | |
194 widget()->Close(); | |
195 notification_view_.reset(); | |
196 views::ViewsTestBase::TearDown(); | |
197 } | |
198 | |
199 void PressCloseButton() { | |
200 DummyEvent dummy_event; | |
201 views::ImageButton* close_button = GetArcNotificationView()->close_button_; | |
202 ASSERT_NE(nullptr, close_button); | |
203 GetArcNotificationView()->ButtonPressed(close_button, dummy_event); | |
204 } | |
205 | |
206 TestMessageCenterController* controller() { return &controller_; } | |
207 views::Widget* widget() { return notification_view_->GetWidget(); } | |
208 | |
209 ArcCustomNotificationView* GetArcNotificationView() { | |
210 views::View* view = notification_view_->GetContentsViewForTesting(); | |
211 EXPECT_EQ(ArcCustomNotificationView::kViewClassName, view->GetClassName()); | |
212 return static_cast<ArcCustomNotificationView*>(view); | |
213 } | |
214 | |
215 MockArcNotificationItem* notification_item() const { | |
216 return notification_item_.get(); | |
217 } | |
218 | |
219 private: | |
220 MockNotificationSurface* surface_; | |
221 TestMessageCenterController controller_; | |
222 scoped_refptr<ArcNotificationDelegate> notification_delegate_; | |
223 std::unique_ptr<message_center::Notification> notification_; | |
224 std::unique_ptr<message_center::CustomNotificationView> notification_view_; | |
225 std::unique_ptr<MockArcNotificationItem> notification_item_; | |
226 | |
227 DISALLOW_COPY_AND_ASSIGN(ArcCustomNotificationViewTest); | |
228 }; | |
229 | |
230 TEST_F(ArcCustomNotificationViewTest, CloseButton) { | |
231 EXPECT_EQ(0u, notification_item()->count_close_from_close_button()); | |
232 PressCloseButton(); | |
233 EXPECT_EQ(1u, notification_item()->count_close_from_close_button()); | |
234 } | |
235 | |
236 } // namespace arc | |
OLD | NEW |