Home | History | Annotate | Download | only in cts
      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.net.Uri;
     21 import android.os.Bundle;
     22 import android.telephony.MbmsDownloadSession;
     23 import android.telephony.cts.embmstestapp.CtsDownloadService;
     24 import android.telephony.mbms.DownloadRequest;
     25 import android.telephony.mbms.FileInfo;
     26 import android.telephony.mbms.MbmsDownloadReceiver;
     27 
     28 import java.io.File;
     29 import java.io.InputStream;
     30 import java.util.Collections;
     31 import java.util.List;
     32 
     33 public class MbmsDownloadFlowTest extends MbmsDownloadTestBase {
     34     private File tempFileRootDir;
     35 
     36     @Override
     37     public void setUp() throws Exception {
     38         super.setUp();
     39         tempFileRootDir = new File(mContext.getFilesDir(), "CtsTestDir");
     40         tempFileRootDir.mkdir();
     41         mDownloadSession.setTempFileRootDirectory(tempFileRootDir);
     42     }
     43 
     44     @Override
     45     public void tearDown() throws Exception {
     46         recursiveDelete(tempFileRootDir);
     47         tempFileRootDir = null;
     48         super.tearDown();
     49     }
     50 
     51     public void testSingleFileDownloadFlow() throws Exception {
     52         MbmsDownloadReceiverTest.AppIntentCapture captor =
     53                 new MbmsDownloadReceiverTest.AppIntentCapture(mContext, mHandler);
     54         DownloadRequest request = downloadRequestTemplate
     55                 .setAppIntent(new Intent(MbmsDownloadReceiverTest.APP_INTENT_ACTION))
     56                 .build();
     57         mDownloadSession.download(request);
     58         mMiddlewareControl.actuallyStartDownloadFlow();
     59 
     60         Uri fileUri = checkReceivedDownloadCompleteIntent(captor.getIntent(), request,
     61                 CtsDownloadService.FILE_INFO_1);
     62         checkFileContentIntegrity(CtsDownloadService.FILE_INFO_1, fileUri);
     63         checkDownloadResultAck(1);
     64     }
     65 
     66     public void testFileInSubdirectoryDownloadFlow() throws Exception {
     67         MbmsDownloadReceiverTest.AppIntentCapture captor =
     68                 new MbmsDownloadReceiverTest.AppIntentCapture(mContext, mHandler);
     69         DownloadRequest request = new DownloadRequest.Builder(
     70                 CtsDownloadService.SOURCE_URI_2, destinationDirectoryUri)
     71                 .setServiceInfo(CtsDownloadService.FILE_SERVICE_INFO)
     72                 .setAppIntent(new Intent(MbmsDownloadReceiverTest.APP_INTENT_ACTION))
     73                 .build();
     74 
     75         mDownloadSession.download(request);
     76         mMiddlewareControl.actuallyStartDownloadFlow();
     77 
     78         Uri fileUri = checkReceivedDownloadCompleteIntent(captor.getIntent(), request,
     79                 CtsDownloadService.FILE_INFO_2);
     80         // Make sure that the received file is placed in the proper subdirectory.
     81         String file2RelativePath = CtsDownloadService.FILE_INFO_2.getUri().getPath().substring(
     82                 CtsDownloadService.SOURCE_URI_2.getPath().length());
     83         assertTrue("got path: " + fileUri.getPath() + ", should end with: " + file2RelativePath,
     84                 fileUri.getPath().endsWith(file2RelativePath));
     85         checkFileContentIntegrity(CtsDownloadService.FILE_INFO_2, fileUri);
     86         checkDownloadResultAck(1);
     87     }
     88 
     89     public void testMultiFileDownloadFlow() throws Exception {
     90         MbmsDownloadReceiverTest.AppIntentCapture captor =
     91                 new MbmsDownloadReceiverTest.AppIntentCapture(mContext, mHandler);
     92         DownloadRequest request = new DownloadRequest.Builder(
     93                 CtsDownloadService.SOURCE_URI_3, destinationDirectoryUri)
     94                 .setServiceInfo(CtsDownloadService.FILE_SERVICE_INFO)
     95                 .setAppIntent(new Intent(MbmsDownloadReceiverTest.APP_INTENT_ACTION))
     96                 .build();
     97 
     98         mDownloadSession.download(request);
     99         mMiddlewareControl.actuallyStartDownloadFlow();
    100 
    101         for (Intent i : captor.getIntents(2)) {
    102             FileInfo fileInfo = i.getParcelableExtra(MbmsDownloadSession.EXTRA_MBMS_FILE_INFO);
    103             Uri fileUri = null;
    104             if (CtsDownloadService.FILE_INFO_1.equals(fileInfo)) {
    105                 fileUri = checkReceivedDownloadCompleteIntent(
    106                         i, request, CtsDownloadService.FILE_INFO_1);
    107             } else if (CtsDownloadService.FILE_INFO_2.equals(fileInfo)) {
    108                 fileUri = checkReceivedDownloadCompleteIntent(
    109                         i, request, CtsDownloadService.FILE_INFO_2);
    110             } else {
    111                 fail("Got unknown file info: " + fileInfo);
    112             }
    113             String relativePath = fileInfo.getUri().getPath().substring(
    114                     CtsDownloadService.SOURCE_URI_3.getPath().length());
    115             assertTrue("got path: " + fileUri.getPath() + ", should end with: " + relativePath,
    116                     fileUri.getPath().endsWith(relativePath));
    117             checkFileContentIntegrity(fileInfo, fileUri);
    118         }
    119         checkDownloadResultAck(2);
    120     }
    121 
    122 
    123     private Uri checkReceivedDownloadCompleteIntent(Intent downloadDoneIntent,
    124             DownloadRequest downloadRequest, FileInfo expectedFileInfo) {
    125         assertEquals(MbmsDownloadReceiverTest.APP_INTENT_ACTION, downloadDoneIntent.getAction());
    126         assertEquals(MbmsDownloadSession.RESULT_SUCCESSFUL,
    127                 downloadDoneIntent.getIntExtra(MbmsDownloadSession.EXTRA_MBMS_DOWNLOAD_RESULT, -1));
    128         assertEquals(downloadRequest,
    129                 downloadDoneIntent.getParcelableExtra(
    130                         MbmsDownloadSession.EXTRA_MBMS_DOWNLOAD_REQUEST));
    131         assertEquals(downloadRequest.getSubscriptionId(),
    132                 ((DownloadRequest) downloadDoneIntent.getParcelableExtra(
    133                         MbmsDownloadSession.EXTRA_MBMS_DOWNLOAD_REQUEST)).getSubscriptionId());
    134         assertEquals(downloadRequest.getDestinationUri(),
    135                 ((DownloadRequest) downloadDoneIntent.getParcelableExtra(
    136                         MbmsDownloadSession.EXTRA_MBMS_DOWNLOAD_REQUEST)).getDestinationUri());
    137         assertEquals(expectedFileInfo,
    138                 downloadDoneIntent.getParcelableExtra(MbmsDownloadSession.EXTRA_MBMS_FILE_INFO));
    139         return downloadDoneIntent.getParcelableExtra(
    140                 MbmsDownloadSession.EXTRA_MBMS_COMPLETED_FILE_URI);
    141     }
    142 
    143     private void checkFileContentIntegrity(FileInfo fileInfo, Uri completedFileUri)
    144             throws Exception {
    145         assertEquals(fileInfo.getUri().getLastPathSegment(),
    146                 completedFileUri.getLastPathSegment());
    147         InputStream is = mContext.getContentResolver().openInputStream(completedFileUri);
    148         byte[] contents = new byte[CtsDownloadService.SAMPLE_FILE_DATA.length];
    149         is.read(contents);
    150         for (int i = 0; i < contents.length; i++) {
    151             assertEquals(contents[i], CtsDownloadService.SAMPLE_FILE_DATA[i]);
    152         }
    153     }
    154 
    155     private void checkDownloadResultAck(int numAcks) throws Exception {
    156         // Poll until we get to the right number.
    157         List<Bundle> downloadResultAck = Collections.emptyList();
    158         long currentTime = System.currentTimeMillis();
    159         while (System.currentTimeMillis() < currentTime + ASYNC_TIMEOUT) {
    160             downloadResultAck = getMiddlewareCalls(CtsDownloadService.METHOD_DOWNLOAD_RESULT_ACK);
    161             if (numAcks == downloadResultAck.size()) {
    162                 break;
    163             }
    164             Thread.sleep(200);
    165         }
    166         assertEquals(numAcks, downloadResultAck.size());
    167         downloadResultAck.forEach((ack) -> assertEquals(MbmsDownloadReceiver.RESULT_OK,
    168                 ack.getInt(CtsDownloadService.ARGUMENT_RESULT_CODE, -1)));
    169     }
    170 }
    171