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