Index: test/Notification.cpp |
diff --git a/test/Notification.cpp b/test/Notification.cpp |
index 490881f07b698e7f02d3eb849c2143d08fe7f508..7993b898aa9aac590849f81ca4468f84fb5d743e 100644 |
--- a/test/Notification.cpp |
+++ b/test/Notification.cpp |
@@ -47,6 +47,24 @@ namespace |
"require('notification').Notification.addNotification(" + notification + ");" |
"})();"); |
} |
+ |
+ struct PeekNotificationResult |
+ { |
+ bool isCallbackCalled; |
+ NotificationPtr notification; |
+ }; |
+ PeekNotificationResult PeekNotification(const std::string& url = std::string()) |
+ { |
+ PeekNotificationResult retValue = {false}; |
+ filterEngine->SetNotificationAvailableCallback([&retValue](const NotificationPtr& notification) |
+ { |
+ retValue.isCallbackCalled = true; |
+ retValue.notification = notification; |
+ }); |
+ filterEngine->ShowNextNotification(url); |
+ filterEngine->RemoveNotificationAvailableCallback(); |
+ return retValue; |
+ } |
}; |
class MockWebRequest : public WebRequest |
@@ -103,22 +121,24 @@ namespace |
TEST_F(NotificationTest, NoNotifications) |
{ |
- NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
- EXPECT_EQ(NULL, notification.get()); |
+ EXPECT_FALSE(PeekNotification().isCallbackCalled); |
} |
#ifdef NotificationMockWebRequestTest_ENABLED |
TEST_F(NotificationMockWebRequestTest, SingleNotification) |
{ |
+ bool isNotificationCallbackCalled = false; |
+ filterEngine->SetNotificationAvailableCallback([&isNotificationCallbackCalled](const NotificationPtr& notification) |
+ { |
+ isNotificationCallbackCalled = true; |
+ ASSERT_TRUE(notification); |
+ EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetType()); |
+ EXPECT_EQ("Title", notification->GetTitle()); |
+ EXPECT_EQ("message", notification->GetMessageString()); |
+ notification->MarkAsShown(); |
+ }); |
AdblockPlus::Sleep(5000/*msec*/); // it's a hack |
- NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
- // try another one immediately to avoid queuing of the next notification by |
- // the timer. |
- EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); |
- ASSERT_TRUE(notification); |
- EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetType()); |
- EXPECT_EQ("Title", notification->GetTitle()); |
- EXPECT_EQ("message", notification->GetMessageString()); |
+ EXPECT_TRUE(isNotificationCallbackCalled); |
} |
#endif |
@@ -129,63 +149,61 @@ TEST_F(NotificationTest, AddNotification) |
"title: 'testTitle'," |
"message: 'testMessage'," |
"}"); |
- NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
- ASSERT_TRUE(notification); |
- EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType()); |
- EXPECT_EQ("testTitle", notification->GetTitle()); |
- EXPECT_EQ("testMessage", notification->GetMessageString()); |
+ PeekNotificationResult peekNotificationResult = PeekNotification(); |
+ ASSERT_TRUE(peekNotificationResult.notification); |
+ EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, peekNotificationResult.notification->GetType()); |
+ EXPECT_EQ("testTitle", peekNotificationResult.notification->GetTitle()); |
+ EXPECT_EQ("testMessage", peekNotificationResult.notification->GetMessageString()); |
} |
TEST_F(NotificationTest, FilterByUrl) |
{ |
AddNotification("{ id: 'no-filter', type: 'critical' }"); |
AddNotification("{ id: 'www.com', type: 'information'," |
- "urlFilters:['http://www.com']" |
+ "urlFilters:['||www.com$document']" |
"}"); |
AddNotification("{ id: 'www.de', type: 'question'," |
- "urlFilters:['http://www.de']" |
+ "urlFilters:['||www.de$document']" |
"}"); |
- NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
- ASSERT_TRUE(notification); |
- EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType()); |
+ PeekNotificationResult peekNotificationResult = PeekNotification(); |
+ ASSERT_TRUE(peekNotificationResult.notification); |
+ EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, peekNotificationResult.notification->GetType()); |
- notification = filterEngine->GetNextNotificationToShow("http://www.de"); |
- ASSERT_TRUE(notification); |
- EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType()); |
+ peekNotificationResult = PeekNotification("http://www.de"); |
+ ASSERT_TRUE(peekNotificationResult.notification); |
+ EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, peekNotificationResult.notification->GetType()); |
- notification = filterEngine->GetNextNotificationToShow("http://www.com"); |
- ASSERT_TRUE(notification); |
- EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetType()); |
+ peekNotificationResult = PeekNotification("http://www.com"); |
+ ASSERT_TRUE(peekNotificationResult.notification); |
+ EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, peekNotificationResult.notification->GetType()); |
} |
TEST_F(NotificationTest, MarkAsShown) |
{ |
AddNotification("{ id: 'id', type: 'question' }"); |
- NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
- EXPECT_TRUE(notification); |
- notification = filterEngine->GetNextNotificationToShow(); |
- ASSERT_TRUE(notification); |
- notification->MarkAsShown(); |
- EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get()); |
+ EXPECT_TRUE(PeekNotification().notification); |
+ PeekNotificationResult peekNotificationResult = PeekNotification(); |
+ ASSERT_TRUE(peekNotificationResult.notification); |
+ peekNotificationResult.notification->MarkAsShown(); |
+ EXPECT_FALSE(PeekNotification().isCallbackCalled); |
} |
TEST_F(NotificationTest, NoLinks) |
{ |
AddNotification("{ id: 'id'}"); |
- NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
- ASSERT_TRUE(notification); |
- std::vector<std::string> notificationLinks = notification->GetLinks(); |
- EXPECT_EQ(0, notificationLinks.size()); |
+ PeekNotificationResult peekNotificationResult = PeekNotification(); |
+ ASSERT_TRUE(peekNotificationResult.notification); |
+ EXPECT_EQ(0, peekNotificationResult.notification->GetLinks().size()); |
} |
TEST_F(NotificationTest, Links) |
{ |
AddNotification("{ id: 'id', links: ['link1', 'link2'] }"); |
- NotificationPtr notification = filterEngine->GetNextNotificationToShow(); |
- ASSERT_TRUE(notification); |
- std::vector<std::string> notificationLinks = notification->GetLinks(); |
+ PeekNotificationResult peekNotificationResult = PeekNotification(); |
+ ASSERT_TRUE(peekNotificationResult.notification); |
+ std::vector<std::string> notificationLinks = peekNotificationResult.notification->GetLinks(); |
ASSERT_EQ(2, notificationLinks.size()); |
EXPECT_EQ("link1", notificationLinks[0]); |
EXPECT_EQ("link2", notificationLinks[1]); |
-} |
+} |