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

Side by Side Diff: ui/arc/notification/arc_custom_notification_view_unittest.cc

Issue 2723143002: Add unittests of ArcCustomNotificationView (Closed)
Patch Set: wip Created 3 years, 6 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
« no previous file with comments | « ui/arc/BUILD.gn ('k') | ui/arc/notification/arc_notification_content_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <memory>
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/arc/notification/arc_notification_content_view.h"
9 #include "ui/arc/notification/arc_notification_delegate.h"
10 #include "ui/arc/notification/arc_notification_item.h"
11 #include "ui/arc/notification/arc_notification_surface.h"
12 #include "ui/arc/notification/arc_notification_view.h"
13 #include "ui/message_center/notification.h"
14 #include "ui/message_center/views/message_center_controller.h"
15 #include "ui/message_center/views/message_view_factory.h"
16 #include "ui/views/controls/button/image_button.h"
17 #include "ui/views/test/views_test_base.h"
18
19 namespace arc {
20
21 namespace {
22
23 class MockNotificationSurface : public ArcNotificationSurface {
24 public:
25 MockNotificationSurface(const std::string& notification_id,
26 aura::Window* window)
27 : notification_id_(notification_id), window_(window) {}
28
29 gfx::Size GetSize() const override { return gfx::Size(100, 200); }
30
31 void AttachWindow(views::NativeViewHost* nvh) override {}
32
33 aura::Window* GetWindow() const override { return window_; }
34
35 const std::string& GetNotificationId() const override {
36 return notification_id_;
37 }
38
39 private:
40 std::string notification_id_;
41 aura::Window* window_;
42
43 DISALLOW_COPY_AND_ASSIGN(MockNotificationSurface);
44 };
45
46 } // anonymous namespace
47
48 class MockArcNotificationItem : public ArcNotificationItem {
49 public:
50 MockArcNotificationItem(const std::string& notification_id)
51 : notification_id_(notification_id), weak_factory_(this) {}
52
53 void OnUpdatedFromAndroid(mojom::ArcNotificationDataPtr data) override {}
54
55 void OnClosedFromAndroid() override {}
56
57 void Close(bool by_user) override { count_close_++; }
58 size_t count_close() { return count_close_; }
59
60 void Click() override {}
61 void ToggleExpansion() override {}
62 bool IsOpeningSettingsSupported() const override { return true; }
63 mojom::ArcNotificationExpandState GetExpandState() const override {
64 return mojom::ArcNotificationExpandState::FIXED_SIZE;
65 }
66 mojom::ArcNotificationShownContents GetShownContents() const override {
67 return mojom::ArcNotificationShownContents::CONTENTS_SHOWN;
68 }
69
70 const base::string16& GetAccessibleName() const override {
71 return base::EmptyString16();
72 };
73 void OpenSettings() override {}
74
75 void AddObserver(Observer* observer) override {}
76 void RemoveObserver(Observer* observer) override {}
77
78 void IncrementWindowRefCount() override {}
79 void DecrementWindowRefCount() override {}
80 bool GetPinned() const override { return false; }
81 const gfx::ImageSkia& GetSnapshot() const override { return snapshot_; }
82
83 const std::string& GetNotificationKey() const override {
84 return notification_key_;
85 }
86 const std::string& GetNotificationId() const override {
87 return notification_id_;
88 }
89
90 base::WeakPtr<MockArcNotificationItem> GetWeakPtr() {
91 return weak_factory_.GetWeakPtr();
92 }
93
94 private:
95 std::string notification_id_;
96 std::string notification_key_;
97 gfx::ImageSkia snapshot_;
98 size_t count_close_ = 0;
99
100 base::WeakPtrFactory<MockArcNotificationItem> weak_factory_;
101
102 DISALLOW_COPY_AND_ASSIGN(MockArcNotificationItem);
103 };
104
105 class TestMessageCenterController
106 : public message_center::MessageCenterController {
107 public:
108 TestMessageCenterController() {}
109
110 // MessageCenterController
111 void ClickOnNotification(const std::string& notification_id) override {
112 // For this test, this method should not be invoked.
113 NOTREACHED();
114 }
115
116 void RemoveNotification(const std::string& notification_id,
117 bool by_user) override {
118 removed_ids_.insert(notification_id);
119 }
120
121 std::unique_ptr<ui::MenuModel> CreateMenuModel(
122 const message_center::NotifierId& notifier_id,
123 const base::string16& display_source) override {
124 // For this test, this method should not be invoked.
125 NOTREACHED();
126 return nullptr;
127 }
128
129 bool HasClickedListener(const std::string& notification_id) override {
130 return false;
131 }
132
133 void ClickOnNotificationButton(const std::string& notification_id,
134 int button_index) override {
135 // For this test, this method should not be invoked.
136 NOTREACHED();
137 }
138
139 void ClickOnSettingsButton(const std::string& notification_id) override {
140 // For this test, this method should not be invoked.
141 NOTREACHED();
142 }
143
144 void UpdateNotificationSize(const std::string& notification_id) override {}
145
146 bool IsRemoved(const std::string& notification_id) const {
147 return (removed_ids_.find(notification_id) != removed_ids_.end());
148 }
149
150 private:
151 std::set<std::string> removed_ids_;
152
153 DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController);
154 };
155
156 class DummyEvent : public ui::Event {
157 public:
158 DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {}
159 ~DummyEvent() override {}
160 };
161
162 class ArcNotificationContentViewTest : public views::ViewsTestBase {
163 public:
164 ArcNotificationContentViewTest() {}
165 ~ArcNotificationContentViewTest() override {}
166
167 void SetUp() override {
168 std::string notification_id("notification id");
169
170 views::ViewsTestBase::SetUp();
171 notification_item_.reset(new MockArcNotificationItem(notification_id));
172
173 notification_delegate_ =
174 new ArcNotificationDelegate(notification_item_->GetWeakPtr());
175
176 notification_.reset(new message_center::Notification(
177 message_center::NOTIFICATION_TYPE_CUSTOM, notification_id,
178 base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message"), gfx::Image(),
179 base::UTF8ToUTF16("display source"), GURL(),
180 message_center::NotifierId(message_center::NotifierId::APPLICATION,
181 "extension_id"),
182 message_center::RichNotificationData(), notification_delegate_.get()));
183
184 notification_view_.reset(static_cast<ArcNotificationView*>(
185 message_center::MessageViewFactory::Create(controller(), *notification_,
186 true)));
187 notification_view_->set_owned_by_client();
188
189 views::Widget::InitParams init_params(
190 CreateParams(views::Widget::InitParams::TYPE_POPUP));
191 views::Widget* widget = new views::Widget();
192 widget->Init(init_params);
193
194 surface_ =
195 new MockNotificationSurface(notification_id, widget->GetNativeWindow());
196 GetArcNotificationContentView()->OnNotificationSurfaceAdded(surface_);
197
198 widget->SetContentsView(notification_view_.get());
199 widget->SetSize(notification_view_->GetPreferredSize());
200 }
201
202 void TearDown() override {
203 GetArcNotificationContentView()->OnNotificationSurfaceRemoved(surface_);
204 widget()->Close();
205 notification_view_.reset();
206 views::ViewsTestBase::TearDown();
207 }
208
209 void PressCloseButton() {
210 DummyEvent dummy_event;
211 views::ImageButton* close_button =
212 GetArcNotificationContentView()->close_button_.get();
213 ASSERT_NE(nullptr, close_button);
214 GetArcNotificationContentView()->ButtonPressed(close_button, dummy_event);
215 }
216
217 TestMessageCenterController* controller() { return &controller_; }
218 views::Widget* widget() { return notification_view_->GetWidget(); }
219
220 ArcNotificationContentView* GetArcNotificationContentView() {
221 views::View* view = notification_view_->contents_view_;
222 EXPECT_EQ(ArcNotificationContentView::kViewClassName, view->GetClassName());
223 return static_cast<ArcNotificationContentView*>(view);
224 }
225
226 MockArcNotificationItem* notification_item() const {
227 return notification_item_.get();
228 }
229
230 private:
231 MockNotificationSurface* surface_;
232 TestMessageCenterController controller_;
233 scoped_refptr<ArcNotificationDelegate> notification_delegate_;
234 std::unique_ptr<message_center::Notification> notification_;
235 std::unique_ptr<ArcNotificationView> notification_view_;
236 std::unique_ptr<MockArcNotificationItem> notification_item_;
237
238 DISALLOW_COPY_AND_ASSIGN(ArcNotificationContentViewTest);
239 };
240
241 TEST_F(ArcNotificationContentViewTest, CloseButton) {
242 EXPECT_EQ(0u, notification_item()->count_close());
243 PressCloseButton();
244 EXPECT_EQ(1u, notification_item()->count_close());
245 }
246
247 } // namespace arc
OLDNEW
« no previous file with comments | « ui/arc/BUILD.gn ('k') | ui/arc/notification/arc_notification_content_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698