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 21 import android.content.Context; 22 import android.graphics.Rect; 23 import android.platform.test.annotations.AppModeSdkSandbox; 24 import android.view.ActionMode; 25 import android.view.Menu; 26 import android.view.MenuItem; 27 import android.view.View; 28 29 import androidx.test.InstrumentationRegistry; 30 import androidx.test.filters.SmallTest; 31 import androidx.test.runner.AndroidJUnit4; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @SmallTest 38 @RunWith(AndroidJUnit4.class) 39 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 40 public class ActionModeCallback2Test { 41 private static final int VIEW_WIDTH = 123; 42 private static final int VIEW_HEIGHT = 456; 43 44 private Context mContext; 45 46 @Before setup()47 public void setup() { 48 mContext = InstrumentationRegistry.getTargetContext(); 49 } 50 51 @Test testCallbackOnGetContentRectDefaultWithView()52 public void testCallbackOnGetContentRectDefaultWithView() { 53 View view = new View(mContext); 54 view.setLeft(0); 55 view.setRight(VIEW_WIDTH); 56 view.setTop(0); 57 view.setBottom(VIEW_HEIGHT); 58 59 Rect outRect = new Rect(); 60 ActionMode.Callback2 callback = new MockActionModeCallback2(); 61 callback.onGetContentRect(null, view, outRect); 62 63 assertEquals(0, outRect.top); 64 assertEquals(0, outRect.left); 65 assertEquals(VIEW_HEIGHT, outRect.bottom); 66 assertEquals(VIEW_WIDTH, outRect.right); 67 } 68 69 @Test testCallbackOnGetContentRectDefaultWithoutView()70 public void testCallbackOnGetContentRectDefaultWithoutView() { 71 Rect outRect = new Rect(); 72 ActionMode.Callback2 callback = new MockActionModeCallback2(); 73 callback.onGetContentRect(null, null, outRect); 74 75 assertEquals(0, outRect.top); 76 assertEquals(0, outRect.left); 77 assertEquals(0, outRect.bottom); 78 assertEquals(0, outRect.right); 79 } 80 81 private static class MockActionModeCallback2 extends ActionMode.Callback2 { 82 @Override onCreateActionMode(ActionMode mode, Menu menu)83 public boolean onCreateActionMode(ActionMode mode, Menu menu) { 84 return false; 85 } 86 87 @Override onPrepareActionMode(ActionMode mode, Menu menu)88 public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 89 return false; 90 } 91 92 @Override onActionItemClicked(ActionMode mode, MenuItem item)93 public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 94 return false; 95 } 96 97 @Override onDestroyActionMode(ActionMode mode)98 public void onDestroyActionMode(ActionMode mode) {} 99 } 100 101 } 102