Index: ui/arc/notification/arc_custom_notification_view_unittest.cc |
diff --git a/ui/arc/notification/arc_custom_notification_view_unittest.cc b/ui/arc/notification/arc_custom_notification_view_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b4cfd0f601fbe894f6d382aaa445e4bf2f4cbadf |
--- /dev/null |
+++ b/ui/arc/notification/arc_custom_notification_view_unittest.cc |
@@ -0,0 +1,236 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/arc/notification/arc_custom_notification_view.h" |
+ |
+#include <memory> |
+ |
+#include "base/strings/utf_string_conversions.h" |
+#include "components/exo/notification_surface.h" |
+#include "ui/arc/notification/arc_notification_delegate.h" |
+#include "ui/arc/notification/arc_notification_item.h" |
+#include "ui/message_center/notification.h" |
+#include "ui/message_center/views/custom_notification_view.h" |
+#include "ui/message_center/views/message_center_controller.h" |
+#include "ui/message_center/views/message_view_factory.h" |
+#include "ui/views/controls/button/image_button.h" |
+#include "ui/views/test/views_test_base.h" |
+ |
+namespace arc { |
+ |
+namespace { |
+ |
+class MockNotificationSurface : public exo::NotificationSurface { |
hidehiko
2017/03/02 15:38:16
Optional: Mock implies gmock in Chromium, IIUC.
Ho
|
+ public: |
+ MockNotificationSurface(const std::string& notification_id, |
+ aura::Window* window) |
+ : notification_id_(notification_id), window_(window) {} |
+ |
+ gfx::Size GetSize() const override { return gfx::Size(100, 200); } |
+ |
+ void AttachWindow(views::NativeViewHost* nvh) override {} |
+ |
+ aura::Window* window() override { return window_; } |
+ |
+ const std::string& notification_id() const override { |
+ return notification_id_; |
+ } |
+ |
+ private: |
+ std::string notification_id_; |
+ aura::Window* window_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockNotificationSurface); |
+}; |
+ |
+} // anonymous namespace |
+ |
+class MockArcNotificationItem : public ArcNotificationItem { |
hidehiko
2017/03/02 15:38:16
Moving to anonymous namespace, too?
Also how abou
|
+ public: |
+ MockArcNotificationItem(const std::string& notification_key) |
+ : notification_key_(notification_key), weak_factory_(this) {} |
+ |
+ void UpdateWithArcNotificationData( |
+ mojom::ArcNotificationDataPtr data) override {} |
hidehiko
2017/03/02 15:38:16
s/{}/= default;/
|
+ |
+ void OnClosedFromAndroid() override {} |
+ |
+ void Close(bool by_user) override {} |
+ |
+ void CloseFromCloseButton() override { count_close_from_close_button_++; } |
+ size_t count_close_from_close_button() { |
+ return count_close_from_close_button_; |
+ } |
+ |
+ void OpenSettings() override {} |
+ |
+ void AddObserver(Observer* observer) override {} |
+ void RemoveObserver(Observer* observer) override {} |
+ |
+ void IncrementWindowRefCount() override {} |
+ void DecrementWindowRefCount() override {} |
+ bool pinned() const override { return false; } |
+ const gfx::ImageSkia& snapshot() const override { return snapshot_; } |
+ |
+ const std::string& notification_key() const override { |
+ return notification_key_; |
+ } |
+ |
+ base::WeakPtr<MockArcNotificationItem> GetWeakPtr() { |
+ return weak_factory_.GetWeakPtr(); |
+ } |
+ |
+ private: |
+ std::string notification_key_; |
+ gfx::ImageSkia snapshot_; |
+ size_t count_close_from_close_button_ = 0; |
+ |
+ base::WeakPtrFactory<MockArcNotificationItem> weak_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockArcNotificationItem); |
+}; |
+ |
+class TestMessageCenterController |
+ : public message_center::MessageCenterController { |
+ public: |
+ TestMessageCenterController() {} |
hidehiko
2017/03/02 15:38:16
s/{}/= default;/
|
+ |
+ // MessageCenterController |
+ void ClickOnNotification(const std::string& notification_id) override { |
+ // For this test, this method should not be invoked. |
+ NOTREACHED(); |
+ } |
+ |
+ void RemoveNotification(const std::string& notification_id, |
+ bool by_user) override { |
+ removed_ids_.insert(notification_id); |
+ } |
+ |
+ std::unique_ptr<ui::MenuModel> CreateMenuModel( |
+ const message_center::NotifierId& notifier_id, |
+ const base::string16& display_source) override { |
+ // For this test, this method should not be invoked. |
+ NOTREACHED(); |
+ return nullptr; |
+ } |
+ |
+ bool HasClickedListener(const std::string& notification_id) override { |
+ return false; |
+ } |
+ |
+ void ClickOnNotificationButton(const std::string& notification_id, |
+ int button_index) override { |
+ // For this test, this method should not be invoked. |
+ NOTREACHED(); |
+ } |
+ |
+ void ClickOnSettingsButton(const std::string& notification_id) override { |
+ // For this test, this method should not be invoked. |
+ NOTREACHED(); |
+ } |
+ |
+ void UpdateNotificationSize(const std::string& notification_id) override {} |
+ |
+ bool IsRemoved(const std::string& notification_id) const { |
+ return (removed_ids_.find(notification_id) != removed_ids_.end()); |
+ } |
+ |
+ private: |
+ std::set<std::string> removed_ids_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController); |
+}; |
+ |
+class DummyEvent : public ui::Event { |
+ public: |
+ DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {} |
+ ~DummyEvent() override {} |
hidehiko
2017/03/02 15:38:16
s/{}/= default;/
Or, you may be able to drop it si
|
+}; |
+ |
+class ArcCustomNotificationViewTest : public views::ViewsTestBase { |
+ public: |
+ ArcCustomNotificationViewTest() {} |
hidehiko
2017/03/02 15:38:16
ditto.
|
+ ~ArcCustomNotificationViewTest() override {} |
+ |
+ void SetUp() override { |
+ std::string notification_id("notification id"); |
hidehiko
2017/03/02 15:38:16
Optional: in Chromium, std::string foo = "string";
|
+ |
+ views::ViewsTestBase::SetUp(); |
hidehiko
2017/03/02 15:38:16
Could you do this at beginning of SetUp()?
|
+ notification_item_.reset(new MockArcNotificationItem(notification_id)); |
hidehiko
2017/03/02 15:38:16
Optional: I recommend to use notification_item_ =
|
+ |
+ notification_delegate_ = |
+ new ArcNotificationDelegate(notification_item_->GetWeakPtr()); |
+ |
+ notification_.reset(new message_center::Notification( |
+ message_center::NOTIFICATION_TYPE_CUSTOM, notification_id, |
+ base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message"), gfx::Image(), |
+ base::UTF8ToUTF16("display source"), GURL(), |
+ message_center::NotifierId(message_center::NotifierId::APPLICATION, |
+ "extension_id"), |
+ message_center::RichNotificationData(), notification_delegate_.get())); |
+ |
+ notification_view_.reset( |
+ static_cast<message_center::CustomNotificationView*>( |
+ message_center::MessageViewFactory::Create(controller(), |
+ *notification_, true))); |
+ notification_view_->set_owned_by_client(); |
+ |
+ views::Widget::InitParams init_params( |
+ CreateParams(views::Widget::InitParams::TYPE_POPUP)); |
+ views::Widget* widget = new views::Widget(); |
hidehiko
2017/03/02 15:38:16
Looks leak?
|
+ widget->Init(init_params); |
+ |
+ surface_ = |
+ new MockNotificationSurface(notification_id, widget->GetNativeWindow()); |
+ GetArcNotificationView()->OnNotificationSurfaceAdded(surface_); |
+ |
+ widget->SetContentsView(notification_view_.get()); |
+ widget->SetSize(notification_view_->GetPreferredSize()); |
+ } |
+ |
+ void TearDown() override { |
+ GetArcNotificationView()->OnNotificationSurfaceRemoved(surface_); |
+ widget()->Close(); |
+ notification_view_.reset(); |
+ views::ViewsTestBase::TearDown(); |
+ } |
+ |
+ void PressCloseButton() { |
+ DummyEvent dummy_event; |
+ views::ImageButton* close_button = GetArcNotificationView()->close_button_; |
+ ASSERT_NE(nullptr, close_button); |
+ GetArcNotificationView()->ButtonPressed(close_button, dummy_event); |
+ } |
+ |
+ TestMessageCenterController* controller() { return &controller_; } |
+ views::Widget* widget() { return notification_view_->GetWidget(); } |
+ |
+ ArcCustomNotificationView* GetArcNotificationView() { |
+ views::View* view = notification_view_->GetContentsViewForTesting(); |
+ EXPECT_EQ(ArcCustomNotificationView::kViewClassName, view->GetClassName()); |
+ return static_cast<ArcCustomNotificationView*>(view); |
+ } |
+ |
+ MockArcNotificationItem* notification_item() const { |
+ return notification_item_.get(); |
+ } |
+ |
+ private: |
+ MockNotificationSurface* surface_; |
+ TestMessageCenterController controller_; |
+ scoped_refptr<ArcNotificationDelegate> notification_delegate_; |
+ std::unique_ptr<message_center::Notification> notification_; |
+ std::unique_ptr<message_center::CustomNotificationView> notification_view_; |
+ std::unique_ptr<MockArcNotificationItem> notification_item_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ArcCustomNotificationViewTest); |
+}; |
+ |
+TEST_F(ArcCustomNotificationViewTest, CloseButton) { |
+ EXPECT_EQ(0u, notification_item()->count_close_from_close_button()); |
+ PressCloseButton(); |
+ EXPECT_EQ(1u, notification_item()->count_close_from_close_button()); |
+} |
+ |
+} // namespace arc |