Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a4a23b07 authored by Garfield Tan's avatar Garfield Tan
Browse files

Add unit tests for DocumentStack.

Bug: 32344918
Change-Id: I39b9fe897dc04332c0fd25ce4bc85e452076b540
parent 5a9d100f
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Nullable;

/**
 * Representation of a stack of {@link DocumentInfo}, usually the result of a
 * user-driven traversal.
@@ -45,7 +47,7 @@ public class DocumentStack implements Durable, Parcelable {
    private static final int VERSION_ADD_ROOT = 2;

    private LinkedList<DocumentInfo> mList;
    private RootInfo mRoot;
    private @Nullable RootInfo mRoot;

    private boolean mInitialRootChanged;
    private boolean mInitialDocChanged;
@@ -90,7 +92,7 @@ public class DocumentStack implements Durable, Parcelable {
        mRoot = src.mRoot;
    }

    public RootInfo getRoot() {
    public @Nullable RootInfo getRoot() {
        return mRoot;
    }

+107 −7
Original line number Diff line number Diff line
@@ -18,8 +18,10 @@ package com.android.documentsui.base;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertTrue;

import android.net.Uri;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

@@ -30,15 +32,25 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class DocumentStackTest {
    private static final RootInfo ROOT_1;
    private static final RootInfo ROOT_2;

    private static final DocumentInfo DIR_1;
    private static final DocumentInfo DIR_2;

    private DocumentStack mStack;

    static {
        ROOT_1 = new RootInfo();
        ROOT_1.rootId = "home";
        ROOT_2 = new RootInfo();
        ROOT_2.rootId = "downloads";

        DIR_1 = new DocumentInfo();
        DIR_1.derivedUri = Uri.parse("content://authority/document/firstId");
        DIR_1.displayName = "firstDirectory";
        DIR_2 = new DocumentInfo();
        DIR_2.derivedUri = Uri.parse("content://authority/document/secondId");
        DIR_2.displayName = "secondDirectory";
    }

@@ -50,13 +62,10 @@ public class DocumentStackTest {
    @Test
    public void testInitialStateEmpty() {
        assertFalse(mStack.hasLocationChanged());
    }

    @Test
    public void testPushDocument_ChangesLocation() {
        mStack.push(DIR_1);
        mStack.push(DIR_2);
        assertTrue(mStack.hasLocationChanged());
        assertFalse(mStack.hasInitialLocationChanged());
        assertTrue(mStack.isEmpty());
        assertEquals(0, mStack.size());
        assertNull(mStack.getRoot());
    }

    @Test
@@ -73,4 +82,95 @@ public class DocumentStackTest {
        mStack.pop();
        assertEquals(DIR_1, mStack.peek());
    }

    @Test
    public void testGetDocument() {
        mStack.push(DIR_1);
        mStack.push(DIR_2);

        assertEquals(DIR_1, mStack.get(0));
        assertEquals(DIR_2, mStack.get(1));
    }

    @Test
    public void testChangeRoot() {
        mStack.changeRoot(ROOT_1);

        assertEquals(ROOT_1, mStack.getRoot());
    }

    @Test
    public void testChangeRoot_ClearsStack() {
        mStack.push(DIR_1);

        mStack.changeRoot(ROOT_1);

        assertTrue(mStack.isEmpty());
        assertEquals(0, mStack.size());
    }

    @Test
    public void testReset() {
        mStack.changeRoot(ROOT_1);
        mStack.push(DIR_1);

        mStack.reset();

        assertNull(mStack.getRoot());
        assertTrue(mStack.isEmpty());
        assertEquals(0, mStack.size());
    }

    @Test
    public void testCopyConstructor() {
        mStack.changeRoot(ROOT_1);
        mStack.push(DIR_1);
        mStack.push(DIR_2);

        DocumentStack stack = new DocumentStack(mStack);

        assertEquals(2, stack.size());
        assertEquals(DIR_1, stack.get(0));
        assertEquals(DIR_2, stack.get(1));
        assertEquals(ROOT_1, stack.getRoot());
    }

    @Test
    public void testCopyConstructor_MakesDeepCopy() {
        mStack.changeRoot(ROOT_1);
        mStack.push(DIR_1);
        mStack.push(DIR_2);

        DocumentStack stack = new DocumentStack(mStack);

        mStack.changeRoot(ROOT_2);

        assertEquals(2, stack.size());
        assertEquals(DIR_1, stack.get(0));
        assertEquals(DIR_2, stack.get(1));
        assertEquals(ROOT_1, stack.getRoot());
    }

    @Test
    public void testPushDocument_ChangesLocation() {
        mStack.push(DIR_1);

        assertTrue(mStack.hasLocationChanged());
    }

    @Test
    public void testPushDocument_ChangesInitialLocation() {
        mStack.push(DIR_1);
        mStack.push(DIR_2);

        assertTrue(mStack.hasInitialLocationChanged());
    }

    @Test
    public void testChangeRoot_ChangesInitialLocation() {
        mStack.changeRoot(ROOT_1);
        mStack.changeRoot(ROOT_2);

        assertTrue(mStack.hasInitialLocationChanged());
    }
}