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