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