Home | History | Annotate | Download | only in sorting
      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.sorting;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.support.test.filters.SmallTest;
     22 import android.support.test.runner.AndroidJUnit4;
     23 import android.view.View;
     24 
     25 import com.android.documentsui.base.State;
     26 
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 
     31 @RunWith(AndroidJUnit4.class)
     32 @SmallTest
     33 public class SortControllerTest {
     34 
     35     private TestWidget mTableHeader;
     36     private TestWidget mDropHeader;
     37     private SortController mController;
     38 
     39     @Test
     40     public void testGridMode_ShowsDrop() {
     41         createWidget(true);
     42         mController.onViewModeChanged(State.MODE_GRID);
     43         mDropHeader.assertVisible();
     44         mTableHeader.assertGone();
     45     }
     46 
     47     @Test
     48     public void testListMode_ShowsDrop_NoHeader() {
     49         createWidget(false);
     50         mController.onViewModeChanged(State.MODE_LIST);
     51         mDropHeader.assertVisible();
     52     }
     53 
     54     @Test
     55     public void testListMode_ShowsTable() {
     56         createWidget(true);
     57         mController.onViewModeChanged(State.MODE_LIST);
     58         mDropHeader.assertGone();
     59         mTableHeader.assertVisible();
     60     }
     61 
     62     @Test
     63     public void testDestroysWidgets() {
     64         createWidget(true);
     65         mController.destroy();
     66 
     67         mDropHeader.assertDestroyed();
     68         mTableHeader.assertDestroyed();
     69     }
     70 
     71     private void createWidget(boolean hasTableHeader) {
     72         mDropHeader = new TestWidget();
     73         if (hasTableHeader) {
     74             mTableHeader = new TestWidget();
     75         }
     76         mController = new SortController(mDropHeader, mTableHeader);
     77     }
     78 
     79     static class TestWidget implements SortController.WidgetController {
     80         private int mVisibility;
     81         private boolean mDestroyed;
     82 
     83         @Override
     84         public void setVisibility(int visibility) {
     85             mVisibility = visibility;
     86         }
     87 
     88         @Override
     89         public void destroy() {
     90             mDestroyed = true;
     91         }
     92 
     93         void assertVisible() {
     94             assertTrue(
     95                     "Expected mode VISIBLE, but was " + mVisibility,
     96                     mVisibility == View.VISIBLE);
     97         }
     98 
     99         void assertGone() {
    100             assertTrue(
    101                     "Expected mode GONE, but was " + mVisibility,
    102                     mVisibility == View.GONE);
    103         }
    104 
    105         void assertDestroyed() {
    106             assertTrue("Widget is not destroyed.", mDestroyed);
    107         }
    108     }
    109 }
    110