diff options
author | mukai@chromium.org <mukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-18 21:49:02 +0000 |
---|---|---|
committer | mukai@chromium.org <mukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-18 21:49:02 +0000 |
commit | 7c728ff10fd7da887326a10f82829f51eee96868 (patch) | |
tree | 891da87dbb0998b31e3dbb22ba99b97f225b35bf /ui/message_center | |
parent | 375db13aefaca77bce16bc0a71467038c81652c4 (diff) | |
download | chromium_src-7c728ff10fd7da887326a10f82829f51eee96868.zip chromium_src-7c728ff10fd7da887326a10f82829f51eee96868.tar.gz chromium_src-7c728ff10fd7da887326a10f82829f51eee96868.tar.bz2 |
Add message_center_unittests.
Currently it just tests notification_list.cc. Will cover more files in further CLs.
BUG=166454
TEST=new message_center_unittests passed
Review URL: https://codereview.chromium.org/11613013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@173792 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/message_center')
-rw-r--r-- | ui/message_center/message_center.gyp | 16 | ||||
-rw-r--r-- | ui/message_center/notification_list.cc | 19 | ||||
-rw-r--r-- | ui/message_center/notification_list_unittest.cc | 339 |
3 files changed, 365 insertions, 9 deletions
diff --git a/ui/message_center/message_center.gyp b/ui/message_center/message_center.gyp index 22a9ce3..15c3124 100644 --- a/ui/message_center/message_center.gyp +++ b/ui/message_center/message_center.gyp @@ -49,5 +49,21 @@ 'quiet_mode_bubble.h', ], }, + { + 'target_name': 'message_center_unittests', + 'type': 'executable', + 'dependencies': [ + '../../base/base.gyp:base', + '../../base/base.gyp:test_support_base', + '../../skia/skia.gyp:skia', + '../../testing/gtest.gyp:gtest', + '../../testing/gtest.gyp:gtest_main', + '../ui.gyp:ui', + 'message_center', + ], + 'sources': [ + 'notification_list_unittest.cc', + ], + }, ], } diff --git a/ui/message_center/notification_list.cc b/ui/message_center/notification_list.cc index 3bd0302..0fcf9a1 100644 --- a/ui/message_center/notification_list.cc +++ b/ui/message_center/notification_list.cc @@ -152,6 +152,7 @@ bool NotificationList::RemoveNotification(const std::string& id) { void NotificationList::RemoveAllNotifications() { notifications_.clear(); + unread_count_ = 0; } void NotificationList::SendRemoveNotificationsBySource( @@ -216,7 +217,7 @@ void NotificationList::GetPopupNotifications( NotificationList::Notifications* notifications) { typedef std::pair<Notifications::iterator, Notifications::iterator> NotificationRange; - // In the popup, earlier should come earlier. + // In the popup, latest should come earlier. std::list<NotificationRange> iters; for (int i = ui::notifications::DEFAULT_PRIORITY; i <= ui::notifications::MAX_PRIORITY; ++i) { @@ -227,17 +228,17 @@ void NotificationList::GetPopupNotifications( } notifications->clear(); while (!iters.empty()) { - std::list<NotificationRange>::iterator min_iter = iters.begin(); - std::list<NotificationRange>::iterator iter = min_iter; + std::list<NotificationRange>::iterator max_iter = iters.begin(); + std::list<NotificationRange>::iterator iter = max_iter; iter++; for (; iter != iters.end(); ++iter) { - if (min_iter->first->timestamp > iter->first->timestamp) - min_iter = iter; + if (max_iter->first->timestamp < iter->first->timestamp) + max_iter = iter; } - notifications->push_back(*(min_iter->first)); - ++(min_iter->first); - if (min_iter->first == min_iter->second) - iters.erase(min_iter); + notifications->push_back(*(max_iter->first)); + ++(max_iter->first); + if (max_iter->first == max_iter->second) + iters.erase(max_iter); } } diff --git a/ui/message_center/notification_list_unittest.cc b/ui/message_center/notification_list_unittest.cc new file mode 100644 index 0000000..308e568 --- /dev/null +++ b/ui/message_center/notification_list_unittest.cc @@ -0,0 +1,339 @@ +// Copyright (c) 2012 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/message_center/notification_list.h" + +#include "base/basictypes.h" +#include "base/i18n/time_formatting.h" +#include "base/stringprintf.h" +#include "base/utf_string_conversions.h" +#include "base/values.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/notifications/notification_types.h" + +namespace message_center { +namespace { + +class MockNotificationListDelegate : public NotificationList::Delegate { + public: + MockNotificationListDelegate() : send_remove_count_(0) {} + virtual ~MockNotificationListDelegate() {} + + size_t GetSendRemoveCountAndReset() { + size_t result = send_remove_count_; + send_remove_count_ = 0; + return result; + } + + private: + // NotificationList::Delegate overrides: + virtual void SendRemoveNotification(const std::string& id) OVERRIDE { + send_remove_count_++; + } + + virtual void SendRemoveAllNotifications() OVERRIDE { + } + + virtual void DisableNotificationByExtension(const std::string& id) OVERRIDE { + } + + virtual void DisableNotificationByUrl(const std::string& id) OVERRIDE { + } + + virtual void ShowNotificationSettings(const std::string& id) OVERRIDE { + } + + virtual void OnNotificationClicked(const std::string& id) OVERRIDE { + } + + virtual void OnQuietModeChanged(bool quiet_mode) OVERRIDE { + } + + virtual void OnButtonClicked(const std::string& id, + int button_index) OVERRIDE { + } + + virtual NotificationList* GetNotificationList() OVERRIDE { + return NULL; + } + + size_t send_remove_count_; + DISALLOW_COPY_AND_ASSIGN(MockNotificationListDelegate); +}; + +class NotificationListTest : public testing::Test { + public: + NotificationListTest() {} + virtual ~NotificationListTest() {} + + virtual void SetUp() { + delegate_.reset(new MockNotificationListDelegate); + notification_list_.reset(new NotificationList(delegate_.get())); + counter_ = 0; + } + + protected: + // Currently NotificationListTest doesn't care about some fields like title or + // message, so put a simple template on it. Returns the id of the new + // notification. + std::string AddNotification(const base::DictionaryValue* optional_fields) { + std::string new_id = base::StringPrintf(kIdFormat, counter_); + notification_list_->AddNotification( + ui::notifications::NOTIFICATION_TYPE_SIMPLE, new_id, + UTF8ToUTF16(StringPrintf(kTitleFormat, counter_)), + UTF8ToUTF16(StringPrintf(kMessageFormat, counter_)), + UTF8ToUTF16(kDisplaySource), kExtensionId, + optional_fields); + counter_++; + return new_id; + } + + // Utility methods of AddNotification. + std::string AddPriorityNotification(int priority) { + base::DictionaryValue optional; + optional.SetInteger(ui::notifications::kPriorityKey, priority); + return AddNotification(&optional); + } + void SetupTimestampKey(const base::Time& time, + base::DictionaryValue* optional) { + string16 time_formatted = base::TimeFormatShortDateAndTime(time); + optional->SetString(ui::notifications::kTimestampKey, time_formatted); + } + + size_t GetPopupCounts() { + NotificationList::Notifications popups; + notification_list()->GetPopupNotifications(&popups); + return popups.size(); + } + + MockNotificationListDelegate* delegate() { return delegate_.get(); } + NotificationList* notification_list() { return notification_list_.get(); } + + private: + static const char kIdFormat[]; + static const char kTitleFormat[]; + static const char kMessageFormat[]; + static const char kDisplaySource[]; + static const char kExtensionId[]; + + scoped_ptr<MockNotificationListDelegate> delegate_; + scoped_ptr<NotificationList> notification_list_; + uint counter_; + + DISALLOW_COPY_AND_ASSIGN(NotificationListTest); +}; + +const char NotificationListTest::kIdFormat[] = "id%ld"; +const char NotificationListTest::kTitleFormat[] = "id%ld"; +const char NotificationListTest::kMessageFormat[] = "message%ld"; +const char NotificationListTest::kDisplaySource[] = "source"; +const char NotificationListTest::kExtensionId[] = "ext"; + +} // namespace + +TEST_F(NotificationListTest, Basic) { + ASSERT_EQ(0u, notification_list()->NotificationCount()); + ASSERT_EQ(0u, notification_list()->unread_count()); + + std::string id0 = AddNotification(NULL); + EXPECT_EQ(1u, notification_list()->NotificationCount()); + std::string id1 = AddNotification(NULL); + EXPECT_EQ(2u, notification_list()->NotificationCount()); + EXPECT_EQ(2u, notification_list()->unread_count()); + + EXPECT_TRUE(notification_list()->HasPopupNotifications()); + EXPECT_TRUE(notification_list()->HasNotification(id0)); + EXPECT_TRUE(notification_list()->HasNotification(id1)); + EXPECT_FALSE(notification_list()->HasNotification(id1 + "foo")); + + EXPECT_EQ(2u, GetPopupCounts()); + + notification_list()->MarkPopupsAsShown(0); + EXPECT_EQ(2u, notification_list()->NotificationCount()); + EXPECT_EQ(0u, GetPopupCounts()); + + EXPECT_TRUE(notification_list()->RemoveNotification(id0)); + EXPECT_EQ(1u, notification_list()->NotificationCount()); + EXPECT_EQ(1u, notification_list()->unread_count()); + + AddNotification(NULL); + EXPECT_EQ(2u, notification_list()->NotificationCount()); + + notification_list()->RemoveAllNotifications(); + EXPECT_EQ(0u, notification_list()->NotificationCount()); + EXPECT_EQ(0u, notification_list()->unread_count()); +} + +TEST_F(NotificationListTest, MessageCenterVisible) { + AddNotification(NULL); + EXPECT_EQ(1u, notification_list()->NotificationCount()); + ASSERT_EQ(1u, notification_list()->unread_count()); + + // Toggle the message center visibility. It resets the unread count when + // hidden. + notification_list()->SetMessageCenterVisible(true); + ASSERT_EQ(1u, notification_list()->unread_count()); + notification_list()->SetMessageCenterVisible(false); + ASSERT_EQ(0u, notification_list()->unread_count()); +} + +TEST_F(NotificationListTest, UpdateNotification) { + std::string id0 = AddNotification(NULL); + std::string replaced = id0 + "_replaced"; + EXPECT_EQ(1u, notification_list()->NotificationCount()); + notification_list()->UpdateNotificationMessage( + id0, replaced, UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), NULL); + EXPECT_EQ(1u, notification_list()->NotificationCount()); + NotificationList::Notifications notifications; + notification_list()->GetNotifications(¬ifications); + EXPECT_EQ(replaced, notifications.begin()->id); + EXPECT_EQ(UTF8ToUTF16("newtitle"), notifications.begin()->title); + EXPECT_EQ(UTF8ToUTF16("newbody"), notifications.begin()->message); +} + +TEST_F(NotificationListTest, SendRemoveNotifications) { + notification_list()->AddNotification( + ui::notifications::NOTIFICATION_TYPE_SIMPLE, "id0", UTF8ToUTF16("title0"), + UTF8ToUTF16("message0"), UTF8ToUTF16("source0"), "ext0", NULL); + notification_list()->AddNotification( + ui::notifications::NOTIFICATION_TYPE_SIMPLE, "id1", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), UTF8ToUTF16("source0"), "ext0", NULL); + notification_list()->AddNotification( + ui::notifications::NOTIFICATION_TYPE_SIMPLE, "id2", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), UTF8ToUTF16("source1"), "ext0", NULL); + notification_list()->AddNotification( + ui::notifications::NOTIFICATION_TYPE_SIMPLE, "id3", UTF8ToUTF16("title1"), + UTF8ToUTF16("message1"), UTF8ToUTF16("source2"), "ext1", NULL); + + notification_list()->SendRemoveNotificationsBySource("id0"); + EXPECT_EQ(2u, delegate()->GetSendRemoveCountAndReset()); + notification_list()->SendRemoveNotificationsByExtension("id0"); + EXPECT_EQ(3u, delegate()->GetSendRemoveCountAndReset()); +} + +TEST_F(NotificationListTest, Priority) { + ASSERT_EQ(0u, notification_list()->NotificationCount()); + ASSERT_EQ(0u, notification_list()->unread_count()); + + // Default priority has the limit on the number of the popups. + for (size_t i = 0; i <= NotificationList::kMaxVisiblePopupNotifications; + ++i) { + AddNotification(NULL); + } + EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications + 1, + notification_list()->NotificationCount()); + EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications, GetPopupCounts()); + + // Low priority: not visible to popups. + notification_list()->SetMessageCenterVisible(true); + notification_list()->SetMessageCenterVisible(false); + EXPECT_EQ(0u, notification_list()->unread_count()); + AddPriorityNotification(-1); + EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications + 2, + notification_list()->NotificationCount()); + EXPECT_EQ(1u, notification_list()->unread_count()); + EXPECT_EQ(0u, GetPopupCounts()); + + // Minimum priority: doesn't update the unread count. + AddPriorityNotification(-2); + EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications + 3, + notification_list()->NotificationCount()); + EXPECT_EQ(1u, notification_list()->unread_count()); + EXPECT_EQ(0u, GetPopupCounts()); + + notification_list()->RemoveAllNotifications(); + + // Higher priority: no limits to the number of popups. + for (size_t i = 0; i < NotificationList::kMaxVisiblePopupNotifications * 2; + ++i) { + AddPriorityNotification(1); + } + for (size_t i = 0; i < NotificationList::kMaxVisiblePopupNotifications * 2; + ++i) { + AddPriorityNotification(2); + } + EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications * 4, + notification_list()->NotificationCount()); + EXPECT_EQ(NotificationList::kMaxVisiblePopupNotifications * 4, + GetPopupCounts()); +} + +TEST_F(NotificationListTest, PriorityPromotion) { + std::string id0 = AddPriorityNotification(-1); + std::string replaced = id0 + "_replaced"; + EXPECT_EQ(1u, notification_list()->NotificationCount()); + EXPECT_EQ(0u, GetPopupCounts()); + base::DictionaryValue optional; + optional.SetInteger(ui::notifications::kPriorityKey, 1); + notification_list()->UpdateNotificationMessage( + id0, replaced, UTF8ToUTF16("newtitle"), UTF8ToUTF16("newbody"), + &optional); + EXPECT_EQ(1u, notification_list()->NotificationCount()); + EXPECT_EQ(1u, GetPopupCounts()); + NotificationList::Notifications notifications; + notification_list()->GetNotifications(¬ifications); + EXPECT_EQ(replaced, notifications.begin()->id); + EXPECT_EQ(UTF8ToUTF16("newtitle"), notifications.begin()->title); + EXPECT_EQ(UTF8ToUTF16("newbody"), notifications.begin()->message); + EXPECT_EQ(1, notifications.begin()->priority); +} + +TEST_F(NotificationListTest, NotificationOrderAndPriority) { + base::Time now = base::Time::Now(); + base::DictionaryValue optional; + SetupTimestampKey(now, &optional); + optional.SetInteger(ui::notifications::kPriorityKey, 2); + std::string max_id = AddNotification(&optional); + now += base::TimeDelta::FromSeconds(1); + SetupTimestampKey(now, &optional); + optional.SetInteger(ui::notifications::kPriorityKey, 1); + std::string high_id = AddNotification(&optional); + now += base::TimeDelta::FromSeconds(1); + SetupTimestampKey(now, &optional); + optional.SetInteger(ui::notifications::kPriorityKey, 0); + std::string default_id = AddNotification(&optional); + + // Popups: latest comes first. + NotificationList::Notifications popups; + notification_list()->GetPopupNotifications(&popups); + EXPECT_EQ(3u, popups.size()); + NotificationList::Notifications::const_iterator iter = popups.begin(); + EXPECT_EQ(default_id, iter->id); + iter++; + EXPECT_EQ(high_id, iter->id); + iter++; + EXPECT_EQ(max_id, iter->id); + + // Notifications: high priority comes ealier. + NotificationList::Notifications notifications; + notification_list()->GetNotifications(¬ifications); + EXPECT_EQ(3u, notifications.size()); + iter = notifications.begin(); + EXPECT_EQ(max_id, iter->id); + iter++; + EXPECT_EQ(high_id, iter->id); + iter++; + EXPECT_EQ(default_id, iter->id); +} + +TEST_F(NotificationListTest, QuietMode) { + notification_list()->SetQuietMode(true); + AddNotification(NULL); + AddPriorityNotification(1); + AddPriorityNotification(2); + EXPECT_EQ(3u, notification_list()->NotificationCount()); + EXPECT_EQ(0u, GetPopupCounts()); + // TODO(mukai): fix here when notification_list distinguish dismiss by quiet + // mode and by user operation. + EXPECT_EQ(0u, notification_list()->unread_count()); + + notification_list()->SetQuietMode(false); + AddNotification(NULL); + EXPECT_EQ(4u, notification_list()->NotificationCount()); + EXPECT_EQ(1u, GetPopupCounts()); + + // TODO(mukai): Add test of quiet mode with expiration. +} + +} // namespace message_center |