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

Commit 56e8dc19 authored by Rhed Jao's avatar Rhed Jao Committed by Android (Google) Code Review
Browse files

Merge "Add TestDrawerController for testBackButton_CloseDrawer."

parents bf564f7c 23c4bc0b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -157,7 +157,8 @@ public class FilesActivity extends BaseActivity implements ActionHandler.Addons
                        mInjector.selectionMgr,
                        mInjector.searchManager::cancelSearch,
                        this::popDir,
                        mInjector.features, mDrawer);
                        mInjector.features,
                        mDrawer);

        RootsFragment.show(getFragmentManager(), null);

+2 −1
Original line number Diff line number Diff line
@@ -134,7 +134,8 @@ public class PickActivity extends BaseActivity implements ActionHandler.Addons {
                        mInjector.selectionMgr,
                        mInjector.searchManager::cancelSearch,
                        this::popDir,
                        mInjector.features, mDrawer);
                        mInjector.features,
                        mDrawer);
        setupLayout(intent);
        mInjector.actions.initLocation(intent);
    }
+55 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui.testing;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.android.documentsui.DrawerController;

import org.mockito.Mockito;

/**
 * We use abstract so we don't have to implement all the necessary methods from the interface.
 * To get an instance, use {@link #create()}.
 */
public abstract class TestDrawerController extends DrawerController {

    public static TestDrawerController create() {
        TestDrawerController drawController = Mockito.mock(
                TestDrawerController.class);
        Mockito.doCallRealMethod().when(drawController).openDrawer(Mockito.anyBoolean());
        Mockito.doCallRealMethod().when(drawController).assertWasOpened();
        Mockito.doCallRealMethod().when(drawController).assertWasClosed();

        return drawController;
    }

    public void openDrawer(boolean open) {
        when(isPresent()).thenReturn(open);
        when(isOpen()).thenReturn(open);
    }

    public void assertWasOpened() {
        verify(this).setOpen(false);
    }

    public void assertWasClosed() {
        verify(this, never()).setOpen(false);
    }
}
 No newline at end of file
+7 −14
Original line number Diff line number Diff line
@@ -19,9 +19,6 @@ package com.android.documentsui;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
@@ -32,13 +29,12 @@ import com.android.documentsui.base.Procedure;
import com.android.documentsui.dirlist.TestFocusHandler;
import com.android.documentsui.selection.SelectionHelper;
import com.android.documentsui.testing.SelectionHelpers;
import com.android.documentsui.testing.TestDrawerController;
import com.android.documentsui.testing.TestFeatures;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@RunWith(AndroidJUnit4.class)
@SmallTest
@@ -48,7 +44,7 @@ public class SharedInputHandlerTest {
    private SelectionHelper mSelectionMgr = SelectionHelpers.createTestInstance();
    private TestFeatures mFeatures = new TestFeatures();
    private TestFocusHandler mFocusHandler = new TestFocusHandler();
    @Mock private DrawerController mDrawer;
    private TestDrawerController mDrawer = TestDrawerController.create();
    private boolean mDirPopHappened;
    private boolean mCanceledSearch;
    private Procedure mDirPopper = new Procedure() {
@@ -61,8 +57,6 @@ public class SharedInputHandlerTest {

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mDirPopHappened = false;
        mSharedInputHandler = new SharedInputHandler(
                mFocusHandler,
@@ -102,7 +96,7 @@ public class SharedInputHandlerTest {
        assertTrue(mCanceledSearch);
        assertEquals(mSelectionMgr.getSelection().size(), 1);
        assertFalse(mDirPopHappened);
        verify(mDrawer, never()).setOpen(false);
        mDrawer.assertWasClosed();
    }

    @Test
@@ -116,7 +110,7 @@ public class SharedInputHandlerTest {
        assertFalse(mCanceledSearch);
        assertEquals(mSelectionMgr.getSelection().size(), 0);
        assertFalse(mDirPopHappened);
        verify(mDrawer, never()).setOpen(false);
        mDrawer.assertWasClosed();
    }

    @Test
@@ -128,17 +122,16 @@ public class SharedInputHandlerTest {
        assertFalse(mCanceledSearch);
        assertEquals(mSelectionMgr.getSelection().size(), 0);
        assertTrue(mDirPopHappened);
        verify(mDrawer, never()).setOpen(false);
        mDrawer.assertWasClosed();
    }

    @Test
    public void testBackButton_CloseDrawer() {
        KeyEvent backEvent =
                new KeyEvent(0, 0, MotionEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK, 0, 0);
        when(mDrawer.isPresent()).thenReturn(true);
        when(mDrawer.isOpen()).thenReturn(true);
        mDrawer.openDrawer(true);
        assertTrue(mSharedInputHandler.onKeyDown(backEvent.getKeyCode(), backEvent));
        verify(mDrawer).setOpen(false);
        mDrawer.assertWasOpened();
    }

    @Test