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

Side by Side Diff: test/UpdateCheck.cpp

Issue 29435645: Issue 4688 - stop using of LazyWebRequest in tests/UpdateCheck.cpp (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created May 10, 2017, 4:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/BaseJsTest.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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
(...skipping 13 matching lines...) Expand all
24 namespace 24 namespace
25 { 25 {
26 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; 26 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
27 27
28 void FindAndReplace(std::string& source, const std::string& find, const std::s tring& replace) 28 void FindAndReplace(std::string& source, const std::string& find, const std::s tring& replace)
29 { 29 {
30 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po s += replace.size()) 30 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po s += replace.size())
31 source.replace(pos, find.size(), replace); 31 source.replace(pos, find.size(), replace);
32 } 32 }
33 33
34 std::string previousRequestUrl;
35 class TestWebRequest : public LazyWebRequest
36 {
37 public:
38 AdblockPlus::ServerResponse response;
39 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const
40 {
41 if (url.find("easylist") != std::string::npos)
42 return LazyWebRequest::GET(url, requestHeaders);
43
44 previousRequestUrl = url;
45 return response;
46 }
47 };
48
49 class UpdateCheckTest : public ::testing::Test 34 class UpdateCheckTest : public ::testing::Test
50 { 35 {
51 protected: 36 protected:
52 AdblockPlus::AppInfo appInfo; 37 AdblockPlus::AppInfo appInfo;
53 std::shared_ptr<TestWebRequest> webRequest; 38 AdblockPlus::ServerResponse webRequestResponse;
39 DelayedWebRequest::SharedTasks webRequestTasks;
40 DelayedTimer::SharedTasks timerTasks;
54 AdblockPlus::JsEnginePtr jsEngine; 41 AdblockPlus::JsEnginePtr jsEngine;
55 FilterEnginePtr filterEngine; 42 FilterEnginePtr filterEngine;
56 43
57 bool eventCallbackCalled; 44 bool eventCallbackCalled;
58 AdblockPlus::JsValueList eventCallbackParams; 45 AdblockPlus::JsValueList eventCallbackParams;
59 bool updateCallbackCalled; 46 bool updateCallbackCalled;
60 std::string updateError; 47 std::string updateError;
61 48
62 void SetUp() 49 void SetUp()
63 { 50 {
64 webRequest = std::make_shared<TestWebRequest>();
65
66 eventCallbackCalled = false; 51 eventCallbackCalled = false;
67 updateCallbackCalled = false; 52 updateCallbackCalled = false;
68 Reset(); 53 Reset();
69 } 54 }
70 55
71 void Reset() 56 void Reset()
72 { 57 {
73 JsEngineCreationParameters jsEngineParams; 58 JsEngineCreationParameters jsEngineParams;
74 jsEngineParams.appInfo = appInfo; 59 jsEngineParams.appInfo = appInfo;
75 jsEngineParams.logSystem.reset(new LazyLogSystem()); 60 jsEngineParams.logSystem.reset(new LazyLogSystem());
76 jsEngineParams.fileSystem.reset(new LazyFileSystem()); 61 jsEngineParams.fileSystem.reset(new LazyFileSystem());
77 jsEngineParams.timer = CreateDefaultTimer(); 62 jsEngineParams.timer = DelayedTimer::New(timerTasks);
63 jsEngineParams.webRequest = DelayedWebRequest::New(webRequestTasks);
78 jsEngine = CreateJsEngine(std::move(jsEngineParams)); 64 jsEngine = CreateJsEngine(std::move(jsEngineParams));
79 jsEngine->SetWebRequest(webRequest);
80 jsEngine->SetEventCallback("updateAvailable", [this](JsValueList&& params) 65 jsEngine->SetEventCallback("updateAvailable", [this](JsValueList&& params)
81 { 66 {
82 eventCallbackCalled = true; 67 eventCallbackCalled = true;
83 eventCallbackParams = std::move(params); 68 eventCallbackParams = std::move(params);
84 }); 69 });
85 70
86 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine); 71 filterEngine = AdblockPlus::FilterEngine::Create(jsEngine);
87 } 72 }
88 73
74 // Returns a URL or the empty string if there is no such request.
75 std::string ProcessPendingUpdateWebRequest()
76 {
77 auto ii = webRequestTasks->begin();
78 while (ii != webRequestTasks->end())
79 {
hub 2017/05/10 17:53:19 Maybe I'm missing something, but wouldn't a for lo
sergei 2017/05/10 18:15:54 it will but it won't remove the found entry. Do yo
hub 2017/05/10 18:55:54 uh. I missed that little detail :-/
80 if (ii->url.find("update.json") != std::string::npos)
81 {
82 ii->getCallback(webRequestResponse);
83 auto url = ii->url;
84 webRequestTasks->erase(ii);
85 return url;
86 }
87 ++ii;
88 }
89 return std::string();
90 }
91
89 void ForceUpdateCheck() 92 void ForceUpdateCheck()
90 { 93 {
91 filterEngine->ForceUpdateCheck( 94 filterEngine->ForceUpdateCheck([this](const std::string& error)
92 std::bind(&UpdateCheckTest::UpdateCallback, this, std::placeholders::_ 1)); 95 {
93 } 96 updateCallbackCalled = true;
94 97 updateError = error;
95 void UpdateCallback(const std::string& error) 98 });
96 { 99 DelayedTimer::ProcessImmediateTimers(timerTasks);
97 updateCallbackCalled = true;
98 updateError = error;
99 } 100 }
100 }; 101 };
101 } 102 }
102 103
103 TEST_F(UpdateCheckTest, RequestFailure) 104 TEST_F(UpdateCheckTest, RequestFailure)
104 { 105 {
105 webRequest->response.status = IWebRequest::NS_ERROR_FAILURE; 106 webRequestResponse.status = IWebRequest::NS_ERROR_FAILURE;
106 107
107 appInfo.name = "1"; 108 appInfo.name = "1";
108 appInfo.version = "3"; 109 appInfo.version = "3";
109 appInfo.application = "4"; 110 appInfo.application = "4";
110 appInfo.applicationVersion = "2"; 111 appInfo.applicationVersion = "2";
111 appInfo.developmentBuild = false; 112 appInfo.developmentBuild = false;
112 113
113 Reset(); 114 Reset();
114 ForceUpdateCheck(); 115 ForceUpdateCheck();
115 116
116 AdblockPlus::Sleep(100); 117 auto requestUrl = ProcessPendingUpdateWebRequest();
117 118
118 ASSERT_FALSE(eventCallbackCalled); 119 ASSERT_FALSE(eventCallbackCalled);
119 ASSERT_TRUE(updateCallbackCalled); 120 ASSERT_TRUE(updateCallbackCalled);
120 ASSERT_FALSE(updateError.empty()); 121 ASSERT_FALSE(updateError.empty());
121 122
122 std::string expectedUrl(filterEngine->GetPref("update_url_release").AsString() ); 123 std::string expectedUrl(filterEngine->GetPref("update_url_release").AsString() );
123 std::string platform = jsEngine->Evaluate("require('info').platform").AsString (); 124 std::string platform = jsEngine->Evaluate("require('info').platform").AsString ();
124 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion").AsString(); 125 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion").AsString();
125 126
126 FindAndReplace(expectedUrl, "%NAME%", appInfo.name); 127 FindAndReplace(expectedUrl, "%NAME%", appInfo.name);
127 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update 128 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update
128 expectedUrl += "&addonName=" + appInfo.name + 129 expectedUrl += "&addonName=" + appInfo.name +
129 "&addonVersion=" + appInfo.version + 130 "&addonVersion=" + appInfo.version +
130 "&application=" + appInfo.application + 131 "&application=" + appInfo.application +
131 "&applicationVersion=" + appInfo.applicationVersion + 132 "&applicationVersion=" + appInfo.applicationVersion +
132 "&platform=" + platform + 133 "&platform=" + platform +
133 "&platformVersion=" + platformVersion + 134 "&platformVersion=" + platformVersion +
134 "&lastVersion=0&downloadCount=0"; 135 "&lastVersion=0&downloadCount=0";
135 ASSERT_EQ(expectedUrl, previousRequestUrl); 136 ASSERT_EQ(expectedUrl, requestUrl);
136 } 137 }
137 138
138 TEST_F(UpdateCheckTest, UpdateAvailable) 139 TEST_F(UpdateCheckTest, UpdateAvailable)
139 { 140 {
140 webRequest->response.status = IWebRequest::NS_OK; 141 webRequestResponse.status = IWebRequest::NS_OK;
141 webRequest->response.responseStatus = 200; 142 webRequestResponse.responseStatus = 200;
142 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tps://foo.bar/\"}}"; 143 webRequestResponse.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"http s://foo.bar/\"}}";
143 144
144 appInfo.name = "1"; 145 appInfo.name = "1";
145 appInfo.version = "3"; 146 appInfo.version = "3";
146 appInfo.application = "4"; 147 appInfo.application = "4";
147 appInfo.applicationVersion = "2"; 148 appInfo.applicationVersion = "2";
148 appInfo.developmentBuild = true; 149 appInfo.developmentBuild = true;
149 150
150 Reset(); 151 Reset();
151 ForceUpdateCheck(); 152 ForceUpdateCheck();
152 153
153 AdblockPlus::Sleep(100); 154 auto requestUrl = ProcessPendingUpdateWebRequest();
154 155
155 ASSERT_TRUE(eventCallbackCalled); 156 ASSERT_TRUE(eventCallbackCalled);
156 ASSERT_EQ(1u, eventCallbackParams.size()); 157 ASSERT_EQ(1u, eventCallbackParams.size());
157 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0].AsString()); 158 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0].AsString());
158 ASSERT_TRUE(updateCallbackCalled); 159 ASSERT_TRUE(updateCallbackCalled);
159 ASSERT_TRUE(updateError.empty()); 160 ASSERT_TRUE(updateError.empty());
160 161
161 std::string expectedUrl(filterEngine->GetPref("update_url_devbuild").AsString( )); 162 std::string expectedUrl(filterEngine->GetPref("update_url_devbuild").AsString( ));
162 std::string platform = jsEngine->Evaluate("require('info').platform").AsString (); 163 std::string platform = jsEngine->Evaluate("require('info').platform").AsString ();
163 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion").AsString(); 164 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion").AsString();
164 165
165 FindAndReplace(expectedUrl, "%NAME%", appInfo.name); 166 FindAndReplace(expectedUrl, "%NAME%", appInfo.name);
166 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update 167 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update
167 expectedUrl += "&addonName=" + appInfo.name + 168 expectedUrl += "&addonName=" + appInfo.name +
168 "&addonVersion=" + appInfo.version + 169 "&addonVersion=" + appInfo.version +
169 "&application=" + appInfo.application + 170 "&application=" + appInfo.application +
170 "&applicationVersion=" + appInfo.applicationVersion + 171 "&applicationVersion=" + appInfo.applicationVersion +
171 "&platform=" + platform + 172 "&platform=" + platform +
172 "&platformVersion=" + platformVersion + 173 "&platformVersion=" + platformVersion +
173 "&lastVersion=0&downloadCount=0"; 174 "&lastVersion=0&downloadCount=0";
174 ASSERT_EQ(expectedUrl, previousRequestUrl); 175 ASSERT_EQ(expectedUrl, requestUrl);
175 } 176 }
176 177
177 TEST_F(UpdateCheckTest, ApplicationUpdateAvailable) 178 TEST_F(UpdateCheckTest, ApplicationUpdateAvailable)
178 { 179 {
179 webRequest->response.status = IWebRequest::NS_OK; 180 webRequestResponse.status = IWebRequest::NS_OK;
180 webRequest->response.responseStatus = 200; 181 webRequestResponse.responseStatus = 200;
181 webRequest->response.responseText = "{\"1/4\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}"; 182 webRequestResponse.responseText = "{\"1/4\": {\"version\":\"3.1\",\"url\":\"ht tps://foo.bar/\"}}";
182 183
183 appInfo.name = "1"; 184 appInfo.name = "1";
184 appInfo.version = "3"; 185 appInfo.version = "3";
185 appInfo.application = "4"; 186 appInfo.application = "4";
186 appInfo.applicationVersion = "2"; 187 appInfo.applicationVersion = "2";
187 appInfo.developmentBuild = true; 188 appInfo.developmentBuild = true;
188 189
189 Reset(); 190 Reset();
190 ForceUpdateCheck(); 191 ForceUpdateCheck();
191 192
192 AdblockPlus::Sleep(100); 193 ProcessPendingUpdateWebRequest();
193 194
194 ASSERT_TRUE(eventCallbackCalled); 195 ASSERT_TRUE(eventCallbackCalled);
195 ASSERT_EQ(1u, eventCallbackParams.size()); 196 ASSERT_EQ(1u, eventCallbackParams.size());
196 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0].AsString()); 197 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0].AsString());
197 ASSERT_TRUE(updateCallbackCalled);
198 ASSERT_TRUE(updateError.empty()); 198 ASSERT_TRUE(updateError.empty());
199 } 199 }
200 200
201 TEST_F(UpdateCheckTest, WrongApplication) 201 TEST_F(UpdateCheckTest, WrongApplication)
202 { 202 {
203 webRequest->response.status = IWebRequest::NS_OK; 203 webRequestResponse.status = IWebRequest::NS_OK;
204 webRequest->response.responseStatus = 200; 204 webRequestResponse.responseStatus = 200;
205 webRequest->response.responseText = "{\"1/3\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}"; 205 webRequestResponse.responseText = "{\"1/3\": {\"version\":\"3.1\",\"url\":\"ht tps://foo.bar/\"}}";
206 206
207 appInfo.name = "1"; 207 appInfo.name = "1";
208 appInfo.version = "3"; 208 appInfo.version = "3";
209 appInfo.application = "4"; 209 appInfo.application = "4";
210 appInfo.applicationVersion = "2"; 210 appInfo.applicationVersion = "2";
211 appInfo.developmentBuild = true; 211 appInfo.developmentBuild = true;
212 212
213 Reset(); 213 Reset();
214 ForceUpdateCheck(); 214 ForceUpdateCheck();
215 215
216 AdblockPlus::Sleep(100); 216 ProcessPendingUpdateWebRequest();
217 217
218 ASSERT_FALSE(eventCallbackCalled); 218 ASSERT_FALSE(eventCallbackCalled);
219 ASSERT_TRUE(updateCallbackCalled); 219 ASSERT_TRUE(updateCallbackCalled);
220 ASSERT_TRUE(updateError.empty()); 220 ASSERT_TRUE(updateError.empty());
221 } 221 }
222 222
223 TEST_F(UpdateCheckTest, WrongVersion) 223 TEST_F(UpdateCheckTest, WrongVersion)
224 { 224 {
225 webRequest->response.status = IWebRequest::NS_OK; 225 webRequestResponse.status = IWebRequest::NS_OK;
226 webRequest->response.responseStatus = 200; 226 webRequestResponse.responseStatus = 200;
227 webRequest->response.responseText = "{\"1\": {\"version\":\"3\",\"url\":\"http s://foo.bar/\"}}"; 227 webRequestResponse.responseText = "{\"1\": {\"version\":\"3\",\"url\":\"https: //foo.bar/\"}}";
228 228
229 appInfo.name = "1"; 229 appInfo.name = "1";
230 appInfo.version = "3"; 230 appInfo.version = "3";
231 appInfo.application = "4"; 231 appInfo.application = "4";
232 appInfo.applicationVersion = "2"; 232 appInfo.applicationVersion = "2";
233 appInfo.developmentBuild = true; 233 appInfo.developmentBuild = true;
234 234
235 Reset(); 235 Reset();
236 ForceUpdateCheck(); 236 ForceUpdateCheck();
237 237
238 AdblockPlus::Sleep(100); 238 ProcessPendingUpdateWebRequest();
239 239
240 ASSERT_FALSE(eventCallbackCalled); 240 ASSERT_FALSE(eventCallbackCalled);
241 ASSERT_TRUE(updateCallbackCalled); 241 ASSERT_TRUE(updateCallbackCalled);
242 ASSERT_TRUE(updateError.empty()); 242 ASSERT_TRUE(updateError.empty());
243 } 243 }
244 244
245 TEST_F(UpdateCheckTest, WrongURL) 245 TEST_F(UpdateCheckTest, WrongURL)
246 { 246 {
247 webRequest->response.status = IWebRequest::NS_OK; 247 webRequestResponse.status = IWebRequest::NS_OK;
248 webRequest->response.responseStatus = 200; 248 webRequestResponse.responseStatus = 200;
249 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tp://insecure/\"}}"; 249 webRequestResponse.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"http ://insecure/\"}}";
250 250
251 appInfo.name = "1"; 251 appInfo.name = "1";
252 appInfo.version = "3"; 252 appInfo.version = "3";
253 appInfo.application = "4"; 253 appInfo.application = "4";
254 appInfo.applicationVersion = "2"; 254 appInfo.applicationVersion = "2";
255 appInfo.developmentBuild = true; 255 appInfo.developmentBuild = true;
256 256
257 Reset(); 257 Reset();
258 ForceUpdateCheck(); 258 ForceUpdateCheck();
259 259
260 AdblockPlus::Sleep(100); 260 ProcessPendingUpdateWebRequest();
261 261
262 ASSERT_FALSE(eventCallbackCalled); 262 ASSERT_FALSE(eventCallbackCalled);
263 ASSERT_TRUE(updateCallbackCalled); 263 ASSERT_TRUE(updateCallbackCalled);
264 ASSERT_FALSE(updateError.empty()); 264 ASSERT_FALSE(updateError.empty());
265 } 265 }
OLDNEW
« no previous file with comments | « test/BaseJsTest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld