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 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertFalse;
21 import static junit.framework.TestCase.assertNotNull;
22 import static junit.framework.TestCase.assertNull;
23 import static junit.framework.TestCase.assertTrue;
24 
25 import android.provider.DocumentsContract;
26 
27 import androidx.test.filters.SmallTest;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import com.android.documentsui.testing.Parcelables;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.util.Objects;
37 
38 @RunWith(AndroidJUnit4.class)
39 @SmallTest
40 public class DocumentStackTest {
41     private static final RootInfo ROOT_1;
42     private static final RootInfo ROOT_2;
43 
44     private static final DocumentInfo DIR_1;
45     private static final DocumentInfo DIR_2;
46 
47     private DocumentStack mStack;
48 
49     static {
50         ROOT_1 = new RootInfo();
51         ROOT_1.rootId = "home";
52         ROOT_2 = new RootInfo();
53         ROOT_2.rootId = "downloads";
54 
55         DIR_1 = createDir("first");
56         DIR_2 = createDir("second");
57     }
58 
createDir(String docId)59     private static DocumentInfo createDir(String docId) {
60         DocumentInfo info = new DocumentInfo();
61         info.authority = "authority";
62         info.documentId = docId;
63         info.displayName = docId;
64         info.mimeType = DocumentsContract.Document.MIME_TYPE_DIR;
65         info.deriveFields();
66         return info;
67     }
68 
69     @Before
setUp()70     public void setUp() {
71         mStack = new DocumentStack();
72     }
73 
74     @Test
testInitialStateEmpty()75     public void testInitialStateEmpty() {
76         assertFalse(mStack.hasLocationChanged());
77         assertTrue(mStack.isEmpty());
78         assertEquals(0, mStack.size());
79         assertNull(mStack.getRoot());
80     }
81 
82     @Test
testPushDocument_ModifiesStack()83     public void testPushDocument_ModifiesStack() {
84         mStack.push(DIR_1);
85         mStack.push(DIR_2);
86         assertEquals(DIR_2, mStack.peek());
87     }
88 
89     @Test
testPopDocument_ModifiesStack()90     public void testPopDocument_ModifiesStack() {
91         mStack.push(DIR_1);
92         mStack.push(DIR_2);
93         mStack.pop();
94         assertEquals(DIR_1, mStack.peek());
95     }
96 
97     @Test
testGetDocument()98     public void testGetDocument() {
99         mStack.push(DIR_1);
100         mStack.push(DIR_2);
101 
102         assertEquals(DIR_1, mStack.get(0));
103         assertEquals(DIR_2, mStack.get(1));
104     }
105 
106     @Test
testChangeRoot()107     public void testChangeRoot() {
108         mStack.changeRoot(ROOT_1);
109 
110         assertEquals(ROOT_1, mStack.getRoot());
111     }
112 
113     @Test
testChangeRoot_ClearsStack()114     public void testChangeRoot_ClearsStack() {
115         mStack.push(DIR_1);
116 
117         mStack.changeRoot(ROOT_1);
118 
119         assertTrue(mStack.isEmpty());
120         assertEquals(0, mStack.size());
121     }
122 
123     @Test
testReset()124     public void testReset() {
125         mStack.changeRoot(ROOT_1);
126         mStack.push(DIR_1);
127 
128         mStack.reset();
129 
130         assertNull(mStack.getRoot());
131         assertTrue(mStack.isEmpty());
132         assertEquals(0, mStack.size());
133     }
134 
135     @Test
testCopyConstructor()136     public void testCopyConstructor() {
137         mStack.changeRoot(ROOT_1);
138         mStack.push(DIR_1);
139         mStack.push(DIR_2);
140 
141         DocumentStack stack = new DocumentStack(mStack);
142 
143         assertEquals(2, stack.size());
144         assertEquals(DIR_1, stack.get(0));
145         assertEquals(DIR_2, stack.get(1));
146         assertEquals(ROOT_1, stack.getRoot());
147     }
148 
149     @Test
testCopyConstructor_MakesDeepCopy()150     public void testCopyConstructor_MakesDeepCopy() {
151         mStack.changeRoot(ROOT_1);
152         mStack.push(DIR_1);
153         mStack.push(DIR_2);
154 
155         DocumentStack stack = new DocumentStack(mStack);
156 
157         mStack.changeRoot(ROOT_2);
158 
159         assertEquals(2, stack.size());
160         assertEquals(DIR_1, stack.get(0));
161         assertEquals(DIR_2, stack.get(1));
162         assertEquals(ROOT_1, stack.getRoot());
163     }
164 
165     @Test
testPushDocument_ChangesLocation()166     public void testPushDocument_ChangesLocation() {
167         mStack.push(DIR_1);
168 
169         assertTrue(mStack.hasLocationChanged());
170     }
171 
172     @Test
testParceling()173     public void testParceling() {
174         mStack.changeRoot(ROOT_1);
175         mStack.push(DIR_1);
176         mStack.push(DIR_2);
177 
178         Parcelables.assertParcelable(mStack, 0, (DocumentStack left, DocumentStack right) -> {
179             if (!Objects.equals(left.getRoot(), right.getRoot()) || left.size() != right.size()) {
180                 return false;
181             }
182 
183             for (int i = 0; i < left.size(); ++i) {
184                 if (!Objects.equals(left.get(i), right.get(i))) {
185                     return false;
186                 }
187             }
188 
189             return true;
190         });
191     }
192 
193     @Test
testIsRecent()194     public void testIsRecent() {
195         final RootInfo rootRecent = new RootInfo();
196         mStack.changeRoot(rootRecent);
197 
198         assertEquals(1, mStack.size());
199         assertEquals(true, mStack.isRecents());
200         assertNotNull(mStack.peek().derivedUri);
201     }
202 }
203