OLD | NEW |
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.app.DownloadManager; | 7 import android.app.DownloadManager; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.database.Cursor; | 9 import android.database.Cursor; |
10 import android.os.Environment; | 10 import android.os.Environment; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 92 |
93 /** | 93 /** |
94 * Check the last download matches the given name and exists in DownloadMana
ger. | 94 * Check the last download matches the given name and exists in DownloadMana
ger. |
95 */ | 95 */ |
96 public void checkLastDownload(String fileName) throws IOException { | 96 public void checkLastDownload(String fileName) throws IOException { |
97 String lastDownload = getLastDownloadFile(); | 97 String lastDownload = getLastDownloadFile(); |
98 assertTrue(isSameDownloadFile(fileName, lastDownload)); | 98 assertTrue(isSameDownloadFile(fileName, lastDownload)); |
99 assertTrue(hasDownload(lastDownload, null)); | 99 assertTrue(hasDownload(lastDownload, null)); |
100 } | 100 } |
101 | 101 |
102 public EnqueueHttpGetDownloadCallbackHelper getHttpGetDownloadCallbackHelper
() { | |
103 return mEnqueueHttpGetDownloadCallbackHelper; | |
104 } | |
105 | |
106 /** | 102 /** |
107 * Delete all download entries in DownloadManager and delete the correspondi
ng files. | 103 * Delete all download entries in DownloadManager and delete the correspondi
ng files. |
108 */ | 104 */ |
109 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") | 105 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") |
110 private void cleanUpAllDownloads() { | 106 private void cleanUpAllDownloads() { |
111 DownloadManager manager = | 107 DownloadManager manager = |
112 (DownloadManager) getActivity().getSystemService(Context.DOWNLOA
D_SERVICE); | 108 (DownloadManager) getActivity().getSystemService(Context.DOWNLOA
D_SERVICE); |
113 Cursor cursor = manager.query(new DownloadManager.Query()); | 109 Cursor cursor = manager.query(new DownloadManager.Query()); |
114 int idColumnIndex = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_
ID); | 110 int idColumnIndex = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_
ID); |
115 cursor.moveToFirst(); | 111 cursor.moveToFirst(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 cursor = manager.query(query); | 145 cursor = manager.query(query); |
150 if (!cursor.moveToFirst()) { | 146 if (!cursor.moveToFirst()) { |
151 return null; | 147 return null; |
152 } | 148 } |
153 return getPathFromCursor(cursor); | 149 return getPathFromCursor(cursor); |
154 } finally { | 150 } finally { |
155 if (cursor != null) cursor.close(); | 151 if (cursor != null) cursor.close(); |
156 } | 152 } |
157 } | 153 } |
158 | 154 |
159 protected static class EnqueueHttpGetDownloadCallbackHelper extends Callback
Helper { | |
160 private DownloadInfo mDownloadInfo; | |
161 | |
162 public void notifyCalled(DownloadInfo downloadInfo, boolean notifyComple
ted) { | |
163 mDownloadInfo = downloadInfo; | |
164 super.notifyCalled(); | |
165 } | |
166 | |
167 public DownloadInfo getDownloadInfo() { | |
168 return mDownloadInfo; | |
169 } | |
170 } | |
171 | |
172 private final EnqueueHttpGetDownloadCallbackHelper mEnqueueHttpGetDownloadCa
llbackHelper = | |
173 new EnqueueHttpGetDownloadCallbackHelper(); | |
174 private String mLastDownloadFilePath; | 155 private String mLastDownloadFilePath; |
175 private final CallbackHelper mHttpDownloadFinished = new CallbackHelper(); | 156 private final CallbackHelper mHttpDownloadFinished = new CallbackHelper(); |
176 private DownloadManagerService mSavedDownloadManagerService; | 157 private DownloadManagerService mSavedDownloadManagerService; |
177 | 158 |
178 protected String getLastDownloadFile() { | 159 protected String getLastDownloadFile() { |
179 return new File(mLastDownloadFilePath).getName(); | 160 return new File(mLastDownloadFilePath).getName(); |
180 } | 161 } |
181 | 162 |
182 // The Android DownloadManager sometimes appends a number to a file name whe
n it downloads it | 163 // The Android DownloadManager sometimes appends a number to a file name whe
n it downloads it |
183 // ex: google.png becomes google-23.png | 164 // ex: google.png becomes google-23.png |
(...skipping 29 matching lines...) Expand all Loading... |
213 Handler handler, long updateDelayInMillis) { | 194 Handler handler, long updateDelayInMillis) { |
214 super(context, downloadNotifier, handler, updateDelayInMillis); | 195 super(context, downloadNotifier, handler, updateDelayInMillis); |
215 } | 196 } |
216 | 197 |
217 @Override | 198 @Override |
218 public void broadcastDownloadSuccessful(DownloadInfo downloadInfo) { | 199 public void broadcastDownloadSuccessful(DownloadInfo downloadInfo) { |
219 super.broadcastDownloadSuccessful(downloadInfo); | 200 super.broadcastDownloadSuccessful(downloadInfo); |
220 mLastDownloadFilePath = downloadInfo.getFilePath(); | 201 mLastDownloadFilePath = downloadInfo.getFilePath(); |
221 mHttpDownloadFinished.notifyCalled(); | 202 mHttpDownloadFinished.notifyCalled(); |
222 } | 203 } |
223 | |
224 @Override | |
225 public void enqueueDownloadManagerRequest( | |
226 final DownloadItem item, boolean notifyCompleted) { | |
227 // Intentionally do not call super, since DownloadManager does not w
ork in test | |
228 // environment. | |
229 mEnqueueHttpGetDownloadCallbackHelper.notifyCalled( | |
230 item.getDownloadInfo(), notifyCompleted); | |
231 } | |
232 } | 204 } |
233 | 205 |
234 @Override | 206 @Override |
235 protected void setUp() throws Exception { | 207 protected void setUp() throws Exception { |
236 super.setUp(); | 208 super.setUp(); |
237 cleanUpAllDownloads(); | 209 cleanUpAllDownloads(); |
238 | 210 |
239 try { | 211 try { |
240 ApplicationUtils.waitForLibraryDependencies(getInstrumentation()); | 212 ApplicationUtils.waitForLibraryDependencies(getInstrumentation()); |
241 } catch (InterruptedException e) { | 213 } catch (InterruptedException e) { |
(...skipping 29 matching lines...) Expand all Loading... |
271 protected void deleteFilesInDownloadDirectory(String...filenames) { | 243 protected void deleteFilesInDownloadDirectory(String...filenames) { |
272 for (String filename : filenames) { | 244 for (String filename : filenames) { |
273 final File fileToDelete = new File(DOWNLOAD_DIRECTORY, filename); | 245 final File fileToDelete = new File(DOWNLOAD_DIRECTORY, filename); |
274 if (fileToDelete.exists()) { | 246 if (fileToDelete.exists()) { |
275 assertTrue("Could not delete file that would block this test", | 247 assertTrue("Could not delete file that would block this test", |
276 fileToDelete.delete()); | 248 fileToDelete.delete()); |
277 } | 249 } |
278 } | 250 } |
279 } | 251 } |
280 } | 252 } |
OLD | NEW |