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

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: Fixed test 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/events/event.h"
16 #include "ui/events/event_utils.h"
17 #include "ui/message_center/notification.h"
18 #include "ui/message_center/notification_delegate.h"
19 #include "ui/message_center/views/custom_notification_view.h"
20 #include "ui/message_center/views/message_center_controller.h"
21 #include "ui/message_center/views/message_view_factory.h"
22 #include "ui/views/background.h"
23 #include "ui/views/controls/button/image_button.h"
24 #include "ui/views/test/views_test_base.h"
25
26 namespace message_center {
27
28 namespace {
29
30 const SkColor kBackgroundColor = SK_ColorGREEN;
31
32 class TestCustomView : public views::View {
33 public:
34 TestCustomView() {
35 SetFocusBehavior(FocusBehavior::ALWAYS);
36 set_background(views::Background::CreateSolidBackground(kBackgroundColor));
37 }
38 ~TestCustomView() override {}
39
40 void Reset() {
41 mouse_event_count_ = 0;
42 keyboard_event_count_ = 0;
43 }
44
45 void set_preferred_size(gfx::Size size) { preferred_size_ = size; }
46
47 // views::View
48 gfx::Size GetPreferredSize() const override { return preferred_size_; }
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 gfx::Size preferred_size_ = gfx::Size(100, 100);
71
72 DISALLOW_COPY_AND_ASSIGN(TestCustomView);
73 };
74
75 class TestContentViewDelegate : public CustomNotificationContentViewDelegate {
76 public:
77 bool IsCloseButtonFocused() const override { return false; }
78 void RequestFocusOnCloseButton() override {}
79 void UpdateControlButtonsVisibility() 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 }
197
198 void TearDown() override {
199 widget()->Close();
200 notification_view_.reset();
201 views::ViewsTestBase::TearDown();
202 }
203
204 SkColor GetBackgroundColor() const {
205 return notification_view_->background_view()->background()->get_color();
206 }
207
208 void PerformClick(const gfx::Point& point) {
209 ui::MouseEvent pressed_event = ui::MouseEvent(
210 ui::ET_MOUSE_PRESSED, point, point, ui::EventTimeForNow(),
211 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
212 widget()->OnMouseEvent(&pressed_event);
213 ui::MouseEvent released_event = ui::MouseEvent(
214 ui::ET_MOUSE_RELEASED, point, point, ui::EventTimeForNow(),
215 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
216 widget()->OnMouseEvent(&released_event);
217 }
218
219 void PerformKeyEvents(ui::KeyboardCode code) {
220 ui::KeyEvent event1 = ui::KeyEvent(ui::ET_KEY_PRESSED, code, ui::EF_NONE);
221 widget()->OnKeyEvent(&event1);
222 ui::KeyEvent event2 = ui::KeyEvent(ui::ET_KEY_RELEASED, code, ui::EF_NONE);
223 widget()->OnKeyEvent(&event2);
224 }
225
226 void KeyPress(ui::KeyboardCode key_code) {
227 ui::KeyEvent event(ui::ET_KEY_PRESSED, key_code, ui::EF_NONE);
228 widget()->OnKeyEvent(&event);
229 }
230
231 void UpdateNotificationViews() {
232 notification_view()->UpdateWithNotification(*notification());
233 }
234
235 TestMessageCenterController* controller() { return &controller_; }
236 Notification* notification() { return notification_.get(); }
237 TestCustomView* custom_view() {
238 return static_cast<TestCustomView*>(notification_view_->contents_view_);
239 }
240 views::Widget* widget() { return notification_view_->GetWidget(); }
241 CustomNotificationView* notification_view() {
242 return notification_view_.get();
243 }
244
245 private:
246 TestMessageCenterController controller_;
247 scoped_refptr<TestNotificationDelegate> notification_delegate_;
248 std::unique_ptr<Notification> notification_;
249 std::unique_ptr<CustomNotificationView> notification_view_;
250
251 DISALLOW_COPY_AND_ASSIGN(CustomNotificationViewTest);
252 };
253
254 TEST_F(CustomNotificationViewTest, Background) {
255 EXPECT_EQ(kBackgroundColor, GetBackgroundColor());
256 }
257
258 TEST_F(CustomNotificationViewTest, Events) {
259 widget()->Show();
260 custom_view()->RequestFocus();
261
262 EXPECT_EQ(0, custom_view()->mouse_event_count());
263 gfx::Point cursor_location(1, 1);
264 views::View::ConvertPointToWidget(custom_view(), &cursor_location);
265 PerformClick(cursor_location);
266 EXPECT_EQ(2, custom_view()->mouse_event_count());
267
268 ui::MouseEvent move(ui::ET_MOUSE_MOVED, cursor_location, cursor_location,
269 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE);
270 widget()->OnMouseEvent(&move);
271 EXPECT_EQ(3, custom_view()->mouse_event_count());
272
273 EXPECT_EQ(0, custom_view()->keyboard_event_count());
274 KeyPress(ui::VKEY_A);
275 EXPECT_EQ(1, custom_view()->keyboard_event_count());
276 }
277
278 TEST_F(CustomNotificationViewTest, PressBackspaceKey) {
279 std::string notification_id = notification()->id();
280 custom_view()->RequestFocus();
281
282 ui::InputMethod* input_method = custom_view()->GetInputMethod();
283 ASSERT_TRUE(input_method);
284 TestTextInputClient text_input_client;
285 input_method->SetFocusedTextInputClient(&text_input_client);
286 ASSERT_EQ(&text_input_client, input_method->GetTextInputClient());
287
288 EXPECT_FALSE(controller()->IsRemoved(notification_id));
289 PerformKeyEvents(ui::VKEY_BACK);
290 EXPECT_TRUE(controller()->IsRemoved(notification_id));
291
292 input_method->SetFocusedTextInputClient(nullptr);
293 }
294
295 TEST_F(CustomNotificationViewTest, PressBackspaceKeyOnEditBox) {
296 std::string notification_id = notification()->id();
297 custom_view()->RequestFocus();
298
299 ui::InputMethod* input_method = custom_view()->GetInputMethod();
300 ASSERT_TRUE(input_method);
301 TestTextInputClient text_input_client;
302 input_method->SetFocusedTextInputClient(&text_input_client);
303 ASSERT_EQ(&text_input_client, input_method->GetTextInputClient());
304
305 text_input_client.set_text_input_type(ui::TEXT_INPUT_TYPE_TEXT);
306
307 EXPECT_FALSE(controller()->IsRemoved(notification_id));
308 PerformKeyEvents(ui::VKEY_BACK);
309 EXPECT_FALSE(controller()->IsRemoved(notification_id));
310
311 input_method->SetFocusedTextInputClient(nullptr);
312 }
313
314 TEST_F(CustomNotificationViewTest, ChangeContentHeight) {
315 // Default size.
316 gfx::Size size = notification_view()->GetPreferredSize();
317 size.Enlarge(0, -notification_view()->GetInsets().height());
318 EXPECT_EQ("360x100", size.ToString());
319
320 // Allow small notifications.
321 custom_view()->set_preferred_size(gfx::Size(10, 10));
322 size = notification_view()->GetPreferredSize();
323 size.Enlarge(0, -notification_view()->GetInsets().height());
324 EXPECT_EQ("360x10", size.ToString());
325
326 // The long notification.
327 custom_view()->set_preferred_size(gfx::Size(1000, 1000));
328 size = notification_view()->GetPreferredSize();
329 size.Enlarge(0, -notification_view()->GetInsets().height());
330 EXPECT_EQ("360x1000", size.ToString());
331 }
332
333 } // namespace message_center
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698