Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/download/DownloadNotificationServiceTest.java

Issue 2542483002: Add paused download into SharedPreferences (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.download; 5 package org.chromium.chrome.browser.download;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.content.Intent; 8 import android.content.Intent;
9 import android.content.SharedPreferences; 9 import android.content.SharedPreferences;
10 import android.os.Handler; 10 import android.os.Handler;
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 @SmallTest 136 @SmallTest
137 @Feature({"Download"}) 137 @Feature({"Download"})
138 public void testResumptionScheduledWithoutDownloadOperationIntent() throws E xception { 138 public void testResumptionScheduledWithoutDownloadOperationIntent() throws E xception {
139 MockDownloadResumptionScheduler scheduler = new MockDownloadResumptionSc heduler( 139 MockDownloadResumptionScheduler scheduler = new MockDownloadResumptionSc heduler(
140 getSystemContext().getApplicationContext()); 140 getSystemContext().getApplicationContext());
141 DownloadResumptionScheduler.setDownloadResumptionScheduler(scheduler); 141 DownloadResumptionScheduler.setDownloadResumptionScheduler(scheduler);
142 setupService(); 142 setupService();
143 Set<String> notifications = new HashSet<String>(); 143 Set<String> notifications = new HashSet<String>();
144 notifications.add( 144 notifications.add(
145 new DownloadSharedPreferenceEntry(1, false, true, UUID.randomUUI D().toString(), 145 new DownloadSharedPreferenceEntry(1, false, true, UUID.randomUUI D().toString(),
146 "test1", DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOA D) 146 "test1", DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOA D, true)
147 .getSharedPreferenceString()); 147 .getSharedPreferenceString());
148 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); 148 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
149 SharedPreferences.Editor editor = sharedPrefs.edit(); 149 SharedPreferences.Editor editor = sharedPrefs.edit();
150 editor.putStringSet( 150 editor.putStringSet(
151 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications); 151 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications);
152 editor.apply(); 152 editor.apply();
153 startNotificationService(); 153 startNotificationService();
154 assertTrue(scheduler.mScheduled); 154 assertTrue(scheduler.mScheduled);
155 } 155 }
156 156
(...skipping 13 matching lines...) Expand all
170 public void run() { 170 public void run() {
171 Intent intent = new Intent(getService(), MockDownloadNotificatio nService.class); 171 Intent intent = new Intent(getService(), MockDownloadNotificatio nService.class);
172 intent.setAction(DownloadNotificationService.ACTION_DOWNLOAD_RES UME_ALL); 172 intent.setAction(DownloadNotificationService.ACTION_DOWNLOAD_RES UME_ALL);
173 startService(intent); 173 startService(intent);
174 } 174 }
175 }); 175 });
176 assertFalse(scheduler.mScheduled); 176 assertFalse(scheduler.mScheduled);
177 } 177 }
178 178
179 /** 179 /**
180 * Tests that download resumption task is not scheduled when there is no aut o resumable
181 * download in SharedPreferences.
182 */
183 @SmallTest
184 @Feature({"Download"})
185 public void testResumptionNotScheduledWithoutAutoResumableDownload() throws Exception {
186 MockDownloadResumptionScheduler scheduler = new MockDownloadResumptionSc heduler(
187 getSystemContext().getApplicationContext());
188 DownloadResumptionScheduler.setDownloadResumptionScheduler(scheduler);
189 setupService();
190 Set<String> notifications = new HashSet<String>();
gone 2016/11/29 23:58:37 new HashSet<>()
qinmin 2016/11/30 00:44:48 Done.
191 notifications.add(
192 new DownloadSharedPreferenceEntry(1, false, true, UUID.randomUUI D().toString(),
193 "test1", DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOA D, false)
194 .getSharedPreferenceString());
195 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
196 SharedPreferences.Editor editor = sharedPrefs.edit();
197 editor.putStringSet(
198 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications);
199 editor.apply();
200 startNotificationService();
201 assertTrue(scheduler.mScheduled);
202 }
203
204 /**
180 * Tests that creating the service without launching chrome will pause all o ngoing downloads. 205 * Tests that creating the service without launching chrome will pause all o ngoing downloads.
181 */ 206 */
182 @SmallTest 207 @SmallTest
183 @Feature({"Download"}) 208 @Feature({"Download"})
184 public void testPausingWithOngoingDownloads() { 209 public void testPausingWithOngoingDownloads() {
185 setupService(); 210 setupService();
186 Context mockContext = new AdvancedMockContext(getSystemContext()); 211 Context mockContext = new AdvancedMockContext(getSystemContext());
187 getService().setContext(mockContext); 212 getService().setContext(mockContext);
188 Set<String> notifications = new HashSet<String>(); 213 Set<String> notifications = new HashSet<String>();
189 notifications.add( 214 notifications.add(
190 new DownloadSharedPreferenceEntry(1, false, true, UUID.randomUUI D().toString(), 215 new DownloadSharedPreferenceEntry(1, false, true, UUID.randomUUI D().toString(),
191 "test1", DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOA D) 216 "test1", DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOA D, true)
192 .getSharedPreferenceString()); 217 .getSharedPreferenceString());
193 notifications.add( 218 notifications.add(
194 new DownloadSharedPreferenceEntry(2, false, true, UUID.randomUUI D().toString(), 219 new DownloadSharedPreferenceEntry(2, false, true, UUID.randomUUI D().toString(),
195 "test2", DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOA D) 220 "test2", DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOA D, true)
196 .getSharedPreferenceString()); 221 .getSharedPreferenceString());
197 SharedPreferences sharedPrefs = 222 SharedPreferences sharedPrefs =
198 ContextUtils.getAppSharedPreferences(); 223 ContextUtils.getAppSharedPreferences();
199 SharedPreferences.Editor editor = sharedPrefs.edit(); 224 SharedPreferences.Editor editor = sharedPrefs.edit();
200 editor.putStringSet( 225 editor.putStringSet(
201 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications); 226 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications);
202 editor.apply(); 227 editor.apply();
203 startNotificationService(); 228 startNotificationService();
204 assertTrue(getService().isPaused()); 229 assertTrue(getService().isPaused());
205 assertEquals(2, getService().getNotificationIds().size()); 230 assertEquals(2, getService().getNotificationIds().size());
206 assertTrue(getService().getNotificationIds().contains(1)); 231 assertTrue(getService().getNotificationIds().contains(1));
207 assertTrue(getService().getNotificationIds().contains(2)); 232 assertTrue(getService().getNotificationIds().contains(2));
208 assertTrue( 233 assertTrue(
209 sharedPrefs.contains(DownloadNotificationService.PENDING_DOWNLOA D_NOTIFICATIONS)); 234 sharedPrefs.contains(DownloadNotificationService.PENDING_DOWNLOA D_NOTIFICATIONS));
210 } 235 }
211 236
212 /** 237 /**
213 * Tests adding and cancelling notifications. 238 * Tests adding and cancelling notifications.
214 */ 239 */
215 @SmallTest 240 @SmallTest
216 @Feature({"Download"}) 241 @Feature({"Download"})
217 public void testAddingAndCancelingNotifications() { 242 public void testAddingAndCancelingNotifications() {
218 setupService(); 243 setupService();
219 Context mockContext = new AdvancedMockContext(getSystemContext()); 244 Context mockContext = new AdvancedMockContext(getSystemContext());
220 getService().setContext(mockContext); 245 getService().setContext(mockContext);
221 Set<String> notifications = new HashSet<String>(); 246 Set<String> notifications = new HashSet<String>();
222 String guid1 = UUID.randomUUID().toString(); 247 String guid1 = UUID.randomUUID().toString();
223 notifications.add(new DownloadSharedPreferenceEntry(3, false, true, guid 1, "success", 248 notifications.add(new DownloadSharedPreferenceEntry(3, false, true, guid 1, "success",
224 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLO AD) 249 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD, true)
225 .getSharedPreferenceString()); 250 .getSharedPreferenceString());
226 String guid2 = UUID.randomUUID().toString(); 251 String guid2 = UUID.randomUUID().toString();
227 notifications.add(new DownloadSharedPreferenceEntry(4, false, true, guid 2, "failed", 252 notifications.add(new DownloadSharedPreferenceEntry(4, false, true, guid 2, "failed",
228 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLO AD) 253 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD, true)
229 .getSharedPreferenceString()); 254 .getSharedPreferenceString());
230 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); 255 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
231 SharedPreferences.Editor editor = sharedPrefs.edit(); 256 SharedPreferences.Editor editor = sharedPrefs.edit();
232 editor.putStringSet( 257 editor.putStringSet(
233 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications); 258 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications);
234 editor.apply(); 259 editor.apply();
235 startNotificationService(); 260 startNotificationService();
236 assertEquals(2, getService().getNotificationIds().size()); 261 assertEquals(2, getService().getNotificationIds().size());
237 assertTrue(getService().getNotificationIds().contains(3)); 262 assertTrue(getService().getNotificationIds().contains(3));
238 assertTrue(getService().getNotificationIds().contains(4)); 263 assertTrue(getService().getNotificationIds().contains(4));
239 264
(...skipping 30 matching lines...) Expand all
270 public void testDownloadSuccessNotification() { 295 public void testDownloadSuccessNotification() {
271 setupService(); 296 setupService();
272 startNotificationService(); 297 startNotificationService();
273 DownloadNotificationService service = bindNotificationService(); 298 DownloadNotificationService service = bindNotificationService();
274 String guid = UUID.randomUUID().toString(); 299 String guid = UUID.randomUUID().toString();
275 service.notifyDownloadSuccessful(guid, "/path/to/test", "test", 100L, fa lse, false); 300 service.notifyDownloadSuccessful(guid, "/path/to/test", "test", 100L, fa lse, false);
276 assertEquals(1, getService().getNotificationIds().size()); 301 assertEquals(1, getService().getNotificationIds().size());
277 } 302 }
278 303
279 /** 304 /**
280 * Tests resume all pending downloads. 305 * Tests resume all pending downloads. Only auto resumable downloads can res ume.
281 */ 306 */
282 @SmallTest 307 @SmallTest
283 @Feature({"Download"}) 308 @Feature({"Download"})
284 public void testResumeAllPendingDownloads() throws Exception { 309 public void testResumeAllPendingDownloads() throws Exception {
285 setupService(); 310 setupService();
286 Context mockContext = new AdvancedMockContext(getSystemContext()); 311 Context mockContext = new AdvancedMockContext(getSystemContext());
287 getService().setContext(mockContext); 312 getService().setContext(mockContext);
288 Set<String> notifications = new HashSet<String>(); 313 Set<String> notifications = new HashSet<String>();
289 String guid1 = UUID.randomUUID().toString(); 314 String guid1 = UUID.randomUUID().toString();
290 notifications.add(new DownloadSharedPreferenceEntry(3, false, false, gui d1, "success", 315 notifications.add(new DownloadSharedPreferenceEntry(3, false, false, gui d1, "success",
291 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLO AD) 316 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD, true)
292 .getSharedPreferenceString()); 317 .getSharedPreferenceString());
293 String guid2 = UUID.randomUUID().toString(); 318 String guid2 = UUID.randomUUID().toString();
294 notifications.add(new DownloadSharedPreferenceEntry(4, false, true, guid 2, "failed", 319 notifications.add(new DownloadSharedPreferenceEntry(4, false, true, guid 2, "failed",
295 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLO AD) 320 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD, true)
296 .getSharedPreferenceString()); 321 .getSharedPreferenceString());
322 String guid3 = UUID.randomUUID().toString();
323 notifications.add(new DownloadSharedPreferenceEntry(5, false, true, guid 3, "nonresumable",
324 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD, false)
325 .getSharedPreferenceString());
297 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences(); 326 SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
298 SharedPreferences.Editor editor = sharedPrefs.edit(); 327 SharedPreferences.Editor editor = sharedPrefs.edit();
299 editor.putStringSet( 328 editor.putStringSet(
300 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications); 329 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications);
301 editor.apply(); 330 editor.apply();
302 startNotificationService(); 331 startNotificationService();
303 DownloadNotificationService service = bindNotificationService(); 332 DownloadNotificationService service = bindNotificationService();
304 DownloadManagerService.disableNetworkListenerForTest(); 333 DownloadManagerService.disableNetworkListenerForTest();
305 334
306 final MockDownloadManagerService manager = 335 final MockDownloadManagerService manager =
(...skipping 21 matching lines...) Expand all
328 */ 357 */
329 @SmallTest 358 @SmallTest
330 @Feature({"Download"}) 359 @Feature({"Download"})
331 public void testIncognitoDownloadCanceledOnBrowserKill() throws Exception { 360 public void testIncognitoDownloadCanceledOnBrowserKill() throws Exception {
332 setupService(); 361 setupService();
333 Context mockContext = new AdvancedMockContext(getSystemContext()); 362 Context mockContext = new AdvancedMockContext(getSystemContext());
334 getService().setContext(mockContext); 363 getService().setContext(mockContext);
335 Set<String> notifications = new HashSet<String>(); 364 Set<String> notifications = new HashSet<String>();
336 String uuid = UUID.randomUUID().toString(); 365 String uuid = UUID.randomUUID().toString();
337 notifications.add(new DownloadSharedPreferenceEntry(1, true, true, uuid, "test1", 366 notifications.add(new DownloadSharedPreferenceEntry(1, true, true, uuid, "test1",
338 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD).getSharedPrefe renceString()); 367 DownloadSharedPreferenceEntry.ITEM_TYPE_DOWNLOAD, true)
368 .getSharedPreferenceString());
339 SharedPreferences sharedPrefs = 369 SharedPreferences sharedPrefs =
340 ContextUtils.getAppSharedPreferences(); 370 ContextUtils.getAppSharedPreferences();
341 SharedPreferences.Editor editor = sharedPrefs.edit(); 371 SharedPreferences.Editor editor = sharedPrefs.edit();
342 editor.putStringSet( 372 editor.putStringSet(
343 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications); 373 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS, noti fications);
344 editor.apply(); 374 editor.apply();
345 startNotificationService(); 375 startNotificationService();
346 assertTrue(getService().isPaused()); 376 assertTrue(getService().isPaused());
347 assertFalse(sharedPrefs.contains( 377 assertFalse(sharedPrefs.contains(
348 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS)); 378 DownloadNotificationService.PENDING_DOWNLOAD_NOTIFICATIONS));
(...skipping 19 matching lines...) Expand all
368 assertEquals("3 hours left", DownloadNotificationService.formatRemaining Time(context, 398 assertEquals("3 hours left", DownloadNotificationService.formatRemaining Time(context,
369 150 * DownloadNotificationService.SECONDS_PER_MINUTE * MILLIS_PE R_SECOND)); 399 150 * DownloadNotificationService.SECONDS_PER_MINUTE * MILLIS_PE R_SECOND));
370 assertEquals("1 day left", DownloadNotificationService.formatRemainingTi me(context, 400 assertEquals("1 day left", DownloadNotificationService.formatRemainingTi me(context,
371 DownloadNotificationService.SECONDS_PER_DAY * MILLIS_PER_SECOND) ); 401 DownloadNotificationService.SECONDS_PER_DAY * MILLIS_PER_SECOND) );
372 assertEquals("2 days left", DownloadNotificationService.formatRemainingT ime(context, 402 assertEquals("2 days left", DownloadNotificationService.formatRemainingT ime(context,
373 59 * DownloadNotificationService.SECONDS_PER_HOUR * MILLIS_PER_S ECOND)); 403 59 * DownloadNotificationService.SECONDS_PER_HOUR * MILLIS_PER_S ECOND));
374 assertEquals("3 days left", DownloadNotificationService.formatRemainingT ime(context, 404 assertEquals("3 days left", DownloadNotificationService.formatRemainingT ime(context,
375 60 * DownloadNotificationService.SECONDS_PER_HOUR * MILLIS_PER_S ECOND)); 405 60 * DownloadNotificationService.SECONDS_PER_HOUR * MILLIS_PER_S ECOND));
376 } 406 }
377 } 407 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698