Chromium Code Reviews| Index: ui/arc/notification/arc_notification_content_view_unittest.cc |
| diff --git a/ui/arc/notification/arc_notification_content_view_unittest.cc b/ui/arc/notification/arc_notification_content_view_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..32f25bf63ba13652b9e55908b3d90a6007667845 |
| --- /dev/null |
| +++ b/ui/arc/notification/arc_notification_content_view_unittest.cc |
| @@ -0,0 +1,311 @@ |
| +// 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 <memory> |
| + |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "ui/arc/notification/arc_notification_content_view.h" |
| +#include "ui/arc/notification/arc_notification_delegate.h" |
| +#include "ui/arc/notification/arc_notification_item.h" |
| +#include "ui/arc/notification/arc_notification_surface.h" |
| +#include "ui/arc/notification/arc_notification_view.h" |
| +#include "ui/aura/test/test_window_delegate.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/message_center/notification.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 { |
| + |
| +constexpr char kNotificationIdPrefix[] = "ARC_NOTIFICATION_"; |
| + |
| +class MockNotificationSurface : public ArcNotificationSurface { |
| + public: |
| + MockNotificationSurface(const std::string& notification_key, |
| + std::unique_ptr<aura::Window> window) |
| + : 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.
|
| + |
| + gfx::Size GetSize() const override { return gfx::Size(100, 200); } |
| + |
| + void Attach(views::NativeViewHost* nvh) override { |
| + native_view_host_ = nvh; |
| + nvh->Attach(window_.get()); |
| + } |
| + |
| + void Detach() override { |
| + EXPECT_TRUE(native_view_host_); |
| + EXPECT_EQ(window_.get(), native_view_host_->native_view()); |
| + native_view_host_->Detach(); |
| + native_view_host_ = nullptr; |
| + } |
| + |
| + bool IsAttached() const override { return native_view_host_; } |
| + |
| + aura::Window* GetWindow() const override { return window_.get(); } |
| + aura::Window* GetContentWindow() const override { return window_.get(); } |
| + |
| + const std::string& GetNotificationKey() const override { |
| + return notification_key_; |
| + } |
| + |
| + private: |
| + std::string notification_key_; |
|
hidehiko
2017/06/15 15:26:20
#include <string> ?
yoshiki
2017/06/16 11:29:08
Done.
|
| + std::unique_ptr<aura::Window> window_; |
| + views::NativeViewHost* native_view_host_ = nullptr; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockNotificationSurface); |
| +}; |
| + |
| +class TestNotificationSurfaceManager : public ArcNotificationSurfaceManager { |
| + public: |
| + TestNotificationSurfaceManager() = default; |
| + |
| + void PrepareSurface(std::string& notification_key) { |
| + auto surface_window = base::MakeUnique<aura::Window>(&window_delegate_); |
| + surface_window->SetType(aura::client::WINDOW_TYPE_CONTROL); |
| + surface_window->Init(ui::LAYER_NOT_DRAWN); |
| + surface_window->set_owned_by_parent(false); |
| + surface_window->SetBounds(gfx::Rect(0, 0, 100, 200)); |
| + |
| + surface_map_[notification_key] = base::MakeUnique<MockNotificationSurface>( |
| + notification_key, std::move(surface_window)); |
| + } |
| + size_t surface_found_count() const { return surface_found_count_; } |
| + |
| + ArcNotificationSurface* GetArcSurface( |
| + const std::string& notification_key) const override { |
| + auto it = surface_map_.find(notification_key); |
| + if (it != surface_map_.end()) { |
| + ++surface_found_count_; |
| + return it->second.get(); |
| + } |
| + return nullptr; |
| + } |
| + void AddObserver(Observer* observer) override {} |
| + void RemoveObserver(Observer* observer) override {} |
| + |
| + private: |
| + aura::test::TestWindowDelegate window_delegate_; |
| + 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.
|
| + 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
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestNotificationSurfaceManager); |
| +}; |
| + |
| +int TestNotificationSurfaceManager::surface_found_count_ = 0; |
| + |
| +} // anonymous namespace |
| + |
| +class MockArcNotificationItem : public ArcNotificationItem { |
| + public: |
| + MockArcNotificationItem(const std::string& notification_key) |
| + : notification_key_(notification_key), |
| + notification_id_(kNotificationIdPrefix + notification_key), |
| + weak_factory_(this) {} |
| + |
| + // Methods for testing. |
| + size_t count_close() { return count_close_; } |
| + base::WeakPtr<MockArcNotificationItem> GetWeakPtr() { |
| + return weak_factory_.GetWeakPtr(); |
| + } |
| + |
| + // Overriding methods for testing. |
| + void Close(bool by_user) override { count_close_++; } |
| + const gfx::ImageSkia& GetSnapshot() const override { return snapshot_; } |
| + const std::string& GetNotificationKey() const override { |
| + return notification_key_; |
| + } |
| + const std::string& GetNotificationId() const override { |
| + return notification_id_; |
| + } |
| + |
| + // Overriding methods for returning dummy data or doing nothing. |
| + void OnClosedFromAndroid() override {} |
| + void Click() override {} |
| + void ToggleExpansion() override {} |
| + void OpenSettings() override {} |
| + void AddObserver(Observer* observer) override {} |
| + void RemoveObserver(Observer* observer) override {} |
| + void IncrementWindowRefCount() override {} |
| + void DecrementWindowRefCount() override {} |
| + bool GetPinned() const override { return false; } |
| + bool IsOpeningSettingsSupported() const override { return true; } |
| + mojom::ArcNotificationExpandState GetExpandState() const override { |
| + return mojom::ArcNotificationExpandState::FIXED_SIZE; |
| + } |
| + mojom::ArcNotificationShownContents GetShownContents() const override { |
| + return mojom::ArcNotificationShownContents::CONTENTS_SHOWN; |
| + } |
| + const base::string16& GetAccessibleName() const override { |
| + return base::EmptyString16(); |
| + }; |
| + void OnUpdatedFromAndroid(mojom::ArcNotificationDataPtr data) override {} |
| + |
| + private: |
| + std::string notification_key_; |
| + std::string notification_id_; |
| + gfx::ImageSkia snapshot_; |
| + size_t count_close_ = 0; |
| + |
| + base::WeakPtrFactory<MockArcNotificationItem> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MockArcNotificationItem); |
| +}; |
| + |
| +class TestMessageCenterController |
| + : public message_center::MessageCenterController { |
| + public: |
| + TestMessageCenterController() {} |
| + |
| + // 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_; |
|
hidehiko
2017/06/15 15:26:20
#include <set> ?
yoshiki
2017/06/16 11:29:08
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestMessageCenterController); |
| +}; |
| + |
| +class DummyEvent : public ui::Event { |
| + public: |
| + DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {} |
| + ~DummyEvent() override {} |
| +}; |
| + |
| +class ArcNotificationContentViewTest : public views::ViewsTestBase { |
| + public: |
| + ArcNotificationContentViewTest() {} |
| + ~ArcNotificationContentViewTest() override {} |
| + |
| + void SetUp() override { |
| + std::string notification_key("notification id"); |
| + |
| + 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.
|
| + |
| + 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.
|
| + notification_item_.reset(new MockArcNotificationItem(notification_key)); |
| + |
| + notification_delegate_ = |
| + new ArcNotificationDelegate(notification_item_->GetWeakPtr()); |
| + |
| + surface_manager_->PrepareSurface(notification_key); |
| + |
| + notification_.reset(new message_center::Notification( |
| + message_center::NOTIFICATION_TYPE_CUSTOM, |
| + notification_item_->GetNotificationId(), 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<ArcNotificationView*>( |
| + message_center::MessageViewFactory::Create(controller(), *notification_, |
| + true))); |
| + notification_view_->set_owned_by_client(); |
| + |
| + views::Widget::InitParams params( |
| + CreateParams(views::Widget::InitParams::TYPE_POPUP)); |
| + params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| + wrapper_widget_ = base::MakeUnique<views::Widget>(); |
| + wrapper_widget_->Init(params); |
| + wrapper_widget_->SetContentsView(notification_view_.get()); |
| + wrapper_widget_->SetSize(notification_view_->GetPreferredSize()); |
| + } |
| + |
| + void TearDown() override { |
| + wrapper_widget_->Close(); |
| + wrapper_widget_.reset(); |
| + notification_view_.reset(); |
|
hidehiko
2017/06/15 15:26:20
nit: notificatrion_item_.reset() and notification_
|
| + surface_manager_.reset(); |
| + views::ViewsTestBase::TearDown(); |
| + } |
| + |
| + void PressCloseButton() { |
| + DummyEvent dummy_event; |
| + views::ImageButton* close_button = |
| + GetArcNotificationContentView()->close_button_.get(); |
| + ASSERT_NE(nullptr, close_button); |
| + GetArcNotificationContentView()->ButtonPressed(close_button, dummy_event); |
| + } |
| + |
| + TestMessageCenterController* controller() { return &controller_; } |
| + TestNotificationSurfaceManager* surface_manager() { |
| + return surface_manager_.get(); |
| + } |
| + views::Widget* widget() { return notification_view_->GetWidget(); } |
| + |
| + ArcNotificationContentView* GetArcNotificationContentView() { |
| + views::View* view = notification_view_->contents_view_; |
| + EXPECT_EQ(ArcNotificationContentView::kViewClassName, view->GetClassName()); |
| + return static_cast<ArcNotificationContentView*>(view); |
| + } |
| + |
| + MockArcNotificationItem* notification_item() const { |
| + return notification_item_.get(); |
| + } |
| + |
| + private: |
| + TestMessageCenterController controller_; |
| + 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.
|
| + std::unique_ptr<message_center::Notification> notification_; |
| + std::unique_ptr<ArcNotificationView> notification_view_; |
| + std::unique_ptr<MockArcNotificationItem> notification_item_; |
| + std::unique_ptr<TestNotificationSurfaceManager> surface_manager_; |
| + std::unique_ptr<views::Widget> wrapper_widget_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcNotificationContentViewTest); |
| +}; |
| + |
| +TEST_F(ArcNotificationContentViewTest, CloseButton) { |
| + EXPECT_EQ(1u, surface_manager()->surface_found_count()); |
| + EXPECT_FALSE( |
| + controller()->IsRemoved(notification_item()->GetNotificationId())); |
| + PressCloseButton(); |
| + EXPECT_TRUE( |
| + controller()->IsRemoved(notification_item()->GetNotificationId())); |
| +} |
| + |
| +} // namespace arc |