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