Home | History | Annotate | Download | only in opp
      1 /*
      2  * Copyright 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 com.android.bluetooth.opp;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import static org.mockito.ArgumentMatchers.any;
     22 import static org.mockito.ArgumentMatchers.eq;
     23 import static org.mockito.Mockito.doReturn;
     24 
     25 import android.content.ContentResolver;
     26 import android.content.Context;
     27 import android.net.Uri;
     28 
     29 import com.android.bluetooth.TestConfig;
     30 
     31 import java.io.FileInputStream;
     32 import java.io.IOException;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.mockito.Mock;
     38 import org.mockito.MockitoAnnotations;
     39 import org.robolectric.RobolectricTestRunner;
     40 import org.robolectric.annotation.Config;
     41 
     42 @RunWith(RobolectricTestRunner.class)
     43 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     44 public class OppSendFileInfoTest {
     45 
     46     // Constants for test file size.
     47     private static final int TEST_FILE_SIZE = 10;
     48     private static final int MAXIMUM_FILE_SIZE = 0xFFFFFFFF;
     49 
     50     @Mock Context mContext;
     51     @Mock ContentResolver mContentResolver;
     52     @Mock FileInputStream mFileInputStream;
     53 
     54     @Before
     55     public void setUp() {
     56         MockitoAnnotations.initMocks(this);
     57     }
     58 
     59     /**
     60      * Test a BluetoothOppSendFileInfo generated from a local file (MIME type: text/plain,
     61      * size: #TEST_FILE_SIZE).
     62      * Check whether the BluetoothOppSendFileInfo matches the input.
     63      */
     64     @Test
     65     public void testFileOpen() throws IOException {
     66         Uri uri = Uri.parse("file:///android_asset/opp/OppTestFile.txt");
     67         doReturn(mContentResolver).when(mContext).getContentResolver();
     68         doReturn(mFileInputStream).when(mContentResolver).openInputStream(uri);
     69         doReturn(TEST_FILE_SIZE, -1).when(mFileInputStream).read(any(), eq(0), eq(4096));
     70         BluetoothOppSendFileInfo sendFileInfo = BluetoothOppSendFileInfo.generateFileInfo(
     71                 mContext, uri, "text/plain", false);
     72         assertThat(sendFileInfo).isNotEqualTo(BluetoothOppSendFileInfo.SEND_FILE_INFO_ERROR);
     73         assertThat(sendFileInfo.mFileName).isEqualTo("OppTestFile.txt");
     74         assertThat(sendFileInfo.mMimetype).isEqualTo("text/plain");
     75         assertThat(sendFileInfo.mLength).isEqualTo(TEST_FILE_SIZE);
     76         assertThat(sendFileInfo.mInputStream).isEqualTo(mFileInputStream);
     77         assertThat(sendFileInfo.mStatus).isEqualTo(0);
     78     }
     79 
     80     /**
     81      * Test a BluetoothOppSendFileInfo generated from a web page, which is not supported.
     82      * Should return an error BluetoothOppSendFileInfo.
     83      */
     84     @Test
     85     public void testInvalidUriScheme() {
     86         Uri webpage = Uri.parse("http://www.android.com/");
     87         BluetoothOppSendFileInfo sendFileInfo = BluetoothOppSendFileInfo.generateFileInfo(
     88                 mContext, webpage, "html", false);
     89         assertThat(sendFileInfo).isEqualTo(BluetoothOppSendFileInfo.SEND_FILE_INFO_ERROR);
     90     }
     91 
     92     /**
     93      * Test a BluetoothOppSendFileInfo generated from a big local file (MIME type: text/plain,
     94      * size: 8GB). It should return an error BluetoothOppSendFileInfo because the maximum size of
     95      * file supported is #MAXIMUM_FILE_SIZE (4GB).
     96      */
     97     @Test
     98     public void testBigFileOpen() throws IOException {
     99         Uri uri = Uri.parse("file:///android_asset/opp/OppTestFile.txt");
    100         doReturn(mContentResolver).when(mContext).getContentResolver();
    101         doReturn(mFileInputStream).when(mContentResolver).openInputStream(uri);
    102         doReturn(MAXIMUM_FILE_SIZE, MAXIMUM_FILE_SIZE, -1).when(mFileInputStream).read(any(),
    103                 eq(0), eq(4096));
    104         BluetoothOppSendFileInfo sendFileInfo = BluetoothOppSendFileInfo.generateFileInfo(
    105                 mContext, uri, "text/plain", false);
    106         assertThat(sendFileInfo).isEqualTo(BluetoothOppSendFileInfo.SEND_FILE_INFO_ERROR);
    107 
    108     }
    109 
    110 }
    111