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 android.content.Intent; 20 import android.os.Bundle; 21 import android.telephony.MbmsDownloadSession; 22 import android.telephony.cts.embmstestapp.CtsDownloadService; 23 import android.telephony.mbms.DownloadRequest; 24 import android.telephony.mbms.FileServiceInfo; 25 import android.telephony.mbms.MbmsErrors; 26 27 import java.io.File; 28 import java.util.Arrays; 29 import java.util.Collections; 30 import java.util.List; 31 32 public class MbmsDownloadSessionTest extends MbmsDownloadTestBase { 33 public void testDuplicateSession() throws Exception { 34 try { 35 MbmsDownloadSession failure = MbmsDownloadSession.create( 36 mContext, mCallbackExecutor, mCallback); 37 fail("Duplicate create should've thrown an exception"); 38 } catch (IllegalStateException e) { 39 // Succeed 40 } 41 } 42 43 public void testRequestUpdateDownloadServices() throws Exception { 44 List<String> testClasses = Arrays.asList("class1", "class2"); 45 mDownloadSession.requestUpdateFileServices(testClasses); 46 47 // Make sure we got the streaming services 48 List<FileServiceInfo> serviceInfos = 49 (List<FileServiceInfo>) mCallback.waitOnFileServicesUpdated().arg1; 50 assertEquals(CtsDownloadService.FILE_SERVICE_INFO, serviceInfos.get(0)); 51 assertEquals(0, mCallback.getNumErrorCalls()); 52 53 // Make sure the middleware got the call with the right args 54 List<Bundle> requestDownloadServicesCalls = 55 getMiddlewareCalls(CtsDownloadService.METHOD_REQUEST_UPDATE_FILE_SERVICES); 56 assertEquals(1, requestDownloadServicesCalls.size()); 57 List<String> middlewareReceivedServiceClasses = 58 requestDownloadServicesCalls.get(0) 59 .getStringArrayList(CtsDownloadService.ARGUMENT_SERVICE_CLASSES); 60 assertEquals(testClasses.size(), middlewareReceivedServiceClasses.size()); 61 for (int i = 0; i < testClasses.size(); i++) { 62 assertEquals(testClasses.get(i), middlewareReceivedServiceClasses.get(i)); 63 } 64 } 65 66 public void testClose() throws Exception { 67 mDownloadSession.close(); 68 69 // Make sure we can't use it anymore 70 try { 71 mDownloadSession.requestUpdateFileServices(Collections.emptyList()); 72 fail("Download session should not be usable after close"); 73 } catch (IllegalStateException e) { 74 // Succeed 75 } 76 77 // Make sure that the middleware got the call to close 78 List<Bundle> closeCalls = getMiddlewareCalls(CtsDownloadService.METHOD_CLOSE); 79 assertEquals(1, closeCalls.size()); 80 } 81 82 public void testSetTempFileDirectory() throws Exception { 83 String tempFileDirName = "CTSTestDir"; 84 File tempFileRootDirectory = new File(mContext.getFilesDir(), tempFileDirName); 85 tempFileRootDirectory.mkdirs(); 86 87 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 88 List<Bundle> setTempRootCalls = 89 getMiddlewareCalls(CtsDownloadService.METHOD_SET_TEMP_FILE_ROOT); 90 assertEquals(1, setTempRootCalls.size()); 91 assertEquals(tempFileRootDirectory.getCanonicalPath(), 92 setTempRootCalls.get(0).getString(CtsDownloadService.ARGUMENT_ROOT_DIRECTORY_PATH)); 93 tempFileRootDirectory.delete(); 94 } 95 96 public void testSetInvalidTempFileRoot() throws Exception { 97 File tempFileRootDirectory = new File(mContext.getFilesDir(), "NNN-DoesNotExist"); 98 tempFileRootDirectory.delete(); 99 100 try { 101 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 102 fail("Should not be able to set temp file root to non-existent directory"); 103 } catch (IllegalArgumentException e) { 104 // success 105 } 106 107 tempFileRootDirectory = new File(mContext.getFilesDir(), "this-is-a-file.dat"); 108 tempFileRootDirectory.createNewFile(); 109 110 try { 111 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 112 fail("Should not be able to set temp file root to a file"); 113 } catch (IllegalArgumentException e) { 114 // success 115 } 116 117 try { 118 mDownloadSession.setTempFileRootDirectory(mContext.getFilesDir()); 119 fail("Should not be able to set temp file root to the files dir"); 120 } catch (IllegalArgumentException e) { 121 // success 122 } 123 124 try { 125 mDownloadSession.setTempFileRootDirectory(mContext.getCacheDir()); 126 fail("Should not be able to set temp file root to the cache dir"); 127 } catch (IllegalArgumentException e) { 128 // success 129 } 130 131 try { 132 mDownloadSession.setTempFileRootDirectory(mContext.getDataDir()); 133 fail("Should not be able to set temp file root to the data dir"); 134 } catch (IllegalArgumentException e) { 135 // success 136 } 137 138 tempFileRootDirectory.delete(); 139 } 140 141 public void testResetDownloadKnowledge() throws Exception { 142 DownloadRequest request = downloadRequestTemplate.build(); 143 mDownloadSession.resetDownloadKnowledge(request); 144 145 List<Bundle> resetDownloadKnowledgeCalls = 146 getMiddlewareCalls(CtsDownloadService.METHOD_RESET_DOWNLOAD_KNOWLEDGE); 147 assertEquals(1, resetDownloadKnowledgeCalls.size()); 148 assertEquals(request, resetDownloadKnowledgeCalls.get(0).getParcelable( 149 CtsDownloadService.ARGUMENT_DOWNLOAD_REQUEST)); 150 } 151 152 public void testGetDownloadStatus() throws Exception { 153 DownloadRequest request = downloadRequestTemplate.build(); 154 mDownloadSession.requestDownloadState(request, CtsDownloadService.FILE_INFO_1); 155 156 List<Bundle> getDownloadStatusCalls = 157 getMiddlewareCalls(CtsDownloadService.METHOD_GET_DOWNLOAD_STATUS); 158 assertEquals(1, getDownloadStatusCalls.size()); 159 assertEquals(request, getDownloadStatusCalls.get(0).getParcelable( 160 CtsDownloadService.ARGUMENT_DOWNLOAD_REQUEST)); 161 assertEquals(CtsDownloadService.FILE_INFO_1, getDownloadStatusCalls.get(0).getParcelable( 162 CtsDownloadService.ARGUMENT_FILE_INFO)); 163 } 164 165 public void testCancelDownload() throws Exception { 166 DownloadRequest request = downloadRequestTemplate.build(); 167 mDownloadSession.cancelDownload(request); 168 169 List<Bundle> cancelDownloadCalls = 170 getMiddlewareCalls(CtsDownloadService.METHOD_CANCEL_DOWNLOAD); 171 assertEquals(1, cancelDownloadCalls.size()); 172 assertEquals(request, cancelDownloadCalls.get(0).getParcelable( 173 CtsDownloadService.ARGUMENT_DOWNLOAD_REQUEST)); 174 } 175 176 public void testListPendingDownloads() throws Exception { 177 File tempFileRootDir = new File(mContext.getFilesDir(), "CtsTestDir"); 178 tempFileRootDir.mkdir(); 179 mDownloadSession.setTempFileRootDirectory(tempFileRootDir); 180 181 DownloadRequest request = downloadRequestTemplate.setAppIntent(new Intent()).build(); 182 mDownloadSession.download(request); 183 184 List<DownloadRequest> downloads = mDownloadSession.listPendingDownloads(); 185 assertEquals(1, downloads.size()); 186 assertEquals(request, downloads.get(0)); 187 } 188 189 public void testSetTempFileDirFailure() throws Exception { 190 String tempFileDirName = "CTSTestDir101010"; 191 File tempFileRootDirectory = new File(mContext.getFilesDir(), tempFileDirName); 192 tempFileRootDirectory.mkdirs(); 193 194 mMiddlewareControl.forceErrorCode( 195 MbmsErrors.DownloadErrors.ERROR_CANNOT_CHANGE_TEMP_FILE_ROOT); 196 mDownloadSession.setTempFileRootDirectory(tempFileRootDirectory); 197 assertNotNull(mCallback.waitOnError()); 198 assertNotSame(mDownloadSession.getTempFileRootDirectory(), tempFileDirName); 199 } 200 201 public void testDownloadRequestSerialization() throws Exception { 202 Intent intent = new Intent("sample_intent_action"); 203 DownloadRequest request = downloadRequestTemplate.setAppIntent(intent).build(); 204 DownloadRequest newRequest = 205 DownloadRequest.Builder.fromSerializedRequest(request.toByteArray()) 206 .build(); 207 assertEquals(request, newRequest); 208 } 209 210 public void testErrorDelivery() throws Exception { 211 mMiddlewareControl.forceErrorCode( 212 MbmsErrors.GeneralErrors.ERROR_MIDDLEWARE_TEMPORARILY_UNAVAILABLE); 213 mDownloadSession.requestUpdateFileServices(Collections.emptyList()); 214 assertEquals(MbmsErrors.GeneralErrors.ERROR_MIDDLEWARE_TEMPORARILY_UNAVAILABLE, 215 mCallback.waitOnError().arg1); 216 } 217 } 218