1 /* 2 * Copyright (C) 2015 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 android.view.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertSame; 24 import static org.junit.Assert.assertTrue; 25 import static org.mockito.Matchers.any; 26 import static org.mockito.Mockito.atLeastOnce; 27 import static org.mockito.Mockito.doReturn; 28 import static org.mockito.Mockito.mock; 29 import static org.mockito.Mockito.never; 30 import static org.mockito.Mockito.spy; 31 import static org.mockito.Mockito.verify; 32 33 import android.app.Instrumentation; 34 import android.graphics.Rect; 35 import android.support.test.InstrumentationRegistry; 36 import android.support.test.filters.SmallTest; 37 import android.support.test.rule.ActivityTestRule; 38 import android.support.test.runner.AndroidJUnit4; 39 import android.view.ActionMode; 40 import android.view.Menu; 41 import android.view.MenuInflater; 42 import android.view.View; 43 44 import org.junit.Before; 45 import org.junit.Rule; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 49 @SmallTest 50 @RunWith(AndroidJUnit4.class) 51 public class ActionModeTest { 52 private Instrumentation mInstrumentation; 53 private ActionModeCtsActivity mActivity; 54 55 @Rule 56 public ActivityTestRule<ActionModeCtsActivity> mActivityRule = 57 new ActivityTestRule<>(ActionModeCtsActivity.class); 58 59 @Before setup()60 public void setup() { 61 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 62 mActivity = mActivityRule.getActivity(); 63 } 64 65 @Test testSetType()66 public void testSetType() { 67 final ActionMode mockActionMode = new MockActionMode(); 68 assertEquals(ActionMode.TYPE_PRIMARY, mockActionMode.getType()); 69 70 mockActionMode.setType(ActionMode.TYPE_FLOATING); 71 assertEquals(ActionMode.TYPE_FLOATING, mockActionMode.getType()); 72 73 mockActionMode.setType(ActionMode.TYPE_PRIMARY); 74 assertEquals(ActionMode.TYPE_PRIMARY, mockActionMode.getType()); 75 } 76 77 @Test testInvalidateContentRectDoesNotInvalidateFull()78 public void testInvalidateContentRectDoesNotInvalidateFull() { 79 final ActionMode mockActionMode = spy(new MockActionMode()); 80 81 mockActionMode.invalidateContentRect(); 82 83 verify(mockActionMode, never()).invalidate(); 84 } 85 86 @Test testInvalidateContentRectOnFloatingCallsCallback()87 public void testInvalidateContentRectOnFloatingCallsCallback() throws Throwable { 88 final View view = mActivity.contentView; 89 final ActionMode.Callback2 mockCallback = mock(ActionMode.Callback2.class); 90 doReturn(Boolean.TRUE).when(mockCallback).onCreateActionMode( 91 any(ActionMode.class), any(Menu.class)); 92 doReturn(Boolean.TRUE).when(mockCallback).onPrepareActionMode( 93 any(ActionMode.class), any(Menu.class)); 94 95 mActivityRule.runOnUiThread(() -> { 96 ActionMode mode = view.startActionMode(mockCallback, ActionMode.TYPE_FLOATING); 97 assertNotNull(mode); 98 mode.invalidateContentRect(); 99 }); 100 mInstrumentation.waitForIdleSync(); 101 102 verify(mockCallback, atLeastOnce()).onGetContentRect(any(ActionMode.class), any(View.class), 103 any(Rect.class)); 104 } 105 106 @Test testSetAndGetTitleOptionalHint()107 public void testSetAndGetTitleOptionalHint() { 108 final ActionMode actionMode = new MockActionMode(); 109 110 // Check default value. 111 assertFalse(actionMode.getTitleOptionalHint()); 112 // Test set and get. 113 actionMode.setTitleOptionalHint(true); 114 assertTrue(actionMode.getTitleOptionalHint()); 115 actionMode.setTitleOptionalHint(false); 116 assertFalse(actionMode.getTitleOptionalHint()); 117 } 118 119 @Test testSetAndGetTag()120 public void testSetAndGetTag() { 121 final ActionMode actionMode = new MockActionMode(); 122 Object tag = new Object(); 123 124 // Check default value. 125 assertNull(actionMode.getTag()); 126 127 actionMode.setTag(tag); 128 assertSame(tag, actionMode.getTag()); 129 } 130 131 @Test testIsTitleOptional()132 public void testIsTitleOptional() { 133 final ActionMode actionMode = new MockActionMode(); 134 135 // Check default value. 136 assertFalse(actionMode.isTitleOptional()); 137 } 138 139 @Test testIsUiFocusable()140 public void testIsUiFocusable() { 141 final ActionMode actionMode = new MockActionMode(); 142 143 // Check default value. 144 assertTrue(actionMode.isUiFocusable()); 145 } 146 147 @Test testHide()148 public void testHide() { 149 final ActionMode actionMode = new MockActionMode(); 150 151 actionMode.hide(0); 152 actionMode.hide(ActionMode.DEFAULT_HIDE_DURATION); 153 } 154 155 @Test testOnWindowFocusChanged()156 public void testOnWindowFocusChanged() { 157 final ActionMode actionMode = new MockActionMode(); 158 159 actionMode.onWindowFocusChanged(true); 160 actionMode.onWindowFocusChanged(false); 161 } 162 163 protected static class MockActionMode extends ActionMode { 164 @Override setTitle(CharSequence title)165 public void setTitle(CharSequence title) {} 166 167 @Override setTitle(int resId)168 public void setTitle(int resId) {} 169 170 @Override setSubtitle(CharSequence subtitle)171 public void setSubtitle(CharSequence subtitle) {} 172 173 @Override setSubtitle(int resId)174 public void setSubtitle(int resId) {} 175 176 @Override setCustomView(View view)177 public void setCustomView(View view) {} 178 179 @Override invalidate()180 public void invalidate() { 181 } 182 183 @Override finish()184 public void finish() {} 185 186 @Override getMenu()187 public Menu getMenu() { 188 return null; 189 } 190 191 @Override getTitle()192 public CharSequence getTitle() { 193 return null; 194 } 195 196 @Override getSubtitle()197 public CharSequence getSubtitle() { 198 return null; 199 } 200 201 @Override getCustomView()202 public View getCustomView() { 203 return null; 204 } 205 206 @Override getMenuInflater()207 public MenuInflater getMenuInflater() { 208 return null; 209 } 210 } 211 } 212