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