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

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

Issue 2935893004: Add unittest for ArcNotificationContentView (Closed)
Patch Set: Rebased 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
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/aura/test/test_window_delegate.h"
14 #include "ui/aura/window.h"
15 #include "ui/message_center/notification.h"
16 #include "ui/message_center/views/message_center_controller.h"
17 #include "ui/message_center/views/message_view_factory.h"
18 #include "ui/views/controls/button/image_button.h"
19 #include "ui/views/test/views_test_base.h"
20
21 namespace arc {
22
23 namespace {
24
25 constexpr char kNotificationIdPrefix[] = "ARC_NOTIFICATION_";
26
27 class MockNotificationSurface : public ArcNotificationSurface {
28 public:
29 MockNotificationSurface(const std::string& notification_key,
30 std::unique_ptr<aura::Window> window)
31 : notification_key_(notification_key), window_(std::move(window)) {}
hidehiko 2017/06/15 15:26:20 #include <utility> ?
yoshiki 2017/06/16 11:29:08 Done.
32
33 gfx::Size GetSize() const override { return gfx::Size(100, 200); }
34
35 void Attach(views::NativeViewHost* nvh) override {
36 native_view_host_ = nvh;
37 nvh->Attach(window_.get());
38 }
39
40 void Detach() override {
41 EXPECT_TRUE(native_view_host_);
42 EXPECT_EQ(window_.get(), native_view_host_->native_view());
43 native_view_host_->Detach();
44 native_view_host_ = nullptr;
45 }
46
47 bool IsAttached() const override { return native_view_host_; }
48
49 aura::Window* GetWindow() const override { return window_.get(); }
50 aura::Window* GetContentWindow() const override { return window_.get(); }
51
52 const std::string& GetNotificationKey() const override {
53 return notification_key_;
54 }
55
56 private:
57 std::string notification_key_;
hidehiko 2017/06/15 15:26:20 #include <string> ?
yoshiki 2017/06/16 11:29:08 Done.
58 std::unique_ptr<aura::Window> window_;
59 views::NativeViewHost* native_view_host_ = nullptr;
60
61 DISALLOW_COPY_AND_ASSIGN(MockNotificationSurface);
62 };
63
64 class TestNotificationSurfaceManager : public ArcNotificationSurfaceManager {
65 public:
66 TestNotificationSurfaceManager() = default;
67
68 void PrepareSurface(std::string& notification_key) {
69 auto surface_window = base::MakeUnique<aura::Window>(&window_delegate_);
70 surface_window->SetType(aura::client::WINDOW_TYPE_CONTROL);
71 surface_window->Init(ui::LAYER_NOT_DRAWN);
72 surface_window->set_owned_by_parent(false);
73 surface_window->SetBounds(gfx::Rect(0, 0, 100, 200));
74
75 surface_map_[notification_key] = base::MakeUnique<MockNotificationSurface>(
76 notification_key, std::move(surface_window));
77 }
78 size_t surface_found_count() const { return surface_found_count_; }
79
80 ArcNotificationSurface* GetArcSurface(
81 const std::string& notification_key) const override {
82 auto it = surface_map_.find(notification_key);
83 if (it != surface_map_.end()) {
84 ++surface_found_count_;
85 return it->second.get();
86 }
87 return nullptr;
88 }
89 void AddObserver(Observer* observer) override {}
90 void RemoveObserver(Observer* observer) override {}
91
92 private:
93 aura::test::TestWindowDelegate window_delegate_;
94 std::map<std::string, std::unique_ptr<ArcNotificationSurface>> surface_map_;
hidehiko 2017/06/15 15:26:20 #include <map> ?
yoshiki 2017/06/16 11:29:08 Done.
95 static int surface_found_count_;
hidehiko 2017/06/15 15:26:20 Please make this non-static.
yoshiki 2017/06/16 11:29:08 This needs to be static, since this property is mo
96
97 DISALLOW_COPY_AND_ASSIGN(TestNotificationSurfaceManager);
98 };
99
100 int TestNotificationSurfaceManager::surface_found_count_ = 0;
101
102 } // anonymous namespace
103
104 class MockArcNotificationItem : public ArcNotificationItem {
105 public:
106 MockArcNotificationItem(const std::string& notification_key)
107 : notification_key_(notification_key),
108 notification_id_(kNotificationIdPrefix + notification_key),
109 weak_factory_(this) {}
110
111 // Methods for testing.
112 size_t count_close() { return count_close_; }
113 base::WeakPtr<MockArcNotificationItem> GetWeakPtr() {
114 return weak_factory_.GetWeakPtr();
115 }
116
117 // Overriding methods for testing.
118 void Close(bool by_user) override { count_close_++; }
119 const gfx::ImageSkia& GetSnapshot() const override { return snapshot_; }
120 const std::string& GetNotificationKey() const override {
121 return notification_key_;
122 }
123 const std::string& GetNotificationId() const override {
124 return notification_id_;
125 }
126
127 // Overriding methods for returning dummy data or doing nothing.
128 void OnClosedFromAndroid() override {}
129 void Click() override {}
130 void ToggleExpansion() override {}
131 void OpenSettings() override {}
132 void AddObserver(Observer* observer) override {}
133 void RemoveObserver(Observer* observer) override {}
134 void IncrementWindowRefCount() override {}
135 void DecrementWindowRefCount() override {}
136 bool GetPinned() const override { return false; }
137 bool IsOpeningSettingsSupported() const override { return true; }
138 mojom::ArcNotificationExpandState GetExpandState() const override {
139 return mojom::ArcNotificationExpandState::FIXED_SIZE;
140 }
141 mojom::ArcNotificationShownContents GetShownContents() const override {
142 return mojom::ArcNotificationShownContents::CONTENTS_SHOWN;
143 }
144 const base::string16& GetAccessibleName() const override {
145 return base::EmptyString16();
146 };
147 void OnUpdatedFromAndroid(mojom::ArcNotificationDataPtr data) override {}
148
149 private:
150 std::string notification_key_;
151 std::string notification_id_;
152 gfx::ImageSkia snapshot_;
153 size_t count_close_ = 0;
154
155 base::WeakPtrFactory<MockArcNotificationItem> weak_factory_;
156
157 DISALLOW_COPY_AND_ASSIGN(MockArcNotificationItem);
158 };
159
160 class TestMessageCenterController
161 : public message_center::MessageCenterController {
162 public:
163 TestMessageCenterController() {}
164
165 // MessageCenterController
166 void ClickOnNotification(const std::string& notification_id) override {
167 // For this test, this method should not be invoked.
168 NOTREACHED();
169 }
170
171 void RemoveNotification(const std::string& notification_id,
172 bool by_user) override {
173 removed_ids_.insert(notification_id);
174 }
175
176 std::unique_ptr<ui::MenuModel> CreateMenuModel(
177 const message_center::NotifierId& notifier_id,
178 const base::string16& display_source) override {
179 // For this test, this method should not be invoked.
180 NOTREACHED();
181 return nullptr;
182 }
183
184 bool HasClickedListener(const std::string& notification_id) override {
185 return false;
186 }
187
188 void ClickOnNotificationButton(const std::string& notification_id,
189 int button_index) override {
190 // For this test, this method should not be invoked.
191 NOTREACHED();
192 }
193
194 void ClickOnSettingsButton(const std::string& notification_id) override {
195 // For this test, this method should not be invoked.
196 NOTREACHED();
197 }
198
199 void UpdateNotificationSize(const std::string& notification_id) override {}
200
201 bool IsRemoved(const std::string& notification_id) const {
202 return (removed_ids_.find(notification_id) != removed_ids_.end());
203 }
204
205 private:
206 std::set<std::string> removed_ids_;
hidehiko 2017/06/15 15:26:20 #include <set> ?
yoshiki 2017/06/16 11:29:08 Done.
207
208 DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController);
209 };
210
211 class DummyEvent : public ui::Event {
212 public:
213 DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {}
214 ~DummyEvent() override {}
215 };
216
217 class ArcNotificationContentViewTest : public views::ViewsTestBase {
218 public:
219 ArcNotificationContentViewTest() {}
220 ~ArcNotificationContentViewTest() override {}
221
222 void SetUp() override {
223 std::string notification_key("notification id");
224
225 surface_manager_.reset(new TestNotificationSurfaceManager());
hidehiko 2017/06/15 15:26:20 nit: please use base::MakeUnique<>. Ditto for bel
yoshiki 2017/06/16 11:29:08 Thanks. I added some test and updated around here.
226
227 views::ViewsTestBase::SetUp();
hidehiko 2017/06/15 15:26:20 nit: in general, parent's SetUp() should be called
yoshiki 2017/06/16 11:29:08 Done.
228 notification_item_.reset(new MockArcNotificationItem(notification_key));
229
230 notification_delegate_ =
231 new ArcNotificationDelegate(notification_item_->GetWeakPtr());
232
233 surface_manager_->PrepareSurface(notification_key);
234
235 notification_.reset(new message_center::Notification(
236 message_center::NOTIFICATION_TYPE_CUSTOM,
237 notification_item_->GetNotificationId(), base::UTF8ToUTF16("title"),
238 base::UTF8ToUTF16("message"), gfx::Image(),
239 base::UTF8ToUTF16("display source"), GURL(),
240 message_center::NotifierId(message_center::NotifierId::APPLICATION,
241 "extension_id"),
242 message_center::RichNotificationData(), notification_delegate_.get()));
243
244 notification_view_.reset(static_cast<ArcNotificationView*>(
245 message_center::MessageViewFactory::Create(controller(), *notification_,
246 true)));
247 notification_view_->set_owned_by_client();
248
249 views::Widget::InitParams params(
250 CreateParams(views::Widget::InitParams::TYPE_POPUP));
251 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
252 wrapper_widget_ = base::MakeUnique<views::Widget>();
253 wrapper_widget_->Init(params);
254 wrapper_widget_->SetContentsView(notification_view_.get());
255 wrapper_widget_->SetSize(notification_view_->GetPreferredSize());
256 }
257
258 void TearDown() override {
259 wrapper_widget_->Close();
260 wrapper_widget_.reset();
261 notification_view_.reset();
hidehiko 2017/06/15 15:26:20 nit: notificatrion_item_.reset() and notification_
262 surface_manager_.reset();
263 views::ViewsTestBase::TearDown();
264 }
265
266 void PressCloseButton() {
267 DummyEvent dummy_event;
268 views::ImageButton* close_button =
269 GetArcNotificationContentView()->close_button_.get();
270 ASSERT_NE(nullptr, close_button);
271 GetArcNotificationContentView()->ButtonPressed(close_button, dummy_event);
272 }
273
274 TestMessageCenterController* controller() { return &controller_; }
275 TestNotificationSurfaceManager* surface_manager() {
276 return surface_manager_.get();
277 }
278 views::Widget* widget() { return notification_view_->GetWidget(); }
279
280 ArcNotificationContentView* GetArcNotificationContentView() {
281 views::View* view = notification_view_->contents_view_;
282 EXPECT_EQ(ArcNotificationContentView::kViewClassName, view->GetClassName());
283 return static_cast<ArcNotificationContentView*>(view);
284 }
285
286 MockArcNotificationItem* notification_item() const {
287 return notification_item_.get();
288 }
289
290 private:
291 TestMessageCenterController controller_;
292 scoped_refptr<ArcNotificationDelegate> notification_delegate_;
hidehiko 2017/06/15 15:26:20 nit: Could you sort in the order of initialization
yoshiki 2017/06/16 11:29:08 Done.
293 std::unique_ptr<message_center::Notification> notification_;
294 std::unique_ptr<ArcNotificationView> notification_view_;
295 std::unique_ptr<MockArcNotificationItem> notification_item_;
296 std::unique_ptr<TestNotificationSurfaceManager> surface_manager_;
297 std::unique_ptr<views::Widget> wrapper_widget_;
298
299 DISALLOW_COPY_AND_ASSIGN(ArcNotificationContentViewTest);
300 };
301
302 TEST_F(ArcNotificationContentViewTest, CloseButton) {
303 EXPECT_EQ(1u, surface_manager()->surface_found_count());
304 EXPECT_FALSE(
305 controller()->IsRemoved(notification_item()->GetNotificationId()));
306 PressCloseButton();
307 EXPECT_TRUE(
308 controller()->IsRemoved(notification_item()->GetNotificationId()));
309 }
310
311 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698