Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: ui/message_center/views/custom_notification_view_unittest.cc

Issue 2870283002: Move message_center::CustomNotificationView to arc::ArcNotificationView (Closed)
Patch Set: Addressed comment Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 <memory>
6
7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "third_party/skia/include/core/SkColor.h"
12 #include "ui/base/ime/dummy_text_input_client.h"
13 #include "ui/base/ime/input_method.h"
14 #include "ui/base/ime/text_input_client.h"
15 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
16 #include "ui/events/event.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/events/test/event_generator.h"
19 #include "ui/message_center/notification.h"
20 #include "ui/message_center/notification_delegate.h"
21 #include "ui/message_center/views/custom_notification_view.h"
22 #include "ui/message_center/views/message_center_controller.h"
23 #include "ui/message_center/views/message_view_factory.h"
24 #include "ui/views/background.h"
25 #include "ui/views/controls/button/image_button.h"
26 #include "ui/views/test/views_test_base.h"
27
28 namespace message_center {
29
30 namespace {
31
32 const SkColor kBackgroundColor = SK_ColorGREEN;
33
34 class TestCustomView : public views::View {
35 public:
36 TestCustomView() {
37 SetFocusBehavior(FocusBehavior::ALWAYS);
38 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
39 set_preferred_size(gfx::Size(100, 100));
40 }
41 ~TestCustomView() override {}
42
43 void Reset() {
44 mouse_event_count_ = 0;
45 keyboard_event_count_ = 0;
46 }
47
48 // views::View
49 bool OnMousePressed(const ui::MouseEvent& event) override {
50 ++mouse_event_count_;
51 return true;
52 }
53 void OnMouseMoved(const ui::MouseEvent& event) override {
54 ++mouse_event_count_;
55 }
56 void OnMouseReleased(const ui::MouseEvent& event) override {
57 ++mouse_event_count_;
58 }
59 bool OnKeyPressed(const ui::KeyEvent& event) override {
60 ++keyboard_event_count_;
61 return false;
62 }
63
64 int mouse_event_count() const { return mouse_event_count_; }
65 int keyboard_event_count() const { return keyboard_event_count_; }
66
67 private:
68 int mouse_event_count_ = 0;
69 int keyboard_event_count_ = 0;
70
71 DISALLOW_COPY_AND_ASSIGN(TestCustomView);
72 };
73
74 class TestContentViewDelegate : public CustomNotificationContentViewDelegate {
75 public:
76 bool IsCloseButtonFocused() const override { return false; }
77 void RequestFocusOnCloseButton() override {}
78 void UpdateControlButtonsVisibility() override {}
79 void OnSlideChanged() override {}
80 };
81
82 class TestNotificationDelegate : public NotificationDelegate {
83 public:
84 TestNotificationDelegate() {}
85
86 // NotificateDelegate
87 std::unique_ptr<CustomContent> CreateCustomContent() override {
88 return base::MakeUnique<CustomContent>(
89 base::MakeUnique<TestCustomView>(),
90 base::MakeUnique<TestContentViewDelegate>());
91 }
92
93 private:
94 ~TestNotificationDelegate() override {}
95
96 DISALLOW_COPY_AND_ASSIGN(TestNotificationDelegate);
97 };
98
99 class TestMessageCenterController : public MessageCenterController {
100 public:
101 TestMessageCenterController() {}
102
103 // MessageCenterController
104 void ClickOnNotification(const std::string& notification_id) override {
105 // For this test, this method should not be invoked.
106 NOTREACHED();
107 }
108
109 void RemoveNotification(const std::string& notification_id,
110 bool by_user) override {
111 removed_ids_.insert(notification_id);
112 }
113
114 std::unique_ptr<ui::MenuModel> CreateMenuModel(
115 const NotifierId& notifier_id,
116 const base::string16& display_source) override {
117 // For this test, this method should not be invoked.
118 NOTREACHED();
119 return nullptr;
120 }
121
122 bool HasClickedListener(const std::string& notification_id) override {
123 return false;
124 }
125
126 void ClickOnNotificationButton(const std::string& notification_id,
127 int button_index) override {
128 // For this test, this method should not be invoked.
129 NOTREACHED();
130 }
131
132 void ClickOnSettingsButton(const std::string& notification_id) override {
133 // For this test, this method should not be invoked.
134 NOTREACHED();
135 }
136
137 void UpdateNotificationSize(const std::string& notification_id) override {
138 // For this test, this method should not be invoked.
139 NOTREACHED();
140 }
141
142 bool IsRemoved(const std::string& notification_id) const {
143 return (removed_ids_.find(notification_id) != removed_ids_.end());
144 }
145
146 private:
147 std::set<std::string> removed_ids_;
148
149 DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController);
150 };
151
152 class TestTextInputClient : public ui::DummyTextInputClient {
153 public:
154 TestTextInputClient() : ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT) {}
155
156 ui::TextInputType GetTextInputType() const override { return type_; }
157
158 void set_text_input_type(ui::TextInputType type) { type_ = type; }
159
160 private:
161 ui::TextInputType type_ = ui::TEXT_INPUT_TYPE_NONE;
162
163 DISALLOW_COPY_AND_ASSIGN(TestTextInputClient);
164 };
165
166 } // namespace
167
168 class CustomNotificationViewTest : public views::ViewsTestBase {
169 public:
170 CustomNotificationViewTest() {}
171 ~CustomNotificationViewTest() override {}
172
173 // views::ViewsTestBase
174 void SetUp() override {
175 views::ViewsTestBase::SetUp();
176
177 notification_delegate_ = new TestNotificationDelegate;
178
179 notification_.reset(new Notification(
180 NOTIFICATION_TYPE_CUSTOM, std::string("notification id"),
181 base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message"), gfx::Image(),
182 base::UTF8ToUTF16("display source"), GURL(),
183 NotifierId(NotifierId::APPLICATION, "extension_id"),
184 message_center::RichNotificationData(), notification_delegate_.get()));
185
186 notification_view_.reset(static_cast<CustomNotificationView*>(
187 MessageViewFactory::Create(controller(), *notification_, true)));
188 notification_view_->set_owned_by_client();
189
190 views::Widget::InitParams init_params(
191 CreateParams(views::Widget::InitParams::TYPE_POPUP));
192 views::Widget* widget = new views::Widget();
193 widget->Init(init_params);
194 widget->SetContentsView(notification_view_.get());
195 widget->SetSize(notification_view_->GetPreferredSize());
196 widget->Show();
197 }
198
199 void TearDown() override {
200 widget()->Close();
201 notification_view_.reset();
202 views::ViewsTestBase::TearDown();
203 }
204
205 SkColor GetBackgroundColor() const {
206 return notification_view_->background_view()->background()->get_color();
207 }
208
209 void PerformClick(const gfx::Point& point) {
210 ui::MouseEvent pressed_event = ui::MouseEvent(
211 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
212 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
213 widget()->OnMouseEvent(&pressed_event);
214 ui::MouseEvent released_event = ui::MouseEvent(
215 ui::ET_MOUSE_RELEASED, point, point, ui::EventTimeForNow(),
216 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
217 widget()->OnMouseEvent(&released_event);
218 }
219
220 void PerformKeyEvents(ui::KeyboardCode code) {
221 ui::KeyEvent event1 = ui::KeyEvent(ui::ET_KEY_PRESSED, code, ui::EF_NONE);
222 widget()->OnKeyEvent(&event1);
223 ui::KeyEvent event2 = ui::KeyEvent(ui::ET_KEY_RELEASED, code, ui::EF_NONE);
224 widget()->OnKeyEvent(&event2);
225 }
226
227 void KeyPress(ui::KeyboardCode key_code) {
228 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE);
229 widget()->OnKeyEvent(&event);
230 }
231
232 void UpdateNotificationViews() {
233 notification_view()->UpdateWithNotification(*notification());
234 }
235
236 float GetNotificationSlideAmount() const {
237 return notification_view_->GetSlideOutLayer()
238 ->transform()
239 .To2dTranslation()
240 .x();
241 }
242
243 void DispatchGesture(const ui::GestureEventDetails& details) {
244 ui::test::EventGenerator generator(
245 notification_view()->GetWidget()->GetNativeWindow());
246 ui::GestureEvent event(0, 0, 0, ui::EventTimeForNow(), details);
247 generator.Dispatch(&event);
248 }
249
250 void BeginScroll() {
251 DispatchGesture(ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN));
252 }
253
254 void EndScroll() {
255 DispatchGesture(ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_END));
256 }
257
258 void ScrollBy(int dx) {
259 DispatchGesture(
260 ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, dx, 0));
261 }
262
263 TestMessageCenterController* controller() { return &controller_; }
264 Notification* notification() { return notification_.get(); }
265 TestCustomView* custom_view() {
266 return static_cast<TestCustomView*>(notification_view_->contents_view_);
267 }
268 views::Widget* widget() { return notification_view_->GetWidget(); }
269 CustomNotificationView* notification_view() {
270 return notification_view_.get();
271 }
272
273 private:
274 TestMessageCenterController controller_;
275 scoped_refptr<TestNotificationDelegate> notification_delegate_;
276 std::unique_ptr<Notification> notification_;
277 std::unique_ptr<CustomNotificationView> notification_view_;
278
279 DISALLOW_COPY_AND_ASSIGN(CustomNotificationViewTest);
280 };
281
282 TEST_F(CustomNotificationViewTest, Background) {
283 EXPECT_EQ(kBackgroundColor, GetBackgroundColor());
284 }
285
286 TEST_F(CustomNotificationViewTest, Events) {
287 widget()->Show();
288 custom_view()->RequestFocus();
289
290 EXPECT_EQ(0, custom_view()->mouse_event_count());
291 gfx::Point cursor_location(1, 1);
292 views::View::ConvertPointToWidget(custom_view(), &cursor_location);
293 PerformClick(cursor_location);
294 EXPECT_EQ(2, custom_view()->mouse_event_count());
295
296 ui::MouseEvent move(ui::ET_MOUSE_MOVED, cursor_location, cursor_location,
297 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
298 widget()->OnMouseEvent(&move);
299 EXPECT_EQ(3, custom_view()->mouse_event_count());
300
301 EXPECT_EQ(0, custom_view()->keyboard_event_count());
302 KeyPress(ui::VKEY_A);
303 EXPECT_EQ(1, custom_view()->keyboard_event_count());
304 }
305
306 TEST_F(CustomNotificationViewTest, SlideOut) {
307 ui::ScopedAnimationDurationScaleMode zero_duration_scope(
308 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
309
310 UpdateNotificationViews();
311 std::string notification_id = notification()->id();
312
313 BeginScroll();
314 ScrollBy(-10);
315 EXPECT_FALSE(controller()->IsRemoved(notification_id));
316 EXPECT_EQ(-10.f, GetNotificationSlideAmount());
317 EndScroll();
318 EXPECT_FALSE(controller()->IsRemoved(notification_id));
319 EXPECT_EQ(0.f, GetNotificationSlideAmount());
320
321 BeginScroll();
322 ScrollBy(-200);
323 EXPECT_FALSE(controller()->IsRemoved(notification_id));
324 EXPECT_EQ(-200.f, GetNotificationSlideAmount());
325 EndScroll();
326 EXPECT_TRUE(controller()->IsRemoved(notification_id));
327 }
328
329 TEST_F(CustomNotificationViewTest, SlideOutNested) {
330 ui::ScopedAnimationDurationScaleMode zero_duration_scope(
331 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
332
333 UpdateNotificationViews();
334 notification_view()->SetIsNested();
335 std::string notification_id = notification()->id();
336
337 BeginScroll();
338 ScrollBy(-10);
339 EXPECT_FALSE(controller()->IsRemoved(notification_id));
340 EXPECT_EQ(-10.f, GetNotificationSlideAmount());
341 EndScroll();
342 EXPECT_FALSE(controller()->IsRemoved(notification_id));
343 EXPECT_EQ(0.f, GetNotificationSlideAmount());
344
345 BeginScroll();
346 ScrollBy(-200);
347 EXPECT_FALSE(controller()->IsRemoved(notification_id));
348 EXPECT_EQ(-200.f, GetNotificationSlideAmount());
349 EndScroll();
350 EXPECT_TRUE(controller()->IsRemoved(notification_id));
351 }
352
353 // Pinning notification is ChromeOS only feature.
354 #if defined(OS_CHROMEOS)
355
356 TEST_F(CustomNotificationViewTest, SlideOutPinned) {
357 ui::ScopedAnimationDurationScaleMode zero_duration_scope(
358 ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);
359
360 notification()->set_pinned(true);
361 UpdateNotificationViews();
362 std::string notification_id = notification()->id();
363
364 BeginScroll();
365 ScrollBy(-200);
366 EXPECT_FALSE(controller()->IsRemoved(notification_id));
367 EXPECT_LT(-200.f, GetNotificationSlideAmount());
368 EndScroll();
369 EXPECT_FALSE(controller()->IsRemoved(notification_id));
370 }
371
372 #endif // defined(OS_CHROMEOS)
373
374 TEST_F(CustomNotificationViewTest, PressBackspaceKey) {
375 std::string notification_id = notification()->id();
376 custom_view()->RequestFocus();
377
378 ui::InputMethod* input_method = custom_view()->GetInputMethod();
379 ASSERT_TRUE(input_method);
380 TestTextInputClient text_input_client;
381 input_method->SetFocusedTextInputClient(&text_input_client);
382 ASSERT_EQ(&text_input_client, input_method->GetTextInputClient());
383
384 EXPECT_FALSE(controller()->IsRemoved(notification_id));
385 PerformKeyEvents(ui::VKEY_BACK);
386 EXPECT_TRUE(controller()->IsRemoved(notification_id));
387
388 input_method->SetFocusedTextInputClient(nullptr);
389 }
390
391 TEST_F(CustomNotificationViewTest, PressBackspaceKeyOnEditBox) {
392 std::string notification_id = notification()->id();
393 custom_view()->RequestFocus();
394
395 ui::InputMethod* input_method = custom_view()->GetInputMethod();
396 ASSERT_TRUE(input_method);
397 TestTextInputClient text_input_client;
398 input_method->SetFocusedTextInputClient(&text_input_client);
399 ASSERT_EQ(&text_input_client, input_method->GetTextInputClient());
400
401 text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_TEXT);
402
403 EXPECT_FALSE(controller()->IsRemoved(notification_id));
404 PerformKeyEvents(ui::VKEY_BACK);
405 EXPECT_FALSE(controller()->IsRemoved(notification_id));
406
407 input_method->SetFocusedTextInputClient(nullptr);
408 }
409
410 TEST_F(CustomNotificationViewTest, ChangeContentHeight) {
411 // Default size.
412 gfx::Size size = notification_view()->GetPreferredSize();
413 size.Enlarge(0, -notification_view()->GetInsets().height());
414 EXPECT_EQ("360x100", size.ToString());
415
416 // Allow small notifications.
417 custom_view()->set_preferred_size(gfx::Size(10, 10));
418 size = notification_view()->GetPreferredSize();
419 size.Enlarge(0, -notification_view()->GetInsets().height());
420 EXPECT_EQ("360x10", size.ToString());
421
422 // The long notification.
423 custom_view()->set_preferred_size(gfx::Size(1000, 1000));
424 size = notification_view()->GetPreferredSize();
425 size.Enlarge(0, -notification_view()->GetInsets().height());
426 EXPECT_EQ("360x1000", size.ToString());
427 }
428
429 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/views/custom_notification_view.cc ('k') | ui/message_center/views/message_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698