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

Side by Side Diff: test/UpdateCheck.cpp

Issue 10800079: Implemented update checking functionality (Closed)
Patch Set: Created June 5, 2013, 6:47 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.h ('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
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 #include <AdblockPlus/tr1_functional.h>
19
20 #include "BaseJsTest.h"
21
22 namespace
23 {
24 typedef std::tr1::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
25
26 void FindAndReplace(std::string& source, const std::string& find, const std::s tring& replace)
27 {
28 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po s += replace.size())
29 source.replace(pos, find.size(), replace);
30 }
31
32 std::string previousRequestUrl;
33 class TestWebRequest : public LazyWebRequest
34 {
35 public:
36 AdblockPlus::ServerResponse response;
37 AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::H eaderList& requestHeaders) const
38 {
39 if (url.find("easylist") != std::string::npos)
40 return LazyWebRequest::GET(url, requestHeaders);
41
42 previousRequestUrl = url;
43 return response;
44 }
45 };
46
47 class UpdateCheckTest : public ::testing::Test
48 {
49 protected:
50 AdblockPlus::AppInfo appInfo;
51 TestWebRequest* webRequest;
52 AdblockPlus::WebRequestPtr webRequestPtr;
53 AdblockPlus::JsEnginePtr jsEngine;
54 FilterEnginePtr filterEngine;
55
56 bool eventCallbackCalled;
57 AdblockPlus::JsValueList eventCallbackParams;
58 bool updateCallbackCalled;
59 std::string updateError;
60
61 void SetUp()
62 {
63 webRequest = new TestWebRequest();
64 webRequestPtr.reset(webRequest);
65
66 eventCallbackCalled = false;
67 updateCallbackCalled = false;
68 Reset();
69 }
70
71 void Reset()
72 {
73 jsEngine = AdblockPlus::JsEngine::New(appInfo);
74 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(new LazyLogSystem));
75 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(new LazyFileSystem));
76 jsEngine->SetWebRequest(webRequestPtr);
77 jsEngine->SetEventCallback("updateAvailable",
78 std::tr1::bind(&UpdateCheckTest::EventCallback, this, std::tr1::placeh olders::_1));
79
80 filterEngine.reset(new AdblockPlus::FilterEngine(jsEngine));
81 }
82
83 void ForceUpdateCheck()
84 {
85 filterEngine->ForceUpdateCheck(
86 std::tr1::bind(&UpdateCheckTest::UpdateCallback, this, std::tr1::place holders::_1));
87 }
88
89 void EventCallback(AdblockPlus::JsValueList& params)
90 {
91 eventCallbackCalled = true;
92 eventCallbackParams = params;
93 }
94
95 void UpdateCallback(const std::string& error)
96 {
97 updateCallbackCalled = true;
98 updateError = error;
99 }
100 };
101 }
102
103 TEST_F(UpdateCheckTest, RequestFailure)
104 {
105 webRequest->response.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
106
107 appInfo.name = "1";
108 appInfo.id = "2";
109 appInfo.version = "3";
110 appInfo.platform = "4";
111 appInfo.developmentBuild = false;
112
113 Reset();
114 ForceUpdateCheck();
115
116 AdblockPlus::Sleep(100);
117
118 ASSERT_FALSE(eventCallbackCalled);
119 ASSERT_TRUE(updateCallbackCalled);
120 ASSERT_FALSE(updateError.empty());
121
122 std::string expectedUrl(filterEngine->GetPref("update_url_release")->AsString( ));
123 FindAndReplace(expectedUrl, "%NAME%", appInfo.name);
124 FindAndReplace(expectedUrl, "%ID%", appInfo.id);
125 FindAndReplace(expectedUrl, "%VERSION%", appInfo.version);
126 FindAndReplace(expectedUrl, "%APP%", appInfo.platform);
127 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update
128 ASSERT_EQ(expectedUrl, previousRequestUrl);
129 }
130
131 TEST_F(UpdateCheckTest, UpdateAvailable)
132 {
133 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
134 webRequest->response.responseStatus = 200;
135 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tps://foo.bar/\"}}";
136
137 appInfo.name = "1";
138 appInfo.id = "2";
139 appInfo.version = "3";
140 appInfo.platform = "4";
141 appInfo.developmentBuild = true;
142
143 Reset();
144 ForceUpdateCheck();
145
146 AdblockPlus::Sleep(100);
147
148 ASSERT_TRUE(eventCallbackCalled);
149 ASSERT_EQ(1u, eventCallbackParams.size());
150 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString());
151 ASSERT_TRUE(updateCallbackCalled);
152 ASSERT_TRUE(updateError.empty());
153
154 std::string expectedUrl(filterEngine->GetPref("update_url_devbuild")->AsString ());
155 FindAndReplace(expectedUrl, "%NAME%", appInfo.name);
156 FindAndReplace(expectedUrl, "%ID%", appInfo.id);
157 FindAndReplace(expectedUrl, "%VERSION%", appInfo.version);
158 FindAndReplace(expectedUrl, "%APP%", appInfo.platform);
159 FindAndReplace(expectedUrl, "%TYPE%", "1"); // manual update
160 ASSERT_EQ(expectedUrl, previousRequestUrl);
161 }
162
163 TEST_F(UpdateCheckTest, PlatformUpdateAvailable)
164 {
165 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
166 webRequest->response.responseStatus = 200;
167 webRequest->response.responseText = "{\"1/4\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}";
168
169 appInfo.name = "1";
170 appInfo.id = "2";
171 appInfo.version = "3";
172 appInfo.platform = "4";
173 appInfo.developmentBuild = true;
174
175 Reset();
176 ForceUpdateCheck();
177
178 AdblockPlus::Sleep(100);
179
180 ASSERT_TRUE(eventCallbackCalled);
181 ASSERT_EQ(1u, eventCallbackParams.size());
182 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString());
183 ASSERT_TRUE(updateCallbackCalled);
184 ASSERT_TRUE(updateError.empty());
185 }
186
187 TEST_F(UpdateCheckTest, WrongPlatform)
188 {
189 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
190 webRequest->response.responseStatus = 200;
191 webRequest->response.responseText = "{\"1/3\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}";
192
193 appInfo.name = "1";
194 appInfo.id = "2";
195 appInfo.version = "3";
196 appInfo.platform = "4";
197 appInfo.developmentBuild = true;
198
199 Reset();
200 ForceUpdateCheck();
201
202 AdblockPlus::Sleep(100);
203
204 ASSERT_FALSE(eventCallbackCalled);
205 ASSERT_TRUE(updateCallbackCalled);
206 ASSERT_TRUE(updateError.empty());
207 }
208
209 TEST_F(UpdateCheckTest, WrongVersion)
210 {
211 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
212 webRequest->response.responseStatus = 200;
213 webRequest->response.responseText = "{\"1\": {\"version\":\"3\",\"url\":\"http s://foo.bar/\"}}";
214
215 appInfo.name = "1";
216 appInfo.id = "2";
217 appInfo.version = "3";
218 appInfo.platform = "4";
219 appInfo.developmentBuild = true;
220
221 Reset();
222 ForceUpdateCheck();
223
224 AdblockPlus::Sleep(100);
225
226 ASSERT_FALSE(eventCallbackCalled);
227 ASSERT_TRUE(updateCallbackCalled);
228 ASSERT_TRUE(updateError.empty());
229 }
230
231 TEST_F(UpdateCheckTest, WrongURL)
232 {
233 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
234 webRequest->response.responseStatus = 200;
235 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tp://insecure/\"}}";
236
237 appInfo.name = "1";
238 appInfo.id = "2";
239 appInfo.version = "3";
240 appInfo.platform = "4";
241 appInfo.developmentBuild = true;
242
243 Reset();
244 ForceUpdateCheck();
245
246 AdblockPlus::Sleep(100);
247
248 ASSERT_FALSE(eventCallbackCalled);
249 ASSERT_TRUE(updateCallbackCalled);
250 ASSERT_FALSE(updateError.empty());
251 }
OLDNEW
« no previous file with comments | « test/BaseJsTest.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld