• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
testPhotoPicking_onlyOneImageType()92     public void testPhotoPicking_onlyOneImageType() {
93         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, MIME_TYPES[0]);
94         mState.initAcceptMimes(mIntent, "*/*");
95         mState.action = State.ACTION_GET_CONTENT;
96 
97         assertTrue(mState.isPhotoPicking());
98     }
99 
100     @Test
testPhotoPicking_allImageTypes()101     public void testPhotoPicking_allImageTypes() {
102         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, MIME_TYPES);
103         mState.initAcceptMimes(mIntent, "*/*");
104         mState.action = State.ACTION_GET_CONTENT;
105 
106         assertTrue(mState.isPhotoPicking());
107     }
108 
109     @Test
testPhotoPicking_noImageType()110     public void testPhotoPicking_noImageType() {
111         final String[] mimeTypes = { "audio/mp3", "video/mp4" };
112         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
113         mState.initAcceptMimes(mIntent, "*/*");
114         mState.action = State.ACTION_GET_CONTENT;
115 
116         assertFalse(mState.isPhotoPicking());
117     }
118 
119     @Test
testPhotoPicking_oneIsNotImageType()120     public void testPhotoPicking_oneIsNotImageType() {
121         final String[] mimeTypes = { "image/gif", "image/jpg", "audio/mp3" };
122         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
123         mState.initAcceptMimes(mIntent, "*/*");
124         mState.action = State.ACTION_GET_CONTENT;
125 
126         assertFalse(mState.isPhotoPicking());
127     }
128 
129     @Test
testPhotoPicking_browseMode()130     public void testPhotoPicking_browseMode() {
131         mState.initAcceptMimes(mIntent, "*/*");
132         mState.action = State.ACTION_BROWSE;
133 
134         assertFalse(mState.isPhotoPicking());
135     }
136 
137     @Test
testPhotoPicking_nullExrta()138     public void testPhotoPicking_nullExrta() {
139         final String[] mimeTypes = null;
140         mIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
141         mState.initAcceptMimes(mIntent, "*/*");
142         mState.action = State.ACTION_GET_CONTENT;
143 
144         assertFalse(mState.isPhotoPicking());
145     }
146 }
147