1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package android.telephony.embms.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertNotSame; 22 import static org.junit.Assert.fail; 23 24 import android.content.Intent; 25 import android.os.Bundle; 26 import android.telephony.MbmsDownloadSession; 27 import android.telephony.cts.embmstestapp.CtsDownloadService; 28 import android.telephony.mbms.DownloadRequest; 29 import android.telephony.mbms.FileServiceInfo; 30 import android.telephony.mbms.MbmsErrors; 31 32 import java.io.File; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.List; 36 import org.junit.Test; 37 38 public class MbmsDownloadSessionTest extends MbmsDownloadTestBase { 39 40 @Test 41 public void testDuplicateSession() throws Exception { 42 try { 43 MbmsDownloadSession failure = MbmsDownloadSession.create( 44 mContext, mCallbackExecutor, mCallback); 45 fail("Duplicate create should've thrown an exception"); 46 } catch (IllegalStateException e) { 47 // Succeed 48 } 49 } 50 51 @Test 52 public void testRequestUpdateDownloadServices() throws Exception { 53 List<String> testClasses = Arrays.asList("class1", "class2"); 54 mDownloadSession.requestUpdateFileServices(testClasses); 55 56 // Make sure we got the streaming services 57 List<FileServiceInfo> serviceInfos = 58 (List<FileServiceInfo>) mCallback.waitOnFileServicesUpdated().arg1; 59 assertEquals(CtsDownloadService.FILE_SERVICE_INFO, serviceInfos.get(0)); 60 assertEquals(0, mCallback.getNumErrorCalls()); 61 62 // Make sure the middleware got the call with the right args 63 List<Bundle> requestDownloadServicesCalls = 64 getMiddlewareCalls(CtsDownloadService.METHOD_REQUEST_UPDATE_FILE_SERVICES); 65 assertEquals(1, requestDownloadServicesCalls.size()); 66 List<String> middlewareReceivedServiceClasses = 67 requestDownloadServicesCalls.get(0) 68 .getStringArrayList(CtsDownloadService.ARGUMENT_SERVICE_CLASSES); 69 assertEquals(testClasses.size(), middlewareReceivedServiceClasses.size()); 70 for (int i = 0; i < testClasses.size(); i++) { 71 assertEquals(testClasses.get(i), middlewareReceivedServiceClasses.get(i)); 72 } 73 } 74 75 @Test 76 public void testClose() throws Exception { 77 mDownloadSession.close(); 78 79 // Make sure we can't use it anymore 80 try { 81 mDownloadSession.requestUpdateFileServices(Collections.emptyList()); 82 fail("Download session should not be usable after close"); 83 } catch (IllegalStateException e) { 84 // Succeed 85 } 86 87 // Make sure that the middleware got the call to close 88 List<Bundle> closeCalls = getMiddlewareCalls(CtsDownloadService.METHOD_CLOSE); 89 assertEquals(1, closeCalls.size()); 90 } 91 92 @Test 93 public void testSetTempFileDirectory() throws Exception { 94 String tempFileDirName = "CTSTestDir"; 95 File tempFileRootDirectory = new File(mContext.getFilesDir(), tempFileDirName); 96 tempFileRootDirectory.mkdirs(); 97 98 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 99 List<Bundle> setTempRootCalls = 100 getMiddlewareCalls(CtsDownloadService.METHOD_SET_TEMP_FILE_ROOT); 101 assertEquals(1, setTempRootCalls.size()); 102 assertEquals(tempFileRootDirectory.getCanonicalPath(), 103 setTempRootCalls.get(0).getString(CtsDownloadService.ARGUMENT_ROOT_DIRECTORY_PATH)); 104 tempFileRootDirectory.delete(); 105 } 106 107 @Test 108 public void testSetInvalidTempFileRoot() throws Exception { 109 File tempFileRootDirectory = new File(mContext.getFilesDir(), "NNN-DoesNotExist"); 110 tempFileRootDirectory.delete(); 111 112 try { 113 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 114 fail("Should not be able to set temp file root to non-existent directory"); 115 } catch (IllegalArgumentException e) { 116 // success 117 } 118 119 tempFileRootDirectory = new File(mContext.getFilesDir(), "this-is-a-file.dat"); 120 tempFileRootDirectory.createNewFile(); 121 122 try { 123 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 124 fail("Should not be able to set temp file root to a file"); 125 } catch (IllegalArgumentException e) { 126 // success 127 } 128 129 try { 130 mDownloadSession.setTempFileRootDirectory(mContext.getFilesDir()); 131 fail("Should not be able to set temp file root to the files dir"); 132 } catch (IllegalArgumentException e) { 133 // success 134 } 135 136 try { 137 mDownloadSession.setTempFileRootDirectory(mContext.getCacheDir()); 138 fail("Should not be able to set temp file root to the cache dir"); 139 } catch (IllegalArgumentException e) { 140 // success 141 } 142 143 try { 144 mDownloadSession.setTempFileRootDirectory(mContext.getDataDir()); 145 fail("Should not be able to set temp file root to the data dir"); 146 } catch (IllegalArgumentException e) { 147 // success 148 } 149 150 tempFileRootDirectory.delete(); 151 } 152 153 @Test 154 public void testResetDownloadKnowledge() throws Exception { 155 DownloadRequest request = downloadRequestTemplate.build(); 156 mDownloadSession.resetDownloadKnowledge(request); 157 158 List<Bundle> resetDownloadKnowledgeCalls = 159 getMiddlewareCalls(CtsDownloadService.METHOD_RESET_DOWNLOAD_KNOWLEDGE); 160 assertEquals(1, resetDownloadKnowledgeCalls.size()); 161 assertEquals(request, resetDownloadKnowledgeCalls.get(0).getParcelable( 162 CtsDownloadService.ARGUMENT_DOWNLOAD_REQUEST)); 163 } 164 165 @Test 166 public void testGetDownloadStatus() throws Exception { 167 DownloadRequest request = downloadRequestTemplate.build(); 168 mDownloadSession.requestDownloadState(request, CtsDownloadService.FILE_INFO_1); 169 170 List<Bundle> getDownloadStatusCalls = 171 getMiddlewareCalls(CtsDownloadService.METHOD_GET_DOWNLOAD_STATUS); 172 assertEquals(1, getDownloadStatusCalls.size()); 173 assertEquals(request, getDownloadStatusCalls.get(0).getParcelable( 174 CtsDownloadService.ARGUMENT_DOWNLOAD_REQUEST)); 175 assertEquals(CtsDownloadService.FILE_INFO_1, getDownloadStatusCalls.get(0).getParcelable( 176 CtsDownloadService.ARGUMENT_FILE_INFO)); 177 } 178 179 @Test 180 public void testCancelDownload() throws Exception { 181 DownloadRequest request = downloadRequestTemplate.build(); 182 mDownloadSession.cancelDownload(request); 183 184 List<Bundle> cancelDownloadCalls = 185 getMiddlewareCalls(CtsDownloadService.METHOD_CANCEL_DOWNLOAD); 186 assertEquals(1, cancelDownloadCalls.size()); 187 assertEquals(request, cancelDownloadCalls.get(0).getParcelable( 188 CtsDownloadService.ARGUMENT_DOWNLOAD_REQUEST)); 189 } 190 191 @Test 192 public void testListPendingDownloads() throws Exception { 193 File tempFileRootDir = new File(mContext.getFilesDir(), "CtsTestDir"); 194 tempFileRootDir.mkdir(); 195 mDownloadSession.setTempFileRootDirectory(tempFileRootDir); 196 197 DownloadRequest request = downloadRequestTemplate.setAppIntent(new Intent()).build(); 198 mDownloadSession.download(request); 199 200 List<DownloadRequest> downloads = mDownloadSession.listPendingDownloads(); 201 assertEquals(1, downloads.size()); 202 assertEquals(request, downloads.get(0)); 203 } 204 205 @Test 206 public void testSetTempFileDirFailure() throws Exception { 207 String tempFileDirName = "CTSTestDir101010"; 208 File tempFileRootDirectory = new File(mContext.getFilesDir(), tempFileDirName); 209 tempFileRootDirectory.mkdirs(); 210 211 mMiddlewareControl.forceErrorCode( 212 MbmsErrors.DownloadErrors.ERROR_CANNOT_CHANGE_TEMP_FILE_ROOT); 213 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 214 assertNotNull(mCallback.waitOnError()); 215 assertNotSame(mDownloadSession.getTempFileRootDirectory(), tempFileDirName); 216 } 217 218 @Test 219 public void testDownloadRequestSerialization() throws Exception { 220 Intent intent = new Intent("sample_intent_action"); 221 DownloadRequest request = downloadRequestTemplate.setAppIntent(intent).build(); 222 DownloadRequest newRequest = 223 DownloadRequest.Builder.fromSerializedRequest(request.toByteArray()) 224 .build(); 225 assertEquals(request, newRequest); 226 } 227 228 @Test 229 public void testErrorDelivery() throws Exception { 230 mMiddlewareControl.forceErrorCode( 231 MbmsErrors.GeneralErrors.ERROR_MIDDLEWARE_TEMPORARILY_UNAVAILABLE); 232 mDownloadSession.requestUpdateFileServices(Collections.emptyList()); 233 assertEquals(MbmsErrors.GeneralErrors.ERROR_MIDDLEWARE_TEMPORARILY_UNAVAILABLE, 234 mCallback.waitOnError().arg1); 235 } 236 } 237