Chromium Code Reviews| 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 <map> | |
| 6 #include <memory> | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/strings/utf_string_conversions.h" | |
| 12 #include "ui/arc/notification/arc_notification_content_view.h" | |
| 13 #include "ui/arc/notification/arc_notification_delegate.h" | |
| 14 #include "ui/arc/notification/arc_notification_item.h" | |
| 15 #include "ui/arc/notification/arc_notification_surface.h" | |
| 16 #include "ui/arc/notification/arc_notification_view.h" | |
| 17 #include "ui/aura/test/test_window_delegate.h" | |
| 18 #include "ui/aura/window.h" | |
| 19 #include "ui/message_center/notification.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/controls/button/image_button.h" | |
| 23 #include "ui/views/test/views_test_base.h" | |
| 24 | |
| 25 namespace arc { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 constexpr char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; | |
| 30 | |
| 31 class MockNotificationSurface : public ArcNotificationSurface { | |
| 32 public: | |
| 33 MockNotificationSurface(const std::string& notification_key, | |
| 34 std::unique_ptr<aura::Window> window) | |
| 35 : notification_key_(notification_key), window_(std::move(window)) {} | |
| 36 | |
| 37 gfx::Size GetSize() const override { return gfx::Size(100, 200); } | |
| 38 | |
| 39 void Attach(views::NativeViewHost* nvh) override { | |
| 40 native_view_host_ = nvh; | |
| 41 nvh->Attach(window_.get()); | |
| 42 } | |
| 43 | |
| 44 void Detach() override { | |
| 45 EXPECT_TRUE(native_view_host_); | |
| 46 EXPECT_EQ(window_.get(), native_view_host_->native_view()); | |
| 47 native_view_host_->Detach(); | |
| 48 native_view_host_ = nullptr; | |
| 49 } | |
| 50 | |
| 51 bool IsAttached() const override { return native_view_host_; } | |
| 52 | |
| 53 aura::Window* GetWindow() const override { return window_.get(); } | |
| 54 aura::Window* GetContentWindow() const override { return window_.get(); } | |
| 55 | |
| 56 const std::string& GetNotificationKey() const override { | |
| 57 return notification_key_; | |
| 58 } | |
| 59 | |
| 60 private: | |
| 61 std::string notification_key_; | |
| 62 std::unique_ptr<aura::Window> window_; | |
| 63 views::NativeViewHost* native_view_host_ = nullptr; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(MockNotificationSurface); | |
| 66 }; | |
| 67 | |
| 68 class TestNotificationSurfaceManager : public ArcNotificationSurfaceManager { | |
| 69 public: | |
| 70 TestNotificationSurfaceManager() = default; | |
| 71 | |
| 72 void PrepareSurface(std::string& notification_key) { | |
| 73 auto surface_window = base::MakeUnique<aura::Window>(&window_delegate_); | |
| 74 surface_window->SetType(aura::client::WINDOW_TYPE_CONTROL); | |
| 75 surface_window->Init(ui::LAYER_NOT_DRAWN); | |
| 76 surface_window->set_owned_by_parent(false); | |
| 77 surface_window->SetBounds(gfx::Rect(0, 0, 100, 200)); | |
| 78 | |
| 79 surface_map_[notification_key] = base::MakeUnique<MockNotificationSurface>( | |
| 80 notification_key, std::move(surface_window)); | |
| 81 } | |
| 82 size_t surface_found_count() const { return surface_found_count_; } | |
| 83 | |
| 84 ArcNotificationSurface* GetArcSurface( | |
| 85 const std::string& notification_key) const override { | |
| 86 auto it = surface_map_.find(notification_key); | |
| 87 if (it != surface_map_.end()) { | |
| 88 ++surface_found_count_; | |
| 89 return it->second.get(); | |
| 90 } | |
| 91 return nullptr; | |
| 92 } | |
| 93 void AddObserver(Observer* observer) override {} | |
| 94 void RemoveObserver(Observer* observer) override {} | |
| 95 | |
| 96 private: | |
| 97 // Mutable for modifying in const method. | |
| 98 mutable int surface_found_count_; | |
|
hidehiko
2017/06/19 10:04:33
Uninitialized memory.
mutable int surface_found_c
yoshiki
2017/06/19 10:07:45
Done.
| |
| 99 | |
| 100 aura::test::TestWindowDelegate window_delegate_; | |
| 101 std::map<std::string, std::unique_ptr<ArcNotificationSurface>> surface_map_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(TestNotificationSurfaceManager); | |
| 104 }; | |
| 105 | |
| 106 } // anonymous namespace | |
| 107 | |
| 108 class MockArcNotificationItem : public ArcNotificationItem { | |
| 109 public: | |
| 110 MockArcNotificationItem(const std::string& notification_key) | |
| 111 : notification_key_(notification_key), | |
| 112 notification_id_(kNotificationIdPrefix + notification_key), | |
| 113 weak_factory_(this) {} | |
| 114 | |
| 115 // Methods for testing. | |
| 116 size_t count_close() { return count_close_; } | |
| 117 base::WeakPtr<MockArcNotificationItem> GetWeakPtr() { | |
| 118 return weak_factory_.GetWeakPtr(); | |
| 119 } | |
| 120 | |
| 121 // Overriding methods for testing. | |
| 122 void Close(bool by_user) override { count_close_++; } | |
| 123 const gfx::ImageSkia& GetSnapshot() const override { return snapshot_; } | |
| 124 const std::string& GetNotificationKey() const override { | |
| 125 return notification_key_; | |
| 126 } | |
| 127 const std::string& GetNotificationId() const override { | |
| 128 return notification_id_; | |
| 129 } | |
| 130 | |
| 131 // Overriding methods for returning dummy data or doing nothing. | |
| 132 void OnClosedFromAndroid() override {} | |
| 133 void Click() override {} | |
| 134 void ToggleExpansion() override {} | |
| 135 void OpenSettings() override {} | |
| 136 void AddObserver(Observer* observer) override {} | |
| 137 void RemoveObserver(Observer* observer) override {} | |
| 138 void IncrementWindowRefCount() override {} | |
| 139 void DecrementWindowRefCount() override {} | |
| 140 bool GetPinned() const override { return false; } | |
| 141 bool IsOpeningSettingsSupported() const override { return true; } | |
| 142 mojom::ArcNotificationExpandState GetExpandState() const override { | |
| 143 return mojom::ArcNotificationExpandState::FIXED_SIZE; | |
| 144 } | |
| 145 mojom::ArcNotificationShownContents GetShownContents() const override { | |
| 146 return mojom::ArcNotificationShownContents::CONTENTS_SHOWN; | |
| 147 } | |
| 148 const base::string16& GetAccessibleName() const override { | |
| 149 return base::EmptyString16(); | |
| 150 }; | |
| 151 void OnUpdatedFromAndroid(mojom::ArcNotificationDataPtr data) override {} | |
| 152 | |
| 153 private: | |
| 154 std::string notification_key_; | |
| 155 std::string notification_id_; | |
| 156 gfx::ImageSkia snapshot_; | |
| 157 size_t count_close_ = 0; | |
| 158 | |
| 159 base::WeakPtrFactory<MockArcNotificationItem> weak_factory_; | |
| 160 | |
| 161 DISALLOW_COPY_AND_ASSIGN(MockArcNotificationItem); | |
| 162 }; | |
| 163 | |
| 164 class TestMessageCenterController | |
| 165 : public message_center::MessageCenterController { | |
| 166 public: | |
| 167 TestMessageCenterController() = default; | |
| 168 | |
| 169 // MessageCenterController | |
| 170 void ClickOnNotification(const std::string& notification_id) override { | |
| 171 // For this test, this method should not be invoked. | |
| 172 NOTREACHED(); | |
| 173 } | |
| 174 | |
| 175 void RemoveNotification(const std::string& notification_id, | |
| 176 bool by_user) override { | |
| 177 removed_ids_.insert(notification_id); | |
| 178 } | |
| 179 | |
| 180 std::unique_ptr<ui::MenuModel> CreateMenuModel( | |
| 181 const message_center::NotifierId& notifier_id, | |
| 182 const base::string16& display_source) override { | |
| 183 // For this test, this method should not be invoked. | |
| 184 NOTREACHED(); | |
| 185 return nullptr; | |
| 186 } | |
| 187 | |
| 188 bool HasClickedListener(const std::string& notification_id) override { | |
| 189 return false; | |
| 190 } | |
| 191 | |
| 192 void ClickOnNotificationButton(const std::string& notification_id, | |
| 193 int button_index) override { | |
| 194 // For this test, this method should not be invoked. | |
| 195 NOTREACHED(); | |
| 196 } | |
| 197 | |
| 198 void ClickOnSettingsButton(const std::string& notification_id) override { | |
| 199 // For this test, this method should not be invoked. | |
| 200 NOTREACHED(); | |
| 201 } | |
| 202 | |
| 203 void UpdateNotificationSize(const std::string& notification_id) override {} | |
| 204 | |
| 205 bool IsRemoved(const std::string& notification_id) const { | |
| 206 return (removed_ids_.find(notification_id) != removed_ids_.end()); | |
| 207 } | |
| 208 | |
| 209 private: | |
| 210 std::set<std::string> removed_ids_; | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController); | |
| 213 }; | |
| 214 | |
| 215 class DummyEvent : public ui::Event { | |
| 216 public: | |
| 217 DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {} | |
| 218 ~DummyEvent() override = default; | |
| 219 }; | |
| 220 | |
| 221 class ArcNotificationContentViewTest : public views::ViewsTestBase { | |
| 222 public: | |
| 223 ArcNotificationContentViewTest() = default; | |
| 224 ~ArcNotificationContentViewTest() override = default; | |
| 225 | |
| 226 void SetUp() override { | |
| 227 views::ViewsTestBase::SetUp(); | |
| 228 | |
| 229 surface_manager_ = base::MakeUnique<TestNotificationSurfaceManager>(); | |
| 230 } | |
| 231 | |
| 232 void TearDown() override { | |
| 233 // Widget and view need to be closed before TearDown() if have been created. | |
| 234 EXPECT_FALSE(wrapper_widget_); | |
| 235 EXPECT_FALSE(notification_view_); | |
| 236 | |
| 237 surface_manager_.reset(); | |
| 238 | |
| 239 views::ViewsTestBase::TearDown(); | |
| 240 } | |
| 241 | |
| 242 void PressCloseButton() { | |
| 243 DummyEvent dummy_event; | |
| 244 views::ImageButton* close_button = | |
| 245 GetArcNotificationContentView()->close_button_.get(); | |
| 246 ASSERT_NE(nullptr, close_button); | |
| 247 GetArcNotificationContentView()->ButtonPressed(close_button, dummy_event); | |
| 248 } | |
| 249 | |
| 250 void CreateAndShowNotificationView( | |
| 251 const message_center::Notification& notification) { | |
| 252 DCHECK(!notification_view_); | |
| 253 | |
| 254 notification_view_.reset(static_cast<ArcNotificationView*>( | |
| 255 message_center::MessageViewFactory::Create(controller(), notification, | |
| 256 true))); | |
| 257 notification_view_->set_owned_by_client(); | |
| 258 views::Widget::InitParams params( | |
| 259 CreateParams(views::Widget::InitParams::TYPE_POPUP)); | |
| 260 | |
| 261 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 262 wrapper_widget_ = base::MakeUnique<views::Widget>(); | |
| 263 wrapper_widget_->Init(params); | |
| 264 wrapper_widget_->SetContentsView(notification_view_.get()); | |
| 265 wrapper_widget_->SetSize(notification_view_->GetPreferredSize()); | |
| 266 } | |
| 267 | |
| 268 void CloseNotificationView() { | |
| 269 wrapper_widget_->Close(); | |
| 270 wrapper_widget_.reset(); | |
| 271 | |
| 272 notification_view_.reset(); | |
| 273 } | |
| 274 | |
| 275 message_center::Notification CreateNotification( | |
| 276 MockArcNotificationItem* notification_item) { | |
| 277 return message_center::Notification( | |
| 278 message_center::NOTIFICATION_TYPE_CUSTOM, | |
| 279 notification_item->GetNotificationId(), base::UTF8ToUTF16("title"), | |
| 280 base::UTF8ToUTF16("message"), gfx::Image(), base::UTF8ToUTF16("arc"), | |
| 281 GURL(), | |
| 282 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 283 "ARC_NOTIFICATION"), | |
| 284 message_center::RichNotificationData(), | |
| 285 new ArcNotificationDelegate(notification_item->GetWeakPtr())); | |
| 286 } | |
| 287 | |
| 288 TestMessageCenterController* controller() { return &controller_; } | |
| 289 TestNotificationSurfaceManager* surface_manager() { | |
| 290 return surface_manager_.get(); | |
| 291 } | |
| 292 views::Widget* widget() { return notification_view_->GetWidget(); } | |
| 293 | |
| 294 ArcNotificationContentView* GetArcNotificationContentView() { | |
| 295 views::View* view = notification_view_->contents_view_; | |
| 296 EXPECT_EQ(ArcNotificationContentView::kViewClassName, view->GetClassName()); | |
| 297 return static_cast<ArcNotificationContentView*>(view); | |
| 298 } | |
| 299 | |
| 300 TestNotificationSurfaceManager* surface_manager() const { | |
| 301 return surface_manager_.get(); | |
| 302 } | |
| 303 | |
| 304 private: | |
| 305 TestMessageCenterController controller_; | |
| 306 std::unique_ptr<TestNotificationSurfaceManager> surface_manager_; | |
| 307 std::unique_ptr<ArcNotificationView> notification_view_; | |
| 308 std::unique_ptr<views::Widget> wrapper_widget_; | |
| 309 | |
| 310 DISALLOW_COPY_AND_ASSIGN(ArcNotificationContentViewTest); | |
| 311 }; | |
| 312 | |
| 313 TEST_F(ArcNotificationContentViewTest, CreateSurfaceAfterNotification) { | |
| 314 std::string notification_key("notification id"); | |
| 315 | |
| 316 auto notification_item = | |
| 317 base::MakeUnique<MockArcNotificationItem>(notification_key); | |
| 318 message_center::Notification notification = | |
| 319 CreateNotification(notification_item.get()); | |
| 320 | |
| 321 surface_manager()->PrepareSurface(notification_key); | |
| 322 | |
| 323 CreateAndShowNotificationView(notification); | |
| 324 CloseNotificationView(); | |
| 325 } | |
| 326 | |
| 327 TEST_F(ArcNotificationContentViewTest, CreateSurfaceBeforeNotification) { | |
| 328 std::string notification_key("notification id"); | |
| 329 | |
| 330 surface_manager()->PrepareSurface(notification_key); | |
| 331 | |
| 332 auto notification_item = | |
| 333 base::MakeUnique<MockArcNotificationItem>(notification_key); | |
| 334 message_center::Notification notification = | |
| 335 CreateNotification(notification_item.get()); | |
| 336 | |
| 337 CreateAndShowNotificationView(notification); | |
| 338 CloseNotificationView(); | |
| 339 } | |
| 340 | |
| 341 TEST_F(ArcNotificationContentViewTest, CreateNotificationWithoutSurface) { | |
| 342 std::string notification_key("notification id"); | |
| 343 | |
| 344 auto notification_item = | |
| 345 base::MakeUnique<MockArcNotificationItem>(notification_key); | |
| 346 message_center::Notification notification = | |
| 347 CreateNotification(notification_item.get()); | |
| 348 | |
| 349 CreateAndShowNotificationView(notification); | |
| 350 CloseNotificationView(); | |
| 351 } | |
| 352 | |
| 353 TEST_F(ArcNotificationContentViewTest, CloseButton) { | |
| 354 std::string notification_key("notification id"); | |
| 355 | |
| 356 auto notification_item = | |
| 357 base::MakeUnique<MockArcNotificationItem>(notification_key); | |
| 358 surface_manager()->PrepareSurface(notification_key); | |
| 359 message_center::Notification notification = | |
| 360 CreateNotification(notification_item.get()); | |
| 361 CreateAndShowNotificationView(notification); | |
| 362 | |
| 363 EXPECT_EQ(1u, surface_manager()->surface_found_count()); | |
| 364 EXPECT_FALSE(controller()->IsRemoved(notification_item->GetNotificationId())); | |
| 365 PressCloseButton(); | |
| 366 EXPECT_TRUE(controller()->IsRemoved(notification_item->GetNotificationId())); | |
| 367 | |
| 368 CloseNotificationView(); | |
| 369 } | |
| 370 | |
| 371 } // namespace arc | |
| OLD | NEW |