Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/Notification.cpp

Issue 5797488346791936: Issue 1107 - Support notifications (Closed)
Left Patch Set: simplify the expression Created Jan. 22, 2015, 7:54 p.m.
Right Patch Set: fix comment Created Jan. 23, 2015, 3:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/Notification.cpp ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include "BaseJsTest.h" 18 #include "BaseJsTest.h"
19 19
20 using namespace AdblockPlus; 20 using namespace AdblockPlus;
21 21
22 /* This define enables NotificationMockWebRequestTest but to run it 22 // This define enables NotificationMockWebRequestTest but to run it
23 // one need to set INITIAL_DELAY to about 2000 msec in notification.js. 23 // one need to set INITIAL_DELAY to about 2000 msec in notification.js.
24 #include <chrono> 24 //#define NotificationMockWebRequestTest_ENABLED
25 #include <thread>
26 #define NotificationMockWebRequestTest_ENABLED
27 //*/
28 25
29 namespace 26 namespace
30 { 27 {
31 typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr; 28 typedef std::tr1::shared_ptr<FilterEngine> FilterEnginePtr;
32 29
33 class NotificationTest : public BaseJsTest 30 class NotificationTest : public BaseJsTest
34 { 31 {
35 protected: 32 protected:
36 FilterEnginePtr filterEngine; 33 FilterEnginePtr filterEngine;
37 void SetUp() override 34 void SetUp() override
38 { 35 {
39 BaseJsTest::SetUp(); 36 BaseJsTest::SetUp();
40 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem())); 37 jsEngine->SetFileSystem(FileSystemPtr(new LazyFileSystem()));
41 jsEngine->SetWebRequest(std::tr1::make_shared<LazyWebRequest>()); 38 jsEngine->SetWebRequest(WebRequestPtr(new LazyWebRequest()));
42 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); 39 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem()));
43 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); 40 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine);
44 } 41 }
45 42
46 void AddNotificaiton(const std::string& notification) 43 void AddNotification(const std::string& notification)
47 { 44 {
48 jsEngine->Evaluate("(function()" 45 jsEngine->Evaluate("(function()"
49 "{" 46 "{"
50 "require('notification').Notification.addNotification(" + notification + ");" 47 "require('notification').Notification.addNotification(" + notification + ");"
51 "})();"); 48 "})();");
52 } 49 }
53 }; 50 };
54 51
55 class MockWebRequest : public WebRequest 52 class MockWebRequest : public WebRequest
56 { 53 {
57 public: 54 public:
58 std::string m_responseText; 55 std::string responseText;
59 explicit MockWebRequest(const std::string& notification) 56 explicit MockWebRequest(const std::string& notification)
60 : m_responseText(notification) 57 : responseText(notification)
61 { 58 {
62 } 59 }
63 ServerResponse GET(const std::string& url, 60 ServerResponse GET(const std::string& url,
64 const HeaderList& requestHeaders) const override 61 const HeaderList& requestHeaders) const
65 { 62 {
66 if (url.find("/notification.json") == std::string::npos) 63 if (url.find("/notification.json") == std::string::npos)
67 { 64 {
68 return ServerResponse(); 65 return ServerResponse();
69 } 66 }
70 ServerResponse serverResponse; 67 ServerResponse serverResponse;
71 serverResponse.status = NS_OK; 68 serverResponse.status = NS_OK;
72 serverResponse.responseStatus = 200; 69 serverResponse.responseStatus = 200;
73 serverResponse.responseText = m_responseText; 70 serverResponse.responseText = responseText;
74 return serverResponse; 71 return serverResponse;
75 } 72 }
76 }; 73 };
77 74
78 #ifdef NotificationMockWebRequestTest_ENABLED 75 #ifdef NotificationMockWebRequestTest_ENABLED
79 class NotificationMockWebRequestTest : public BaseJsTest 76 class NotificationMockWebRequestTest : public BaseJsTest
80 { 77 {
81 protected: 78 protected:
82 FilterEnginePtr filterEngine; 79 FilterEnginePtr filterEngine;
83 void SetUp() override 80 void SetUp() override
(...skipping 13 matching lines...) Expand all
97 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson Text)); 94 jsEngine->SetWebRequest(std::tr1::make_shared<MockWebRequest>(responseJson Text));
98 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem())); 95 jsEngine->SetLogSystem(LogSystemPtr(new DefaultLogSystem()));
99 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine); 96 filterEngine = std::tr1::make_shared<FilterEngine>(jsEngine);
100 } 97 }
101 }; 98 };
102 #endif 99 #endif
103 } 100 }
104 101
105 TEST_F(NotificationTest, NoNotifications) 102 TEST_F(NotificationTest, NoNotifications)
106 { 103 {
107 auto notification = filterEngine->GetNextNotificationToShow(); 104 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
108 EXPECT_EQ(nullptr, notification); 105 EXPECT_EQ(NULL, notification.get());
109 } 106 }
110 107
111 #ifdef NotificationMockWebRequestTest_ENABLED 108 #ifdef NotificationMockWebRequestTest_ENABLED
112 TEST_F(NotificationMockWebRequestTest, SingleNotification) 109 TEST_F(NotificationMockWebRequestTest, SingleNotification)
113 { 110 {
114 std::this_thread::sleep_for(std::chrono::seconds(5)); // it's a hack 111 AdblockPlus::Sleep(5000/*msec*/); // it's a hack
115 auto notification = filterEngine->GetNextNotificationToShow(); 112 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
116 // try another one immediately to avoid queuing of the next notification by 113 // try another one immediately to avoid queuing of the next notification by
117 // the timer. 114 // the timer.
118 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); 115 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get());
119 ASSERT_NE(nullptr, notification.get()); 116 ASSERT_TRUE(notification);
120 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); 117 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
121 EXPECT_EQ("Title", notification->GetTitle()); 118 EXPECT_EQ("Title", notification->GetTitle());
122 EXPECT_EQ("message", notification->GetMessageString()); 119 EXPECT_EQ("message", notification->GetMessageString());
123 } 120 }
124 #endif 121 #endif
125 122
126 TEST_F(NotificationTest, AddNotification) 123 TEST_F(NotificationTest, AddNotification)
127 { 124 {
128 AddNotificaiton("{" 125 AddNotification("{"
129 "type: 'critical'," 126 "type: 'critical',"
130 "title: 'testTitle'," 127 "title: 'testTitle',"
131 "message: 'testMessage'," 128 "message: 'testMessage',"
132 "}"); 129 "}");
133 auto notification = filterEngine->GetNextNotificationToShow(); 130 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
134 ASSERT_NE(nullptr, notification.get()); 131 ASSERT_TRUE(notification);
135 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); 132 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
136 EXPECT_EQ("testTitle", notification->GetTitle()); 133 EXPECT_EQ("testTitle", notification->GetTitle());
137 EXPECT_EQ("testMessage", notification->GetMessageString()); 134 EXPECT_EQ("testMessage", notification->GetMessageString());
138 } 135 }
139 136
140 TEST_F(NotificationTest, FilterByUrl1) 137 TEST_F(NotificationTest, FilterByUrl)
141 { 138 {
142 AddNotificaiton("{ id: 'no-filter', type: 'critical' }"); 139 AddNotification("{ id: 'no-filter', type: 'critical' }");
143 AddNotificaiton("{ id: 'www.com', type: 'information'," 140 AddNotification("{ id: 'www.com', type: 'information',"
144 "urlFilters:['http://www.com']" 141 "urlFilters:['http://www.com']"
145 "}"); 142 "}");
146 AddNotificaiton("{ id: 'www.de', type: 'question'," 143 AddNotification("{ id: 'www.de', type: 'question',"
147 "urlFilters:['http://www.de']" 144 "urlFilters:['http://www.de']"
148 "}"); 145 "}");
149 146
150 auto notification = filterEngine->GetNextNotificationToShow(); 147 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
151 ASSERT_NE(nullptr, notification.get()); 148 ASSERT_TRUE(notification);
152 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( )); 149 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_CRITICAL, notification->GetType( ));
153 150
154 notification = filterEngine->GetNextNotificationToShow("http://www.de"); 151 notification = filterEngine->GetNextNotificationToShow("http://www.de");
155 ASSERT_NE(nullptr, notification.get()); 152 ASSERT_TRUE(notification);
156 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( )); 153 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_QUESTION, notification->GetType( ));
157 154
158 notification = filterEngine->GetNextNotificationToShow("http://www.com"); 155 notification = filterEngine->GetNextNotificationToShow("http://www.com");
159 ASSERT_NE(nullptr, notification.get()); 156 ASSERT_TRUE(notification);
160 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe()); 157 EXPECT_EQ(NotificationType::NOTIFICATION_TYPE_INFORMATION, notification->GetTy pe());
161 } 158 }
162 159
163 TEST_F(NotificationTest, MarkAsShown) 160 TEST_F(NotificationTest, MarkAsShown)
164 { 161 {
165 AddNotificaiton("{ id: 'id', type: 'question' }"); 162 AddNotification("{ id: 'id', type: 'question' }");
166 auto notification = filterEngine->GetNextNotificationToShow(); 163 NotificationPtr notification = filterEngine->GetNextNotificationToShow();
167 EXPECT_NE(nullptr, notification); 164 EXPECT_TRUE(notification);
168 notification = filterEngine->GetNextNotificationToShow(); 165 notification = filterEngine->GetNextNotificationToShow();
169 ASSERT_NE(nullptr, notification); 166 ASSERT_TRUE(notification);
170 notification->MarkAsShown(); 167 notification->MarkAsShown();
171 EXPECT_EQ(nullptr, filterEngine->GetNextNotificationToShow().get()); 168 EXPECT_EQ(NULL, filterEngine->GetNextNotificationToShow().get());
172 } 169 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld