Home | History | Annotate | Download | only in testing
      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.testing;
     18 
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.annotation.StringRes;
     23 import android.view.MenuItem;
     24 
     25 import org.mockito.Mockito;
     26 
     27 /**
     28 *
     29 * Test copy of {@link android.view.MenuItem}.
     30 *
     31 * We use abstract so we don't have to implement all the necessary methods from the interface,
     32 * and we use Mockito to just mock out the methods we need.
     33 * To get an instance, use {@link #create(int)}.
     34 */
     35 
     36 public abstract class TestMenuItem implements MenuItem {
     37 
     38     boolean enabled;
     39     boolean visible;
     40     @StringRes int title;
     41 
     42     public static TestMenuItem create(int id) {
     43         final TestMenuItem mockMenuItem = Mockito.mock(TestMenuItem.class,
     44                 Mockito.withSettings().defaultAnswer(Mockito.CALLS_REAL_METHODS));
     45 
     46         // By default all menu items are enabled and visible.
     47         mockMenuItem.enabled = true;
     48         mockMenuItem.visible = true;
     49 
     50         return mockMenuItem;
     51     }
     52 
     53     @Override
     54     public TestMenuItem setTitle(@StringRes int title) {
     55         this.title = title;
     56         return this;
     57     }
     58 
     59     @Override
     60     public TestMenuItem setTitle(@StringRes CharSequence title) {
     61         return this;
     62     }
     63 
     64     @Override
     65     public MenuItem setEnabled(boolean enabled) {
     66         this.enabled = enabled;
     67         return this;
     68     }
     69 
     70     @Override
     71     public MenuItem setVisible(boolean visible) {
     72         this.visible = visible;
     73         return this;
     74     }
     75 
     76     @Override
     77     public boolean isVisible() {
     78         return this.visible;
     79     }
     80 
     81     @Override
     82     public boolean isEnabled() {
     83         return this.enabled;
     84     }
     85 
     86     public void assertEnabled() {
     87         assertTrue(this.enabled);
     88     }
     89 
     90     public void assertDisabled() {
     91         assertFalse(this.enabled);
     92     }
     93 
     94     public void assertVisible() {
     95         assertTrue(this.visible);
     96     }
     97 
     98     public void assertInvisible() {
     99         assertFalse(this.visible);
    100     }
    101 
    102     public void assertTitle(@StringRes int title) {
    103         assertTrue(this.title == title);
    104     }
    105 }
    106