1cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes/* 2cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * Copyright (C) 2015 The Android Open Source Project 3cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * 4cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * Licensed under the Apache License, Version 2.0 (the "License"); 5cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * you may not use this file except in compliance with the License. 6cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * You may obtain a copy of the License at 7cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * 8cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * http://www.apache.org/licenses/LICENSE-2.0 9cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * 10cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * Unless required by applicable law or agreed to in writing, software 11cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * distributed under the License is distributed on an "AS IS" BASIS, 12cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * See the License for the specific language governing permissions and 14cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes * limitations under the License. 15cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes */ 16cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes 17cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banespackage android.support.v7.app; 18cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes 19bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.junit.Assert.assertEquals; 20bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.junit.Assert.assertNotNull; 21bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.junit.Assert.assertNull; 22bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.junit.Assert.assertTrue; 23bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.mockito.Matchers.any; 24bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.mockito.Mockito.mock; 25bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.mockito.Mockito.never; 26bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.mockito.Mockito.reset; 27bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.mockito.Mockito.verify; 28bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.mockito.Mockito.verifyNoMoreInteractions; 29bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport static org.mockito.Mockito.when; 3024391daa4e5831395924e2f48df86e19294cc211Chris Banes 31e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banesimport android.os.Build; 32bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport android.support.test.annotation.UiThreadTest; 33e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banesimport android.support.v7.appcompat.test.R; 34e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banesimport android.support.v7.custom.FitWindowsContentLayout; 35a37e9311de7ff4407104965ff66146f87e55ba5cKirill Grouchnikovimport android.support.v7.testutils.BaseTestActivity; 3624391daa4e5831395924e2f48df86e19294cc211Chris Banesimport android.support.v7.testutils.TestUtils; 37bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport android.support.v7.view.ActionMode; 38daea069fe33cc750bcb733ebcb6206d2dcedae76Chris Banesimport android.test.suitebuilder.annotation.SmallTest; 39bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport android.view.Menu; 40e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banesimport android.view.View; 41e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banesimport android.view.WindowInsets; 42e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes 43bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport org.junit.Test; 44daea069fe33cc750bcb733ebcb6206d2dcedae76Chris Banes 45bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banesimport java.util.concurrent.atomic.AtomicBoolean; 46cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes 47cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banespublic abstract class BaseBasicsTestCase<A extends BaseTestActivity> 48cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes extends BaseInstrumentationTestCase<A> { 49cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes 50cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes protected BaseBasicsTestCase(Class<A> activityClass) { 51cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes super(activityClass); 52cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes } 53cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes 54cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes @Test 55daea069fe33cc750bcb733ebcb6206d2dcedae76Chris Banes @SmallTest 56cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes public void testActionBarExists() { 57cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes assertNotNull("ActionBar is not null", getActivity().getSupportActionBar()); 58cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes } 59cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes 60cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes @Test 61daea069fe33cc750bcb733ebcb6206d2dcedae76Chris Banes @SmallTest 62733d0b2acb07ede7455b9d020901a10586053923Chris Banes public void testDefaultActionBarTitle() { 63cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes assertEquals(getActivity().getTitle(), getActivity().getSupportActionBar().getTitle()); 64cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes } 65cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes 66cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes @Test 67daea069fe33cc750bcb733ebcb6206d2dcedae76Chris Banes @SmallTest 68cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes public void testSetActionBarTitle() throws Throwable { 69cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes final String newTitle = "hello"; 70cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes runTestOnUiThread(new Runnable() { 71cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes @Override 72cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes public void run() { 73cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes getActivity().setTitle(newTitle); 74cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes assertEquals("New title is set to ActionBar", 75cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes newTitle, getActivity().getSupportActionBar().getTitle()); 76cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes } 77cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes }); 78cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes } 79733d0b2acb07ede7455b9d020901a10586053923Chris Banes 80733d0b2acb07ede7455b9d020901a10586053923Chris Banes @Test 8124391daa4e5831395924e2f48df86e19294cc211Chris Banes @SmallTest 82733d0b2acb07ede7455b9d020901a10586053923Chris Banes public void testMenuInvalidationAfterDestroy() throws Throwable { 83733d0b2acb07ede7455b9d020901a10586053923Chris Banes final A activity = getActivity(); 8424391daa4e5831395924e2f48df86e19294cc211Chris Banes // Reset to make sure that we don't have a menu currently 8524391daa4e5831395924e2f48df86e19294cc211Chris Banes activity.reset(); 8624391daa4e5831395924e2f48df86e19294cc211Chris Banes assertNull(activity.getMenu()); 87733d0b2acb07ede7455b9d020901a10586053923Chris Banes 8824391daa4e5831395924e2f48df86e19294cc211Chris Banes // Now destroy the Activity 8924391daa4e5831395924e2f48df86e19294cc211Chris Banes activity.finish(); 9024391daa4e5831395924e2f48df86e19294cc211Chris Banes TestUtils.waitForActivityDestroyed(activity); 91733d0b2acb07ede7455b9d020901a10586053923Chris Banes 9224391daa4e5831395924e2f48df86e19294cc211Chris Banes // Now dispatch a menu invalidation and wait for an idle sync 9324391daa4e5831395924e2f48df86e19294cc211Chris Banes activity.supportInvalidateOptionsMenu(); 9424391daa4e5831395924e2f48df86e19294cc211Chris Banes getInstrumentation().waitForIdleSync(); 95733d0b2acb07ede7455b9d020901a10586053923Chris Banes 96733d0b2acb07ede7455b9d020901a10586053923Chris Banes // Make sure that we don't have a menu given to use after being destroyed 97733d0b2acb07ede7455b9d020901a10586053923Chris Banes assertNull(activity.getMenu()); 98733d0b2acb07ede7455b9d020901a10586053923Chris Banes } 99e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes 100e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes @Test 101e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes @SmallTest 102e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes public void testFitSystemWindowsReachesContent() { 103e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes final FitWindowsContentLayout content = 104e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes (FitWindowsContentLayout) getActivity().findViewById(R.id.test_content); 105e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes assertNotNull(content); 106e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes assertTrue(content.getFitsSystemWindowsCalled()); 107e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes } 108e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes 109e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes @Test 110e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes @SmallTest 111e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes public void testOnApplyWindowInsetsReachesContent() { 112e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes if (Build.VERSION.SDK_INT < 21) { 113e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes // OnApplyWindowInsetsListener is only available on API 21+ 114e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes return; 115e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes } 116e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes 117e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes final View content = getActivity().findViewById(R.id.test_content); 118e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes assertNotNull(content); 119e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes 120e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes final AtomicBoolean applyWindowInsetsCalled = new AtomicBoolean(); 121e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes content.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() { 122e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes @Override 123e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) { 124e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes applyWindowInsetsCalled.set(true); 125e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes return windowInsets; 126e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes } 127e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes }); 128e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes assertTrue(applyWindowInsetsCalled.get()); 129e91ec2c1719ac56156909abeb69bd8f934632b8eChris Banes } 130bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 131bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @Test 132bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @SmallTest 133bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @UiThreadTest 134bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes public void testSupportActionModeCallbacks() { 135bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final A activity = getActivity(); 136bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 137bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Create a mock action mode callback which returns true from onCreateActionMode 138bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode.Callback callback = mock(ActionMode.Callback.class); 139bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes when(callback.onCreateActionMode(any(ActionMode.class), any(Menu.class))).thenReturn(true); 140bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 141bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Start an action mode 142bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode actionMode = activity.startSupportActionMode(callback); 143bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes assertNotNull(actionMode); 144bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 145bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now verify that onCreateActionMode and onPrepareActionMode are called once 146bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(callback).onCreateActionMode(any(ActionMode.class), any(Menu.class)); 147bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(callback).onPrepareActionMode(any(ActionMode.class), any(Menu.class)); 148bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 149bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now finish and verify that onDestroyActionMode is called once, and there are no more 150bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // interactions 151bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes actionMode.finish(); 152bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(callback).onDestroyActionMode(any(ActionMode.class)); 153bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verifyNoMoreInteractions(callback); 154bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes } 155bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 156bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @Test 157bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @SmallTest 158bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @UiThreadTest 159bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes public void testSupportActionModeCallbacksInvalidate() { 160bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final A activity = getActivity(); 161bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 162bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Create a mock action mode callback which returns true from onCreateActionMode 163bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode.Callback callback = mock(ActionMode.Callback.class); 164bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes when(callback.onCreateActionMode(any(ActionMode.class), any(Menu.class))).thenReturn(true); 165bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 166bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Start an action mode 167bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode actionMode = activity.startSupportActionMode(callback); 168bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Assert that one was created 169bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes assertNotNull(actionMode); 170bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Reset the mock so that any callback counts from the create are reset 171bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes reset(callback); 172bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 173bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now invalidate the action mode 174bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes actionMode.invalidate(); 175bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 176bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now verify that onCreateActionMode is not called, and onPrepareActionMode is called once 177bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(callback, never()).onCreateActionMode(any(ActionMode.class), any(Menu.class)); 178bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(callback).onPrepareActionMode(any(ActionMode.class), any(Menu.class)); 179bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes } 180bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 181bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @Test 182bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @SmallTest 183bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes @UiThreadTest 184bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes public void testSupportActionModeCallbacksWithFalseOnCreate() { 185bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final A activity = getActivity(); 186bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 187bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Create a mock action mode callback which returns true from onCreateActionMode 188bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode.Callback callback = mock(ActionMode.Callback.class); 189bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes when(callback.onCreateActionMode(any(ActionMode.class), any(Menu.class))).thenReturn(false); 190bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 191bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Start an action mode 192bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode actionMode = activity.startSupportActionMode(callback); 193bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 194bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now verify that onCreateActionMode is called once 195bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(callback).onCreateActionMode(any(ActionMode.class), any(Menu.class)); 196bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 197bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now verify that onPrepareActionMode is not called (since onCreateActionMode 198bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // returns false) 199bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(callback, never()).onPrepareActionMode(any(ActionMode.class), any(Menu.class)); 200bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 201bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Assert that an action mode was not created 202bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes assertNull(actionMode); 203bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes } 204bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 205bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes protected void testSupportActionModeAppCompatCallbacks(final boolean fromWindow) { 206bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final A activity = getActivity(); 207bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 208bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Create a mock action mode callback which returns true from onCreateActionMode 209bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode.Callback amCallback = mock(ActionMode.Callback.class); 210bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes when(amCallback.onCreateActionMode(any(ActionMode.class), any(Menu.class))) 211bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes .thenReturn(true); 212bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 213bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Create a mock AppCompatCallback, which returns null from 214bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // onWindowStartingSupportActionMode, and set it on the Activity 215bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final AppCompatCallback apCallback = mock(AppCompatCallback.class); 216bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes when(apCallback.onWindowStartingSupportActionMode(any(ActionMode.Callback.class))) 217bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes .thenReturn(null); 218bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes activity.setAppCompatCallback(apCallback); 219bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 220bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Start an action mode with the action mode callback 221bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes final ActionMode actionMode = activity.startSupportActionMode(amCallback); 222bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 223bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes if (fromWindow) { 224bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Verify that the callback's onWindowStartingSupportActionMode was called 225bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(apCallback).onWindowStartingSupportActionMode(any(ActionMode.Callback.class)); 226bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes } 227bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 228bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now assert that an action mode was created 229bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes assertNotNull(actionMode); 230bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 231bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now verify that onSupportActionModeStarted is called once 232bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(apCallback).onSupportActionModeStarted(any(ActionMode.class)); 233bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes 234bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes // Now finish and verify that onDestroyActionMode is called once 235bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes actionMode.finish(); 236bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes verify(apCallback).onSupportActionModeFinished(any(ActionMode.class)); 237bfc9bd50c5c7e6874dc6f32a5d81620398a5c1e5Chris Banes } 238cdbdcb29b4a4e3bf1553f824902562cee3fcb723Chris Banes} 239