diff options
author | yoshiki <yoshiki@chromium.org> | 2016-03-09 23:11:22 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-10 07:12:42 +0000 |
commit | 8e2ddbb20aa4ee99c71c5a60313e0a262bd9d0b1 (patch) | |
tree | c1564841f973df2bcfc51ac03035d921ae5e55e4 | |
parent | f1dcaac3801ed8d0a8abe62f1496e61fbaf639a7 (diff) | |
download | chromium_src-8e2ddbb20aa4ee99c71c5a60313e0a262bd9d0b1.zip chromium_src-8e2ddbb20aa4ee99c71c5a60313e0a262bd9d0b1.tar.gz chromium_src-8e2ddbb20aa4ee99c71c5a60313e0a262bd9d0b1.tar.bz2 |
ARC notification: Add unittests for ArcNotificationManager
BUG=b/27276506
TEST=added unittests pass
Review URL: https://codereview.chromium.org/1719043002
Cr-Commit-Position: refs/heads/master@{#380366}
-rw-r--r-- | BUILD.gn | 1 | ||||
-rw-r--r-- | build/all.gyp | 1 | ||||
-rw-r--r-- | build/gn_migration.gypi | 1 | ||||
-rw-r--r-- | components/arc.gypi | 2 | ||||
-rw-r--r-- | components/arc/BUILD.gn | 2 | ||||
-rw-r--r-- | components/arc/test/fake_notifications_instance.cc | 32 | ||||
-rw-r--r-- | components/arc/test/fake_notifications_instance.h | 39 | ||||
-rw-r--r-- | testing/buildbot/chromium.chromiumos.json | 9 | ||||
-rw-r--r-- | testing/buildbot/chromium.memory.json | 3 | ||||
-rw-r--r-- | testing/buildbot/chromium_trybot.json | 11 | ||||
-rw-r--r-- | testing/buildbot/gn_isolate_map.pyl | 5 | ||||
-rw-r--r-- | ui/arc/BUILD.gn | 18 | ||||
-rw-r--r-- | ui/arc/arc.gyp | 27 | ||||
-rw-r--r-- | ui/arc/notification/arc_notification_manager.cc | 13 | ||||
-rw-r--r-- | ui/arc/notification/arc_notification_manager.h | 7 | ||||
-rw-r--r-- | ui/arc/notification/arc_notification_manager_unittest.cc | 170 | ||||
-rw-r--r-- | ui/arc/test/run_all_unittests.cc | 17 |
17 files changed, 355 insertions, 3 deletions
@@ -340,6 +340,7 @@ group("both_gn_and_gyp") { if (is_chromeos) { deps += [ "//chromeos:chromeos_unittests", + "//ui/arc:ui_arc_unittests", "//ui/chromeos:ui_chromeos_unittests", ] } diff --git a/build/all.gyp b/build/all.gyp index 68a52f4..280dcdd 100644 --- a/build/all.gyp +++ b/build/all.gyp @@ -434,6 +434,7 @@ ['chromeos==1', { 'dependencies': [ '../ui/chromeos/ui_chromeos.gyp:ui_chromeos_unittests', + '../ui/arc/arc.gyp:ui_arc_unittests', ], }], ['OS=="linux"', { diff --git a/build/gn_migration.gypi b/build/gn_migration.gypi index 924e533..5d0f7db 100644 --- a/build/gn_migration.gypi +++ b/build/gn_migration.gypi @@ -389,6 +389,7 @@ 'dependencies': [ '../chromeos/chromeos.gyp:chromeos_unittests', '../ui/chromeos/ui_chromeos.gyp:ui_chromeos_unittests', + '../ui/arc/arc.gyp:ui_arc_unittests', ] }], ['chromeos==1 or OS=="win" or OS=="mac"', { diff --git a/components/arc.gypi b/components/arc.gypi index 20f6600..63e0236 100644 --- a/components/arc.gypi +++ b/components/arc.gypi @@ -73,6 +73,8 @@ 'arc/test/fake_arc_bridge_instance.h', 'arc/test/fake_arc_bridge_service.cc', 'arc/test/fake_arc_bridge_service.h', + 'arc/test/fake_notifications_instance.cc', + 'arc/test/fake_notifications_instance.h', ], }, { diff --git a/components/arc/BUILD.gn b/components/arc/BUILD.gn index 767d926..fdf0076 100644 --- a/components/arc/BUILD.gn +++ b/components/arc/BUILD.gn @@ -86,6 +86,8 @@ static_library("arc_test_support") { "test/fake_arc_bridge_instance.h", "test/fake_arc_bridge_service.cc", "test/fake_arc_bridge_service.h", + "test/fake_notifications_instance.cc", + "test/fake_notifications_instance.h", ] deps = [ diff --git a/components/arc/test/fake_notifications_instance.cc b/components/arc/test/fake_notifications_instance.cc new file mode 100644 index 0000000..3f5bea7 --- /dev/null +++ b/components/arc/test/fake_notifications_instance.cc @@ -0,0 +1,32 @@ +// Copyright 2016 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 "components/arc/test/fake_notifications_instance.h" + +namespace arc { + +FakeNotificationsInstance::FakeNotificationsInstance( + mojo::InterfaceRequest<NotificationsInstance> request) + : binding_(this, std::move(request)) {} + +FakeNotificationsInstance::~FakeNotificationsInstance() {} + +void FakeNotificationsInstance::SendNotificationEventToAndroid( + const mojo::String& key, + ArcNotificationEvent event) { + events_.emplace_back(key, event); +} + +void FakeNotificationsInstance::Init(NotificationsHostPtr host_ptr) {} + +const std::vector<std::pair<mojo::String, ArcNotificationEvent>>& +FakeNotificationsInstance::events() const { + return events_; +} + +void FakeNotificationsInstance::WaitForIncomingMethodCall() { + binding_.WaitForIncomingMethodCall(); +} + +} // namespace arc diff --git a/components/arc/test/fake_notifications_instance.h b/components/arc/test/fake_notifications_instance.h new file mode 100644 index 0000000..fdddc82 --- /dev/null +++ b/components/arc/test/fake_notifications_instance.h @@ -0,0 +1,39 @@ +// Copyright 2016 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. + +#ifndef COMPONENTS_ARC_TEST_FAKE_NOTIFICATIONS_INSTANCE_H_ +#define COMPONENTS_ARC_TEST_FAKE_NOTIFICATIONS_INSTANCE_H_ + +#include "components/arc/common/notifications.mojom.h" +#include "components/arc/test/fake_arc_bridge_instance.h" + +namespace arc { + +class FakeNotificationsInstance : public NotificationsInstance { + public: + FakeNotificationsInstance( + mojo::InterfaceRequest<NotificationsInstance> request); + ~FakeNotificationsInstance() override; + + void Init(NotificationsHostPtr host_ptr) override; + + void SendNotificationEventToAndroid(const mojo::String& key, + ArcNotificationEvent event) override; + + const std::vector<std::pair<mojo::String, ArcNotificationEvent>>& events() + const; + + void WaitForIncomingMethodCall(); + + private: + std::vector<std::pair<mojo::String, ArcNotificationEvent>> events_; + + mojo::Binding<NotificationsInstance> binding_; + + DISALLOW_COPY_AND_ASSIGN(FakeNotificationsInstance); +}; + +} // namespace arc + +#endif // COMPONENTS_ARC_TEST_FAKE_NOTIFICATIONS_INSTANCE_H_ diff --git a/testing/buildbot/chromium.chromiumos.json b/testing/buildbot/chromium.chromiumos.json index 2cfe693..b79122a 100644 --- a/testing/buildbot/chromium.chromiumos.json +++ b/testing/buildbot/chromium.chromiumos.json @@ -267,6 +267,9 @@ "test": "sync_unit_tests" }, { + "test": "ui_arc_unittests" + }, + { "swarming": { "can_use_on_swarming_builders": true }, @@ -563,6 +566,9 @@ "test": "sync_unit_tests" }, { + "test": "ui_arc_unittests" + }, + { "swarming": { "can_use_on_swarming_builders": true }, @@ -858,6 +864,9 @@ "test": "sync_unit_tests" }, { + "test": "ui_arc_unittests" + }, + { "swarming": { "can_use_on_swarming_builders": true }, diff --git a/testing/buildbot/chromium.memory.json b/testing/buildbot/chromium.memory.json index 5057b1d..7460b60 100644 --- a/testing/buildbot/chromium.memory.json +++ b/testing/buildbot/chromium.memory.json @@ -492,6 +492,9 @@ "test": "sync_unit_tests" }, { + "test": "ui_arc_unittests" + }, + { "swarming": { "can_use_on_swarming_builders": true }, diff --git a/testing/buildbot/chromium_trybot.json b/testing/buildbot/chromium_trybot.json index 24a7696..558cec1 100644 --- a/testing/buildbot/chromium_trybot.json +++ b/testing/buildbot/chromium_trybot.json @@ -299,6 +299,17 @@ ], "test": "ui_chromeos_unittests" }, + { + "chromium_configs": [ + "chromium_chromeos", + "chromium_chromeos_clang", + "chromium_chromeos_ozone" + ], + "platforms": [ + "linux" + ], + "test": "ui_arc_unittests" + }, "url_unittests", "skia_unittests", { diff --git a/testing/buildbot/gn_isolate_map.pyl b/testing/buildbot/gn_isolate_map.pyl index 00e2fdd..85bb482 100644 --- a/testing/buildbot/gn_isolate_map.pyl +++ b/testing/buildbot/gn_isolate_map.pyl @@ -518,6 +518,11 @@ "label": "//ui/chromeos:ui_chromeos_unittests", "type": "unknown", }, + "ui_arc_unittests": { + "label": "//ui/arc:ui_arc_unittests", + "type": "raw", + "args": [], + }, "ui_touch_selection_unittests": { "label": "//ui/touch_selection:ui_touch_selection_unittests", "type": "windowed_test_launcher", diff --git a/ui/arc/BUILD.gn b/ui/arc/BUILD.gn index 76a4b95..7b987aa7 100644 --- a/ui/arc/BUILD.gn +++ b/ui/arc/BUILD.gn @@ -21,3 +21,21 @@ static_library("arc") { "//ui/message_center", ] } + +static_library("ui_arc_unittests") { + testonly = true + sources = [ + "notification/arc_notification_manager_unittest.cc", + "test/run_all_unittests.cc", + ] + + deps = [ + ":arc", + "//base", + "//base/test:test_support", + "//components/arc:arc_test_support", + "//mojo/edk/system", + "//mojo/message_pump", + "//ui/message_center:test_support", + ] +} diff --git a/ui/arc/arc.gyp b/ui/arc/arc.gyp index b0789e1..e1a40a7 100644 --- a/ui/arc/arc.gyp +++ b/ui/arc/arc.gyp @@ -8,15 +8,17 @@ }, 'targets': [ { + # GN: //ui/arc:arc 'target_name': 'arc', 'type': 'static_library', 'include_dirs': [ - '..', + '../..', ], 'dependencies': [ '../gfx/gfx.gyp:gfx_geometry', '../message_center/message_center.gyp:message_center', '../../base/base.gyp:base', + '../../url/url.gyp:url_lib', '../../skia/skia.gyp:skia', '../../components/components.gyp:arc_mojo_bindings', '../../components/components.gyp:signin_core_account_id', @@ -28,5 +30,28 @@ 'notification/arc_notification_item.h', ], }, + { + # GN: //ui/arc:ui_arc_unittests + 'target_name': 'ui_arc_unittests', + 'type': '<(gtest_target_type)', + 'include_dirs': [ + '../..', + ], + 'dependencies': [ + '../../base/base.gyp:base', + '../../base/base.gyp:test_support_base', + '../../components/components.gyp:arc_test_support', + '../../mojo/mojo_edk.gyp:mojo_system_impl', + '../../mojo/mojo_public.gyp:mojo_cpp_bindings', + '../../mojo/mojo_public.gyp:mojo_message_pump_lib', + '../../testing/gtest.gyp:gtest', + '../message_center/message_center.gyp:message_center_test_support', + 'arc', + ], + 'sources': [ + 'notification/arc_notification_manager_unittest.cc', + 'test/run_all_unittests.cc', + ], + }, ], } diff --git a/ui/arc/notification/arc_notification_manager.cc b/ui/arc/notification/arc_notification_manager.cc index a43015c..4f65559 100644 --- a/ui/arc/notification/arc_notification_manager.cc +++ b/ui/arc/notification/arc_notification_manager.cc @@ -11,8 +11,17 @@ namespace arc { ArcNotificationManager::ArcNotificationManager(ArcBridgeService* bridge_service, const AccountId& main_profile_id) + : ArcNotificationManager(bridge_service, + main_profile_id, + message_center::MessageCenter::Get()) {} + +ArcNotificationManager::ArcNotificationManager( + ArcBridgeService* bridge_service, + const AccountId& main_profile_id, + message_center::MessageCenter* message_center) : ArcService(bridge_service), main_profile_id_(main_profile_id), + message_center_(message_center), binding_(this) { arc_bridge_service()->AddObserver(this); } @@ -50,8 +59,8 @@ void ArcNotificationManager::OnNotificationPosted(ArcNotificationDataPtr data) { if (it == items_.end()) { // Show a notification on the primary loged-in user's desktop. // TODO(yoshiki): Reconsider when ARC supports multi-user. - ArcNotificationItem* item = new ArcNotificationItem( - this, message_center::MessageCenter::Get(), key, main_profile_id_); + ArcNotificationItem* item = + new ArcNotificationItem(this, message_center_, key, main_profile_id_); // TODO(yoshiki): Use emplacement for performance when it's available. auto result = items_.insert(std::make_pair(key, make_scoped_ptr(item))); DCHECK(result.second); diff --git a/ui/arc/notification/arc_notification_manager.h b/ui/arc/notification/arc_notification_manager.h index 4b21719..2709116 100644 --- a/ui/arc/notification/arc_notification_manager.h +++ b/ui/arc/notification/arc_notification_manager.h @@ -13,6 +13,7 @@ #include "components/arc/common/notifications.mojom.h" #include "components/signin/core/account_id/account_id.h" #include "mojo/public/cpp/bindings/binding.h" +#include "ui/message_center/message_center.h" namespace arc { @@ -24,6 +25,11 @@ class ArcNotificationManager : public ArcService, public: ArcNotificationManager(ArcBridgeService* bridge_service, const AccountId& main_profile_id); + + ArcNotificationManager(ArcBridgeService* bridge_service, + const AccountId& main_profile_id, + message_center::MessageCenter* message_center); + ~ArcNotificationManager() override; // ArcBridgeService::Observer implementation: @@ -42,6 +48,7 @@ class ArcNotificationManager : public ArcService, private: const AccountId main_profile_id_; + message_center::MessageCenter* const message_center_; using ItemMap = std::unordered_map<std::string, scoped_ptr<ArcNotificationItem>>; diff --git a/ui/arc/notification/arc_notification_manager_unittest.cc b/ui/arc/notification/arc_notification_manager_unittest.cc new file mode 100644 index 0000000..9e2f010 --- /dev/null +++ b/ui/arc/notification/arc_notification_manager_unittest.cc @@ -0,0 +1,170 @@ +// Copyright 2016 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 "base/message_loop/message_loop.h" +#include "base/run_loop.h" +#include "components/arc/test/fake_arc_bridge_instance.h" +#include "components/arc/test/fake_arc_bridge_service.h" +#include "components/arc/test/fake_notifications_instance.h" +#include "mojo/message_pump/message_pump_mojo.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/arc/notification/arc_notification_manager.h" +#include "ui/message_center/fake_message_center.h" + +namespace arc { + +namespace { + +const char kDummyNotificationKey[] = "DUMMY_NOTIFICATION_KEY"; + +class MockMessageCenter : public message_center::FakeMessageCenter { + public: + MockMessageCenter() {} + ~MockMessageCenter() override { + STLDeleteContainerPointers( + visible_notifications_.begin(), visible_notifications_.end()); + } + + void AddNotification( + scoped_ptr<message_center::Notification> notification) override { + visible_notifications_.insert(notification.release()); + } + + void RemoveNotification(const std::string& id, bool by_user) override { + for (auto it = visible_notifications_.begin(); + it != visible_notifications_.end(); it++) { + if ((*it)->id() == id) { + delete *it; + visible_notifications_.erase(it); + return; + } + } + } + + const message_center::NotificationList::Notifications& + GetVisibleNotifications() override { + return visible_notifications_; + } + + private: + message_center::NotificationList::Notifications visible_notifications_; + + DISALLOW_COPY_AND_ASSIGN(MockMessageCenter); +}; + +class ArcBridgeServiceObserver : public ArcBridgeService::Observer { + public: + ArcBridgeServiceObserver() = default; + void OnNotificationsInstanceReady() override { ready_ = true; } + + bool IsReady() { return ready_; } + + private: + bool ready_ = false; + + DISALLOW_COPY_AND_ASSIGN(ArcBridgeServiceObserver); +}; + +} // anonymous namespace + +class ArcNotificationManagerTest : public testing::Test { + public: + ArcNotificationManagerTest() + : loop_(mojo::common::MessagePumpMojo::Create()) {} + ~ArcNotificationManagerTest() override { loop_.RunUntilIdle(); } + + protected: + FakeArcBridgeService* service() { return service_.get(); } + FakeNotificationsInstance* arc_notifications_instance() { + return arc_notifications_instance_.get(); + } + ArcNotificationManager* arc_notification_manager() { + return arc_notification_manager_.get(); + } + MockMessageCenter* message_center() { return message_center_.get(); } + + std::string CreateNotification() { + auto data = ArcNotificationData::New(); + data->key = kDummyNotificationKey; + data->title = "TITLE"; + data->message = "MESSAGE"; + + std::vector<unsigned char> icon_data; + data->icon_data.Swap(&icon_data); + + arc_notification_manager()->OnNotificationPosted(std::move(data)); + + return std::string(kDummyNotificationKey); + } + + private: + base::MessageLoop loop_; + scoped_ptr<FakeArcBridgeService> service_; + scoped_ptr<FakeNotificationsInstance> arc_notifications_instance_; + scoped_ptr<ArcNotificationManager> arc_notification_manager_; + scoped_ptr<MockMessageCenter> message_center_; + + void SetUp() override { + NotificationsInstancePtr arc_notifications_instance; + arc_notifications_instance_.reset( + new FakeNotificationsInstance(GetProxy(&arc_notifications_instance))); + service_.reset(new FakeArcBridgeService()); + message_center_.reset(new MockMessageCenter()); + + arc_notification_manager_.reset(new ArcNotificationManager( + service(), EmptyAccountId(), message_center_.get())); + + ArcBridgeServiceObserver observer; + service_->AddObserver(&observer); + service_->OnNotificationsInstanceReady( + std::move(arc_notifications_instance)); + + while (!observer.IsReady()) + loop_.RunUntilIdle(); + + service_->RemoveObserver(&observer); + } + + void TearDown() override { + arc_notification_manager_.reset(); + message_center_.reset(); + service_.reset(); + arc_notifications_instance_.reset(); + } + + DISALLOW_COPY_AND_ASSIGN(ArcNotificationManagerTest); +}; + +TEST_F(ArcNotificationManagerTest, NotificationCreatedAndRemoved) { + EXPECT_EQ(0u, message_center()->GetVisibleNotifications().size()); + std::string key = CreateNotification(); + EXPECT_EQ(1u, message_center()->GetVisibleNotifications().size()); + + arc_notification_manager()->OnNotificationRemoved(key); + + EXPECT_EQ(0u, message_center()->GetVisibleNotifications().size()); +} + +TEST_F(ArcNotificationManagerTest, NotificationRemovedByChrome) { + service()->SetReady(); + EXPECT_EQ(0u, message_center()->GetVisibleNotifications().size()); + std::string key = CreateNotification(); + EXPECT_EQ(1u, message_center()->GetVisibleNotifications().size()); + + { + message_center::Notification* notification = + *message_center()->GetVisibleNotifications().begin(); + notification->delegate()->Close(true /* by_user */); + // |notification| gets stale here. + } + + arc_notifications_instance()->WaitForIncomingMethodCall(); + + ASSERT_EQ(1u, arc_notifications_instance()->events().size()); + EXPECT_EQ(key, arc_notifications_instance()->events().at(0).first); + EXPECT_EQ(ArcNotificationEvent::CLOSED, + arc_notifications_instance()->events().at(0).second); +} + +} // namespace arc diff --git a/ui/arc/test/run_all_unittests.cc b/ui/arc/test/run_all_unittests.cc new file mode 100644 index 0000000..92e28e4b --- /dev/null +++ b/ui/arc/test/run_all_unittests.cc @@ -0,0 +1,17 @@ +// Copyright 2016 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 "base/bind.h" +#include "base/test/launcher/unit_test_launcher.h" +#include "base/test/test_suite.h" +#include "mojo/edk/embedder/embedder.h" + +int main(int argc, char** argv) { + base::TestSuite test_suite(argc, argv); + + mojo::edk::Init(); + return base::LaunchUnitTests( + argc, argv, + base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); +} |