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

Side by Side Diff: test/UpdateCheck.cpp

Issue 29508591: Issue 5450 - don't expose std::shared_ptr<FilterEngine> (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: rebase Created Aug. 7, 2017, 1:08 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/Prefs.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
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 <functional> 18 #include <functional>
19 19
20 #include "BaseJsTest.h" 20 #include "BaseJsTest.h"
21 21
22 using namespace AdblockPlus; 22 using namespace AdblockPlus;
23 23
24 namespace 24 namespace
25 { 25 {
26 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
27
28 void FindAndReplace(std::string& source, const std::string& find, const std::s tring& replace) 26 void FindAndReplace(std::string& source, const std::string& find, const std::s tring& replace)
29 { 27 {
30 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po s += replace.size()) 28 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po s += replace.size())
31 source.replace(pos, find.size(), replace); 29 source.replace(pos, find.size(), replace);
32 } 30 }
33 31
34 class UpdateCheckTest : public BaseJsTest 32 class UpdateCheckTest : public BaseJsTest
35 { 33 {
36 protected: 34 protected:
37 AdblockPlus::AppInfo appInfo; 35 AdblockPlus::AppInfo appInfo;
38 AdblockPlus::ServerResponse webRequestResponse; 36 AdblockPlus::ServerResponse webRequestResponse;
39 DelayedWebRequest::SharedTasks webRequestTasks; 37 DelayedWebRequest::SharedTasks webRequestTasks;
40 DelayedTimer::SharedTasks timerTasks; 38 DelayedTimer::SharedTasks timerTasks;
41 FilterEnginePtr filterEngine;
42 39
43 bool eventCallbackCalled; 40 bool eventCallbackCalled;
44 AdblockPlus::JsValueList eventCallbackParams; 41 AdblockPlus::JsValueList eventCallbackParams;
45 bool updateCallbackCalled; 42 bool updateCallbackCalled;
46 std::string updateError; 43 std::string updateError;
47 44
48 void SetUp() 45 void SetUp()
49 { 46 {
50 eventCallbackCalled = false; 47 eventCallbackCalled = false;
51 updateCallbackCalled = false; 48 updateCallbackCalled = false;
52 } 49 }
53 50
54 void CreateFilterEngine() 51 void CreateFilterEngine()
55 { 52 {
56 LazyFileSystem* fileSystem; 53 LazyFileSystem* fileSystem;
57 ThrowingPlatformCreationParameters platformParams; 54 ThrowingPlatformCreationParameters platformParams;
58 platformParams.logSystem.reset(new LazyLogSystem()); 55 platformParams.logSystem.reset(new LazyLogSystem());
59 platformParams.timer = DelayedTimer::New(timerTasks); 56 platformParams.timer = DelayedTimer::New(timerTasks);
60 platformParams.fileSystem.reset(fileSystem = new LazyFileSystem()); 57 platformParams.fileSystem.reset(fileSystem = new LazyFileSystem());
61 platformParams.webRequest = DelayedWebRequest::New(webRequestTasks); 58 platformParams.webRequest = DelayedWebRequest::New(webRequestTasks);
62 platform.reset(new Platform(std::move(platformParams))); 59 platform.reset(new Platform(std::move(platformParams)));
63 platform->SetUpJsEngine(appInfo); 60 platform->SetUpJsEngine(appInfo);
64 GetJsEngine().SetEventCallback("updateAvailable", [this](JsValueList&& par ams) 61 GetJsEngine().SetEventCallback("updateAvailable", [this](JsValueList&& par ams)
65 { 62 {
66 eventCallbackCalled = true; 63 eventCallbackCalled = true;
67 eventCallbackParams = std::move(params); 64 eventCallbackParams = std::move(params);
68 }); 65 });
69 66
70 filterEngine = ::CreateFilterEngine(*fileSystem, *platform); 67 ::CreateFilterEngine(*fileSystem, *platform);
71 } 68 }
72 69
73 // Returns a URL or the empty string if there is no such request. 70 // Returns a URL or the empty string if there is no such request.
74 std::string ProcessPendingUpdateWebRequest() 71 std::string ProcessPendingUpdateWebRequest()
75 { 72 {
76 auto ii = webRequestTasks->begin(); 73 auto ii = webRequestTasks->begin();
77 while (ii != webRequestTasks->end()) 74 while (ii != webRequestTasks->end())
78 { 75 {
79 if (ii->url.find("update.json") != std::string::npos) 76 if (ii->url.find("update.json") != std::string::npos)
80 { 77 {
81 ii->getCallback(webRequestResponse); 78 ii->getCallback(webRequestResponse);
82 auto url = ii->url; 79 auto url = ii->url;
83 webRequestTasks->erase(ii); 80 webRequestTasks->erase(ii);
84 return url; 81 return url;
85 } 82 }
86 ++ii; 83 ++ii;
87 } 84 }
88 return std::string(); 85 return std::string();
89 } 86 }
90 87
91 void ForceUpdateCheck() 88 void ForceUpdateCheck()
92 { 89 {
93 filterEngine->ForceUpdateCheck([this](const std::string& error) 90 platform->GetFilterEngine().ForceUpdateCheck([this](const std::string& err or)
94 { 91 {
95 updateCallbackCalled = true; 92 updateCallbackCalled = true;
96 updateError = error; 93 updateError = error;
97 }); 94 });
98 DelayedTimer::ProcessImmediateTimers(timerTasks); 95 DelayedTimer::ProcessImmediateTimers(timerTasks);
99 } 96 }
100 }; 97 };
101 } 98 }
102 99
103 TEST_F(UpdateCheckTest, RequestFailure) 100 TEST_F(UpdateCheckTest, RequestFailure)
104 { 101 {
105 webRequestResponse.status = IWebRequest::NS_ERROR_FAILURE; 102 webRequestResponse.status = IWebRequest::NS_ERROR_FAILURE;
106 103
107 appInfo.name = "1"; 104 appInfo.name = "1";
108 appInfo.version = "3"; 105 appInfo.version = "3";
109 appInfo.application = "4"; 106 appInfo.application = "4";
110 appInfo.applicationVersion = "2"; 107 appInfo.applicationVersion = "2";
111 appInfo.developmentBuild = false; 108 appInfo.developmentBuild = false;
112 109
113 CreateFilterEngine(); 110 CreateFilterEngine();
114 ForceUpdateCheck(); 111 ForceUpdateCheck();
115 112
116 auto requestUrl = ProcessPendingUpdateWebRequest(); 113 auto requestUrl = ProcessPendingUpdateWebRequest();
117 114
118 ASSERT_FALSE(eventCallbackCalled); 115 ASSERT_FALSE(eventCallbackCalled);
119 ASSERT_TRUE(updateCallbackCalled); 116 ASSERT_TRUE(updateCallbackCalled);
120 ASSERT_FALSE(updateError.empty()); 117 ASSERT_FALSE(updateError.empty());
121 118
122 std::string expectedUrl(filterEngine->GetPref("update_url_release").AsString() ); 119 std::string expectedUrl(platform->GetFilterEngine().GetPref("update_url_releas e").AsString());
123 std::string platform = GetJsEngine().Evaluate("require('info').platform").AsSt ring(); 120 std::string platform = GetJsEngine().Evaluate("require('info').platform").AsSt ring();
124 std::string platformVersion = GetJsEngine().Evaluate("require('info').platform Version").AsString(); 121 std::string platformVersion = GetJsEngine().Evaluate("require('info').platform Version").AsString();
125 122
126 FindAndReplace(expectedUrl, "%NAME%", appInfo.name); 123 FindAndReplace(expectedUrl, "%NAME%", appInfo.name);
127 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update 124 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update
128 expectedUrl += "&addonName=" + appInfo.name + 125 expectedUrl += "&addonName=" + appInfo.name +
129 "&addonVersion=" + appInfo.version + 126 "&addonVersion=" + appInfo.version +
130 "&application=" + appInfo.application + 127 "&application=" + appInfo.application +
131 "&applicationVersion=" + appInfo.applicationVersion + 128 "&applicationVersion=" + appInfo.applicationVersion +
132 "&platform=" + platform + 129 "&platform=" + platform +
(...skipping 18 matching lines...) Expand all
151 ForceUpdateCheck(); 148 ForceUpdateCheck();
152 149
153 auto requestUrl = ProcessPendingUpdateWebRequest(); 150 auto requestUrl = ProcessPendingUpdateWebRequest();
154 151
155 ASSERT_TRUE(eventCallbackCalled); 152 ASSERT_TRUE(eventCallbackCalled);
156 ASSERT_EQ(1u, eventCallbackParams.size()); 153 ASSERT_EQ(1u, eventCallbackParams.size());
157 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0].AsString()); 154 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0].AsString());
158 ASSERT_TRUE(updateCallbackCalled); 155 ASSERT_TRUE(updateCallbackCalled);
159 ASSERT_TRUE(updateError.empty()); 156 ASSERT_TRUE(updateError.empty());
160 157
161 std::string expectedUrl(filterEngine->GetPref("update_url_devbuild").AsString( )); 158 std::string expectedUrl(platform->GetFilterEngine().GetPref("update_url_devbui ld").AsString());
162 std::string platform = GetJsEngine().Evaluate("require('info').platform").AsSt ring(); 159 std::string platform = GetJsEngine().Evaluate("require('info').platform").AsSt ring();
163 std::string platformVersion = GetJsEngine().Evaluate("require('info').platform Version").AsString(); 160 std::string platformVersion = GetJsEngine().Evaluate("require('info').platform Version").AsString();
164 161
165 FindAndReplace(expectedUrl, "%NAME%", appInfo.name); 162 FindAndReplace(expectedUrl, "%NAME%", appInfo.name);
166 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update 163 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update
167 expectedUrl += "&addonName=" + appInfo.name + 164 expectedUrl += "&addonName=" + appInfo.name +
168 "&addonVersion=" + appInfo.version + 165 "&addonVersion=" + appInfo.version +
169 "&application=" + appInfo.application + 166 "&application=" + appInfo.application +
170 "&applicationVersion=" + appInfo.applicationVersion + 167 "&applicationVersion=" + appInfo.applicationVersion +
171 "&platform=" + platform + 168 "&platform=" + platform +
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 \"version\": \"1.0.2\",\ 270 \"version\": \"1.0.2\",\
274 \"url\": \"https://downloads.adblockplus.org/test-1.0.2.tar.gz?update\"\ 271 \"url\": \"https://downloads.adblockplus.org/test-1.0.2.tar.gz?update\"\
275 }\ 272 }\
276 }"; 273 }";
277 274
278 appInfo.name = "test"; 275 appInfo.name = "test";
279 appInfo.version = "1.0.1"; 276 appInfo.version = "1.0.1";
280 CreateFilterEngine(); 277 CreateFilterEngine();
281 278
282 int timesCalled = 0; 279 int timesCalled = 0;
283 filterEngine->SetUpdateAvailableCallback([&timesCalled](const std::string&)->v oid 280 platform->GetFilterEngine().SetUpdateAvailableCallback([&timesCalled](const st d::string&)->void
284 { 281 {
285 ++timesCalled; 282 ++timesCalled;
286 }); 283 });
287 ForceUpdateCheck(); 284 ForceUpdateCheck();
288 285
289 // ensure that the was the corresponding request 286 // ensure that the was the corresponding request
290 EXPECT_FALSE(ProcessPendingUpdateWebRequest().empty()); 287 EXPECT_FALSE(ProcessPendingUpdateWebRequest().empty());
291 288
292 EXPECT_EQ(1, timesCalled); 289 EXPECT_EQ(1, timesCalled);
293 290
294 // filterEngine->SetUpdateAvailableCallback overriddes previously installed on JsEngine 291 // FilterEngine::SetUpdateAvailableCallback overriddes previously installed on JsEngine
295 // handler for "updateAvailable" event. 292 // handler for "updateAvailable" event.
296 EXPECT_FALSE(eventCallbackCalled); 293 EXPECT_FALSE(eventCallbackCalled);
297 294
298 filterEngine->RemoveUpdateAvailableCallback(); 295 platform->GetFilterEngine().RemoveUpdateAvailableCallback();
299 ForceUpdateCheck(); 296 ForceUpdateCheck();
300 297
301 // ensure that the was the corresponding request 298 // ensure that the was the corresponding request
302 EXPECT_FALSE(ProcessPendingUpdateWebRequest().empty()); 299 EXPECT_FALSE(ProcessPendingUpdateWebRequest().empty());
303 300
304 EXPECT_FALSE(eventCallbackCalled); 301 EXPECT_FALSE(eventCallbackCalled);
305 EXPECT_EQ(1, timesCalled); 302 EXPECT_EQ(1, timesCalled);
306 303
307 // previous handler is not restored 304 // previous handler is not restored
308 EXPECT_FALSE(eventCallbackCalled); 305 EXPECT_FALSE(eventCallbackCalled);
309 } 306 }
OLDNEW
« no previous file with comments | « test/Prefs.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld