| 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.content.Context; | 7 import android.content.Context; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 | 9 |
| 10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 public MockChromeDownloadDelegate(Context context, Tab tab) { | 34 public MockChromeDownloadDelegate(Context context, Tab tab) { |
| 35 super(context, null, tab); | 35 super(context, null, tab); |
| 36 } | 36 } |
| 37 | 37 |
| 38 @Override | 38 @Override |
| 39 protected void onDownloadStartNoStream(DownloadInfo downloadInfo) { | 39 protected void onDownloadStartNoStream(DownloadInfo downloadInfo) { |
| 40 } | 40 } |
| 41 } | 41 } |
| 42 | 42 |
| 43 /** | 43 /** |
| 44 * Test to make sure {@link ChromeDownloadDelegate#fileName} returns the | |
| 45 * right file for different URLs and MIME types. | |
| 46 */ | |
| 47 @SmallTest | |
| 48 @Feature({"Download"}) | |
| 49 public void testFileName() { | |
| 50 String testUrl = "http://server.com/file.pdf"; | |
| 51 assertEquals("file.pdf", ChromeDownloadDelegate.fileName(testUrl, "appli
cation/pdf", "")); | |
| 52 assertEquals("file.pdf", ChromeDownloadDelegate.fileName(testUrl, "", ""
)); | |
| 53 | |
| 54 // .php is an unknown MIME format extension. | |
| 55 // This used to generate file.php even when the MIME type was set. | |
| 56 // http://code.google.com/p/chromium/issues/detail?id=134396 | |
| 57 testUrl = "http://server.com/file.php"; | |
| 58 | |
| 59 assertEquals("file.pdf", ChromeDownloadDelegate.fileName(testUrl, "appli
cation/pdf", "")); | |
| 60 assertEquals("file.php", ChromeDownloadDelegate.fileName(testUrl, "", ""
)); | |
| 61 | |
| 62 // .xml is a known MIME format extension. | |
| 63 testUrl = "http://server.com/file.xml"; | |
| 64 assertEquals("file.xml", ChromeDownloadDelegate.fileName(testUrl, "", ""
)); | |
| 65 | |
| 66 assertEquals("file.pdf", ChromeDownloadDelegate.fileName(testUrl, "appli
cation/pdf", "")); | |
| 67 | |
| 68 // If the file's extension and HTTP header's MIME type are the same, use | |
| 69 // the former to derive the final extension. | |
| 70 // https://code.google.com/p/chromium/issues/detail?id=170852 | |
| 71 testUrl = "http://server.com/file.mp3"; | |
| 72 assertEquals("file.mp3", ChromeDownloadDelegate.fileName(testUrl, "audio
/mpeg", "")); | |
| 73 | |
| 74 testUrl = "http://server.com/"; | |
| 75 assertEquals("downloadfile.bin", ChromeDownloadDelegate.fileName(testUrl
, "", "")); | |
| 76 assertEquals("downloadfile.pdf", | |
| 77 ChromeDownloadDelegate.fileName(testUrl, "application/pdf", ""))
; | |
| 78 | |
| 79 // Fails to match the filename pattern from header; uses one from url. | |
| 80 // Note that header itself is a valid one. | |
| 81 testUrl = "http://server.com/file.pdf"; | |
| 82 assertEquals("file.pdf", ChromeDownloadDelegate.fileName(testUrl, "appli
cation/pdf", | |
| 83 "attachment; name=\"foo\"; filename=\"bar\"")); | |
| 84 assertEquals("file.pdf", ChromeDownloadDelegate.fileName(testUrl, "appli
cation/pdf", | |
| 85 "attachment; filename=\"bar\"; name=\"foo\"")); | |
| 86 assertEquals("file.pdf", ChromeDownloadDelegate.fileName(testUrl, "appli
cation/pdf", | |
| 87 "attachment; filename=\"bar\"; filename*=utf-8''baz")); | |
| 88 } | |
| 89 | |
| 90 /** | |
| 91 * Test to make sure {@link ChromeDownloadDelegate#shouldInterceptContextMen
uDownload} | 44 * Test to make sure {@link ChromeDownloadDelegate#shouldInterceptContextMen
uDownload} |
| 92 * returns true only for ".dd" or ".dm" extensions with http/https scheme. | 45 * returns true only for ".dd" or ".dm" extensions with http/https scheme. |
| 93 */ | 46 */ |
| 94 @SmallTest | 47 @SmallTest |
| 95 @Feature({"Download"}) | 48 @Feature({"Download"}) |
| 96 public void testShouldInterceptContextMenuDownload() { | 49 public void testShouldInterceptContextMenuDownload() { |
| 97 Tab tab = new Tab(0, false, getActivity().getWindowAndroid()); | 50 Tab tab = new Tab(0, false, getActivity().getWindowAndroid()); |
| 98 ChromeDownloadDelegate delegate = | 51 ChromeDownloadDelegate delegate = |
| 99 new MockChromeDownloadDelegate(getInstrumentation().getTargetCon
text(), tab); | 52 new MockChromeDownloadDelegate(getInstrumentation().getTargetCon
text(), tab); |
| 100 assertFalse(delegate.shouldInterceptContextMenuDownload("file://test/tes
t.html")); | 53 assertFalse(delegate.shouldInterceptContextMenuDownload("file://test/tes
t.html")); |
| 101 assertFalse(delegate.shouldInterceptContextMenuDownload("http://test/tes
t.html")); | 54 assertFalse(delegate.shouldInterceptContextMenuDownload("http://test/tes
t.html")); |
| 102 assertFalse(delegate.shouldInterceptContextMenuDownload("ftp://test/test
.dm")); | 55 assertFalse(delegate.shouldInterceptContextMenuDownload("ftp://test/test
.dm")); |
| 103 assertFalse(delegate.shouldInterceptContextMenuDownload("data://test.dd"
)); | 56 assertFalse(delegate.shouldInterceptContextMenuDownload("data://test.dd"
)); |
| 104 assertFalse(delegate.shouldInterceptContextMenuDownload("http://test.dd"
)); | 57 assertFalse(delegate.shouldInterceptContextMenuDownload("http://test.dd"
)); |
| 105 assertFalse(delegate.shouldInterceptContextMenuDownload("http://test/tes
t.dd")); | 58 assertFalse(delegate.shouldInterceptContextMenuDownload("http://test/tes
t.dd")); |
| 106 assertTrue(delegate.shouldInterceptContextMenuDownload("https://test/tes
t.dm")); | 59 assertTrue(delegate.shouldInterceptContextMenuDownload("https://test/tes
t.dm")); |
| 107 } | 60 } |
| 108 } | 61 } |
| OLD | NEW |