OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.download; |
| 6 |
| 7 import android.app.DownloadManager; |
| 8 import android.content.BroadcastReceiver; |
| 9 import android.content.Context; |
| 10 import android.content.Intent; |
| 11 import android.content.IntentFilter; |
| 12 import android.database.Cursor; |
| 13 import android.os.Environment; |
| 14 import android.os.Handler; |
| 15 import android.util.Log; |
| 16 |
| 17 import org.chromium.base.ThreadUtils; |
| 18 import org.chromium.base.annotations.SuppressFBWarnings; |
| 19 import org.chromium.chrome.browser.ChromeActivity; |
| 20 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 21 import org.chromium.content.browser.DownloadController; |
| 22 import org.chromium.content.browser.DownloadInfo; |
| 23 import org.chromium.content.browser.test.util.ApplicationUtils; |
| 24 import org.chromium.content.browser.test.util.CallbackHelper; |
| 25 |
| 26 import java.io.File; |
| 27 import java.io.FileInputStream; |
| 28 import java.io.IOException; |
| 29 import java.util.concurrent.TimeUnit; |
| 30 import java.util.concurrent.TimeoutException; |
| 31 |
| 32 /** |
| 33 * Base case for tests that need to download a file. |
| 34 * |
| 35 * This has to be a base class because some classes (like BrowserEvent) are expo
sed only |
| 36 * to children of ChromeActivityTestCaseBase. It is a very broken approach to sh
aring |
| 37 * but the only other option is to refactor the ChromeActivityTestCaseBase imple
mentation |
| 38 * and all of our test cases. |
| 39 * |
| 40 */ |
| 41 public abstract class DownloadTestBase extends ChromeActivityTestCaseBase<Chrome
Activity> { |
| 42 private static final String TAG = "DownloadTestBase"; |
| 43 private static final File DOWNLOAD_DIRECTORY = |
| 44 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_
DOWNLOADS); |
| 45 public static final long UPDATE_DELAY_MILLIS = 1000; |
| 46 |
| 47 public DownloadTestBase() { |
| 48 super(ChromeActivity.class); |
| 49 } |
| 50 |
| 51 /** |
| 52 * Check the download exists in DownloadManager by matching the local file |
| 53 * path. |
| 54 * |
| 55 * @param fileName Expected file name. Path is built by appending filename t
o |
| 56 * the system downloads path. |
| 57 * |
| 58 * @param expectedContents Expected contents of the file, or null if the con
tents should not be |
| 59 * checked. |
| 60 */ |
| 61 public boolean hasDownload(String fileName, String expectedContents) throws
IOException { |
| 62 File downloadedFile = new File(DOWNLOAD_DIRECTORY, fileName); |
| 63 if (!downloadedFile.exists()) { |
| 64 Log.d(TAG, "The file " + fileName + " does not exist"); |
| 65 return false; |
| 66 } |
| 67 |
| 68 String fullPath = downloadedFile.getAbsolutePath(); |
| 69 |
| 70 DownloadManager manager = |
| 71 (DownloadManager) getActivity().getSystemService(Context.DOWNLOA
D_SERVICE); |
| 72 Cursor cursor = manager.query(new DownloadManager.Query()); |
| 73 |
| 74 cursor.moveToFirst(); |
| 75 boolean result = false; |
| 76 while (!cursor.isAfterLast()) { |
| 77 if (fullPath.equals(getPathFromCursor(cursor))) { |
| 78 if (expectedContents != null) { |
| 79 FileInputStream stream = new FileInputStream(new File(fullPa
th)); |
| 80 byte[] data = new byte[expectedContents.getBytes().length]; |
| 81 try { |
| 82 assertEquals(stream.read(data), data.length); |
| 83 String contents = new String(data); |
| 84 assertEquals(expectedContents, contents); |
| 85 } finally { |
| 86 stream.close(); |
| 87 } |
| 88 } |
| 89 result = true; |
| 90 break; |
| 91 } |
| 92 cursor.moveToNext(); |
| 93 } |
| 94 cursor.close(); |
| 95 return result; |
| 96 } |
| 97 |
| 98 /** |
| 99 * Check the last download matches the given name and exists in DownloadMana
ger. |
| 100 */ |
| 101 public void checkLastDownload(String fileName) throws IOException { |
| 102 String lastDownload = getLastDownloadFile(); |
| 103 assertTrue(isSameDownloadFile(fileName, lastDownload)); |
| 104 assertTrue(hasDownload(lastDownload, null)); |
| 105 } |
| 106 |
| 107 /** |
| 108 * Delete all download entries in DownloadManager and delete the correspondi
ng files. |
| 109 */ |
| 110 @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE") |
| 111 private void cleanUpAllDownloads() { |
| 112 DownloadManager manager = |
| 113 (DownloadManager) getActivity().getSystemService(Context.DOWNLOA
D_SERVICE); |
| 114 Cursor cursor = manager.query(new DownloadManager.Query()); |
| 115 int idColumnIndex = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_
ID); |
| 116 cursor.moveToFirst(); |
| 117 while (!cursor.isAfterLast()) { |
| 118 long id = cursor.getLong(idColumnIndex); |
| 119 String fileName = getPathFromCursor(cursor); |
| 120 manager.remove(id); |
| 121 |
| 122 if (fileName != null) { // Somehow fileName can be null for some en
tries. |
| 123 // manager.remove does not remove downloaded file. |
| 124 File localFile = new File(fileName); |
| 125 if (localFile.exists()) { |
| 126 localFile.delete(); |
| 127 } |
| 128 } |
| 129 |
| 130 cursor.moveToNext(); |
| 131 } |
| 132 cursor.close(); |
| 133 } |
| 134 |
| 135 /** |
| 136 * Retrieve the path of the downloaded file from a DownloadManager cursor. |
| 137 */ |
| 138 private String getPathFromCursor(Cursor cursor) { |
| 139 int columnId = cursor.getColumnIndex("local_filename"); |
| 140 return cursor.getString(columnId); |
| 141 } |
| 142 |
| 143 private String getPathForDownload(long downloadId) { |
| 144 DownloadManager manager = |
| 145 (DownloadManager) getActivity().getSystemService(Context.DOWNLOA
D_SERVICE); |
| 146 DownloadManager.Query query = new DownloadManager.Query(); |
| 147 query.setFilterById(downloadId); |
| 148 Cursor cursor = null; |
| 149 try { |
| 150 cursor = manager.query(query); |
| 151 if (!cursor.moveToFirst()) { |
| 152 return null; |
| 153 } |
| 154 return getPathFromCursor(cursor); |
| 155 } finally { |
| 156 if (cursor != null) cursor.close(); |
| 157 } |
| 158 } |
| 159 |
| 160 private CallbackHelper mHttpGetDownloadFinished; |
| 161 private String mLastDownloadFilePath; |
| 162 private CallbackHelper mHttpDownloadFinished; |
| 163 private DownloadManagerService mSavedDownloadManagerService; |
| 164 |
| 165 protected String getLastDownloadFile() { |
| 166 return new File(mLastDownloadFilePath).getName(); |
| 167 } |
| 168 |
| 169 // The Android DownloadManager sometimes appends a number to a file name whe
n it downloads it |
| 170 // ex: google.png becomes google-23.png |
| 171 // This happens even when there is no other prior download with that name, i
t could be a bug. |
| 172 // TODO(jcivelli): investigate if we can isolate that behavior and file a bu
g to Android. |
| 173 protected boolean isSameDownloadFile(String originalName, String downloadNam
e) { |
| 174 String fileName = originalName; |
| 175 String extension = ""; |
| 176 int dotIndex = originalName.lastIndexOf('.'); |
| 177 if (dotIndex != -1 && dotIndex < originalName.length()) { |
| 178 fileName = originalName.substring(0, dotIndex); |
| 179 extension = originalName.substring(dotIndex); // We include the '.' |
| 180 } |
| 181 return downloadName.startsWith(fileName) && downloadName.endsWith(extens
ion); |
| 182 } |
| 183 |
| 184 public boolean waitForGetDownloadToFinish() throws InterruptedException { |
| 185 mLastDownloadFilePath = null; |
| 186 boolean eventReceived = true; |
| 187 try { |
| 188 mHttpGetDownloadFinished.waitForCallback(0); |
| 189 } catch (TimeoutException e) { |
| 190 eventReceived = false; |
| 191 } |
| 192 mHttpGetDownloadFinished = new CallbackHelper(); |
| 193 return eventReceived; |
| 194 } |
| 195 |
| 196 public boolean waitForChromeDownloadToFinish() throws InterruptedException { |
| 197 boolean eventReceived = true; |
| 198 try { |
| 199 mHttpDownloadFinished.waitForCallback(0, 1, 5, TimeUnit.SECONDS); |
| 200 } catch (TimeoutException e) { |
| 201 eventReceived = false; |
| 202 } |
| 203 mHttpDownloadFinished = new CallbackHelper(); |
| 204 return eventReceived; |
| 205 } |
| 206 |
| 207 @Override |
| 208 protected void setUp() throws Exception { |
| 209 super.setUp(); |
| 210 cleanUpAllDownloads(); |
| 211 |
| 212 try { |
| 213 assertTrue(ApplicationUtils.waitForLibraryDependencies(getInstrument
ation())); |
| 214 } catch (InterruptedException e) { |
| 215 fail("Library dependencies were never initialized."); |
| 216 } |
| 217 final Context context = getInstrumentation().getTargetContext().getAppli
cationContext(); |
| 218 |
| 219 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 220 @Override |
| 221 public void run() { |
| 222 mSavedDownloadManagerService = DownloadManagerService.setDownloa
dManagerService( |
| 223 new DownloadManagerService(context, |
| 224 new SystemDownloadNotifier(context), new Handler
(), |
| 225 UPDATE_DELAY_MILLIS) { |
| 226 @Override |
| 227 protected void broadcastDownloadSuccessful(DownloadI
nfo downloadInfo) { |
| 228 super.broadcastDownloadSuccessful(downloadInfo); |
| 229 mLastDownloadFilePath = downloadInfo.getFilePath
(); |
| 230 mHttpDownloadFinished.notifyCalled(); |
| 231 } |
| 232 }); |
| 233 DownloadController.setDownloadNotificationService( |
| 234 DownloadManagerService.getDownloadManagerService(context
)); |
| 235 } |
| 236 }); |
| 237 |
| 238 mHttpGetDownloadFinished = new CallbackHelper(); |
| 239 mHttpDownloadFinished = new CallbackHelper(); |
| 240 |
| 241 context.registerReceiver(new BroadcastReceiver() { |
| 242 @Override |
| 243 public void onReceive(Context context, Intent intent) { |
| 244 String action = intent.getAction(); |
| 245 if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { |
| 246 // The name of the download does not seem to always be what
we asked for |
| 247 // (ex: google.png becomes google-23.png). Probably a bug in
the Android |
| 248 // DownloadManager. Make sure we always retrieve the actual
download name. |
| 249 long downloadId = intent.getLongExtra(DownloadManager.EXTRA_
DOWNLOAD_ID, -1); |
| 250 assertTrue("No download id with download finished intent", d
ownloadId != -1); |
| 251 mLastDownloadFilePath = getPathForDownload(downloadId); |
| 252 assertNotNull("Download path not found", mLastDownloadFilePa
th); |
| 253 mHttpGetDownloadFinished.notifyCalled(); |
| 254 } |
| 255 } |
| 256 }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); |
| 257 } |
| 258 |
| 259 @Override |
| 260 protected void tearDown() throws Exception { |
| 261 cleanUpAllDownloads(); |
| 262 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 263 @Override |
| 264 public void run() { |
| 265 DownloadManagerService.setDownloadManagerService(mSavedDownloadM
anagerService); |
| 266 DownloadController.setDownloadNotificationService(mSavedDownload
ManagerService); |
| 267 } |
| 268 }); |
| 269 super.tearDown(); |
| 270 } |
| 271 } |
OLD | NEW |