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