Merge lp:~unity-api-team/unity-scope-click/sign-all-the-things into lp:unity-scope-click
- sign-all-the-things
- Merge into trunk
Proposed by
dobey
Status: | Superseded |
---|---|
Proposed branch: | lp:~unity-api-team/unity-scope-click/sign-all-the-things |
Merge into: | lp:unity-scope-click |
Diff against target: |
2403 lines (+517/-1182) 22 files modified
CMakeLists.txt (+1/-1) libclickscope/click/download-manager.cpp (+97/-323) libclickscope/click/download-manager.h (+20/-50) libclickscope/click/preview.cpp (+93/-83) libclickscope/click/preview.h (+11/-11) libclickscope/click/webclient.cpp (+17/-4) libclickscope/click/webclient.h (+2/-1) libclickscope/tests/mock_ubuntu_download_manager.h (+14/-1) libclickscope/tests/mock_webclient.h (+3/-2) libclickscope/tests/test_download_manager.cpp (+174/-439) libclickscope/tests/test_index.cpp (+13/-1) libclickscope/tests/test_preview.cpp (+38/-60) libclickscope/tests/test_reviews.cpp (+13/-0) libclickscope/tests/test_webclient.cpp (+4/-4) scope/clickapps/apps-scope.cpp (+7/-4) scope/clickapps/apps-scope.h (+3/-0) scope/clickstore/store-scope.cpp (+4/-1) scope/clickstore/store-scope.h (+3/-0) scope/tests/CMakeLists.txt (+0/-1) scope/tests/download_manager_tool/CMakeLists.txt (+0/-14) scope/tests/download_manager_tool/download_manager_tool.cpp (+0/-123) scope/tests/download_manager_tool/download_manager_tool.h (+0/-59) |
To merge this branch: | bzr merge lp:~unity-api-team/unity-scope-click/sign-all-the-things |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Unity API Team | Pending | ||
Review via email: mp+287672@code.launchpad.net |
Commit message
Fix the signing of store webservice urls
Description of the change
To post a comment you must log in.
- 441. By dobey
-
Link bug report.
- 442. By dobey
-
More cleanup of usage of the credentials service.
- 443. By dobey
-
Use a cached token.
- 444. By dobey
-
Better signal/error handling.
- 445. By dobey
-
Try to keep qt event loop from being blocked by std::future.
- 446. By dobey
-
Continue forwarding the found signal.
- 447. By Antti Kaijanmäki
-
initialize std::future_status
Unmerged revisions
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'CMakeLists.txt' | |||
2 | --- CMakeLists.txt 2016-02-04 15:24:05 +0000 | |||
3 | +++ CMakeLists.txt 2016-03-01 16:46:59 +0000 | |||
4 | @@ -60,7 +60,7 @@ | |||
5 | 60 | 60 | ||
6 | 61 | include(EnableCoverageReport) | 61 | include(EnableCoverageReport) |
7 | 62 | ENABLE_COVERAGE_REPORT(TARGETS ${SCOPE_LIB_NAME} ${STORE_LIB_UNVERSIONED} ${APPS_LIB_UNVERSIONED} | 62 | ENABLE_COVERAGE_REPORT(TARGETS ${SCOPE_LIB_NAME} ${STORE_LIB_UNVERSIONED} ${APPS_LIB_UNVERSIONED} |
9 | 63 | TESTS click_scope_integration_tests libclick-scope-tests fake_launcher click-scope-tests apps-scope-tests download_manager_tool click_interface_tool init-departments test-integration-harness | 63 | TESTS click_scope_integration_tests libclick-scope-tests fake_launcher click-scope-tests apps-scope-tests click_interface_tool init-departments |
10 | 64 | FILTER /usr/include ${CMAKE_BINARY_DIR}/* | 64 | FILTER /usr/include ${CMAKE_BINARY_DIR}/* |
11 | 65 | ) | 65 | ) |
12 | 66 | 66 | ||
13 | 67 | 67 | ||
14 | === modified file 'libclickscope/click/download-manager.cpp' | |||
15 | --- libclickscope/click/download-manager.cpp 2014-09-24 19:42:19 +0000 | |||
16 | +++ libclickscope/click/download-manager.cpp 2016-03-01 16:46:59 +0000 | |||
17 | @@ -1,5 +1,5 @@ | |||
18 | 1 | /* | 1 | /* |
20 | 2 | * Copyright (C) 2014 Canonical Ltd. | 2 | * Copyright (C) 2014-2016 Canonical Ltd. |
21 | 3 | * | 3 | * |
22 | 4 | * This program is free software: you can redistribute it and/or modify it | 4 | * This program is free software: you can redistribute it and/or modify it |
23 | 5 | * under the terms of the GNU General Public License version 3, as published | 5 | * under the terms of the GNU General Public License version 3, as published |
24 | @@ -47,7 +47,7 @@ | |||
25 | 47 | #include <ubuntu/download_manager/download.h> | 47 | #include <ubuntu/download_manager/download.h> |
26 | 48 | #include <ubuntu/download_manager/error.h> | 48 | #include <ubuntu/download_manager/error.h> |
27 | 49 | 49 | ||
29 | 50 | namespace | 50 | namespace click |
30 | 51 | { | 51 | { |
31 | 52 | 52 | ||
32 | 53 | static const QString DOWNLOAD_APP_ID_KEY = "app_id"; | 53 | static const QString DOWNLOAD_APP_ID_KEY = "app_id"; |
33 | @@ -56,269 +56,30 @@ | |||
34 | 56 | static const QString DOWNLOAD_COMMAND = CLICK_INSTALL_HELPER; | 56 | static const QString DOWNLOAD_COMMAND = CLICK_INSTALL_HELPER; |
35 | 57 | 57 | ||
36 | 58 | static const QString DOWNLOAD_MANAGER_SHA512 = "sha512"; | 58 | static const QString DOWNLOAD_MANAGER_SHA512 = "sha512"; |
69 | 59 | } | 59 | |
70 | 60 | 60 | const QByteArray& CLICK_TOKEN_HEADER() | |
39 | 61 | struct click::DownloadManager::Private | ||
40 | 62 | { | ||
41 | 63 | Private(const QSharedPointer<click::network::AccessManager>& networkAccessManager, | ||
42 | 64 | const QSharedPointer<click::CredentialsService>& credentialsService, | ||
43 | 65 | const QSharedPointer<udm::Manager>& systemDownloadManager) | ||
44 | 66 | : nam(networkAccessManager), credentialsService(credentialsService), | ||
45 | 67 | systemDownloadManager(systemDownloadManager) | ||
46 | 68 | { | ||
47 | 69 | } | ||
48 | 70 | |||
49 | 71 | void updateCredentialsFromService() | ||
50 | 72 | { | ||
51 | 73 | credentialsService->getCredentials(); | ||
52 | 74 | } | ||
53 | 75 | |||
54 | 76 | void invalidateCredentialsFromService() | ||
55 | 77 | { | ||
56 | 78 | credentialsService->invalidateCredentials(); | ||
57 | 79 | } | ||
58 | 80 | |||
59 | 81 | QSharedPointer<click::network::AccessManager> nam; | ||
60 | 82 | QSharedPointer<click::CredentialsService> credentialsService; | ||
61 | 83 | QSharedPointer<udm::Manager> systemDownloadManager; | ||
62 | 84 | QSharedPointer<click::network::Reply> reply; | ||
63 | 85 | QString downloadUrl; | ||
64 | 86 | QString download_sha512; | ||
65 | 87 | QString package_name; | ||
66 | 88 | }; | ||
67 | 89 | |||
68 | 90 | const QByteArray& click::CLICK_TOKEN_HEADER() | ||
71 | 91 | { | 61 | { |
72 | 92 | static const QByteArray result("X-Click-Token"); | 62 | static const QByteArray result("X-Click-Token"); |
73 | 93 | return result; | 63 | return result; |
74 | 94 | } | 64 | } |
75 | 95 | 65 | ||
302 | 96 | click::DownloadManager::DownloadManager(const QSharedPointer<click::network::AccessManager>& networkAccessManager, | 66 | DownloadManager::DownloadManager(const QSharedPointer<click::web::Client>& client, |
303 | 97 | const QSharedPointer<click::CredentialsService>& credentialsService, | 67 | const QSharedPointer<udm::Manager>& manager) : |
304 | 98 | const QSharedPointer<udm::Manager>& systemDownloadManager, | 68 | client(client), |
305 | 99 | QObject *parent) | 69 | dm(manager) |
306 | 100 | : QObject(parent), | 70 | { |
307 | 101 | impl(new Private(networkAccessManager, credentialsService, systemDownloadManager)) | 71 | } |
308 | 102 | { | 72 | |
309 | 103 | QMetaObject::Connection c = connect(impl->credentialsService.data(), | 73 | DownloadManager::~DownloadManager() |
310 | 104 | &click::CredentialsService::credentialsFound, | 74 | { |
311 | 105 | this, &click::DownloadManager::handleCredentialsFound); | 75 | } |
312 | 106 | if (!c) { | 76 | |
313 | 107 | qDebug() << "failed to connect to credentialsFound"; | 77 | void DownloadManager::get_progress(const std::string& package_name, |
314 | 108 | } | 78 | const std::function<void (std::string)>& callback) |
315 | 109 | 79 | { | |
316 | 110 | c = connect(impl->credentialsService.data(), &click::CredentialsService::credentialsNotFound, | 80 | dm->getAllDownloadsWithMetadata(DOWNLOAD_APP_ID_KEY, |
317 | 111 | this, &click::DownloadManager::handleCredentialsNotFound); | 81 | QString::fromStdString(package_name), |
318 | 112 | if (!c) { | 82 | [callback, package_name](const QString& /*key*/, const QString& /*value*/, DownloadsList* downloads_list){ |
93 | 113 | qDebug() << "failed to connect to credentialsNotFound"; | ||
94 | 114 | } | ||
95 | 115 | |||
96 | 116 | // NOTE: using SIGNAL/SLOT macros here because new-style | ||
97 | 117 | // connections are flaky on ARM. | ||
98 | 118 | c = connect(impl->systemDownloadManager.data(), SIGNAL(downloadCreated(Download*)), | ||
99 | 119 | this, SLOT(handleDownloadCreated(Download*))); | ||
100 | 120 | |||
101 | 121 | if (!c) { | ||
102 | 122 | qDebug() << "failed to connect to systemDownloadManager::downloadCreated"; | ||
103 | 123 | |||
104 | 124 | } | ||
105 | 125 | } | ||
106 | 126 | |||
107 | 127 | click::DownloadManager::~DownloadManager(){ | ||
108 | 128 | } | ||
109 | 129 | |||
110 | 130 | void click::DownloadManager::startDownload(const QString& downloadUrl, const QString& download_sha512, const QString& package_name) | ||
111 | 131 | { | ||
112 | 132 | impl->package_name = package_name; | ||
113 | 133 | |||
114 | 134 | // NOTE: using SIGNAL/SLOT macros here because new-style | ||
115 | 135 | // connections are flaky on ARM. | ||
116 | 136 | QObject::connect(this, SIGNAL(clickTokenFetched(QString)), | ||
117 | 137 | this, SLOT(handleClickTokenFetched(QString)), | ||
118 | 138 | Qt::UniqueConnection); | ||
119 | 139 | QObject::connect(this, SIGNAL(clickTokenFetchError(QString)), | ||
120 | 140 | this, SLOT(handleClickTokenFetchError(QString)), | ||
121 | 141 | Qt::UniqueConnection); | ||
122 | 142 | fetchClickToken(downloadUrl, download_sha512); | ||
123 | 143 | } | ||
124 | 144 | |||
125 | 145 | void click::DownloadManager::handleClickTokenFetched(const QString& clickToken) | ||
126 | 146 | { | ||
127 | 147 | QVariantMap metadata; | ||
128 | 148 | |||
129 | 149 | QVariant commandline = QVariant(QStringList() << DOWNLOAD_COMMAND << "$file" << impl->package_name); | ||
130 | 150 | metadata[DOWNLOAD_COMMAND_KEY] = commandline; | ||
131 | 151 | metadata[DOWNLOAD_APP_ID_KEY] = impl->package_name; | ||
132 | 152 | metadata["package_name"] = impl->package_name; | ||
133 | 153 | |||
134 | 154 | QMap<QString, QString> headers; | ||
135 | 155 | headers[CLICK_TOKEN_HEADER()] = clickToken; | ||
136 | 156 | |||
137 | 157 | udm::DownloadStruct downloadStruct(impl->downloadUrl, | ||
138 | 158 | impl->download_sha512, | ||
139 | 159 | DOWNLOAD_MANAGER_SHA512, | ||
140 | 160 | metadata, | ||
141 | 161 | headers); | ||
142 | 162 | |||
143 | 163 | impl->systemDownloadManager->createDownload(downloadStruct); | ||
144 | 164 | |||
145 | 165 | } | ||
146 | 166 | |||
147 | 167 | void click::DownloadManager::handleClickTokenFetchError(const QString& errorMessage) | ||
148 | 168 | { | ||
149 | 169 | emit downloadError(errorMessage); | ||
150 | 170 | } | ||
151 | 171 | |||
152 | 172 | void click::DownloadManager::handleDownloadCreated(udm::Download *download) | ||
153 | 173 | { | ||
154 | 174 | if (download->isError()) { | ||
155 | 175 | QString error = download->error()->errorString(); | ||
156 | 176 | qDebug() << "Received error from ubuntu-download-manager:" << error; | ||
157 | 177 | emit downloadError(error); | ||
158 | 178 | } else { | ||
159 | 179 | download->start(); | ||
160 | 180 | emit downloadStarted(download->id()); | ||
161 | 181 | } | ||
162 | 182 | } | ||
163 | 183 | |||
164 | 184 | void click::DownloadManager::fetchClickToken(const QString& downloadUrl, const QString& download_sha512) | ||
165 | 185 | { | ||
166 | 186 | impl->downloadUrl = downloadUrl; | ||
167 | 187 | impl->download_sha512 = download_sha512; | ||
168 | 188 | impl->updateCredentialsFromService(); | ||
169 | 189 | } | ||
170 | 190 | |||
171 | 191 | void click::DownloadManager::handleCredentialsFound(const u1::Token &token) | ||
172 | 192 | { | ||
173 | 193 | qDebug() << "Credentials found, signing url " << impl->downloadUrl; | ||
174 | 194 | |||
175 | 195 | QString authHeader = token.signUrl(impl->downloadUrl, QStringLiteral("HEAD")); | ||
176 | 196 | |||
177 | 197 | QNetworkRequest req; | ||
178 | 198 | req.setRawHeader(QStringLiteral("Authorization").toUtf8(), | ||
179 | 199 | authHeader.toUtf8()); | ||
180 | 200 | req.setUrl(impl->downloadUrl); | ||
181 | 201 | |||
182 | 202 | impl->reply = impl->nam->head(req); | ||
183 | 203 | |||
184 | 204 | // NOTE: using SIGNAL/SLOT macros here because new-style | ||
185 | 205 | // connections are flaky on ARM. | ||
186 | 206 | QObject::connect(impl->reply.data(), SIGNAL(error(QNetworkReply::NetworkError)), | ||
187 | 207 | this, SLOT(handleNetworkError(QNetworkReply::NetworkError))); | ||
188 | 208 | QObject::connect(impl->reply.data(), SIGNAL(finished()), | ||
189 | 209 | this, SLOT(handleNetworkFinished())); | ||
190 | 210 | } | ||
191 | 211 | |||
192 | 212 | void click::DownloadManager::handleCredentialsNotFound() | ||
193 | 213 | { | ||
194 | 214 | qDebug() << "No credentials were found."; | ||
195 | 215 | emit credentialsNotFound(); | ||
196 | 216 | } | ||
197 | 217 | |||
198 | 218 | void click::DownloadManager::handleNetworkFinished() | ||
199 | 219 | { | ||
200 | 220 | QVariant statusAttr = impl->reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); | ||
201 | 221 | if(!statusAttr.isValid()) { | ||
202 | 222 | QString msg("Invalid HTTP response."); | ||
203 | 223 | qDebug() << msg; | ||
204 | 224 | emit clickTokenFetchError(msg); | ||
205 | 225 | return; | ||
206 | 226 | } | ||
207 | 227 | |||
208 | 228 | int status = statusAttr.toInt(); | ||
209 | 229 | if (status != 200){ | ||
210 | 230 | qDebug() << impl->reply->rawHeaderPairs(); | ||
211 | 231 | qDebug() << impl->reply->readAll(); | ||
212 | 232 | QString msg = QString("HTTP status not OK: %1").arg(status); | ||
213 | 233 | emit clickTokenFetchError(msg); | ||
214 | 234 | return; | ||
215 | 235 | } | ||
216 | 236 | |||
217 | 237 | if(!impl->reply->hasRawHeader(CLICK_TOKEN_HEADER())) { | ||
218 | 238 | QString msg = "Response does not contain Click Header"; | ||
219 | 239 | qDebug() << msg << "Full response:"; | ||
220 | 240 | qDebug() << impl->reply->rawHeaderPairs(); | ||
221 | 241 | qDebug() << impl->reply->readAll(); | ||
222 | 242 | |||
223 | 243 | emit clickTokenFetchError(msg); | ||
224 | 244 | return; | ||
225 | 245 | } | ||
226 | 246 | |||
227 | 247 | QString clickTokenHeaderStr = impl->reply->rawHeader(CLICK_TOKEN_HEADER()); | ||
228 | 248 | |||
229 | 249 | impl->reply.reset(); | ||
230 | 250 | |||
231 | 251 | emit clickTokenFetched(clickTokenHeaderStr); | ||
232 | 252 | } | ||
233 | 253 | |||
234 | 254 | void click::DownloadManager::handleNetworkError(QNetworkReply::NetworkError error) | ||
235 | 255 | { | ||
236 | 256 | switch (error) { | ||
237 | 257 | case QNetworkReply::ContentAccessDenied: | ||
238 | 258 | case QNetworkReply::ContentOperationNotPermittedError: | ||
239 | 259 | case QNetworkReply::AuthenticationRequiredError: | ||
240 | 260 | impl->invalidateCredentialsFromService(); | ||
241 | 261 | emit credentialsNotFound(); | ||
242 | 262 | break; | ||
243 | 263 | default: | ||
244 | 264 | qDebug() << "error in network request for click token: " << error << impl->reply->errorString(); | ||
245 | 265 | emit clickTokenFetchError(QString("Network Error")); | ||
246 | 266 | break; | ||
247 | 267 | } | ||
248 | 268 | impl->reply.reset(); | ||
249 | 269 | } | ||
250 | 270 | |||
251 | 271 | void click::DownloadManager::getAllDownloadsWithMetadata(const QString &key, const QString &value, | ||
252 | 272 | MetadataDownloadsListCb callback, | ||
253 | 273 | MetadataDownloadsListCb errback) | ||
254 | 274 | { | ||
255 | 275 | impl->systemDownloadManager->getAllDownloadsWithMetadata(key, value, callback, errback); | ||
256 | 276 | } | ||
257 | 277 | |||
258 | 278 | // Downloader | ||
259 | 279 | namespace | ||
260 | 280 | { | ||
261 | 281 | click::DownloadManager& downloadManagerInstance( | ||
262 | 282 | const QSharedPointer<click::network::AccessManager>& networkAccessManager) | ||
263 | 283 | { | ||
264 | 284 | static QSharedPointer<click::CredentialsService> ssoService | ||
265 | 285 | { | ||
266 | 286 | new click::CredentialsService() | ||
267 | 287 | }; | ||
268 | 288 | static QSharedPointer<Ubuntu::DownloadManager::Manager> udm | ||
269 | 289 | { | ||
270 | 290 | Ubuntu::DownloadManager::Manager::createSessionManager() | ||
271 | 291 | }; | ||
272 | 292 | |||
273 | 293 | static click::DownloadManager instance(networkAccessManager, | ||
274 | 294 | ssoService, | ||
275 | 295 | udm); | ||
276 | 296 | |||
277 | 297 | return instance; | ||
278 | 298 | } | ||
279 | 299 | } | ||
280 | 300 | |||
281 | 301 | click::Downloader::Downloader(const QSharedPointer<click::network::AccessManager>& networkAccessManager) | ||
282 | 302 | : networkAccessManager(networkAccessManager) | ||
283 | 303 | { | ||
284 | 304 | } | ||
285 | 305 | |||
286 | 306 | click::Downloader::~Downloader() | ||
287 | 307 | { | ||
288 | 308 | |||
289 | 309 | } | ||
290 | 310 | |||
291 | 311 | click::DownloadManager& click::Downloader::getDownloadManager() | ||
292 | 312 | { | ||
293 | 313 | return downloadManagerInstance(networkAccessManager); | ||
294 | 314 | } | ||
295 | 315 | |||
296 | 316 | void click::Downloader::get_download_progress(std::string package_name, const std::function<void (std::string)>& callback) | ||
297 | 317 | { | ||
298 | 318 | auto& dm = getDownloadManager(); | ||
299 | 319 | |||
300 | 320 | dm.getAllDownloadsWithMetadata(DOWNLOAD_APP_ID_KEY, QString::fromStdString(package_name), | ||
301 | 321 | [callback, package_name](const QString& /*key*/, const QString& /*value*/, DownloadsList* downloads_list){ | ||
319 | 322 | // got downloads matching metadata | 83 | // got downloads matching metadata |
320 | 323 | std::string object_path; | 84 | std::string object_path; |
321 | 324 | auto downloads = downloads_list->downloads(); | 85 | auto downloads = downloads_list->downloads(); |
322 | @@ -339,66 +100,79 @@ | |||
323 | 339 | }); | 100 | }); |
324 | 340 | } | 101 | } |
325 | 341 | 102 | ||
389 | 342 | namespace | 103 | click::web::Cancellable DownloadManager::start(const std::string& url, |
390 | 343 | { | 104 | const std::string& download_sha512, |
391 | 344 | class Callback : public QObject | 105 | const std::string& package_name, |
392 | 345 | { | 106 | const std::function<void (std::string, Error)>& callback) |
393 | 346 | Q_OBJECT | 107 | { |
394 | 347 | 108 | QSharedPointer<click::web::Response> response = client->call | |
395 | 348 | public: | 109 | (url, "HEAD", true); |
396 | 349 | Callback(const std::function<void (std::pair<std::string, click::InstallError >)>& cb) : cb(cb) | 110 | |
397 | 350 | { | 111 | QObject::connect(response.data(), &click::web::Response::finished, |
398 | 351 | } | 112 | [this, callback, url, download_sha512, package_name, |
399 | 352 | 113 | response](QString) { | |
400 | 353 | public slots: | 114 | auto status = response->get_status_code(); |
401 | 354 | void onDownloadStarted(const QString& downloadId) | 115 | if (status == 200) { |
402 | 355 | { | 116 | auto clickToken = response->get_header(CLICK_TOKEN_HEADER().data()); |
403 | 356 | cb(std::make_pair(downloadId.toUtf8().data(), click::InstallError::NoError)); | 117 | qDebug() << "Received click token:" << clickToken.c_str(); |
404 | 357 | 118 | QVariantMap metadata; | |
405 | 358 | // We shouldn't do this, but: We have no other indication whether a download finished or not. | 119 | |
406 | 359 | // TODO(tvoss): Remove as soon as a donwload finished signal is available. | 120 | QVariant commandline = QVariant(QStringList() << DOWNLOAD_COMMAND << "$file" << package_name.c_str()); |
407 | 360 | deleteLater(); | 121 | metadata[DOWNLOAD_COMMAND_KEY] = commandline; |
408 | 361 | } | 122 | metadata[DOWNLOAD_APP_ID_KEY] = package_name.c_str(); |
409 | 362 | 123 | metadata["package_name"] = package_name.c_str(); | |
410 | 363 | void onDownloadError(const QString& errorMessage) | 124 | |
411 | 364 | { | 125 | QMap<QString, QString> headers; |
412 | 365 | cb(std::make_pair(errorMessage.toStdString(), click::InstallError::DownloadInstallError)); | 126 | headers[CLICK_TOKEN_HEADER()] = clickToken.c_str(); |
413 | 366 | deleteLater(); | 127 | |
414 | 367 | } | 128 | udm::DownloadStruct downloadStruct(url.c_str(), |
415 | 368 | 129 | download_sha512.c_str(), | |
416 | 369 | void onCredentialsError() | 130 | DOWNLOAD_MANAGER_SHA512, |
417 | 370 | { | 131 | metadata, |
418 | 371 | cb(std::make_pair(std::string(), click::InstallError::CredentialsError)); | 132 | headers); |
419 | 372 | deleteLater(); | 133 | |
420 | 373 | } | 134 | dm->createDownload(downloadStruct, |
421 | 374 | 135 | [callback](Download* download) { | |
422 | 375 | private: | 136 | if (download->isError()) { |
423 | 376 | std::function<void (std::pair<std::string, click::InstallError >)> cb; | 137 | auto error = download->error()->errorString().toUtf8().data(); |
424 | 377 | }; | 138 | qDebug() << "Received error from ubuntu-download-manager:" << error; |
425 | 378 | } | 139 | callback(error, Error::DownloadInstallError); |
426 | 379 | 140 | } else { | |
427 | 380 | void click::Downloader::startDownload(const std::string& url, const std::string& download_sha512, const std::string& package_name, | 141 | download->start(); |
428 | 381 | const std::function<void (std::pair<std::string, click::InstallError >)>& callback) | 142 | callback(download->id().toUtf8().data(), Error::NoError); |
429 | 382 | { | 143 | } |
430 | 383 | qt::core::world::enter_with_task([this, callback, url, download_sha512, package_name] () | 144 | }, |
431 | 384 | { | 145 | [callback](Download* download) { |
432 | 385 | auto& dm = downloadManagerInstance(networkAccessManager); | 146 | callback(download->error()->errorString().toUtf8().data(), |
433 | 386 | 147 | Error::DownloadInstallError); | |
434 | 387 | // Leverage automatic lifetime mgmt for QObjects here. | 148 | }); |
435 | 388 | auto cb = new Callback{callback}; | 149 | } else { |
436 | 389 | 150 | std::string error{"Unhandled HTTP response code: "}; | |
437 | 390 | QObject::connect(&dm, &click::DownloadManager::downloadStarted, | 151 | error += status; |
438 | 391 | cb, &Callback::onDownloadStarted); | 152 | callback(error, Error::DownloadInstallError); |
439 | 392 | 153 | } | |
440 | 393 | QObject::connect(&dm, &click::DownloadManager::credentialsNotFound, | 154 | }); |
441 | 394 | cb, &Callback::onCredentialsError); | 155 | QObject::connect(response.data(), &click::web::Response::error, |
442 | 395 | 156 | [this, callback, package_name](QString error, int error_code) { | |
443 | 396 | QObject::connect(&dm, &click::DownloadManager::downloadError, | 157 | qDebug() << QStringLiteral("Network error (%1) fetching click token for:").arg(error_code) << package_name.c_str(); |
444 | 397 | cb, &Callback::onDownloadError); | 158 | switch(error_code) { |
445 | 398 | 159 | case 401: | |
446 | 399 | dm.startDownload(QString::fromStdString(url), QString::fromStdString(download_sha512), | 160 | case 403: |
447 | 400 | QString::fromStdString(package_name)); | 161 | sso->invalidateCredentials(); |
448 | 401 | }); | 162 | callback(error.toUtf8().data(), Error::CredentialsError); |
449 | 402 | } | 163 | break; |
450 | 403 | 164 | default: | |
451 | 404 | #include "download-manager.moc" | 165 | callback(error.toUtf8().data(), Error::DownloadInstallError); |
452 | 166 | } | ||
453 | 167 | }); | ||
454 | 168 | |||
455 | 169 | return click::web::Cancellable(response); | ||
456 | 170 | } | ||
457 | 171 | |||
458 | 172 | void DownloadManager::setCredentialsService(const QSharedPointer<click::CredentialsService>& credentialsService) | ||
459 | 173 | { | ||
460 | 174 | sso = credentialsService; | ||
461 | 175 | client->setCredentialsService(sso); | ||
462 | 176 | } | ||
463 | 177 | |||
464 | 178 | } // namespace click | ||
465 | 405 | 179 | ||
466 | === modified file 'libclickscope/click/download-manager.h' | |||
467 | --- libclickscope/click/download-manager.h 2014-09-01 21:23:11 +0000 | |||
468 | +++ libclickscope/click/download-manager.h 2016-03-01 16:46:59 +0000 | |||
469 | @@ -1,5 +1,5 @@ | |||
470 | 1 | /* | 1 | /* |
472 | 2 | * Copyright (C) 2014 Canonical Ltd. | 2 | * Copyright (C) 2014-2016 Canonical Ltd. |
473 | 3 | * | 3 | * |
474 | 4 | * This program is free software: you can redistribute it and/or modify it | 4 | * This program is free software: you can redistribute it and/or modify it |
475 | 5 | * under the terms of the GNU General Public License version 3, as published | 5 | * under the terms of the GNU General Public License version 3, as published |
476 | @@ -35,8 +35,8 @@ | |||
477 | 35 | #include <QObject> | 35 | #include <QObject> |
478 | 36 | #include <QString> | 36 | #include <QString> |
479 | 37 | 37 | ||
480 | 38 | #include <click/network_access_manager.h> | ||
481 | 39 | #include <click/ubuntuone_credentials.h> | 38 | #include <click/ubuntuone_credentials.h> |
482 | 39 | #include <click/webclient.h> | ||
483 | 40 | 40 | ||
484 | 41 | #include <ubuntu/download_manager/manager.h> | 41 | #include <ubuntu/download_manager/manager.h> |
485 | 42 | 42 | ||
486 | @@ -56,60 +56,30 @@ | |||
487 | 56 | 56 | ||
488 | 57 | const QByteArray& CLICK_TOKEN_HEADER(); | 57 | const QByteArray& CLICK_TOKEN_HEADER(); |
489 | 58 | 58 | ||
491 | 59 | class DownloadManager : public QObject | 59 | |
492 | 60 | class DownloadManager | ||
493 | 60 | { | 61 | { |
494 | 61 | Q_OBJECT | ||
495 | 62 | |||
496 | 63 | public: | 62 | public: |
502 | 64 | DownloadManager(const QSharedPointer<click::network::AccessManager>& networkAccessManager, | 63 | enum class Error {NoError, CredentialsError, DownloadInstallError}; |
503 | 65 | const QSharedPointer<click::CredentialsService>& ssoService, | 64 | |
504 | 66 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& systemDownloadManager, | 65 | DownloadManager(const QSharedPointer<click::web::Client>& client, |
505 | 67 | QObject *parent = 0); | 66 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager); |
501 | 68 | DownloadManager(); | ||
506 | 69 | virtual ~DownloadManager(); | 67 | virtual ~DownloadManager(); |
507 | 70 | 68 | ||
531 | 71 | public slots: | 69 | virtual void get_progress(const std::string& package_name, |
532 | 72 | virtual void startDownload(const QString& downloadUrl, const QString& download_sha512, const QString& package_name); | 70 | const std::function<void (std::string)>& callback); |
533 | 73 | virtual void fetchClickToken(const QString& downloadUrl, const QString& download_sha512); | 71 | virtual click::web::Cancellable start(const std::string& url, |
534 | 74 | virtual void getAllDownloadsWithMetadata(const QString& key, | 72 | const std::string& download_sha512, |
535 | 75 | const QString& value, | 73 | const std::string& package_name, |
536 | 76 | MetadataDownloadsListCb callback, | 74 | const std::function<void (std::string, |
537 | 77 | MetadataDownloadsListCb errback); | 75 | Error)>& callback); |
538 | 78 | signals: | 76 | |
539 | 79 | 77 | virtual void setCredentialsService(const QSharedPointer<click::CredentialsService>& credentialsService); | |
517 | 80 | void credentialsNotFound(); | ||
518 | 81 | void downloadStarted(const QString& downloadObjectPath); | ||
519 | 82 | void downloadError(const QString& errorMessage); | ||
520 | 83 | void clickTokenFetched(const QString& clickToken); | ||
521 | 84 | void clickTokenFetchError(const QString& errorMessage); | ||
522 | 85 | |||
523 | 86 | protected slots: | ||
524 | 87 | virtual void handleCredentialsFound(const UbuntuOne::Token &token); | ||
525 | 88 | virtual void handleCredentialsNotFound(); | ||
526 | 89 | virtual void handleNetworkFinished(); | ||
527 | 90 | virtual void handleNetworkError(QNetworkReply::NetworkError error); | ||
528 | 91 | virtual void handleDownloadCreated(Download *download); | ||
529 | 92 | virtual void handleClickTokenFetched(const QString& clickToken); | ||
530 | 93 | virtual void handleClickTokenFetchError(const QString& errorMessage); | ||
540 | 94 | 78 | ||
541 | 95 | protected: | 79 | protected: |
559 | 96 | struct Private; | 80 | QSharedPointer<click::web::Client> client; |
560 | 97 | QScopedPointer<Private> impl; | 81 | QSharedPointer<Ubuntu::DownloadManager::Manager> dm; |
561 | 98 | }; | 82 | QSharedPointer<click::CredentialsService> sso; |
545 | 99 | |||
546 | 100 | enum class InstallError {NoError, CredentialsError, DownloadInstallError}; | ||
547 | 101 | |||
548 | 102 | class Downloader | ||
549 | 103 | { | ||
550 | 104 | public: | ||
551 | 105 | Downloader(const QSharedPointer<click::network::AccessManager>& networkAccessManager); | ||
552 | 106 | virtual void get_download_progress(std::string package_name, const std::function<void (std::string)>& callback); | ||
553 | 107 | void startDownload(const std::string& url, const std::string& download_sha512, const std::string& package_name, | ||
554 | 108 | const std::function<void (std::pair<std::string, InstallError>)>& callback); | ||
555 | 109 | virtual ~Downloader(); | ||
556 | 110 | virtual click::DownloadManager& getDownloadManager(); | ||
557 | 111 | private: | ||
558 | 112 | QSharedPointer<click::network::AccessManager> networkAccessManager; | ||
562 | 113 | }; | 83 | }; |
563 | 114 | 84 | ||
564 | 115 | } | 85 | } |
565 | 116 | 86 | ||
566 | === modified file 'libclickscope/click/preview.cpp' | |||
567 | --- libclickscope/click/preview.cpp 2016-02-08 16:24:22 +0000 | |||
568 | +++ libclickscope/click/preview.cpp 2016-03-01 16:46:59 +0000 | |||
569 | @@ -1,5 +1,5 @@ | |||
570 | 1 | /* | 1 | /* |
572 | 2 | * Copyright (C) 2014 Canonical Ltd. | 2 | * Copyright (C) 2014-2016 Canonical Ltd. |
573 | 3 | * | 3 | * |
574 | 4 | * This program is free software: you can redistribute it and/or modify it | 4 | * This program is free software: you can redistribute it and/or modify it |
575 | 5 | * under the terms of the GNU General Public License version 3, as published | 5 | * under the terms of the GNU General Public License version 3, as published |
576 | @@ -47,6 +47,7 @@ | |||
577 | 47 | #include <unity/scopes/VariantBuilder.h> | 47 | #include <unity/scopes/VariantBuilder.h> |
578 | 48 | #include <unity/scopes/ColumnLayout.h> | 48 | #include <unity/scopes/ColumnLayout.h> |
579 | 49 | 49 | ||
580 | 50 | #include <QCoreApplication> | ||
581 | 50 | #include <QDebug> | 51 | #include <QDebug> |
582 | 51 | 52 | ||
583 | 52 | #include <functional> | 53 | #include <functional> |
584 | @@ -167,30 +168,30 @@ | |||
585 | 167 | } | 168 | } |
586 | 168 | 169 | ||
587 | 169 | void Preview::choose_strategy(const QSharedPointer<web::Client> &client, | 170 | void Preview::choose_strategy(const QSharedPointer<web::Client> &client, |
588 | 170 | const QSharedPointer<click::network::AccessManager>& nam, | ||
589 | 171 | const QSharedPointer<pay::Package>& ppackage, | 171 | const QSharedPointer<pay::Package>& ppackage, |
590 | 172 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, | ||
591 | 172 | std::shared_ptr<click::DepartmentsDb> depts) | 173 | std::shared_ptr<click::DepartmentsDb> depts) |
592 | 173 | { | 174 | { |
594 | 174 | strategy.reset(build_strategy(result, metadata, client, nam, ppackage, depts)); | 175 | strategy.reset(build_strategy(result, metadata, client, ppackage, manager, depts)); |
595 | 175 | } | 176 | } |
596 | 176 | 177 | ||
597 | 177 | PreviewStrategy* Preview::build_installing(const std::string& download_url, | 178 | PreviewStrategy* Preview::build_installing(const std::string& download_url, |
598 | 178 | const std::string& download_sha512, | 179 | const std::string& download_sha512, |
599 | 179 | const unity::scopes::Result& result, | 180 | const unity::scopes::Result& result, |
600 | 180 | const QSharedPointer<click::web::Client>& client, | 181 | const QSharedPointer<click::web::Client>& client, |
602 | 181 | const QSharedPointer<click::network::AccessManager>& nam, | 182 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
603 | 182 | std::shared_ptr<click::DepartmentsDb> depts) | 183 | std::shared_ptr<click::DepartmentsDb> depts) |
604 | 183 | { | 184 | { |
606 | 184 | return new InstallingPreview(download_url, download_sha512, result, client, nam, depts); | 185 | return new InstallingPreview(download_url, download_sha512, result, client, manager, depts); |
607 | 185 | } | 186 | } |
608 | 186 | 187 | ||
609 | 187 | 188 | ||
610 | 188 | PreviewStrategy* Preview::build_strategy(const unity::scopes::Result &result, | 189 | PreviewStrategy* Preview::build_strategy(const unity::scopes::Result &result, |
611 | 189 | const unity::scopes::ActionMetadata &metadata, | 190 | const unity::scopes::ActionMetadata &metadata, |
612 | 190 | const QSharedPointer<web::Client> &client, | 191 | const QSharedPointer<web::Client> &client, |
613 | 191 | const QSharedPointer<click::network::AccessManager>& nam, | ||
614 | 192 | const QSharedPointer<pay::Package>& ppackage, | 192 | const QSharedPointer<pay::Package>& ppackage, |
616 | 193 | std::shared_ptr<click::DepartmentsDb> depts) | 193 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
617 | 194 | std::shared_ptr<click::DepartmentsDb> depts) | ||
618 | 194 | { | 195 | { |
619 | 195 | if (metadata.scope_data().which() != scopes::Variant::Type::Null) { | 196 | if (metadata.scope_data().which() != scopes::Variant::Type::Null) { |
620 | 196 | auto metadict = metadata.scope_data().get_dict(); | 197 | auto metadict = metadata.scope_data().get_dict(); |
621 | @@ -210,11 +211,11 @@ | |||
622 | 210 | std::string download_url = metadict["download_url"].get_string(); | 211 | std::string download_url = metadict["download_url"].get_string(); |
623 | 211 | std::string download_sha512 = metadict["download_sha512"].get_string(); | 212 | std::string download_sha512 = metadict["download_sha512"].get_string(); |
624 | 212 | if (action_id == click::Preview::Actions::INSTALL_CLICK) { | 213 | if (action_id == click::Preview::Actions::INSTALL_CLICK) { |
626 | 213 | return build_installing(download_url, download_sha512, result, client, nam, depts); | 214 | return build_installing(download_url, download_sha512, result, client, manager, depts); |
627 | 214 | } else { | 215 | } else { |
628 | 215 | qWarning() << "unexpected action id " << QString::fromStdString(action_id) | 216 | qWarning() << "unexpected action id " << QString::fromStdString(action_id) |
629 | 216 | << " given with download_url" << QString::fromStdString(download_url); | 217 | << " given with download_url" << QString::fromStdString(download_url); |
631 | 217 | return new UninstalledPreview(result, client, depts, nam, ppackage); | 218 | return new UninstalledPreview(result, client, depts, manager, ppackage); |
632 | 218 | } | 219 | } |
633 | 219 | } else if (metadict.count(click::Preview::Actions::CANCEL_PURCHASE_UNINSTALLED) != 0) { | 220 | } else if (metadict.count(click::Preview::Actions::CANCEL_PURCHASE_UNINSTALLED) != 0) { |
634 | 220 | return new CancelPurchasePreview(result, false); | 221 | return new CancelPurchasePreview(result, false); |
635 | @@ -223,18 +224,18 @@ | |||
636 | 223 | } else if (metadict.count(click::Preview::Actions::UNINSTALL_CLICK) != 0) { | 224 | } else if (metadict.count(click::Preview::Actions::UNINSTALL_CLICK) != 0) { |
637 | 224 | return new UninstallConfirmationPreview(result); | 225 | return new UninstallConfirmationPreview(result); |
638 | 225 | } else if (metadict.count(click::Preview::Actions::CONFIRM_UNINSTALL) != 0) { | 226 | } else if (metadict.count(click::Preview::Actions::CONFIRM_UNINSTALL) != 0) { |
640 | 226 | return new UninstallingPreview(result, client, nam, ppackage); | 227 | return new UninstallingPreview(result, client, manager, ppackage); |
641 | 227 | } else if (metadict.count(click::Preview::Actions::CONFIRM_CANCEL_PURCHASE_UNINSTALLED) != 0) { | 228 | } else if (metadict.count(click::Preview::Actions::CONFIRM_CANCEL_PURCHASE_UNINSTALLED) != 0) { |
643 | 228 | return new CancellingPurchasePreview(result, client, nam, ppackage, false); | 229 | return new CancellingPurchasePreview(result, client, ppackage, manager, false); |
644 | 229 | } else if (metadict.count(click::Preview::Actions::CONFIRM_CANCEL_PURCHASE_INSTALLED) != 0) { | 230 | } else if (metadict.count(click::Preview::Actions::CONFIRM_CANCEL_PURCHASE_INSTALLED) != 0) { |
646 | 230 | return new CancellingPurchasePreview(result, client, nam, ppackage, true); | 231 | return new CancellingPurchasePreview(result, client, ppackage, manager, true); |
647 | 231 | } else if (metadict.count(click::Preview::Actions::RATED) != 0) { | 232 | } else if (metadict.count(click::Preview::Actions::RATED) != 0) { |
648 | 232 | return new InstalledPreview(result, metadata, client, ppackage, depts); | 233 | return new InstalledPreview(result, metadata, client, ppackage, depts); |
649 | 233 | } else if (metadict.count(click::Preview::Actions::SHOW_UNINSTALLED) != 0) { | 234 | } else if (metadict.count(click::Preview::Actions::SHOW_UNINSTALLED) != 0) { |
651 | 234 | return new UninstalledPreview(result, client, depts, nam, ppackage); | 235 | return new UninstalledPreview(result, client, depts, manager, ppackage); |
652 | 235 | } else { | 236 | } else { |
653 | 236 | qWarning() << "preview() called with unexpected metadata. returning uninstalled preview"; | 237 | qWarning() << "preview() called with unexpected metadata. returning uninstalled preview"; |
655 | 237 | return new UninstalledPreview(result, client, depts, nam, ppackage); | 238 | return new UninstalledPreview(result, client, depts, manager, ppackage); |
656 | 238 | } | 239 | } |
657 | 239 | } else { | 240 | } else { |
658 | 240 | // metadata.scope_data() is Null, so we return an appropriate "default" preview: | 241 | // metadata.scope_data() is Null, so we return an appropriate "default" preview: |
659 | @@ -245,7 +246,7 @@ | |||
660 | 245 | if (result["installed"].get_bool() == true) { | 246 | if (result["installed"].get_bool() == true) { |
661 | 246 | return new InstalledPreview(result, metadata, client, ppackage, depts); | 247 | return new InstalledPreview(result, metadata, client, ppackage, depts); |
662 | 247 | } else { | 248 | } else { |
664 | 248 | return new UninstalledPreview(result, client, depts, nam, ppackage); | 249 | return new UninstalledPreview(result, client, depts, manager, ppackage); |
665 | 249 | } | 250 | } |
666 | 250 | } | 251 | } |
667 | 251 | 252 | ||
668 | @@ -385,9 +386,14 @@ | |||
669 | 385 | 386 | ||
670 | 386 | void PreviewStrategy::run_under_qt(const std::function<void ()> &task) | 387 | void PreviewStrategy::run_under_qt(const std::function<void ()> &task) |
671 | 387 | { | 388 | { |
673 | 388 | qt::core::world::enter_with_task([task]() { | 389 | auto _app = QCoreApplication::instance(); |
674 | 390 | if (_app != nullptr) { | ||
675 | 391 | qt::core::world::enter_with_task([task]() { | ||
676 | 392 | task(); | ||
677 | 393 | }); | ||
678 | 394 | } else { | ||
679 | 389 | task(); | 395 | task(); |
681 | 390 | }); | 396 | } |
682 | 391 | } | 397 | } |
683 | 392 | 398 | ||
684 | 393 | std::string get_string_maybe_null(scopes::Variant variant) | 399 | std::string get_string_maybe_null(scopes::Variant variant) |
685 | @@ -591,7 +597,7 @@ | |||
686 | 591 | scopes::Variant(_("Close"))); | 597 | scopes::Variant(_("Close"))); |
687 | 592 | } | 598 | } |
688 | 593 | 599 | ||
690 | 594 | scopes::PreviewWidgetList PreviewStrategy::loginErrorWidgets(const PackageDetails& details) | 600 | scopes::PreviewWidgetList PreviewStrategy::loginErrorWidgets(const std::string& download_url, const std::string& download_sha512) |
691 | 595 | { | 601 | { |
692 | 596 | auto widgets = errorWidgets(scopes::Variant(_("Login Error")), | 602 | auto widgets = errorWidgets(scopes::Variant(_("Login Error")), |
693 | 597 | scopes::Variant(_("Please log in to your Ubuntu One account.")), | 603 | scopes::Variant(_("Please log in to your Ubuntu One account.")), |
694 | @@ -605,8 +611,8 @@ | |||
695 | 605 | { | 611 | { |
696 | 606 | {"id", scopes::Variant(click::Preview::Actions::INSTALL_CLICK)}, | 612 | {"id", scopes::Variant(click::Preview::Actions::INSTALL_CLICK)}, |
697 | 607 | {"label", scopes::Variant(_("Go to Accounts"))}, | 613 | {"label", scopes::Variant(_("Go to Accounts"))}, |
700 | 608 | {"download_url", scopes::Variant(details.download_url)}, | 614 | {"download_url", scopes::Variant(download_url)}, |
701 | 609 | {"download_sha512", scopes::Variant(details.download_sha512)}, | 615 | {"download_sha512", scopes::Variant(download_sha512)}, |
702 | 610 | }); | 616 | }); |
703 | 611 | buttons.add_attribute_value("actions", builder.end()); | 617 | buttons.add_attribute_value("actions", builder.end()); |
704 | 612 | oa_client.register_account_login_item(buttons, | 618 | oa_client.register_account_login_item(buttons, |
705 | @@ -698,13 +704,14 @@ | |||
706 | 698 | const std::string &download_sha512, | 704 | const std::string &download_sha512, |
707 | 699 | const unity::scopes::Result &result, | 705 | const unity::scopes::Result &result, |
708 | 700 | const QSharedPointer<click::web::Client>& client, | 706 | const QSharedPointer<click::web::Client>& client, |
716 | 701 | const QSharedPointer<click::network::AccessManager> &nam, | 707 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
717 | 702 | std::shared_ptr<click::DepartmentsDb> depts) | 708 | std::shared_ptr<click::DepartmentsDb> depts) : |
718 | 703 | : PreviewStrategy(result, client), DepartmentUpdater(depts), | 709 | PreviewStrategy(result, client), |
719 | 704 | download_url(download_url), | 710 | DepartmentUpdater(depts), |
720 | 705 | download_sha512(download_sha512), | 711 | download_url(download_url), |
721 | 706 | downloader(new click::Downloader(nam)), | 712 | download_sha512(download_sha512), |
722 | 707 | depts_db(depts) | 713 | dm(new DownloadManager(client, manager)), |
723 | 714 | depts_db(depts) | ||
724 | 708 | { | 715 | { |
725 | 709 | } | 716 | } |
726 | 710 | 717 | ||
727 | @@ -724,49 +731,57 @@ | |||
728 | 724 | void InstallingPreview::run(const unity::scopes::PreviewReplyProxy &reply) | 731 | void InstallingPreview::run(const unity::scopes::PreviewReplyProxy &reply) |
729 | 725 | { | 732 | { |
730 | 726 | qDebug() << "Starting installation" << QString(download_url.c_str()) << QString(download_sha512.c_str()); | 733 | qDebug() << "Starting installation" << QString(download_url.c_str()) << QString(download_sha512.c_str()); |
770 | 727 | downloader->startDownload(download_url, download_sha512, result["name"].get_string(), | 734 | std::promise<bool> promise; |
771 | 728 | [this, reply] (std::pair<std::string, click::InstallError> rc){ | 735 | auto future = promise.get_future(); |
772 | 729 | // NOTE: details not needed by fooErrorWidgets, so no need to populateDetails(): | 736 | run_under_qt([this, reply, &promise]() { |
773 | 730 | bool login_error = false; | 737 | QSharedPointer<click::CredentialsService> sso(new click::CredentialsService()); |
774 | 731 | switch (rc.second) | 738 | dm->setCredentialsService(sso); |
775 | 732 | { | 739 | dm->start(download_url, download_sha512, result["name"].get_string(), |
776 | 733 | case InstallError::DownloadInstallError: | 740 | [this, reply, &promise] (std::string msg, DownloadManager::Error dmerr){ |
777 | 734 | qWarning() << "Error received from UDM during startDownload:" << rc.first.c_str(); | 741 | switch (dmerr) |
778 | 735 | reply->push(downloadErrorWidgets()); | 742 | { |
779 | 736 | return; | 743 | case DownloadManager::Error::DownloadInstallError: |
780 | 737 | case InstallError::CredentialsError: | 744 | qWarning() << "Error received from UDM during startDownload:" << msg.c_str(); |
781 | 738 | qWarning() << "InstallingPreview got error in getting credentials during startDownload"; | 745 | reply->push(downloadErrorWidgets()); |
782 | 739 | login_error = true; | 746 | promise.set_value(false); |
783 | 740 | default: | 747 | break; |
784 | 741 | std::string object_path = rc.first; | 748 | case DownloadManager::Error::CredentialsError: |
785 | 742 | qDebug() << "Successfully created UDM Download."; | 749 | qWarning() << "InstallingPreview got error in getting credentials during startDownload"; |
786 | 743 | populateDetails([this, reply, object_path, login_error](const PackageDetails &details) { | 750 | reply->push(loginErrorWidgets(download_url, download_sha512)); |
787 | 744 | store_department(details); | 751 | promise.set_value(false); |
788 | 745 | if (login_error) { | 752 | break; |
789 | 746 | reply->push(loginErrorWidgets(details)); | 753 | case DownloadManager::Error::NoError: { |
790 | 747 | } else { | 754 | std::string object_path = msg; |
791 | 748 | pushPackagePreviewWidgets(cachedWidgets, details, progressBarWidget(object_path)); | 755 | qDebug() << "Successfully created UDM Download."; |
792 | 749 | startLauncherAnimation(details); | 756 | populateDetails([this, reply, object_path](const PackageDetails &details) { |
793 | 750 | } | 757 | store_department(details); |
794 | 751 | }, | 758 | pushPackagePreviewWidgets(cachedWidgets, details, progressBarWidget(object_path)); |
795 | 752 | [this, reply, login_error](const ReviewList& reviewlist, | 759 | startLauncherAnimation(details); |
796 | 753 | click::Reviews::Error error) { | 760 | }, |
797 | 754 | if (!login_error) { | 761 | [this, reply, &promise](const ReviewList& reviewlist, |
798 | 755 | if (error == click::Reviews::Error::NoError) { | 762 | click::Reviews::Error error) { |
799 | 756 | auto const revs = reviewsWidgets(reviewlist); | 763 | if (error == click::Reviews::Error::NoError) { |
800 | 757 | cachedWidgets.push(revs); | 764 | auto const revs = reviewsWidgets(reviewlist); |
801 | 758 | cachedWidgets.layout.appendToColumn(cachedWidgets.layout.singleColumn.column1, revs); | 765 | cachedWidgets.push(revs); |
802 | 759 | cachedWidgets.layout.appendToColumn(cachedWidgets.layout.twoColumns.column1, revs); | 766 | cachedWidgets.layout.appendToColumn(cachedWidgets.layout.singleColumn.column1, revs); |
803 | 760 | } else { | 767 | cachedWidgets.layout.appendToColumn(cachedWidgets.layout.twoColumns.column1, revs); |
804 | 761 | qDebug() << "There was an error getting reviews for:" << result["name"].get_string().c_str(); | 768 | } else { |
805 | 762 | } | 769 | qDebug() << "There was an error getting reviews for:" << result["name"].get_string().c_str(); |
806 | 763 | } | 770 | } |
807 | 764 | cachedWidgets.flush(reply); | 771 | cachedWidgets.flush(reply); |
808 | 765 | reply->finished(); | 772 | promise.set_value(true); |
809 | 773 | }); | ||
810 | 774 | break; | ||
811 | 775 | } | ||
812 | 776 | default: | ||
813 | 777 | qCritical() << "Unknown error occurred downloading."; | ||
814 | 778 | promise.set_value(false); | ||
815 | 779 | break; | ||
816 | 780 | } | ||
817 | 766 | }); | 781 | }); |
821 | 767 | break; | 782 | }); |
822 | 768 | } | 783 | future.get(); |
823 | 769 | }); | 784 | reply->finished(); |
824 | 770 | } | 785 | } |
825 | 771 | 786 | ||
826 | 772 | scopes::PreviewWidgetList PreviewStrategy::progressBarWidget(const std::string& object_path) | 787 | scopes::PreviewWidgetList PreviewStrategy::progressBarWidget(const std::string& object_path) |
827 | @@ -1195,19 +1210,14 @@ | |||
828 | 1195 | 1210 | ||
829 | 1196 | // class UninstalledPreview | 1211 | // class UninstalledPreview |
830 | 1197 | 1212 | ||
831 | 1198 | click::Downloader* UninstalledPreview::get_downloader(const QSharedPointer<click::network::AccessManager>& nam) | ||
832 | 1199 | { | ||
833 | 1200 | static auto downloader = new click::Downloader(nam); | ||
834 | 1201 | return downloader; | ||
835 | 1202 | } | ||
836 | 1203 | |||
837 | 1204 | UninstalledPreview::UninstalledPreview(const unity::scopes::Result& result, | 1213 | UninstalledPreview::UninstalledPreview(const unity::scopes::Result& result, |
838 | 1205 | const QSharedPointer<click::web::Client>& client, | 1214 | const QSharedPointer<click::web::Client>& client, |
839 | 1206 | const std::shared_ptr<click::DepartmentsDb>& depts, | 1215 | const std::shared_ptr<click::DepartmentsDb>& depts, |
841 | 1207 | const QSharedPointer<click::network::AccessManager>& nam, | 1216 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
842 | 1208 | const QSharedPointer<pay::Package>& ppackage) | 1217 | const QSharedPointer<pay::Package>& ppackage) |
843 | 1209 | : PreviewStrategy(result, client, ppackage), | 1218 | : PreviewStrategy(result, client, ppackage), |
845 | 1210 | DepartmentUpdater(depts), nam(nam) | 1219 | DepartmentUpdater(depts), |
846 | 1220 | dm(new DownloadManager(client, manager)) | ||
847 | 1211 | { | 1221 | { |
848 | 1212 | qDebug() << "Creating new UninstalledPreview for result" << QString::fromStdString(result["name"].get_string()); | 1222 | qDebug() << "Creating new UninstalledPreview for result" << QString::fromStdString(result["name"].get_string()); |
849 | 1213 | } | 1223 | } |
850 | @@ -1226,8 +1236,8 @@ | |||
851 | 1226 | [this, reply](const ReviewList& reviewlist, | 1236 | [this, reply](const ReviewList& reviewlist, |
852 | 1227 | click::Reviews::Error reviewserror) { | 1237 | click::Reviews::Error reviewserror) { |
853 | 1228 | std::string app_name = result["name"].get_string(); | 1238 | std::string app_name = result["name"].get_string(); |
856 | 1229 | get_downloader(nam)->get_download_progress(app_name, | 1239 | dm->get_progress(app_name, |
857 | 1230 | [this, reply, reviewlist, reviewserror](std::string object_path){ | 1240 | [this, reply, reviewlist, reviewserror](std::string object_path){ |
858 | 1231 | found_object_path = object_path; | 1241 | found_object_path = object_path; |
859 | 1232 | scopes::PreviewWidgetList button_widgets; | 1242 | scopes::PreviewWidgetList button_widgets; |
860 | 1233 | if(found_object_path.empty()) { | 1243 | if(found_object_path.empty()) { |
861 | @@ -1304,9 +1314,9 @@ | |||
862 | 1304 | // TODO: this class should be removed once uninstall() is handled elsewhere. | 1314 | // TODO: this class should be removed once uninstall() is handled elsewhere. |
863 | 1305 | UninstallingPreview::UninstallingPreview(const unity::scopes::Result& result, | 1315 | UninstallingPreview::UninstallingPreview(const unity::scopes::Result& result, |
864 | 1306 | const QSharedPointer<click::web::Client>& client, | 1316 | const QSharedPointer<click::web::Client>& client, |
866 | 1307 | const QSharedPointer<click::network::AccessManager>& nam, | 1317 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
867 | 1308 | const QSharedPointer<pay::Package>& ppackage) | 1318 | const QSharedPointer<pay::Package>& ppackage) |
869 | 1309 | : UninstalledPreview(result, client, nullptr, nam, ppackage) | 1319 | : UninstalledPreview(result, client, nullptr, manager, ppackage) |
870 | 1310 | { | 1320 | { |
871 | 1311 | } | 1321 | } |
872 | 1312 | 1322 | ||
873 | @@ -1347,10 +1357,10 @@ | |||
874 | 1347 | 1357 | ||
875 | 1348 | CancellingPurchasePreview::CancellingPurchasePreview(const unity::scopes::Result& result, | 1358 | CancellingPurchasePreview::CancellingPurchasePreview(const unity::scopes::Result& result, |
876 | 1349 | const QSharedPointer<click::web::Client>& client, | 1359 | const QSharedPointer<click::web::Client>& client, |
877 | 1350 | const QSharedPointer<click::network::AccessManager>& nam, | ||
878 | 1351 | const QSharedPointer<pay::Package>& ppackage, | 1360 | const QSharedPointer<pay::Package>& ppackage, |
879 | 1361 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, | ||
880 | 1352 | bool installed) | 1362 | bool installed) |
882 | 1353 | : UninstallingPreview(result, client, nam, ppackage), | 1363 | : UninstallingPreview(result, client, manager, ppackage), |
883 | 1354 | installed(installed) | 1364 | installed(installed) |
884 | 1355 | { | 1365 | { |
885 | 1356 | } | 1366 | } |
886 | 1357 | 1367 | ||
887 | === modified file 'libclickscope/click/preview.h' | |||
888 | --- libclickscope/click/preview.h 2016-02-08 16:24:22 +0000 | |||
889 | +++ libclickscope/click/preview.h 2016-03-01 16:46:59 +0000 | |||
890 | @@ -105,14 +105,14 @@ | |||
891 | 105 | PreviewStrategy* build_strategy(const unity::scopes::Result& result, | 105 | PreviewStrategy* build_strategy(const unity::scopes::Result& result, |
892 | 106 | const unity::scopes::ActionMetadata& metadata, | 106 | const unity::scopes::ActionMetadata& metadata, |
893 | 107 | const QSharedPointer<web::Client> &client, | 107 | const QSharedPointer<web::Client> &client, |
894 | 108 | const QSharedPointer<click::network::AccessManager>& nam, | ||
895 | 109 | const QSharedPointer<pay::Package>& ppackage, | 108 | const QSharedPointer<pay::Package>& ppackage, |
896 | 109 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, | ||
897 | 110 | std::shared_ptr<click::DepartmentsDb> depts); | 110 | std::shared_ptr<click::DepartmentsDb> depts); |
898 | 111 | virtual PreviewStrategy* build_installing(const std::string& download_url, | 111 | virtual PreviewStrategy* build_installing(const std::string& download_url, |
899 | 112 | const std::string& download_sha512, | 112 | const std::string& download_sha512, |
900 | 113 | const unity::scopes::Result& result, | 113 | const unity::scopes::Result& result, |
901 | 114 | const QSharedPointer<click::web::Client>& client, | 114 | const QSharedPointer<click::web::Client>& client, |
903 | 115 | const QSharedPointer<click::network::AccessManager>& nam, | 115 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
904 | 116 | std::shared_ptr<click::DepartmentsDb> depts); | 116 | std::shared_ptr<click::DepartmentsDb> depts); |
905 | 117 | public: | 117 | public: |
906 | 118 | UNITY_DEFINES_PTRS(Preview); | 118 | UNITY_DEFINES_PTRS(Preview); |
907 | @@ -145,8 +145,8 @@ | |||
908 | 145 | const unity::scopes::ActionMetadata& metadata); | 145 | const unity::scopes::ActionMetadata& metadata); |
909 | 146 | virtual ~Preview(); | 146 | virtual ~Preview(); |
910 | 147 | void choose_strategy(const QSharedPointer<web::Client> &client, | 147 | void choose_strategy(const QSharedPointer<web::Client> &client, |
911 | 148 | const QSharedPointer<click::network::AccessManager>& nam, | ||
912 | 149 | const QSharedPointer<pay::Package>& ppackage, | 148 | const QSharedPointer<pay::Package>& ppackage, |
913 | 149 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, | ||
914 | 150 | std::shared_ptr<click::DepartmentsDb> depts); | 150 | std::shared_ptr<click::DepartmentsDb> depts); |
915 | 151 | // From unity::scopes::PreviewQuery | 151 | // From unity::scopes::PreviewQuery |
916 | 152 | void cancelled() override; | 152 | void cancelled() override; |
917 | @@ -181,7 +181,7 @@ | |||
918 | 181 | virtual scopes::PreviewWidgetList progressBarWidget(const std::string& object_path); | 181 | virtual scopes::PreviewWidgetList progressBarWidget(const std::string& object_path); |
919 | 182 | virtual scopes::PreviewWidgetList reviewsWidgets(const click::ReviewList &reviewlist); | 182 | virtual scopes::PreviewWidgetList reviewsWidgets(const click::ReviewList &reviewlist); |
920 | 183 | virtual scopes::PreviewWidgetList downloadErrorWidgets(); | 183 | virtual scopes::PreviewWidgetList downloadErrorWidgets(); |
922 | 184 | virtual scopes::PreviewWidgetList loginErrorWidgets(const PackageDetails& details); | 184 | virtual scopes::PreviewWidgetList loginErrorWidgets(const std::string& download_url, const std::string& download_sha512); |
923 | 185 | virtual scopes::PreviewWidgetList errorWidgets(const scopes::Variant& title, | 185 | virtual scopes::PreviewWidgetList errorWidgets(const scopes::Variant& title, |
924 | 186 | const scopes::Variant& subtitle, | 186 | const scopes::Variant& subtitle, |
925 | 187 | const scopes::Variant& action_id, | 187 | const scopes::Variant& action_id, |
926 | @@ -228,7 +228,7 @@ | |||
927 | 228 | const std::string& download_sha512, | 228 | const std::string& download_sha512, |
928 | 229 | const unity::scopes::Result& result, | 229 | const unity::scopes::Result& result, |
929 | 230 | const QSharedPointer<click::web::Client>& client, | 230 | const QSharedPointer<click::web::Client>& client, |
931 | 231 | const QSharedPointer<click::network::AccessManager>& nam, | 231 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
932 | 232 | std::shared_ptr<click::DepartmentsDb> depts); | 232 | std::shared_ptr<click::DepartmentsDb> depts); |
933 | 233 | 233 | ||
934 | 234 | virtual ~InstallingPreview(); | 234 | virtual ~InstallingPreview(); |
935 | @@ -238,7 +238,7 @@ | |||
936 | 238 | protected: | 238 | protected: |
937 | 239 | std::string download_url; | 239 | std::string download_url; |
938 | 240 | std::string download_sha512; | 240 | std::string download_sha512; |
940 | 241 | QSharedPointer<click::Downloader> downloader; | 241 | QSharedPointer<click::DownloadManager> dm; |
941 | 242 | std::shared_ptr<click::DepartmentsDb> depts_db; | 242 | std::shared_ptr<click::DepartmentsDb> depts_db; |
942 | 243 | CachedPreviewWidgets cachedWidgets; | 243 | CachedPreviewWidgets cachedWidgets; |
943 | 244 | void startLauncherAnimation(const PackageDetails& details); | 244 | void startLauncherAnimation(const PackageDetails& details); |
944 | @@ -314,12 +314,11 @@ | |||
945 | 314 | 314 | ||
946 | 315 | class UninstalledPreview : public PreviewStrategy, public DepartmentUpdater | 315 | class UninstalledPreview : public PreviewStrategy, public DepartmentUpdater |
947 | 316 | { | 316 | { |
948 | 317 | const QSharedPointer<click::network::AccessManager>& nam; | ||
949 | 318 | public: | 317 | public: |
950 | 319 | UninstalledPreview(const unity::scopes::Result& result, | 318 | UninstalledPreview(const unity::scopes::Result& result, |
951 | 320 | const QSharedPointer<click::web::Client>& client, | 319 | const QSharedPointer<click::web::Client>& client, |
952 | 321 | const std::shared_ptr<click::DepartmentsDb>& depts, | 320 | const std::shared_ptr<click::DepartmentsDb>& depts, |
954 | 322 | const QSharedPointer<click::network::AccessManager>& nam, | 321 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
955 | 323 | const QSharedPointer<pay::Package>& ppackage); | 322 | const QSharedPointer<pay::Package>& ppackage); |
956 | 324 | 323 | ||
957 | 325 | virtual ~UninstalledPreview(); | 324 | virtual ~UninstalledPreview(); |
958 | @@ -329,8 +328,9 @@ | |||
959 | 329 | PackageDetails found_details; | 328 | PackageDetails found_details; |
960 | 330 | CachedPreviewWidgets cachedWidgets; | 329 | CachedPreviewWidgets cachedWidgets; |
961 | 331 | std::string found_object_path; | 330 | std::string found_object_path; |
962 | 332 | virtual click::Downloader* get_downloader(const QSharedPointer<click::network::AccessManager>& nam); | ||
963 | 333 | virtual scopes::PreviewWidgetList uninstalledActionButtonWidgets(const PackageDetails &details); | 331 | virtual scopes::PreviewWidgetList uninstalledActionButtonWidgets(const PackageDetails &details); |
964 | 332 | |||
965 | 333 | QSharedPointer<click::DownloadManager> dm; | ||
966 | 334 | }; | 334 | }; |
967 | 335 | 335 | ||
968 | 336 | // TODO: this is only necessary to perform uninstall. | 336 | // TODO: this is only necessary to perform uninstall. |
969 | @@ -340,7 +340,7 @@ | |||
970 | 340 | public: | 340 | public: |
971 | 341 | UninstallingPreview(const unity::scopes::Result& result, | 341 | UninstallingPreview(const unity::scopes::Result& result, |
972 | 342 | const QSharedPointer<click::web::Client>& client, | 342 | const QSharedPointer<click::web::Client>& client, |
974 | 343 | const QSharedPointer<click::network::AccessManager>& nam, | 343 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
975 | 344 | const QSharedPointer<pay::Package>& ppackage); | 344 | const QSharedPointer<pay::Package>& ppackage); |
976 | 345 | 345 | ||
977 | 346 | virtual ~UninstallingPreview(); | 346 | virtual ~UninstallingPreview(); |
978 | @@ -357,8 +357,8 @@ | |||
979 | 357 | public: | 357 | public: |
980 | 358 | CancellingPurchasePreview(const unity::scopes::Result& result, | 358 | CancellingPurchasePreview(const unity::scopes::Result& result, |
981 | 359 | const QSharedPointer<click::web::Client>& client, | 359 | const QSharedPointer<click::web::Client>& client, |
982 | 360 | const QSharedPointer<click::network::AccessManager>& nam, | ||
983 | 361 | const QSharedPointer<pay::Package>& ppackage, | 360 | const QSharedPointer<pay::Package>& ppackage, |
984 | 361 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, | ||
985 | 362 | bool installed); | 362 | bool installed); |
986 | 363 | 363 | ||
987 | 364 | virtual ~CancellingPurchasePreview(); | 364 | virtual ~CancellingPurchasePreview(); |
988 | 365 | 365 | ||
989 | === modified file 'libclickscope/click/webclient.cpp' | |||
990 | --- libclickscope/click/webclient.cpp 2016-02-01 16:09:55 +0000 | |||
991 | +++ libclickscope/click/webclient.cpp 2016-03-01 16:46:59 +0000 | |||
992 | @@ -82,7 +82,7 @@ | |||
993 | 82 | const std::string& iri, | 82 | const std::string& iri, |
994 | 83 | const click::web::CallParams& params) | 83 | const click::web::CallParams& params) |
995 | 84 | { | 84 | { |
997 | 85 | return call(iri, "GET", false, | 85 | return call(iri, "GET", true, |
998 | 86 | std::map<std::string, std::string>(), "", params); | 86 | std::map<std::string, std::string>(), "", params); |
999 | 87 | } | 87 | } |
1000 | 88 | 88 | ||
1001 | @@ -118,9 +118,13 @@ | |||
1002 | 118 | QByteArray verb(method.c_str(), method.length()); | 118 | QByteArray verb(method.c_str(), method.length()); |
1003 | 119 | // | 119 | // |
1004 | 120 | // for 'get' use get method of access manager explicitly as sendCustomRequest disables the use of cache. | 120 | // for 'get' use get method of access manager explicitly as sendCustomRequest disables the use of cache. |
1008 | 121 | auto reply = (method == "GET" && buffer->size() == 0) ? | 121 | auto reply = ((method == "GET" && buffer->size() == 0) ? |
1009 | 122 | impl->network_access_manager->get(*request) : | 122 | impl->network_access_manager->get(*request) : |
1010 | 123 | impl->network_access_manager->sendCustomRequest(*request, verb, buffer.data()); | 123 | (method == "HEAD") ? |
1011 | 124 | impl->network_access_manager->head(*request) : | ||
1012 | 125 | impl->network_access_manager->sendCustomRequest(*request, | ||
1013 | 126 | verb, | ||
1014 | 127 | buffer.data())); | ||
1015 | 124 | responsePtr->setReply(reply); | 128 | responsePtr->setReply(reply); |
1016 | 125 | }; | 129 | }; |
1017 | 126 | 130 | ||
1018 | @@ -159,6 +163,15 @@ | |||
1019 | 159 | impl->setCredentialsService(sso); | 163 | impl->setCredentialsService(sso); |
1020 | 160 | } | 164 | } |
1021 | 161 | 165 | ||
1022 | 166 | void click::web::Client::invalidateCredentials() | ||
1023 | 167 | { | ||
1024 | 168 | if (impl->sso.isNull()) { | ||
1025 | 169 | qCritical() << "Request to delete credentials, but no sso object available."; | ||
1026 | 170 | return; | ||
1027 | 171 | } | ||
1028 | 172 | impl->sso->invalidateCredentials(); | ||
1029 | 173 | } | ||
1030 | 174 | |||
1031 | 162 | click::web::Response::Response(const QSharedPointer<QNetworkRequest>& request, | 175 | click::web::Response::Response(const QSharedPointer<QNetworkRequest>& request, |
1032 | 163 | const QSharedPointer<QBuffer>& buffer, | 176 | const QSharedPointer<QBuffer>& buffer, |
1033 | 164 | QObject* parent) | 177 | QObject* parent) |
1034 | 165 | 178 | ||
1035 | === modified file 'libclickscope/click/webclient.h' | |||
1036 | --- libclickscope/click/webclient.h 2016-02-01 16:09:55 +0000 | |||
1037 | +++ libclickscope/click/webclient.h 2016-03-01 16:46:59 +0000 | |||
1038 | @@ -121,11 +121,12 @@ | |||
1039 | 121 | virtual QSharedPointer<Response> call( | 121 | virtual QSharedPointer<Response> call( |
1040 | 122 | const std::string& iri, | 122 | const std::string& iri, |
1041 | 123 | const std::string& method, | 123 | const std::string& method, |
1043 | 124 | bool sign = false, | 124 | bool sign = true, |
1044 | 125 | const std::map<std::string, std::string>& headers = std::map<std::string, std::string>(), | 125 | const std::map<std::string, std::string>& headers = std::map<std::string, std::string>(), |
1045 | 126 | const std::string& data = "", | 126 | const std::string& data = "", |
1046 | 127 | const CallParams& params = CallParams()); | 127 | const CallParams& params = CallParams()); |
1047 | 128 | void setCredentialsService(const QSharedPointer<click::CredentialsService>& sso); | 128 | void setCredentialsService(const QSharedPointer<click::CredentialsService>& sso); |
1048 | 129 | virtual void invalidateCredentials(); | ||
1049 | 129 | 130 | ||
1050 | 130 | private: | 131 | private: |
1051 | 131 | struct Private; | 132 | struct Private; |
1052 | 132 | 133 | ||
1053 | === modified file 'libclickscope/tests/mock_ubuntu_download_manager.h' | |||
1054 | --- libclickscope/tests/mock_ubuntu_download_manager.h 2015-12-11 15:38:08 +0000 | |||
1055 | +++ libclickscope/tests/mock_ubuntu_download_manager.h 2016-03-01 16:46:59 +0000 | |||
1056 | @@ -1,5 +1,5 @@ | |||
1057 | 1 | /* | 1 | /* |
1059 | 2 | * Copyright (C) 2014 Canonical Ltd. | 2 | * Copyright (C) 2014-2016 Canonical Ltd. |
1060 | 3 | * | 3 | * |
1061 | 4 | * This program is free software: you can redistribute it and/or modify it | 4 | * This program is free software: you can redistribute it and/or modify it |
1062 | 5 | * under the terms of the GNU General Public License version 3, as published | 5 | * under the terms of the GNU General Public License version 3, as published |
1063 | @@ -34,9 +34,11 @@ | |||
1064 | 34 | #include <QDBusObjectPath> | 34 | #include <QDBusObjectPath> |
1065 | 35 | 35 | ||
1066 | 36 | #include <ubuntu/download_manager/download.h> | 36 | #include <ubuntu/download_manager/download.h> |
1067 | 37 | #include <ubuntu/download_manager/downloads_list.h> | ||
1068 | 37 | #include <ubuntu/download_manager/error.h> | 38 | #include <ubuntu/download_manager/error.h> |
1069 | 38 | #include <ubuntu/download_manager/manager.h> | 39 | #include <ubuntu/download_manager/manager.h> |
1070 | 39 | 40 | ||
1071 | 41 | #include <gmock/gmock.h> | ||
1072 | 40 | 42 | ||
1073 | 41 | class MockDownload : public Ubuntu::DownloadManager::Download | 43 | class MockDownload : public Ubuntu::DownloadManager::Download |
1074 | 42 | { | 44 | { |
1075 | @@ -87,6 +89,17 @@ | |||
1076 | 87 | MOCK_METHOD0(errorString, QString()); | 89 | MOCK_METHOD0(errorString, QString()); |
1077 | 88 | }; | 90 | }; |
1078 | 89 | 91 | ||
1079 | 92 | class MockDownloadsList : public Ubuntu::DownloadManager::DownloadsList | ||
1080 | 93 | { | ||
1081 | 94 | public: | ||
1082 | 95 | |||
1083 | 96 | MockDownloadsList() : Ubuntu::DownloadManager::DownloadsList() {}; | ||
1084 | 97 | |||
1085 | 98 | MOCK_CONST_METHOD0(downloads, QList<QSharedPointer<Ubuntu::DownloadManager::Download>>()); | ||
1086 | 99 | MOCK_CONST_METHOD0(isError, bool()); | ||
1087 | 100 | MOCK_CONST_METHOD0(error, Ubuntu::DownloadManager::Error*()); | ||
1088 | 101 | }; | ||
1089 | 102 | |||
1090 | 90 | class MockSystemDownloadManager : public Ubuntu::DownloadManager::Manager | 103 | class MockSystemDownloadManager : public Ubuntu::DownloadManager::Manager |
1091 | 91 | { | 104 | { |
1092 | 92 | public: | 105 | public: |
1093 | 93 | 106 | ||
1094 | === modified file 'libclickscope/tests/mock_webclient.h' | |||
1095 | --- libclickscope/tests/mock_webclient.h 2014-10-01 15:27:44 +0000 | |||
1096 | +++ libclickscope/tests/mock_webclient.h 2016-03-01 16:46:59 +0000 | |||
1097 | @@ -87,13 +87,13 @@ | |||
1098 | 87 | QSharedPointer<click::web::Response> call( | 87 | QSharedPointer<click::web::Response> call( |
1099 | 88 | const std::string& iri, | 88 | const std::string& iri, |
1100 | 89 | const click::web::CallParams& params=click::web::CallParams()) override { | 89 | const click::web::CallParams& params=click::web::CallParams()) override { |
1102 | 90 | return callImpl(iri, "GET", false, | 90 | return callImpl(iri, "GET", true, |
1103 | 91 | std::map<std::string, std::string>(), "", params); | 91 | std::map<std::string, std::string>(), "", params); |
1104 | 92 | } | 92 | } |
1105 | 93 | QSharedPointer<click::web::Response> call( | 93 | QSharedPointer<click::web::Response> call( |
1106 | 94 | const std::string& iri, | 94 | const std::string& iri, |
1107 | 95 | const std::string& method, | 95 | const std::string& method, |
1109 | 96 | bool sign = false, | 96 | bool sign = true, |
1110 | 97 | const std::map<std::string, std::string>& headers = std::map<std::string, std::string>(), | 97 | const std::map<std::string, std::string>& headers = std::map<std::string, std::string>(), |
1111 | 98 | const std::string& data = "", | 98 | const std::string& data = "", |
1112 | 99 | const click::web::CallParams& params=click::web::CallParams()) override { | 99 | const click::web::CallParams& params=click::web::CallParams()) override { |
1113 | @@ -102,6 +102,7 @@ | |||
1114 | 102 | 102 | ||
1115 | 103 | MOCK_METHOD1(has_header, bool(const std::string& header)); | 103 | MOCK_METHOD1(has_header, bool(const std::string& header)); |
1116 | 104 | MOCK_METHOD1(get_header, std::string(const std::string&header)); | 104 | MOCK_METHOD1(get_header, std::string(const std::string&header)); |
1117 | 105 | MOCK_METHOD0(invalidateCredentials, void()); | ||
1118 | 105 | }; | 106 | }; |
1119 | 106 | 107 | ||
1120 | 107 | } | 108 | } |
1121 | 108 | 109 | ||
1122 | === modified file 'libclickscope/tests/test_download_manager.cpp' | |||
1123 | --- libclickscope/tests/test_download_manager.cpp 2014-08-21 13:21:24 +0000 | |||
1124 | +++ libclickscope/tests/test_download_manager.cpp 2016-03-01 16:46:59 +0000 | |||
1125 | @@ -1,5 +1,5 @@ | |||
1126 | 1 | /* | 1 | /* |
1128 | 2 | * Copyright (C) 2014 Canonical Ltd. | 2 | * Copyright (C) 2014-2016 Canonical Ltd. |
1129 | 3 | * | 3 | * |
1130 | 4 | * This program is free software: you can redistribute it and/or modify it | 4 | * This program is free software: you can redistribute it and/or modify it |
1131 | 5 | * under the terms of the GNU General Public License version 3, as published | 5 | * under the terms of the GNU General Public License version 3, as published |
1132 | @@ -27,36 +27,20 @@ | |||
1133 | 27 | * files in the program, then also delete it here. | 27 | * files in the program, then also delete it here. |
1134 | 28 | */ | 28 | */ |
1135 | 29 | 29 | ||
1136 | 30 | #include <QDBusObjectPath> | ||
1137 | 31 | #include <QCoreApplication> | ||
1138 | 32 | #include <QDebug> | ||
1139 | 33 | #include <QString> | ||
1140 | 34 | #include <QStringBuilder> | ||
1141 | 35 | |||
1142 | 36 | #include <QThread> | ||
1143 | 37 | #include <QTimer> | ||
1144 | 38 | |||
1145 | 39 | #include <token.h> | ||
1146 | 40 | |||
1147 | 41 | #include <gmock/gmock.h> | ||
1148 | 42 | #include <gtest/gtest.h> | ||
1149 | 43 | |||
1150 | 44 | #include <click/download-manager.h> | 30 | #include <click/download-manager.h> |
1151 | 45 | #include <tests/mock_network_access_manager.h> | 31 | #include <tests/mock_network_access_manager.h> |
1152 | 32 | #include <tests/mock_webclient.h> | ||
1153 | 33 | #include <tests/mock_ubuntu_download_manager.h> | ||
1154 | 46 | #include <tests/mock_ubuntuone_credentials.h> | 34 | #include <tests/mock_ubuntuone_credentials.h> |
1155 | 47 | 35 | ||
1157 | 48 | #include "mock_ubuntu_download_manager.h" | 36 | #include <gtest/gtest.h> |
1158 | 37 | #include <memory> | ||
1159 | 49 | 38 | ||
1160 | 50 | using namespace ::testing; | 39 | using namespace ::testing; |
1161 | 51 | 40 | ||
1162 | 52 | namespace udm = Ubuntu::DownloadManager; | 41 | namespace udm = Ubuntu::DownloadManager; |
1163 | 53 | #include <ubuntu/download_manager/download_struct.h> | 42 | #include <ubuntu/download_manager/download_struct.h> |
1164 | 54 | 43 | ||
1165 | 55 | void PrintTo(const QString& str, ::std::ostream* os) | ||
1166 | 56 | { | ||
1167 | 57 | *os << "QString(\"" << str.toStdString() << "\")"; | ||
1168 | 58 | } | ||
1169 | 59 | |||
1170 | 60 | namespace | 44 | namespace |
1171 | 61 | { | 45 | { |
1172 | 62 | const QString TEST_URL("http://test.local/"); | 46 | const QString TEST_URL("http://test.local/"); |
1173 | @@ -67,421 +51,172 @@ | |||
1174 | 67 | const QString TEST_DOWNLOAD_ID("/com/ubuntu/download_manager/test"); | 51 | const QString TEST_DOWNLOAD_ID("/com/ubuntu/download_manager/test"); |
1175 | 68 | const QString TEST_DOWNLOADERROR_STRING("test downloadError string"); | 52 | const QString TEST_DOWNLOADERROR_STRING("test downloadError string"); |
1176 | 69 | 53 | ||
1595 | 70 | struct CredsNetworkTestParameters | 54 | |
1596 | 71 | { | 55 | class DownloadManagerTest : public ::testing::Test |
1597 | 72 | public: | 56 | { |
1598 | 73 | CredsNetworkTestParameters(bool credsFound = true, bool replySignalsError = false, | 57 | protected: |
1599 | 74 | int replyStatusCode = 200, bool replyHasClickRawHeader = true, | 58 | QSharedPointer<MockClient> clientPtr; |
1600 | 75 | bool expectSuccessSignal = true) | 59 | QSharedPointer<MockNetworkAccessManager> namPtr; |
1601 | 76 | : credsFound(credsFound), replySignalsError(replySignalsError), replyStatusCode(replyStatusCode), | 60 | QSharedPointer<MockSystemDownloadManager> sdmPtr; |
1602 | 77 | replyHasClickRawHeader(replyHasClickRawHeader), expectSuccessSignal(expectSuccessSignal) {}; | 61 | QSharedPointer<MockCredentialsService> ssoPtr; |
1603 | 78 | 62 | std::shared_ptr<click::DownloadManager> dmPtr; | |
1604 | 79 | bool credsFound; | 63 | |
1605 | 80 | bool replySignalsError; | 64 | virtual void SetUp() |
1606 | 81 | int replyStatusCode; | 65 | { |
1607 | 82 | bool replyHasClickRawHeader; | 66 | namPtr.reset(new MockNetworkAccessManager()); |
1608 | 83 | bool expectSuccessSignal; | 67 | clientPtr.reset(new NiceMock<MockClient>(namPtr)); |
1609 | 84 | }; | 68 | clientPtr->setCredentialsService(ssoPtr); |
1610 | 85 | 69 | dmPtr.reset(new click::DownloadManager(clientPtr, sdmPtr)); | |
1611 | 86 | ::std::ostream& operator<<(::std::ostream& os, const CredsNetworkTestParameters& p) | 70 | } |
1612 | 87 | { | 71 | |
1613 | 88 | return os << "creds[" << (p.credsFound ? "x" : " ") << "] " | 72 | MOCK_METHOD2(start_callback, void(std::string, click::DownloadManager::Error)); |
1614 | 89 | << "replySignalsError[" << (p.replySignalsError ? "x" : " ") << "] " | 73 | MOCK_METHOD1(progress_callback, void(std::string)); |
1615 | 90 | << "statusCode[" << p.replyStatusCode << "] " | 74 | }; |
1616 | 91 | << "replyHasClickRawHeader[" << (p.replyHasClickRawHeader ? "x" : " ") << "] " | 75 | |
1617 | 92 | << "expectSuccessSignal[" << (p.expectSuccessSignal ? "x" : " ") << "] "; | 76 | } |
1618 | 93 | } | 77 | |
1619 | 94 | 78 | TEST_F(DownloadManagerTest, testStartCallsWebservice) | |
1620 | 95 | 79 | { | |
1621 | 96 | struct StartDownloadTestParameters | 80 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; |
1622 | 97 | { | 81 | auto response = responseForReply(reply.asSharedPtr()); |
1623 | 98 | public: | 82 | |
1624 | 99 | StartDownloadTestParameters(bool clickTokenFetchSignalsError = false, | 83 | EXPECT_CALL(*clientPtr, callImpl(_, _, _, _, _, _)) |
1625 | 100 | bool downloadSignalsError = false, | 84 | .Times(1) |
1626 | 101 | bool expectSuccessSignal = true) | 85 | .WillOnce(Return(response)); |
1627 | 102 | : clickTokenFetchSignalsError(clickTokenFetchSignalsError), | 86 | |
1628 | 103 | downloadSignalsError(downloadSignalsError), | 87 | dmPtr->start("", "", "", |
1629 | 104 | expectSuccessSignal(expectSuccessSignal) {}; | 88 | [](std::string, click::DownloadManager::Error) {}); |
1630 | 105 | 89 | } | |
1631 | 106 | bool clickTokenFetchSignalsError; | 90 | |
1632 | 107 | bool downloadSignalsError; | 91 | TEST_F(DownloadManagerTest, testStartCallbackCalled) |
1633 | 108 | bool expectSuccessSignal; | 92 | { |
1634 | 109 | }; | 93 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; |
1635 | 110 | 94 | auto response = responseForReply(reply.asSharedPtr()); | |
1636 | 111 | ::std::ostream& operator<<(::std::ostream& os, const StartDownloadTestParameters& p) | 95 | |
1637 | 112 | { | 96 | EXPECT_CALL(reply.instance, attribute(_)).WillOnce(Return(QVariant(0))); |
1638 | 113 | return os << "clickTokenFetchSignalsError[" << (p.clickTokenFetchSignalsError ? "x" : " ") << "] " | 97 | EXPECT_CALL(reply.instance, readAll()) |
1639 | 114 | << "downloadSignalsError[" << (p.downloadSignalsError ? "x" : " ") << "] " | 98 | .Times(1) |
1640 | 115 | << "expectSuccessSignal[" << (p.expectSuccessSignal ? "x" : " ") << "] "; | 99 | .WillOnce(Return("")); |
1641 | 116 | } | 100 | EXPECT_CALL(*clientPtr, callImpl(_, _, _, _, _, _)) |
1642 | 117 | 101 | .Times(1) | |
1643 | 118 | 102 | .WillOnce(Return(response)); | |
1644 | 119 | struct DownloadManagerTestBase | 103 | EXPECT_CALL(*this, start_callback(_, _)).Times(1); |
1645 | 120 | { | 104 | |
1646 | 121 | DownloadManagerTestBase() | 105 | dmPtr->start("", "", "", |
1647 | 122 | : app(argc, argv), | 106 | [this](std::string msg, click::DownloadManager::Error err) { |
1648 | 123 | mockNam(new MockNetworkAccessManager()), | 107 | start_callback(msg, err); |
1649 | 124 | mockCredentialsService(new MockCredentialsService()), | 108 | }); |
1650 | 125 | mockReplyPtr(&mockReply, [](click::network::Reply*) {}), | 109 | response->replyFinished(); |
1651 | 126 | mockSystemDownloadManager(new MockSystemDownloadManager()) | 110 | } |
1652 | 127 | { | 111 | |
1653 | 128 | signalTimer.setSingleShot(true); | 112 | TEST_F(DownloadManagerTest, testStartHTTPForbidden) |
1654 | 129 | testTimeout.setSingleShot(true); | 113 | { |
1655 | 130 | 114 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; | |
1656 | 131 | QObject::connect( | 115 | auto response = responseForReply(reply.asSharedPtr()); |
1657 | 132 | &testTimeout, &QTimer::timeout, | 116 | |
1658 | 133 | [this]() { app.quit(); FAIL() << "Operation timed out."; } ); | 117 | EXPECT_CALL(reply.instance, attribute(_)).WillOnce(Return(QVariant(403))); |
1659 | 134 | } | 118 | EXPECT_CALL(reply.instance, readAll()) |
1660 | 135 | 119 | .Times(1) | |
1661 | 136 | void SetUp() | 120 | .WillOnce(Return("")); |
1662 | 137 | { | 121 | EXPECT_CALL(*clientPtr, callImpl(_, _, _, _, _, _)) |
1663 | 138 | const int oneSecondInMsec = 1000; | 122 | .Times(1) |
1664 | 139 | testTimeout.start(10 * oneSecondInMsec); | 123 | .WillOnce(Return(response)); |
1665 | 140 | } | 124 | EXPECT_CALL(*this, start_callback(StartsWith("Unhandled HTTP response code:"), |
1666 | 141 | 125 | click::DownloadManager::Error::DownloadInstallError)).Times(1); | |
1667 | 142 | void Quit() | 126 | |
1668 | 143 | { | 127 | dmPtr->start("", "", "", |
1669 | 144 | app.quit(); | 128 | [this](std::string msg, click::DownloadManager::Error err) { |
1670 | 145 | } | 129 | start_callback(msg, err); |
1671 | 146 | 130 | }); | |
1672 | 147 | int argc = 0; | 131 | response->replyFinished(); |
1673 | 148 | char** argv = nullptr; | 132 | } |
1674 | 149 | QCoreApplication app; | 133 | |
1675 | 150 | QTimer testTimeout; | 134 | TEST_F(DownloadManagerTest, testStartHTTPError) |
1676 | 151 | QTimer signalTimer; | 135 | { |
1677 | 152 | QSharedPointer<MockNetworkAccessManager> mockNam; | 136 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; |
1678 | 153 | QSharedPointer<MockCredentialsService> mockCredentialsService; | 137 | auto response = responseForReply(reply.asSharedPtr()); |
1679 | 154 | ::testing::NiceMock<MockNetworkReply> mockReply; | 138 | |
1680 | 155 | QSharedPointer<click::network::Reply> mockReplyPtr; | 139 | EXPECT_CALL(reply.instance, errorString()) |
1681 | 156 | QSharedPointer<MockSystemDownloadManager> mockSystemDownloadManager; | 140 | .WillOnce(Return(QString("ERROR"))); |
1682 | 157 | }; | 141 | EXPECT_CALL(reply.instance, attribute(_)).WillOnce(Return(QVariant(404))); |
1683 | 158 | 142 | EXPECT_CALL(reply.instance, readAll()) | |
1684 | 159 | struct DISABLED_DownloadManagerStartDownloadTest : public DownloadManagerTestBase, | 143 | .Times(1) |
1685 | 160 | public ::testing::TestWithParam<StartDownloadTestParameters> | 144 | .WillOnce(Return("")); |
1686 | 161 | { | 145 | EXPECT_CALL(*clientPtr, callImpl(_, _, _, _, _, _)) |
1687 | 162 | public: | 146 | .Times(1) |
1688 | 163 | }; | 147 | .WillOnce(Return(response)); |
1689 | 164 | 148 | EXPECT_CALL(*this, start_callback("ERROR (203)", | |
1690 | 165 | struct DISABLED_DownloadManagerCredsNetworkTest : public DownloadManagerTestBase, | 149 | click::DownloadManager::Error::DownloadInstallError)).Times(1); |
1691 | 166 | public ::testing::TestWithParam<CredsNetworkTestParameters> | 150 | |
1692 | 167 | { | 151 | dmPtr->start("", "", "", |
1693 | 168 | public: | 152 | [this](std::string msg, click::DownloadManager::Error err) { |
1694 | 169 | 153 | start_callback(msg, err); | |
1695 | 170 | void signalEmptyTokenFromMockCredsService() | 154 | }); |
1696 | 171 | { | 155 | response->errorHandler(QNetworkReply::ContentNotFoundError); |
1697 | 172 | UbuntuOne::Token token; | 156 | } |
1698 | 173 | mockCredentialsService->credentialsFound(token); | 157 | |
1699 | 174 | } | 158 | TEST_F(DownloadManagerTest, testStartCredentialsError) |
1700 | 175 | 159 | { | |
1701 | 176 | void signalErrorAfterDelay() | 160 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; |
1702 | 177 | { | 161 | auto response = responseForReply(reply.asSharedPtr()); |
1703 | 178 | // delay emitting this signal so that the download manager has | 162 | |
1704 | 179 | // time to connect to the signal first, as the (mock)Reply is | 163 | QSharedPointer<MockCredentialsService> sso(new MockCredentialsService()); |
1705 | 180 | // returned by the (mock)Nam. | 164 | dmPtr->setCredentialsService(sso); |
1706 | 181 | QObject::connect(&signalTimer, &QTimer::timeout, [this]() | 165 | |
1707 | 182 | { | 166 | EXPECT_CALL(reply.instance, errorString()) |
1708 | 183 | mockReplyPtr->error(QNetworkReply::UnknownNetworkError); | 167 | .WillOnce(Return(QString("ERROR"))); |
1709 | 184 | }); | 168 | EXPECT_CALL(reply.instance, attribute(_)).WillOnce(Return(QVariant(401))); |
1710 | 185 | signalTimer.start(10); | 169 | EXPECT_CALL(reply.instance, readAll()) |
1711 | 186 | } | 170 | .Times(1) |
1712 | 187 | 171 | .WillOnce(Return("")); | |
1713 | 188 | void signalFinishedAfterDelay() | 172 | EXPECT_CALL(*clientPtr, callImpl(_, _, _, _, _, _)) |
1714 | 189 | { | 173 | .Times(1) |
1715 | 190 | QObject::connect(&signalTimer, &QTimer::timeout, [this]() | 174 | .WillOnce(Return(response)); |
1716 | 191 | { | 175 | EXPECT_CALL(*(sso.data()), invalidateCredentials()); |
1717 | 192 | mockReplyPtr->finished(); | 176 | EXPECT_CALL(*this, start_callback("ERROR (201)", |
1718 | 193 | }); | 177 | click::DownloadManager::Error::CredentialsError)).Times(1); |
1719 | 194 | signalTimer.start(10); | 178 | |
1720 | 195 | } | 179 | dmPtr->start("", "", "test.package", |
1721 | 196 | }; | 180 | [this](std::string msg, click::DownloadManager::Error err) { |
1722 | 197 | 181 | start_callback(msg, err); | |
1723 | 198 | struct DownloadManagerMockClient | 182 | }); |
1724 | 199 | { | 183 | response->errorHandler(QNetworkReply::ContentAccessDenied); |
1725 | 200 | MOCK_METHOD0(onCredentialsNotFoundEmitted, void()); | 184 | } |
1726 | 201 | MOCK_METHOD1(onClickTokenFetchedEmitted, void(QString clickToken)); | 185 | |
1727 | 202 | MOCK_METHOD1(onClickTokenFetchErrorEmitted, void(QString errorMessage)); | 186 | // FIXME: createDownload() SEGV under tests |
1728 | 203 | MOCK_METHOD1(onDownloadStartedEmitted, void(QString id)); | 187 | TEST_F(DownloadManagerTest, DISABLED_testStartDownloadCreated) |
1729 | 204 | MOCK_METHOD1(onDownloadErrorEmitted, void(QString errorMessage)); | 188 | { |
1730 | 205 | }; | 189 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; |
1731 | 206 | 190 | auto response = responseForReply(reply.asSharedPtr()); | |
1732 | 207 | } // anon namespace | 191 | |
1733 | 208 | 192 | EXPECT_CALL(reply.instance, rawHeader(QByteArray("X-Click-Token"))) | |
1734 | 209 | 193 | .Times(1) | |
1735 | 210 | TEST_P(DISABLED_DownloadManagerCredsNetworkTest, TestFetchClickToken) | 194 | .WillOnce(Return(QString("clicktoken"))); |
1736 | 211 | { | 195 | EXPECT_CALL(reply.instance, attribute(_)).WillOnce(Return(QVariant(200))); |
1737 | 212 | using namespace ::testing; | 196 | EXPECT_CALL(reply.instance, readAll()) |
1738 | 213 | 197 | .Times(1) | |
1739 | 214 | CredsNetworkTestParameters p = GetParam(); | 198 | .WillOnce(Return("")); |
1740 | 215 | 199 | EXPECT_CALL(*clientPtr, callImpl(_, _, _, _, _, _)) | |
1741 | 216 | QList<QPair<QByteArray, QByteArray> > emptyHeaderPairs; | 200 | .Times(1) |
1742 | 217 | ON_CALL(mockReply, rawHeaderPairs()).WillByDefault(Return(emptyHeaderPairs)); | 201 | .WillOnce(Return(response)); |
1743 | 218 | ON_CALL(mockReply, readAll()).WillByDefault(Return(QByteArray("bogus readAll() return"))); | 202 | |
1744 | 219 | 203 | EXPECT_CALL(*sdmPtr, createDownload(_, _, _)); | |
1745 | 220 | if (p.credsFound) { | 204 | dmPtr->start("", "", "test.package", |
1746 | 221 | 205 | [this](std::string msg, click::DownloadManager::Error err) { | |
1747 | 222 | EXPECT_CALL(*mockCredentialsService, getCredentials()) | 206 | start_callback(msg, err); |
1748 | 223 | .Times(1).WillOnce( | 207 | }); |
1749 | 224 | InvokeWithoutArgs(this, | 208 | response->replyFinished(); |
1750 | 225 | &DISABLED_DownloadManagerCredsNetworkTest::signalEmptyTokenFromMockCredsService)); | 209 | } |
1751 | 226 | 210 | ||
1752 | 227 | if (p.replySignalsError) { | 211 | // FIXME: getAllDownloadsWithMetadata() SEGV under tests |
1753 | 228 | EXPECT_CALL(*mockNam, head(_)).WillOnce( | 212 | TEST_F(DownloadManagerTest, DISABLED_testGetProgressNoDownloads) |
1754 | 229 | DoAll( | 213 | { |
1755 | 230 | InvokeWithoutArgs(this, &DISABLED_DownloadManagerCredsNetworkTest::signalErrorAfterDelay), | 214 | EXPECT_CALL(*sdmPtr, getAllDownloadsWithMetadata(_, _, _, _)) |
1756 | 231 | Return(mockReplyPtr))); | 215 | .Times(1) |
1757 | 232 | EXPECT_CALL(mockReply, errorString()).Times(1).WillOnce(Return(QString("Bogus error for tests"))); | 216 | .WillOnce(InvokeArgument<3>(QStringLiteral(""), QStringLiteral(""), |
1758 | 233 | 217 | nullptr)); | |
1759 | 234 | } else { | 218 | dmPtr->get_progress("com.example.test", |
1760 | 235 | EXPECT_CALL(*mockNam, head(_)).WillOnce( | 219 | [this](std::string object_path) { |
1761 | 236 | DoAll( | 220 | progress_callback(object_path); |
1762 | 237 | InvokeWithoutArgs(this, &DISABLED_DownloadManagerCredsNetworkTest::signalFinishedAfterDelay), | 221 | }); |
1763 | 238 | Return(mockReplyPtr))); | 222 | } |
1346 | 239 | |||
1347 | 240 | EXPECT_CALL(mockReply, attribute(QNetworkRequest::HttpStatusCodeAttribute)) | ||
1348 | 241 | .Times(1).WillOnce(Return(QVariant(p.replyStatusCode))); | ||
1349 | 242 | |||
1350 | 243 | if (p.replyStatusCode == 200) { | ||
1351 | 244 | EXPECT_CALL(mockReply, hasRawHeader(click::CLICK_TOKEN_HEADER())) | ||
1352 | 245 | .Times(1).WillOnce(Return(p.replyHasClickRawHeader)); | ||
1353 | 246 | |||
1354 | 247 | if (p.replyHasClickRawHeader) { | ||
1355 | 248 | EXPECT_CALL(mockReply, rawHeader(click::CLICK_TOKEN_HEADER())) | ||
1356 | 249 | .Times(1).WillOnce(Return(TEST_HEADER_VALUE)); | ||
1357 | 250 | } | ||
1358 | 251 | } | ||
1359 | 252 | |||
1360 | 253 | } | ||
1361 | 254 | |||
1362 | 255 | } else { | ||
1363 | 256 | EXPECT_CALL(*mockCredentialsService, getCredentials()) | ||
1364 | 257 | .Times(1).WillOnce(InvokeWithoutArgs(mockCredentialsService.data(), | ||
1365 | 258 | &MockCredentialsService::credentialsNotFound)); | ||
1366 | 259 | |||
1367 | 260 | EXPECT_CALL(*mockNam, head(_)).Times(0); | ||
1368 | 261 | } | ||
1369 | 262 | |||
1370 | 263 | click::DownloadManager dm(mockNam, mockCredentialsService, | ||
1371 | 264 | mockSystemDownloadManager); | ||
1372 | 265 | |||
1373 | 266 | DownloadManagerMockClient mockDownloadManagerClient; | ||
1374 | 267 | |||
1375 | 268 | QObject::connect(&dm, &click::DownloadManager::credentialsNotFound, | ||
1376 | 269 | [&mockDownloadManagerClient]() | ||
1377 | 270 | { | ||
1378 | 271 | mockDownloadManagerClient.onCredentialsNotFoundEmitted(); | ||
1379 | 272 | }); | ||
1380 | 273 | |||
1381 | 274 | QObject::connect(&dm, &click::DownloadManager::clickTokenFetchError, | ||
1382 | 275 | [&mockDownloadManagerClient](const QString& error) | ||
1383 | 276 | { | ||
1384 | 277 | mockDownloadManagerClient.onClickTokenFetchErrorEmitted(error); | ||
1385 | 278 | }); | ||
1386 | 279 | |||
1387 | 280 | |||
1388 | 281 | QObject::connect(&dm, &click::DownloadManager::clickTokenFetched, | ||
1389 | 282 | [&mockDownloadManagerClient](const QString& token) | ||
1390 | 283 | { | ||
1391 | 284 | mockDownloadManagerClient.onClickTokenFetchedEmitted(token); | ||
1392 | 285 | }); | ||
1393 | 286 | |||
1394 | 287 | if (p.expectSuccessSignal) { | ||
1395 | 288 | |||
1396 | 289 | EXPECT_CALL(mockDownloadManagerClient, onClickTokenFetchedEmitted(TEST_HEADER_VALUE)) | ||
1397 | 290 | .Times(1) | ||
1398 | 291 | .WillOnce( | ||
1399 | 292 | InvokeWithoutArgs( | ||
1400 | 293 | this, | ||
1401 | 294 | &DISABLED_DownloadManagerCredsNetworkTest::Quit)); | ||
1402 | 295 | |||
1403 | 296 | EXPECT_CALL(mockDownloadManagerClient, onClickTokenFetchErrorEmitted(_)).Times(0); | ||
1404 | 297 | |||
1405 | 298 | } else { | ||
1406 | 299 | |||
1407 | 300 | if (p.credsFound) { | ||
1408 | 301 | |||
1409 | 302 | EXPECT_CALL(mockDownloadManagerClient, onClickTokenFetchErrorEmitted(_)) | ||
1410 | 303 | .Times(1) | ||
1411 | 304 | .WillOnce( | ||
1412 | 305 | InvokeWithoutArgs( | ||
1413 | 306 | this, | ||
1414 | 307 | &DISABLED_DownloadManagerCredsNetworkTest::Quit)); | ||
1415 | 308 | } else { | ||
1416 | 309 | |||
1417 | 310 | EXPECT_CALL(mockDownloadManagerClient, onCredentialsNotFoundEmitted()) | ||
1418 | 311 | .Times(1) | ||
1419 | 312 | .WillOnce( | ||
1420 | 313 | InvokeWithoutArgs( | ||
1421 | 314 | this, | ||
1422 | 315 | &DISABLED_DownloadManagerCredsNetworkTest::Quit)); | ||
1423 | 316 | } | ||
1424 | 317 | |||
1425 | 318 | EXPECT_CALL(mockDownloadManagerClient, onClickTokenFetchedEmitted(_)).Times(0); | ||
1426 | 319 | |||
1427 | 320 | } | ||
1428 | 321 | |||
1429 | 322 | // Now start the function we're testing, after a delay. This is | ||
1430 | 323 | // awkwardly verbose because QTimer::singleShot doesn't accept | ||
1431 | 324 | // arguments or lambdas. | ||
1432 | 325 | |||
1433 | 326 | // We need to delay the call until after the app.exec() call so | ||
1434 | 327 | // that when we call app.quit() on success, there is a running app | ||
1435 | 328 | // to quit. | ||
1436 | 329 | QTimer timer; | ||
1437 | 330 | timer.setSingleShot(true); | ||
1438 | 331 | QObject::connect(&timer, &QTimer::timeout, [&dm]() { | ||
1439 | 332 | dm.fetchClickToken(TEST_URL, TEST_SHA512); | ||
1440 | 333 | } ); | ||
1441 | 334 | timer.start(0); | ||
1442 | 335 | |||
1443 | 336 | // now exec the app so events can proceed: | ||
1444 | 337 | app.exec(); | ||
1445 | 338 | } | ||
1446 | 339 | |||
1447 | 340 | INSTANTIATE_TEST_CASE_P(DownloadManagerCredsNetworkTests, DISABLED_DownloadManagerCredsNetworkTest, | ||
1448 | 341 | ::testing::Values( | ||
1449 | 342 | // CredsNetworkTestParameters(credsFound, replySignalsError, replyStatusCode, replyHasClickRawHeader, expectSuccessSignal) | ||
1450 | 343 | CredsNetworkTestParameters(true, false, 200, true, true), // success | ||
1451 | 344 | CredsNetworkTestParameters(true, true, 200, true, false), // misc QNetworkReply error => error | ||
1452 | 345 | CredsNetworkTestParameters(true, false, 200, false, false), // no header => error | ||
1453 | 346 | CredsNetworkTestParameters(true, false, 401, true, false), // HTTP error status => error | ||
1454 | 347 | CredsNetworkTestParameters(false, false, 200, true, false) // no creds => error | ||
1455 | 348 | )); | ||
1456 | 349 | |||
1457 | 350 | |||
1458 | 351 | MATCHER(DownloadStructIsValid, "Download Struct does not match expected") | ||
1459 | 352 | { | ||
1460 | 353 | auto commandList = arg.getMetadata()["post-download-command"].toStringList(); | ||
1461 | 354 | return arg.getUrl() == TEST_URL | ||
1462 | 355 | && arg.getHash() == "" | ||
1463 | 356 | && arg.getAlgorithm() == "" | ||
1464 | 357 | && arg.getMetadata()["app_id"] == QVariant(TEST_APP_ID) | ||
1465 | 358 | && commandList[0] == "/bin/sh" | ||
1466 | 359 | && commandList[1] == "-c" | ||
1467 | 360 | && commandList[3] == "$file" | ||
1468 | 361 | && arg.getHeaders()["X-Click-Token"] == TEST_CLICK_TOKEN_VALUE; | ||
1469 | 362 | } | ||
1470 | 363 | |||
1471 | 364 | |||
1472 | 365 | TEST_P(DISABLED_DownloadManagerStartDownloadTest, TestStartDownload) | ||
1473 | 366 | { | ||
1474 | 367 | using namespace ::testing; | ||
1475 | 368 | |||
1476 | 369 | StartDownloadTestParameters p = GetParam(); | ||
1477 | 370 | |||
1478 | 371 | click::DownloadManager dm(mockNam, mockCredentialsService, | ||
1479 | 372 | mockSystemDownloadManager); | ||
1480 | 373 | |||
1481 | 374 | // mockError is heap-allocated because downloadWithError will delete it. | ||
1482 | 375 | MockError mockError; // = new MockError(); | ||
1483 | 376 | NiceMock<MockDownload> downloadWithError(&mockError); | ||
1484 | 377 | ON_CALL(downloadWithError, isError()).WillByDefault(Return(true)); | ||
1485 | 378 | ON_CALL(downloadWithError, error()).WillByDefault(Return(&mockError)); | ||
1486 | 379 | NiceMock<MockDownload> successfulDownload; | ||
1487 | 380 | ON_CALL(successfulDownload, isError()).WillByDefault(Return(false)); | ||
1488 | 381 | |||
1489 | 382 | // Just directly signal clickTokenFetched or error from | ||
1490 | 383 | // getCredentials(), no need to re-test the same code as the | ||
1491 | 384 | // previous test | ||
1492 | 385 | |||
1493 | 386 | std::function<void()> clickTokenSignalFunc; | ||
1494 | 387 | if (p.clickTokenFetchSignalsError) { | ||
1495 | 388 | clickTokenSignalFunc = std::function<void()>([&](){ | ||
1496 | 389 | dm.clickTokenFetchError(TEST_DOWNLOADERROR_STRING); | ||
1497 | 390 | }); | ||
1498 | 391 | EXPECT_CALL(*mockSystemDownloadManager, createDownload(_)).Times(0); | ||
1499 | 392 | |||
1500 | 393 | } else { | ||
1501 | 394 | clickTokenSignalFunc = std::function<void()>([&](){ | ||
1502 | 395 | dm.clickTokenFetched(TEST_CLICK_TOKEN_VALUE); | ||
1503 | 396 | }); | ||
1504 | 397 | |||
1505 | 398 | std::function<void()> downloadCreatedSignalFunc; | ||
1506 | 399 | |||
1507 | 400 | // NOTE: udm::Download doesn't have virtual functions, so mocking | ||
1508 | 401 | // them doesn't work and we have to construct objects that will | ||
1509 | 402 | // behave correctly without mock return values, using overridden constructors: | ||
1510 | 403 | if (p.downloadSignalsError) { | ||
1511 | 404 | |||
1512 | 405 | EXPECT_CALL(mockError, errorString()).Times(1).WillOnce(Return(TEST_DOWNLOADERROR_STRING)); | ||
1513 | 406 | downloadCreatedSignalFunc = std::function<void()>([&](){ | ||
1514 | 407 | mockSystemDownloadManager->downloadCreated(&downloadWithError); | ||
1515 | 408 | }); | ||
1516 | 409 | |||
1517 | 410 | } else { | ||
1518 | 411 | EXPECT_CALL(mockError, errorString()).Times(0); | ||
1519 | 412 | downloadCreatedSignalFunc = std::function<void()>([&](){ | ||
1520 | 413 | mockSystemDownloadManager->downloadCreated(&successfulDownload); | ||
1521 | 414 | }); | ||
1522 | 415 | } | ||
1523 | 416 | |||
1524 | 417 | EXPECT_CALL(*mockSystemDownloadManager, | ||
1525 | 418 | createDownload(DownloadStructIsValid())).Times(1).WillOnce(InvokeWithoutArgs(downloadCreatedSignalFunc)); | ||
1526 | 419 | } | ||
1527 | 420 | |||
1528 | 421 | EXPECT_CALL(*mockCredentialsService, getCredentials()) | ||
1529 | 422 | .Times(1).WillOnce(InvokeWithoutArgs(clickTokenSignalFunc)); | ||
1530 | 423 | |||
1531 | 424 | |||
1532 | 425 | DownloadManagerMockClient mockDownloadManagerClient; | ||
1533 | 426 | |||
1534 | 427 | QObject::connect(&dm, &click::DownloadManager::downloadError, | ||
1535 | 428 | [&mockDownloadManagerClient](const QString& error) | ||
1536 | 429 | { | ||
1537 | 430 | mockDownloadManagerClient.onDownloadErrorEmitted(error); | ||
1538 | 431 | }); | ||
1539 | 432 | |||
1540 | 433 | |||
1541 | 434 | QObject::connect(&dm, &click::DownloadManager::downloadStarted, | ||
1542 | 435 | [&mockDownloadManagerClient](const QString& downloadId) | ||
1543 | 436 | { | ||
1544 | 437 | qDebug() << "in lambda connected to click::dm::downloadstarted"; | ||
1545 | 438 | |||
1546 | 439 | mockDownloadManagerClient.onDownloadStartedEmitted(downloadId); | ||
1547 | 440 | }); | ||
1548 | 441 | |||
1549 | 442 | if (p.expectSuccessSignal) { | ||
1550 | 443 | |||
1551 | 444 | EXPECT_CALL(mockDownloadManagerClient, onDownloadStartedEmitted(TEST_DOWNLOAD_ID)) | ||
1552 | 445 | .Times(1) | ||
1553 | 446 | .WillOnce( | ||
1554 | 447 | InvokeWithoutArgs( | ||
1555 | 448 | this, | ||
1556 | 449 | &DISABLED_DownloadManagerStartDownloadTest::Quit)); | ||
1557 | 450 | |||
1558 | 451 | EXPECT_CALL(mockDownloadManagerClient, onDownloadErrorEmitted(_)).Times(0); | ||
1559 | 452 | EXPECT_CALL(successfulDownload, id()).Times(1).WillOnce(Return(TEST_DOWNLOAD_ID)); | ||
1560 | 453 | EXPECT_CALL(successfulDownload, start()).Times(1); | ||
1561 | 454 | |||
1562 | 455 | |||
1563 | 456 | } else { | ||
1564 | 457 | |||
1565 | 458 | EXPECT_CALL(mockDownloadManagerClient, onDownloadErrorEmitted(TEST_DOWNLOADERROR_STRING)) | ||
1566 | 459 | .Times(1) | ||
1567 | 460 | .WillOnce( | ||
1568 | 461 | InvokeWithoutArgs( | ||
1569 | 462 | this, | ||
1570 | 463 | &DISABLED_DownloadManagerStartDownloadTest::Quit)); | ||
1571 | 464 | |||
1572 | 465 | EXPECT_CALL(mockDownloadManagerClient, onDownloadStartedEmitted(_)).Times(0); | ||
1573 | 466 | |||
1574 | 467 | } | ||
1575 | 468 | |||
1576 | 469 | QTimer timer; | ||
1577 | 470 | timer.setSingleShot(true); | ||
1578 | 471 | QObject::connect(&timer, &QTimer::timeout, [&dm]() { | ||
1579 | 472 | dm.startDownload(TEST_URL, TEST_SHA512, TEST_APP_ID); | ||
1580 | 473 | } ); | ||
1581 | 474 | timer.start(0); | ||
1582 | 475 | |||
1583 | 476 | // now exec the app so events can proceed: | ||
1584 | 477 | app.exec(); | ||
1585 | 478 | |||
1586 | 479 | } | ||
1587 | 480 | |||
1588 | 481 | INSTANTIATE_TEST_CASE_P(DownloadManagerStartDownloadTests, DISABLED_DownloadManagerStartDownloadTest, | ||
1589 | 482 | ::testing::Values( | ||
1590 | 483 | // params: (clickTokenFetchSignalsError, downloadSignalsError, expectSuccessSignal) | ||
1591 | 484 | StartDownloadTestParameters(false, false, true), | ||
1592 | 485 | StartDownloadTestParameters(true, false, false), | ||
1593 | 486 | StartDownloadTestParameters(false, true, false) | ||
1594 | 487 | )); | ||
1764 | 488 | 223 | ||
1765 | === modified file 'libclickscope/tests/test_index.cpp' | |||
1766 | --- libclickscope/tests/test_index.cpp 2016-01-21 23:25:38 +0000 | |||
1767 | +++ libclickscope/tests/test_index.cpp 2016-03-01 16:46:59 +0000 | |||
1768 | @@ -1,5 +1,5 @@ | |||
1769 | 1 | /* | 1 | /* |
1771 | 2 | * Copyright (C) 2014 Canonical Ltd. | 2 | * Copyright (C) 2014-2016 Canonical Ltd. |
1772 | 3 | * | 3 | * |
1773 | 4 | * This program is free software: you can redistribute it and/or modify it | 4 | * This program is free software: you can redistribute it and/or modify it |
1774 | 5 | * under the terms of the GNU General Public License version 3, as published | 5 | * under the terms of the GNU General Public License version 3, as published |
1775 | @@ -190,6 +190,18 @@ | |||
1776 | 190 | indexPtr->departments("departments", [](const click::DepartmentList&, const click::HighlightList&, click::Index::Error, int) {}); | 190 | indexPtr->departments("departments", [](const click::DepartmentList&, const click::HighlightList&, click::Index::Error, int) {}); |
1777 | 191 | } | 191 | } |
1778 | 192 | 192 | ||
1779 | 193 | TEST_F(IndexTest, testDetailsSignsCall) | ||
1780 | 194 | { | ||
1781 | 195 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; | ||
1782 | 196 | auto response = responseForReply(reply.asSharedPtr()); | ||
1783 | 197 | |||
1784 | 198 | EXPECT_CALL(*clientPtr, callImpl(_, _, true, _, _, _)) | ||
1785 | 199 | .Times(1) | ||
1786 | 200 | .WillOnce(Return(response)); | ||
1787 | 201 | |||
1788 | 202 | indexPtr->get_details("fake-app", [](const click::PackageDetails, click::Index::Error) {}); | ||
1789 | 203 | } | ||
1790 | 204 | |||
1791 | 193 | TEST_F(IndexTest, testSearchSendsRightPath) | 205 | TEST_F(IndexTest, testSearchSendsRightPath) |
1792 | 194 | { | 206 | { |
1793 | 195 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; | 207 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; |
1794 | 196 | 208 | ||
1795 | === modified file 'libclickscope/tests/test_preview.cpp' | |||
1796 | --- libclickscope/tests/test_preview.cpp 2016-01-28 14:37:39 +0000 | |||
1797 | +++ libclickscope/tests/test_preview.cpp 2016-03-01 16:46:59 +0000 | |||
1798 | @@ -27,19 +27,23 @@ | |||
1799 | 27 | * files in the program, then also delete it here. | 27 | * files in the program, then also delete it here. |
1800 | 28 | */ | 28 | */ |
1801 | 29 | 29 | ||
1802 | 30 | #include <time.h> | ||
1803 | 31 | |||
1804 | 32 | #include <unity/scopes/testing/MockPreviewReply.h> | ||
1805 | 33 | #include <unity/scopes/testing/Result.h> | ||
1806 | 34 | |||
1807 | 35 | #include <gtest/gtest.h> | ||
1808 | 36 | #include <click/preview.h> | 30 | #include <click/preview.h> |
1811 | 37 | #include <fake_json.h> | 31 | |
1810 | 38 | #include <mock_pay.h> | ||
1812 | 39 | #include <click/index.h> | 32 | #include <click/index.h> |
1813 | 40 | #include <click/interface.h> | 33 | #include <click/interface.h> |
1814 | 41 | #include <click/reviews.h> | 34 | #include <click/reviews.h> |
1815 | 35 | #include <fake_json.h> | ||
1816 | 36 | #include <mock_pay.h> | ||
1817 | 37 | #include <mock_ubuntu_download_manager.h> | ||
1818 | 38 | |||
1819 | 39 | #include <QCoreApplication> | ||
1820 | 40 | #include <QTimer> | ||
1821 | 41 | |||
1822 | 42 | #include <boost/locale/time_zone.hpp> | 42 | #include <boost/locale/time_zone.hpp> |
1823 | 43 | #include <gtest/gtest.h> | ||
1824 | 44 | #include <time.h> | ||
1825 | 45 | #include <unity/scopes/testing/MockPreviewReply.h> | ||
1826 | 46 | #include <unity/scopes/testing/Result.h> | ||
1827 | 43 | 47 | ||
1828 | 44 | using namespace ::testing; | 48 | using namespace ::testing; |
1829 | 45 | using ::testing::Matcher; | 49 | using ::testing::Matcher; |
1830 | @@ -346,8 +350,8 @@ | |||
1831 | 346 | unity::scopes::ActionMetadata metadata; | 350 | unity::scopes::ActionMetadata metadata; |
1832 | 347 | unity::scopes::VariantMap metadict; | 351 | unity::scopes::VariantMap metadict; |
1833 | 348 | QSharedPointer<click::web::Client> client; | 352 | QSharedPointer<click::web::Client> client; |
1834 | 349 | QSharedPointer<click::network::AccessManager> nam; | ||
1835 | 350 | QSharedPointer<MockPayPackage> pay_package; | 353 | QSharedPointer<MockPayPackage> pay_package; |
1836 | 354 | QSharedPointer<MockSystemDownloadManager> dm; | ||
1837 | 351 | std::shared_ptr<click::DepartmentsDb> depts; | 355 | std::shared_ptr<click::DepartmentsDb> depts; |
1838 | 352 | const std::string FAKE_SHA512 = "FAKE_SHA512"; | 356 | const std::string FAKE_SHA512 = "FAKE_SHA512"; |
1839 | 353 | 357 | ||
1840 | @@ -364,9 +368,13 @@ | |||
1841 | 364 | 368 | ||
1842 | 365 | } | 369 | } |
1843 | 366 | 370 | ||
1847 | 367 | MOCK_METHOD6(build_installing, click::PreviewStrategy*(const std::string&, const std::string&, | 371 | MOCK_METHOD6(build_installing, |
1848 | 368 | const unity::scopes::Result&, const QSharedPointer<click::web::Client>&, | 372 | click::PreviewStrategy*(const std::string&, |
1849 | 369 | const QSharedPointer<click::network::AccessManager>&, std::shared_ptr<click::DepartmentsDb>)); | 373 | const std::string&, |
1850 | 374 | const unity::scopes::Result&, | ||
1851 | 375 | const QSharedPointer<click::web::Client>&, | ||
1852 | 376 | const QSharedPointer<Ubuntu::DownloadManager::Manager>&, | ||
1853 | 377 | std::shared_ptr<click::DepartmentsDb>)); | ||
1854 | 370 | }; | 378 | }; |
1855 | 371 | 379 | ||
1856 | 372 | TEST_F(StrategyChooserTest, testSha512IsUsed) { | 380 | TEST_F(StrategyChooserTest, testSha512IsUsed) { |
1857 | @@ -376,7 +384,7 @@ | |||
1858 | 376 | metadata.set_scope_data(unity::scopes::Variant(metadict)); | 384 | metadata.set_scope_data(unity::scopes::Variant(metadict)); |
1859 | 377 | MockablePreview preview(result, metadata); | 385 | MockablePreview preview(result, metadata); |
1860 | 378 | EXPECT_CALL(preview, build_installing(_, FAKE_SHA512, _, _, _, _)); | 386 | EXPECT_CALL(preview, build_installing(_, FAKE_SHA512, _, _, _, _)); |
1862 | 379 | preview.choose_strategy(client, nam, pay_package, depts); | 387 | preview.choose_strategy(client, pay_package, dm, depts); |
1863 | 380 | } | 388 | } |
1864 | 381 | 389 | ||
1865 | 382 | 390 | ||
1866 | @@ -386,53 +394,25 @@ | |||
1867 | 386 | click::PackageDetails details; | 394 | click::PackageDetails details; |
1868 | 387 | unity::scopes::PreviewWidgetList widgets; | 395 | unity::scopes::PreviewWidgetList widgets; |
1869 | 388 | QSharedPointer<click::web::Client> client; | 396 | QSharedPointer<click::web::Client> client; |
1870 | 389 | QSharedPointer<click::network::AccessManager> nam; | ||
1871 | 390 | QSharedPointer<MockPayPackage> pay_package; | 397 | QSharedPointer<MockPayPackage> pay_package; |
1872 | 398 | QSharedPointer<MockSystemDownloadManager> sdm; | ||
1873 | 391 | std::shared_ptr<click::DepartmentsDb> depts; | 399 | std::shared_ptr<click::DepartmentsDb> depts; |
1874 | 392 | unity::scopes::testing::MockPreviewReply reply; | 400 | unity::scopes::testing::MockPreviewReply reply; |
1875 | 393 | std::shared_ptr<unity::scopes::testing::MockPreviewReply> replyptr{&reply, [](unity::scopes::testing::MockPreviewReply*){}}; | 401 | std::shared_ptr<unity::scopes::testing::MockPreviewReply> replyptr{&reply, [](unity::scopes::testing::MockPreviewReply*){}}; |
1876 | 394 | }; | 402 | }; |
1877 | 395 | 403 | ||
1878 | 396 | class FakeDownloader : public click::Downloader { | ||
1879 | 397 | std::string object_path; | ||
1880 | 398 | std::function<void (std::string)> callback; | ||
1881 | 399 | public: | ||
1882 | 400 | FakeDownloader(const std::string& object_path, const QSharedPointer<click::network::AccessManager>& networkAccessManager) | ||
1883 | 401 | : click::Downloader(networkAccessManager), object_path(object_path) | ||
1884 | 402 | { | ||
1885 | 403 | |||
1886 | 404 | } | ||
1887 | 405 | void get_download_progress(std::string /*package_name*/, const std::function<void (std::string)> &callback) | ||
1888 | 406 | { | ||
1889 | 407 | this->callback = callback; | ||
1890 | 408 | } | ||
1891 | 409 | |||
1892 | 410 | void activate_callback() | ||
1893 | 411 | { | ||
1894 | 412 | callback(object_path); | ||
1895 | 413 | } | ||
1896 | 414 | }; | ||
1897 | 415 | |||
1898 | 416 | class FakeBaseUninstalledPreview : public click::UninstalledPreview { | 404 | class FakeBaseUninstalledPreview : public click::UninstalledPreview { |
1899 | 417 | std::string object_path; | 405 | std::string object_path; |
1900 | 418 | public: | 406 | public: |
1901 | 419 | std::unique_ptr<FakeDownloader> fake_downloader; | ||
1902 | 420 | FakeBaseUninstalledPreview(const std::string& object_path, | 407 | FakeBaseUninstalledPreview(const std::string& object_path, |
1903 | 421 | const unity::scopes::Result& result, | 408 | const unity::scopes::Result& result, |
1904 | 422 | const QSharedPointer<click::web::Client>& client, | 409 | const QSharedPointer<click::web::Client>& client, |
1905 | 423 | const std::shared_ptr<click::DepartmentsDb>& depts, | 410 | const std::shared_ptr<click::DepartmentsDb>& depts, |
1907 | 424 | const QSharedPointer<click::network::AccessManager>& nam, | 411 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
1908 | 425 | const QSharedPointer<pay::Package> pay_package) | 412 | const QSharedPointer<pay::Package> pay_package) |
1919 | 426 | : click::UninstalledPreview(result, client, depts, nam, pay_package), | 413 | : click::UninstalledPreview(result, client, depts, manager, pay_package), |
1920 | 427 | object_path(object_path), | 414 | object_path(object_path) |
1921 | 428 | fake_downloader(new FakeDownloader(object_path, nam)) | 415 | { |
1912 | 429 | { | ||
1913 | 430 | |||
1914 | 431 | } | ||
1915 | 432 | |||
1916 | 433 | virtual click::Downloader* get_downloader(const QSharedPointer<click::network::AccessManager> &/*nam*/) | ||
1917 | 434 | { | ||
1918 | 435 | return fake_downloader.get(); | ||
1922 | 436 | } | 416 | } |
1923 | 437 | 417 | ||
1924 | 438 | void populateDetails(std::function<void (const click::PackageDetails &)> details_callback, | 418 | void populateDetails(std::function<void (const click::PackageDetails &)> details_callback, |
1925 | @@ -451,39 +431,38 @@ | |||
1926 | 451 | const unity::scopes::Result& result, | 431 | const unity::scopes::Result& result, |
1927 | 452 | const QSharedPointer<click::web::Client>& client, | 432 | const QSharedPointer<click::web::Client>& client, |
1928 | 453 | const std::shared_ptr<click::DepartmentsDb>& depts, | 433 | const std::shared_ptr<click::DepartmentsDb>& depts, |
1930 | 454 | const QSharedPointer<click::network::AccessManager>& nam, | 434 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
1931 | 455 | const QSharedPointer<pay::Package> pay_package) | 435 | const QSharedPointer<pay::Package> pay_package) |
1933 | 456 | : FakeBaseUninstalledPreview(object_path, result, client, depts, nam, pay_package) { | 436 | : FakeBaseUninstalledPreview(object_path, result, client, depts, manager, pay_package) { |
1934 | 457 | } | 437 | } |
1935 | 458 | }; | 438 | }; |
1936 | 459 | 439 | ||
1937 | 460 | 440 | ||
1939 | 461 | TEST_F(UninstalledPreviewTest, testDownloadInProgress) { | 441 | // FIXME: Needs Qt main loop |
1940 | 442 | TEST_F(UninstalledPreviewTest, DISABLED_testDownloadInProgress) { | ||
1941 | 462 | std::string fake_object_path = "/fake/object/path"; | 443 | std::string fake_object_path = "/fake/object/path"; |
1942 | 463 | 444 | ||
1943 | 464 | result["name"] = "fake_app_name"; | 445 | result["name"] = "fake_app_name"; |
1944 | 465 | scopes::PreviewWidgetList response; | 446 | scopes::PreviewWidgetList response; |
1946 | 466 | FakeUninstalledPreview preview(fake_object_path, result, client, depts, nam, pay_package); | 447 | FakeUninstalledPreview preview(fake_object_path, result, client, depts, sdm, pay_package); |
1947 | 467 | EXPECT_CALL(preview, progressBarWidget(_)) | 448 | EXPECT_CALL(preview, progressBarWidget(_)) |
1948 | 468 | .Times(1) | 449 | .Times(1) |
1949 | 469 | .WillOnce(Return(response)); | 450 | .WillOnce(Return(response)); |
1950 | 470 | EXPECT_CALL(*replyptr, register_layout(_)); | 451 | EXPECT_CALL(*replyptr, register_layout(_)); |
1951 | 471 | preview.run(replyptr); | 452 | preview.run(replyptr); |
1952 | 472 | preview.fake_downloader->activate_callback(); | ||
1953 | 473 | } | 453 | } |
1954 | 474 | 454 | ||
1956 | 475 | TEST_F(UninstalledPreviewTest, testNoDownloadProgress) { | 455 | // FIXME: Needs Qt main loop |
1957 | 456 | TEST_F(UninstalledPreviewTest, DISABLED_testNoDownloadProgress) { | ||
1958 | 476 | std::string fake_object_path = ""; | 457 | std::string fake_object_path = ""; |
1959 | 477 | 458 | ||
1960 | 478 | result["name"] = "fake_app_name"; | 459 | result["name"] = "fake_app_name"; |
1961 | 479 | scopes::PreviewWidgetList response; | 460 | scopes::PreviewWidgetList response; |
1963 | 480 | FakeUninstalledPreview preview(fake_object_path, result, client, depts, nam, pay_package); | 461 | FakeUninstalledPreview preview(fake_object_path, result, client, depts, sdm, pay_package); |
1964 | 481 | EXPECT_CALL(preview, uninstalledActionButtonWidgets(_)) | 462 | EXPECT_CALL(preview, uninstalledActionButtonWidgets(_)) |
1965 | 482 | .Times(1) | 463 | .Times(1) |
1966 | 483 | .WillOnce(Return(response)); | 464 | .WillOnce(Return(response)); |
1967 | 484 | EXPECT_CALL(*replyptr, register_layout(_)); | ||
1968 | 485 | preview.run(replyptr); | 465 | preview.run(replyptr); |
1969 | 486 | preview.fake_downloader->activate_callback(); | ||
1970 | 487 | } | 466 | } |
1971 | 488 | 467 | ||
1972 | 489 | class FakeUninstalledRefundablePreview : FakeBaseUninstalledPreview { | 468 | class FakeUninstalledRefundablePreview : FakeBaseUninstalledPreview { |
1973 | @@ -491,9 +470,9 @@ | |||
1974 | 491 | FakeUninstalledRefundablePreview(const unity::scopes::Result& result, | 470 | FakeUninstalledRefundablePreview(const unity::scopes::Result& result, |
1975 | 492 | const QSharedPointer<click::web::Client>& client, | 471 | const QSharedPointer<click::web::Client>& client, |
1976 | 493 | const std::shared_ptr<click::DepartmentsDb>& depts, | 472 | const std::shared_ptr<click::DepartmentsDb>& depts, |
1978 | 494 | const QSharedPointer<click::network::AccessManager>& nam, | 473 | const QSharedPointer<Ubuntu::DownloadManager::Manager>& manager, |
1979 | 495 | const QSharedPointer<pay::Package> pay_package) | 474 | const QSharedPointer<pay::Package> pay_package) |
1981 | 496 | : FakeBaseUninstalledPreview(std::string{""}, result, client, depts, nam, pay_package){ | 475 | : FakeBaseUninstalledPreview(std::string{""}, result, client, depts, manager, pay_package){ |
1982 | 497 | } | 476 | } |
1983 | 498 | using click::UninstalledPreview::uninstalledActionButtonWidgets; | 477 | using click::UninstalledPreview::uninstalledActionButtonWidgets; |
1984 | 499 | MOCK_METHOD0(isRefundable, bool()); | 478 | MOCK_METHOD0(isRefundable, bool()); |
1985 | @@ -514,7 +493,7 @@ | |||
1986 | 514 | result["name"] = "fake_app_name"; | 493 | result["name"] = "fake_app_name"; |
1987 | 515 | result["price"] = 2.99; | 494 | result["price"] = 2.99; |
1988 | 516 | result["purchased"] = true; | 495 | result["purchased"] = true; |
1990 | 517 | FakeUninstalledRefundablePreview preview(result, client, depts, nam, pay_package); | 496 | FakeUninstalledRefundablePreview preview(result, client, depts, sdm, pay_package); |
1991 | 518 | 497 | ||
1992 | 519 | click::PackageDetails pkgdetails; | 498 | click::PackageDetails pkgdetails; |
1993 | 520 | EXPECT_CALL(preview, isRefundable()).Times(1) | 499 | EXPECT_CALL(preview, isRefundable()).Times(1) |
1994 | @@ -527,7 +506,7 @@ | |||
1995 | 527 | result["name"] = "fake_app_name"; | 506 | result["name"] = "fake_app_name"; |
1996 | 528 | result["price"] = 2.99; | 507 | result["price"] = 2.99; |
1997 | 529 | result["purchased"] = true; | 508 | result["purchased"] = true; |
1999 | 530 | FakeUninstalledRefundablePreview preview(result, client, depts, nam, pay_package); | 509 | FakeUninstalledRefundablePreview preview(result, client, depts, sdm, pay_package); |
2000 | 531 | 510 | ||
2001 | 532 | click::PackageDetails pkgdetails; | 511 | click::PackageDetails pkgdetails; |
2002 | 533 | EXPECT_CALL(preview, isRefundable()).Times(1) | 512 | EXPECT_CALL(preview, isRefundable()).Times(1) |
2003 | @@ -542,7 +521,6 @@ | |||
2004 | 542 | unity::scopes::ActionMetadata metadata; | 521 | unity::scopes::ActionMetadata metadata; |
2005 | 543 | unity::scopes::VariantMap metadict; | 522 | unity::scopes::VariantMap metadict; |
2006 | 544 | QSharedPointer<click::web::Client> client; | 523 | QSharedPointer<click::web::Client> client; |
2007 | 545 | QSharedPointer<click::network::AccessManager> nam; | ||
2008 | 546 | QSharedPointer<MockPayPackage> pay_package; | 524 | QSharedPointer<MockPayPackage> pay_package; |
2009 | 547 | std::shared_ptr<click::DepartmentsDb> depts; | 525 | std::shared_ptr<click::DepartmentsDb> depts; |
2010 | 548 | 526 | ||
2011 | 549 | 527 | ||
2012 | === modified file 'libclickscope/tests/test_reviews.cpp' | |||
2013 | --- libclickscope/tests/test_reviews.cpp 2016-01-21 20:43:16 +0000 | |||
2014 | +++ libclickscope/tests/test_reviews.cpp 2016-03-01 16:46:59 +0000 | |||
2015 | @@ -150,6 +150,19 @@ | |||
2016 | 150 | click::Reviews::Error) {}); | 150 | click::Reviews::Error) {}); |
2017 | 151 | } | 151 | } |
2018 | 152 | 152 | ||
2019 | 153 | TEST_F(ReviewsTest, testFetchReviewsSignsCall) | ||
2020 | 154 | { | ||
2021 | 155 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; | ||
2022 | 156 | auto response = responseForReply(reply.asSharedPtr()); | ||
2023 | 157 | |||
2024 | 158 | EXPECT_CALL(*clientPtr, callImpl(_, _, true, _, _, _)) | ||
2025 | 159 | .Times(1) | ||
2026 | 160 | .WillOnce(Return(response)); | ||
2027 | 161 | |||
2028 | 162 | reviewsPtr->fetch_reviews("", [](click::ReviewList, | ||
2029 | 163 | click::Reviews::Error) {}); | ||
2030 | 164 | } | ||
2031 | 165 | |||
2032 | 153 | TEST_F(ReviewsTest, testFetchReviewsSendsQueryAsParam) | 166 | TEST_F(ReviewsTest, testFetchReviewsSendsQueryAsParam) |
2033 | 154 | { | 167 | { |
2034 | 155 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; | 168 | LifetimeHelper<click::network::Reply, MockNetworkReply> reply; |
2035 | 156 | 169 | ||
2036 | === modified file 'libclickscope/tests/test_webclient.cpp' | |||
2037 | --- libclickscope/tests/test_webclient.cpp 2014-10-17 17:07:31 +0000 | |||
2038 | +++ libclickscope/tests/test_webclient.cpp 2016-03-01 16:46:59 +0000 | |||
2039 | @@ -231,7 +231,7 @@ | |||
2040 | 231 | .WillOnce(Return(replyPtr)); | 231 | .WillOnce(Return(replyPtr)); |
2041 | 232 | 232 | ||
2042 | 233 | auto wr = wc.call(FAKE_SERVER + FAKE_PATH, | 233 | auto wr = wc.call(FAKE_SERVER + FAKE_PATH, |
2044 | 234 | "HEAD", true); | 234 | "POST", true); |
2045 | 235 | } | 235 | } |
2046 | 236 | 236 | ||
2047 | 237 | TEST_F(WebClientTest, testSignedCredentialsServiceUnset) | 237 | TEST_F(WebClientTest, testSignedCredentialsServiceUnset) |
2048 | @@ -244,13 +244,13 @@ | |||
2049 | 244 | 244 | ||
2050 | 245 | click::web::Client wc(namPtr); | 245 | click::web::Client wc(namPtr); |
2051 | 246 | 246 | ||
2053 | 247 | EXPECT_CALL(nam, sendCustomRequest(_, _, _)) | 247 | EXPECT_CALL(nam, get(_)) |
2054 | 248 | .Times(1) | 248 | .Times(1) |
2055 | 249 | .WillOnce(Return(replyPtr)); | 249 | .WillOnce(Return(replyPtr)); |
2056 | 250 | EXPECT_CALL(*reply, errorString()).Times(1).WillOnce(Return("auth failed")); | 250 | EXPECT_CALL(*reply, errorString()).Times(1).WillOnce(Return("auth failed")); |
2057 | 251 | 251 | ||
2058 | 252 | auto response = wc.call(FAKE_SERVER + FAKE_PATH, | 252 | auto response = wc.call(FAKE_SERVER + FAKE_PATH, |
2060 | 253 | "HEAD", true); | 253 | "GET", true); |
2061 | 254 | QObject::connect(response.data(), &click::web::Response::error, | 254 | QObject::connect(response.data(), &click::web::Response::error, |
2062 | 255 | [this](QString desc){ | 255 | [this](QString desc){ |
2063 | 256 | errorHandler(desc); | 256 | errorHandler(desc); |
2064 | @@ -279,7 +279,7 @@ | |||
2065 | 279 | .WillOnce(Return(replyPtr)); | 279 | .WillOnce(Return(replyPtr)); |
2066 | 280 | 280 | ||
2067 | 281 | auto wr = wc.call(FAKE_SERVER + FAKE_PATH, | 281 | auto wr = wc.call(FAKE_SERVER + FAKE_PATH, |
2069 | 282 | "HEAD", true); | 282 | "POST", true); |
2070 | 283 | } | 283 | } |
2071 | 284 | 284 | ||
2072 | 285 | 285 | ||
2073 | 286 | 286 | ||
2074 | === modified file 'scope/clickapps/apps-scope.cpp' | |||
2075 | --- scope/clickapps/apps-scope.cpp 2015-11-24 18:23:23 +0000 | |||
2076 | +++ scope/clickapps/apps-scope.cpp 2016-03-01 16:46:59 +0000 | |||
2077 | @@ -27,6 +27,9 @@ | |||
2078 | 27 | * files in the program, then also delete it here. | 27 | * files in the program, then also delete it here. |
2079 | 28 | */ | 28 | */ |
2080 | 29 | 29 | ||
2081 | 30 | #include "apps-scope.h" | ||
2082 | 31 | #include "apps-query.h" | ||
2083 | 32 | |||
2084 | 30 | #include <click/qtbridge.h> | 33 | #include <click/qtbridge.h> |
2085 | 31 | #include <click/preview.h> | 34 | #include <click/preview.h> |
2086 | 32 | #include <click/interface.h> | 35 | #include <click/interface.h> |
2087 | @@ -42,9 +45,6 @@ | |||
2088 | 42 | #include <unity/scopes/CannedQuery.h> | 45 | #include <unity/scopes/CannedQuery.h> |
2089 | 43 | #include <unity/scopes/ActivationResponse.h> | 46 | #include <unity/scopes/ActivationResponse.h> |
2090 | 44 | 47 | ||
2091 | 45 | #include "apps-scope.h" | ||
2092 | 46 | #include "apps-query.h" | ||
2093 | 47 | |||
2094 | 48 | using namespace click; | 48 | using namespace click; |
2095 | 49 | 49 | ||
2096 | 50 | click::Scope::Scope() | 50 | click::Scope::Scope() |
2097 | @@ -81,6 +81,9 @@ | |||
2098 | 81 | static const int zero = 0; | 81 | static const int zero = 0; |
2099 | 82 | auto emptyCb = [this]() | 82 | auto emptyCb = [this]() |
2100 | 83 | { | 83 | { |
2101 | 84 | sso.reset(new click::CredentialsService()); | ||
2102 | 85 | client->setCredentialsService(sso); | ||
2103 | 86 | dm.reset(Ubuntu::DownloadManager::Manager::createSessionManager()); | ||
2104 | 84 | }; | 87 | }; |
2105 | 85 | 88 | ||
2106 | 86 | qt::core::world::build_and_run(zero, nullptr, emptyCb); | 89 | qt::core::world::build_and_run(zero, nullptr, emptyCb); |
2107 | @@ -101,7 +104,7 @@ | |||
2108 | 101 | const unity::scopes::ActionMetadata& metadata) { | 104 | const unity::scopes::ActionMetadata& metadata) { |
2109 | 102 | qDebug() << "Scope::preview() called."; | 105 | qDebug() << "Scope::preview() called."; |
2110 | 103 | auto preview = new click::Preview(result, metadata); | 106 | auto preview = new click::Preview(result, metadata); |
2112 | 104 | preview->choose_strategy(client, nam, pay_package, depts_db); | 107 | preview->choose_strategy(client, pay_package, dm, depts_db); |
2113 | 105 | return unity::scopes::PreviewQueryBase::UPtr{preview}; | 108 | return unity::scopes::PreviewQueryBase::UPtr{preview}; |
2114 | 106 | } | 109 | } |
2115 | 107 | 110 | ||
2116 | 108 | 111 | ||
2117 | === modified file 'scope/clickapps/apps-scope.h' | |||
2118 | --- scope/clickapps/apps-scope.h 2015-11-24 18:23:23 +0000 | |||
2119 | +++ scope/clickapps/apps-scope.h 2016-03-01 16:46:59 +0000 | |||
2120 | @@ -35,6 +35,7 @@ | |||
2121 | 35 | #include <click/pay.h> | 35 | #include <click/pay.h> |
2122 | 36 | #include <click/webclient.h> | 36 | #include <click/webclient.h> |
2123 | 37 | 37 | ||
2124 | 38 | #include <ubuntu/download_manager/manager.h> | ||
2125 | 38 | #include <unity/scopes/ScopeBase.h> | 39 | #include <unity/scopes/ScopeBase.h> |
2126 | 39 | #include <unity/scopes/QueryBase.h> | 40 | #include <unity/scopes/QueryBase.h> |
2127 | 40 | #include <unity/scopes/ActivationQueryBase.h> | 41 | #include <unity/scopes/ActivationQueryBase.h> |
2128 | @@ -69,6 +70,8 @@ | |||
2129 | 69 | QSharedPointer<click::web::Client> client; | 70 | QSharedPointer<click::web::Client> client; |
2130 | 70 | QSharedPointer<click::Index> index; | 71 | QSharedPointer<click::Index> index; |
2131 | 71 | QSharedPointer<pay::Package> pay_package; | 72 | QSharedPointer<pay::Package> pay_package; |
2132 | 73 | QSharedPointer<Ubuntu::DownloadManager::Manager> dm; | ||
2133 | 74 | QSharedPointer<click::CredentialsService> sso; | ||
2134 | 72 | std::shared_ptr<click::DepartmentsDb> depts_db; | 75 | std::shared_ptr<click::DepartmentsDb> depts_db; |
2135 | 73 | 76 | ||
2136 | 74 | std::string installApplication(unity::scopes::Result const& result); | 77 | std::string installApplication(unity::scopes::Result const& result); |
2137 | 75 | 78 | ||
2138 | === modified file 'scope/clickstore/store-scope.cpp' | |||
2139 | --- scope/clickstore/store-scope.cpp 2015-11-24 18:23:23 +0000 | |||
2140 | +++ scope/clickstore/store-scope.cpp 2016-03-01 16:46:59 +0000 | |||
2141 | @@ -83,6 +83,9 @@ | |||
2142 | 83 | static const int zero = 0; | 83 | static const int zero = 0; |
2143 | 84 | auto emptyCb = [this]() | 84 | auto emptyCb = [this]() |
2144 | 85 | { | 85 | { |
2145 | 86 | sso.reset(new click::CredentialsService()); | ||
2146 | 87 | client->setCredentialsService(sso); | ||
2147 | 88 | dm.reset(Ubuntu::DownloadManager::Manager::createSessionManager()); | ||
2148 | 86 | }; | 89 | }; |
2149 | 87 | 90 | ||
2150 | 88 | qt::core::world::build_and_run(zero, nullptr, emptyCb); | 91 | qt::core::world::build_and_run(zero, nullptr, emptyCb); |
2151 | @@ -103,7 +106,7 @@ | |||
2152 | 103 | const unity::scopes::ActionMetadata& metadata) { | 106 | const unity::scopes::ActionMetadata& metadata) { |
2153 | 104 | qDebug() << "Scope::preview() called."; | 107 | qDebug() << "Scope::preview() called."; |
2154 | 105 | auto preview = new click::Preview(result, metadata); | 108 | auto preview = new click::Preview(result, metadata); |
2156 | 106 | preview->choose_strategy(client, nam, pay_package, depts_db); | 109 | preview->choose_strategy(client, pay_package, dm, depts_db); |
2157 | 107 | return unity::scopes::PreviewQueryBase::UPtr{preview}; | 110 | return unity::scopes::PreviewQueryBase::UPtr{preview}; |
2158 | 108 | } | 111 | } |
2159 | 109 | 112 | ||
2160 | 110 | 113 | ||
2161 | === modified file 'scope/clickstore/store-scope.h' | |||
2162 | --- scope/clickstore/store-scope.h 2015-11-24 18:23:23 +0000 | |||
2163 | +++ scope/clickstore/store-scope.h 2016-03-01 16:46:59 +0000 | |||
2164 | @@ -36,6 +36,7 @@ | |||
2165 | 36 | #include <click/network_access_manager.h> | 36 | #include <click/network_access_manager.h> |
2166 | 37 | #include <click/webclient.h> | 37 | #include <click/webclient.h> |
2167 | 38 | 38 | ||
2168 | 39 | #include <ubuntu/download_manager/manager.h> | ||
2169 | 39 | #include <unity/scopes/ScopeBase.h> | 40 | #include <unity/scopes/ScopeBase.h> |
2170 | 40 | #include <unity/scopes/QueryBase.h> | 41 | #include <unity/scopes/QueryBase.h> |
2171 | 41 | #include <unity/scopes/ActivationQueryBase.h> | 42 | #include <unity/scopes/ActivationQueryBase.h> |
2172 | @@ -72,6 +73,8 @@ | |||
2173 | 72 | QSharedPointer<click::web::Client> client; | 73 | QSharedPointer<click::web::Client> client; |
2174 | 73 | QSharedPointer<click::Index> index; | 74 | QSharedPointer<click::Index> index; |
2175 | 74 | QSharedPointer<pay::Package> pay_package; | 75 | QSharedPointer<pay::Package> pay_package; |
2176 | 76 | QSharedPointer<Ubuntu::DownloadManager::Manager> dm; | ||
2177 | 77 | QSharedPointer<click::CredentialsService> sso; | ||
2178 | 75 | std::shared_ptr<click::DepartmentLookup> depts; | 78 | std::shared_ptr<click::DepartmentLookup> depts; |
2179 | 76 | std::shared_ptr<click::HighlightList> highlights; | 79 | std::shared_ptr<click::HighlightList> highlights; |
2180 | 77 | std::shared_ptr<click::DepartmentsDb> depts_db; | 80 | std::shared_ptr<click::DepartmentsDb> depts_db; |
2181 | 78 | 81 | ||
2182 | === modified file 'scope/tests/CMakeLists.txt' | |||
2183 | --- scope/tests/CMakeLists.txt 2015-11-24 18:23:23 +0000 | |||
2184 | +++ scope/tests/CMakeLists.txt 2016-03-01 16:46:59 +0000 | |||
2185 | @@ -105,6 +105,5 @@ | |||
2186 | 105 | ) | 105 | ) |
2187 | 106 | 106 | ||
2188 | 107 | 107 | ||
2189 | 108 | add_subdirectory(download_manager_tool) | ||
2190 | 109 | add_subdirectory(click_interface_tool) | 108 | add_subdirectory(click_interface_tool) |
2191 | 110 | add_subdirectory(fake_launcher) | 109 | add_subdirectory(fake_launcher) |
2192 | 111 | 110 | ||
2193 | === removed directory 'scope/tests/download_manager_tool' | |||
2194 | === removed file 'scope/tests/download_manager_tool/CMakeLists.txt' | |||
2195 | --- scope/tests/download_manager_tool/CMakeLists.txt 2015-05-20 19:58:45 +0000 | |||
2196 | +++ scope/tests/download_manager_tool/CMakeLists.txt 1970-01-01 00:00:00 +0000 | |||
2197 | @@ -1,14 +0,0 @@ | |||
2198 | 1 | set(DOWNLOAD_MANAGER_TOOL_TARGET download_manager_tool) | ||
2199 | 2 | |||
2200 | 3 | include_directories ( | ||
2201 | 4 | ${CMAKE_SOURCE_DIR}/scope/click | ||
2202 | 5 | ) | ||
2203 | 6 | |||
2204 | 7 | add_executable (${DOWNLOAD_MANAGER_TOOL_TARGET} | ||
2205 | 8 | download_manager_tool.cpp | ||
2206 | 9 | download_manager_tool.h | ||
2207 | 10 | ) | ||
2208 | 11 | |||
2209 | 12 | target_link_libraries (${DOWNLOAD_MANAGER_TOOL_TARGET} | ||
2210 | 13 | ${STORE_LIB_UNVERSIONED} | ||
2211 | 14 | ) | ||
2212 | 15 | 0 | ||
2213 | === removed file 'scope/tests/download_manager_tool/download_manager_tool.cpp' | |||
2214 | --- scope/tests/download_manager_tool/download_manager_tool.cpp 2014-08-11 18:30:00 +0000 | |||
2215 | +++ scope/tests/download_manager_tool/download_manager_tool.cpp 1970-01-01 00:00:00 +0000 | |||
2216 | @@ -1,123 +0,0 @@ | |||
2217 | 1 | /* | ||
2218 | 2 | * Copyright (C) 2014 Canonical Ltd. | ||
2219 | 3 | * | ||
2220 | 4 | * This program is free software: you can redistribute it and/or modify it | ||
2221 | 5 | * under the terms of the GNU General Public License version 3, as published | ||
2222 | 6 | * by the Free Software Foundation. | ||
2223 | 7 | * | ||
2224 | 8 | * This program is distributed in the hope that it will be useful, but | ||
2225 | 9 | * WITHOUT ANY WARRANTY; without even the implied warranties of | ||
2226 | 10 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
2227 | 11 | * PURPOSE. See the GNU General Public License for more details. | ||
2228 | 12 | * | ||
2229 | 13 | * You should have received a copy of the GNU General Public License along | ||
2230 | 14 | * with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2231 | 15 | * | ||
2232 | 16 | * In addition, as a special exception, the copyright holders give | ||
2233 | 17 | * permission to link the code of portions of this program with the | ||
2234 | 18 | * OpenSSL library under certain conditions as described in each | ||
2235 | 19 | * individual source file, and distribute linked combinations | ||
2236 | 20 | * including the two. | ||
2237 | 21 | * You must obey the GNU General Public License in all respects | ||
2238 | 22 | * for all of the code used other than OpenSSL. If you modify | ||
2239 | 23 | * file(s) with this exception, you may extend this exception to your | ||
2240 | 24 | * version of the file(s), but you are not obligated to do so. If you | ||
2241 | 25 | * do not wish to do so, delete this exception statement from your | ||
2242 | 26 | * version. If you delete this exception statement from all source | ||
2243 | 27 | * files in the program, then also delete it here. | ||
2244 | 28 | */ | ||
2245 | 29 | |||
2246 | 30 | #include <QCoreApplication> | ||
2247 | 31 | #include <QDebug> | ||
2248 | 32 | #include <QString> | ||
2249 | 33 | #include <QTimer> | ||
2250 | 34 | #include <QTextStream> | ||
2251 | 35 | |||
2252 | 36 | #include <iostream> | ||
2253 | 37 | |||
2254 | 38 | #include <boost/optional.hpp> | ||
2255 | 39 | |||
2256 | 40 | #include <download_manager_tool.h> | ||
2257 | 41 | |||
2258 | 42 | DownloadManagerTool::DownloadManagerTool(QObject *parent): | ||
2259 | 43 | QObject(parent) | ||
2260 | 44 | { | ||
2261 | 45 | _dm = new click::DownloadManager(QSharedPointer<click::network::AccessManager>(new click::network::AccessManager()), | ||
2262 | 46 | QSharedPointer<click::CredentialsService>(new click::CredentialsService()), | ||
2263 | 47 | QSharedPointer<Ubuntu::DownloadManager::Manager>( | ||
2264 | 48 | Ubuntu::DownloadManager::Manager::createSessionManager())); | ||
2265 | 49 | } | ||
2266 | 50 | |||
2267 | 51 | void DownloadManagerTool::handleResponse(QString response) | ||
2268 | 52 | { | ||
2269 | 53 | QTextStream(stdout) << "DONE: response is " << response << "\n"; | ||
2270 | 54 | emit finished(); | ||
2271 | 55 | } | ||
2272 | 56 | |||
2273 | 57 | void DownloadManagerTool::fetchClickToken(QString url, QString sha512) | ||
2274 | 58 | { | ||
2275 | 59 | QObject::connect(_dm, &click::DownloadManager::clickTokenFetched, | ||
2276 | 60 | this, &DownloadManagerTool::handleResponse); | ||
2277 | 61 | QObject::connect(_dm, &click::DownloadManager::clickTokenFetchError, | ||
2278 | 62 | this, &DownloadManagerTool::handleResponse); | ||
2279 | 63 | _dm->fetchClickToken(url, sha512); | ||
2280 | 64 | } | ||
2281 | 65 | |||
2282 | 66 | void DownloadManagerTool::startDownload(QString url, QString sha512, QString appId) | ||
2283 | 67 | { | ||
2284 | 68 | QObject::connect(_dm, &click::DownloadManager::downloadStarted, | ||
2285 | 69 | this, &DownloadManagerTool::handleResponse); | ||
2286 | 70 | QObject::connect(_dm, &click::DownloadManager::downloadError, | ||
2287 | 71 | this, &DownloadManagerTool::handleResponse); | ||
2288 | 72 | _dm->startDownload(url, sha512, appId); | ||
2289 | 73 | } | ||
2290 | 74 | |||
2291 | 75 | int main(int argc, char *argv[]) | ||
2292 | 76 | { | ||
2293 | 77 | |||
2294 | 78 | QCoreApplication a(argc, argv); | ||
2295 | 79 | DownloadManagerTool tool; | ||
2296 | 80 | click::Downloader downloader(QSharedPointer<click::network::AccessManager>(new click::network::AccessManager())); | ||
2297 | 81 | QTimer timer; | ||
2298 | 82 | timer.setSingleShot(true); | ||
2299 | 83 | |||
2300 | 84 | QObject::connect(&tool, SIGNAL(finished()), &a, SLOT(quit())); | ||
2301 | 85 | |||
2302 | 86 | if (argc == 3) { | ||
2303 | 87 | |||
2304 | 88 | QObject::connect(&timer, &QTimer::timeout, [&]() { | ||
2305 | 89 | tool.fetchClickToken(QString(argv[1]), QString(argv[2])); | ||
2306 | 90 | } ); | ||
2307 | 91 | |||
2308 | 92 | } else if (argc == 4) { | ||
2309 | 93 | |||
2310 | 94 | QObject::connect(&timer, &QTimer::timeout, [&]() { | ||
2311 | 95 | downloader.startDownload(std::string(argv[1]), std::string(argv[2]), std::string(argv[3]), | ||
2312 | 96 | [&a] (std::pair<std::string, click::InstallError> arg){ | ||
2313 | 97 | auto error = arg.second; | ||
2314 | 98 | if (error == click::InstallError::NoError) { | ||
2315 | 99 | std::cout << " Success, got download ID:" << arg.first << std::endl; | ||
2316 | 100 | } else { | ||
2317 | 101 | std::cout << " Error:" << arg.first << std::endl; | ||
2318 | 102 | } | ||
2319 | 103 | a.quit(); | ||
2320 | 104 | }); | ||
2321 | 105 | |||
2322 | 106 | } ); | ||
2323 | 107 | |||
2324 | 108 | } else { | ||
2325 | 109 | QTextStream(stderr) << "Usages:\n" | ||
2326 | 110 | << "download_manager_tool https://public.apps.ubuntu.com/download/<<rest of click package dl url>> sha512\n" | ||
2327 | 111 | << "\t - when run with a valid U1 credential in the system, should print the click token to stdout.\n" | ||
2328 | 112 | << "download_manager_tool url sha512 app_id\n" | ||
2329 | 113 | << "\t - with a valid credential, should begin a download.\n"; | ||
2330 | 114 | |||
2331 | 115 | return 1; | ||
2332 | 116 | } | ||
2333 | 117 | |||
2334 | 118 | timer.start(0); | ||
2335 | 119 | |||
2336 | 120 | qInstallMessageHandler(0); | ||
2337 | 121 | return a.exec(); | ||
2338 | 122 | } | ||
2339 | 123 | |||
2340 | 124 | 0 | ||
2341 | === removed file 'scope/tests/download_manager_tool/download_manager_tool.h' | |||
2342 | --- scope/tests/download_manager_tool/download_manager_tool.h 2014-08-11 18:30:00 +0000 | |||
2343 | +++ scope/tests/download_manager_tool/download_manager_tool.h 1970-01-01 00:00:00 +0000 | |||
2344 | @@ -1,59 +0,0 @@ | |||
2345 | 1 | /* | ||
2346 | 2 | * Copyright (C) 2014 Canonical Ltd. | ||
2347 | 3 | * | ||
2348 | 4 | * This program is free software: you can redistribute it and/or modify it | ||
2349 | 5 | * under the terms of the GNU General Public License version 3, as published | ||
2350 | 6 | * by the Free Software Foundation. | ||
2351 | 7 | * | ||
2352 | 8 | * This program is distributed in the hope that it will be useful, but | ||
2353 | 9 | * WITHOUT ANY WARRANTY; without even the implied warranties of | ||
2354 | 10 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR | ||
2355 | 11 | * PURPOSE. See the GNU General Public License for more details. | ||
2356 | 12 | * | ||
2357 | 13 | * You should have received a copy of the GNU General Public License along | ||
2358 | 14 | * with this program. If not, see <http://www.gnu.org/licenses/>. | ||
2359 | 15 | * | ||
2360 | 16 | * In addition, as a special exception, the copyright holders give | ||
2361 | 17 | * permission to link the code of portions of this program with the | ||
2362 | 18 | * OpenSSL library under certain conditions as described in each | ||
2363 | 19 | * individual source file, and distribute linked combinations | ||
2364 | 20 | * including the two. | ||
2365 | 21 | * You must obey the GNU General Public License in all respects | ||
2366 | 22 | * for all of the code used other than OpenSSL. If you modify | ||
2367 | 23 | * file(s) with this exception, you may extend this exception to your | ||
2368 | 24 | * version of the file(s), but you are not obligated to do so. If you | ||
2369 | 25 | * do not wish to do so, delete this exception statement from your | ||
2370 | 26 | * version. If you delete this exception statement from all source | ||
2371 | 27 | * files in the program, then also delete it here. | ||
2372 | 28 | */ | ||
2373 | 29 | |||
2374 | 30 | #ifndef _DOWNLOAD_MANAGER_TOOL_H_ | ||
2375 | 31 | #define _DOWNLOAD_MANAGER_TOOL_H_ | ||
2376 | 32 | |||
2377 | 33 | #include <QString> | ||
2378 | 34 | #include <click/download-manager.h> | ||
2379 | 35 | |||
2380 | 36 | class DownloadManagerTool : public QObject { | ||
2381 | 37 | Q_OBJECT | ||
2382 | 38 | |||
2383 | 39 | public: | ||
2384 | 40 | explicit DownloadManagerTool(QObject *parent=0); | ||
2385 | 41 | |||
2386 | 42 | public slots: | ||
2387 | 43 | void fetchClickToken(QString url, QString sha512); | ||
2388 | 44 | void startDownload(QString url, QString sha512, QString appId); | ||
2389 | 45 | |||
2390 | 46 | private slots: | ||
2391 | 47 | void handleResponse(QString response); | ||
2392 | 48 | |||
2393 | 49 | signals: | ||
2394 | 50 | void finished(); | ||
2395 | 51 | |||
2396 | 52 | private: | ||
2397 | 53 | click::DownloadManager *_dm; | ||
2398 | 54 | |||
2399 | 55 | }; | ||
2400 | 56 | |||
2401 | 57 | #endif /* _DOWNLOAD_MANAGER_TOOL_H_ */ | ||
2402 | 58 | |||
2403 | 59 |