1 /*
2  * Copyright (C) 2016 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.documentsui.base;
18 
19 
20 import static org.junit.Assert.assertArrayEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.content.Intent;
25 
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 @RunWith(AndroidJUnit4.class)
34 @SmallTest
35 public class StateTest {
36 
37     private static final String[] MIME_TYPES = { "image/gif", "image/jpg" };
38 
39     private Intent mIntent;
40     private State mState;
41 
42     @Before
setUp()43     public void setUp() {
44         mIntent = new Intent();
45         mState = new State();
46     }
47 
48     @Test
testAcceptGivenMimeTypesInExtra()49     public void testAcceptGivenMimeTypesInExtra() {
50         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, MIME_TYPES);
51 
52         mState.initAcceptMimes(mIntent, "*/*");
53 
54         assertArrayEquals(MIME_TYPES, mState.acceptMimes);
55     }
56 
57     @Test
testAcceptIntentTypeWithoutExtra()58     public void testAcceptIntentTypeWithoutExtra() {
59         mState.initAcceptMimes(mIntent, MIME_TYPES[0]);
60 
61         assertArrayEquals(new String[] { MIME_TYPES[0] }, mState.acceptMimes);
62     }
63 
64     @Test
testShouldShowPreview_actionBrowse()65     public void testShouldShowPreview_actionBrowse() {
66         mState.action = State.ACTION_BROWSE;
67 
68         assertFalse(mState.shouldShowPreview());
69     }
70 
71     @Test
testShouldShowPreview_actionOpen()72     public void testShouldShowPreview_actionOpen() {
73         mState.action = State.ACTION_OPEN;
74 
75         assertTrue(mState.shouldShowPreview());
76     }
77 
78     @Test
testShouldShowPreview_actionGetContent()79     public void testShouldShowPreview_actionGetContent() {
80         mState.action = State.ACTION_GET_CONTENT;
81 
82         assertTrue(mState.shouldShowPreview());
83     }
84 
85     @Test
testShouldShowPreview_actionOpenTree()86     public void testShouldShowPreview_actionOpenTree() {
87         mState.action = State.ACTION_OPEN_TREE;
88 
89         assertTrue(mState.shouldShowPreview());
90     }
91 
92     @Test
testPhotoPicking_onlyOneImageType()93     public void testPhotoPicking_onlyOneImageType() {
94         String[] stringArray = { MIME_TYPES[0] };
95         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, stringArray);
96 
97         mState.initAcceptMimes(mIntent, "*/*");
98         mState.action = State.ACTION_GET_CONTENT;
99 
100         assertTrue(mState.isPhotoPicking());
101     }
102 
103     @Test
testPhotoPicking_allImageTypes()104     public void testPhotoPicking_allImageTypes() {
105         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, MIME_TYPES);
106         mState.initAcceptMimes(mIntent, "*/*");
107         mState.action = State.ACTION_GET_CONTENT;
108 
109         assertTrue(mState.isPhotoPicking());
110     }
111 
112     @Test
testPhotoPicking_noImageType()113     public void testPhotoPicking_noImageType() {
114         final String[] mimeTypes = { "audio/mp3", "video/mp4" };
115         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
116         mState.initAcceptMimes(mIntent, "*/*");
117         mState.action = State.ACTION_GET_CONTENT;
118 
119         assertFalse(mState.isPhotoPicking());
120     }
121 
122     @Test
testPhotoPicking_oneIsNotImageType()123     public void testPhotoPicking_oneIsNotImageType() {
124         final String[] mimeTypes = { "image/gif", "image/jpg", "audio/mp3" };
125         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
126         mState.initAcceptMimes(mIntent, "*/*");
127         mState.action = State.ACTION_GET_CONTENT;
128 
129         assertFalse(mState.isPhotoPicking());
130     }
131 
132     @Test
testPhotoPicking_browseMode()133     public void testPhotoPicking_browseMode() {
134         mState.initAcceptMimes(mIntent, "*/*");
135         mState.action = State.ACTION_BROWSE;
136 
137         assertFalse(mState.isPhotoPicking());
138     }
139 
140     @Test
testPhotoPicking_nullExrta()141     public void testPhotoPicking_nullExrta() {
142         final String[] mimeTypes = null;
143         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
144         mState.initAcceptMimes(mIntent, "*/*");
145         mState.action = State.ACTION_GET_CONTENT;
146 
147         assertFalse(mState.isPhotoPicking());
148     }
149 }
150