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 android.graphics.Rect;
20 import android.test.ActivityInstrumentationTestCase2;
21 import android.test.UiThreadTest;
22 import android.view.ActionMode;
23 import android.view.Menu;
24 import android.view.MenuInflater;
25 import android.view.MenuItem;
26 import android.view.View;
27 
28 public class ActionModeTest extends ActivityInstrumentationTestCase2<ActionModeCtsActivity> {
29 
ActionModeTest()30     public ActionModeTest() {
31         super(ActionModeCtsActivity.class);
32     }
33 
testSetType()34     public void testSetType() {
35         ActionMode actionMode = new MockActionMode();
36         assertEquals(ActionMode.TYPE_PRIMARY, actionMode.getType());
37 
38         actionMode.setType(ActionMode.TYPE_FLOATING);
39         assertEquals(ActionMode.TYPE_FLOATING, actionMode.getType());
40 
41         actionMode.setType(ActionMode.TYPE_PRIMARY);
42         assertEquals(ActionMode.TYPE_PRIMARY, actionMode.getType());
43     }
44 
testInvalidateContentRectDoesNotInvalidateFull()45     public void testInvalidateContentRectDoesNotInvalidateFull() {
46         MockActionMode actionMode = new MockActionMode();
47 
48         actionMode.invalidateContentRect();
49 
50         assertFalse(actionMode.mInvalidateWasCalled);
51     }
52 
testInvalidateContentRectOnFloatingCallsCallback()53     public void testInvalidateContentRectOnFloatingCallsCallback() {
54         final View view = getActivity().contentView;
55         final MockActionModeCallback2 callback = new MockActionModeCallback2();
56 
57         getActivity().runOnUiThread(new Runnable() {
58             @Override
59             public void run() {
60                 ActionMode mode = view.startActionMode(callback, ActionMode.TYPE_FLOATING);
61                 assertNotNull(mode);
62                 mode.invalidateContentRect();
63             }
64         });
65         getInstrumentation().waitForIdleSync();
66 
67         assertTrue(callback.mIsOnGetContentRectCalled);
68     }
69 
testSetAndGetTitleOptionalHint()70     public void testSetAndGetTitleOptionalHint() {
71         MockActionMode actionMode = new MockActionMode();
72 
73         // Check default value.
74         assertFalse(actionMode.getTitleOptionalHint());
75         // Test set and get.
76         actionMode.setTitleOptionalHint(true);
77         assertTrue(actionMode.getTitleOptionalHint());
78         actionMode.setTitleOptionalHint(false);
79         assertFalse(actionMode.getTitleOptionalHint());
80     }
81 
testSetAndGetTag()82     public void testSetAndGetTag() {
83         MockActionMode actionMode = new MockActionMode();
84         Object tag = new Object();
85 
86         // Check default value.
87         assertNull(actionMode.getTag());
88 
89         actionMode.setTag(tag);
90         assertSame(tag, actionMode.getTag());
91     }
92 
testIsTitleOptional()93     public void testIsTitleOptional() {
94         MockActionMode actionMode = new MockActionMode();
95 
96         // Check default value.
97         assertFalse(actionMode.isTitleOptional());
98     }
99 
testIsUiFocusable()100     public void testIsUiFocusable() {
101         MockActionMode actionMode = new MockActionMode();
102 
103         // Check default value.
104         assertTrue(actionMode.isUiFocusable());
105     }
106 
testHide()107     public void testHide() {
108         MockActionMode actionMode = new MockActionMode();
109 
110         actionMode.hide(0);
111         actionMode.hide(ActionMode.DEFAULT_HIDE_DURATION);
112     }
113 
testOnWindowFocusChanged()114     public void testOnWindowFocusChanged() {
115         MockActionMode actionMode = new MockActionMode();
116 
117         actionMode.onWindowFocusChanged(true);
118         actionMode.onWindowFocusChanged(false);
119     }
120 
121     private static class MockActionModeCallback2 extends ActionMode.Callback2 {
122         boolean mIsOnGetContentRectCalled = false;
123 
124         @Override
onCreateActionMode(ActionMode mode, Menu menu)125         public boolean onCreateActionMode(ActionMode mode, Menu menu) {
126             return true;
127         }
128 
129         @Override
onPrepareActionMode(ActionMode mode, Menu menu)130         public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
131             return true;
132         }
133 
134         @Override
onActionItemClicked(ActionMode mode, MenuItem item)135         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
136             return false;
137         }
138 
139         @Override
onDestroyActionMode(ActionMode mode)140         public void onDestroyActionMode(ActionMode mode) {}
141 
142         @Override
onGetContentRect(ActionMode mode, View view, Rect outRect)143         public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
144             mIsOnGetContentRectCalled = true;
145             super.onGetContentRect(mode, view, outRect);
146         }
147     }
148 
149     private static class MockActionMode extends ActionMode {
150         boolean mInvalidateWasCalled = false;
151 
152         @Override
setTitle(CharSequence title)153         public void setTitle(CharSequence title) {}
154 
155         @Override
setTitle(int resId)156         public void setTitle(int resId) {}
157 
158         @Override
setSubtitle(CharSequence subtitle)159         public void setSubtitle(CharSequence subtitle) {}
160 
161         @Override
setSubtitle(int resId)162         public void setSubtitle(int resId) {}
163 
164         @Override
setCustomView(View view)165         public void setCustomView(View view) {}
166 
167         @Override
invalidate()168         public void invalidate() {
169             mInvalidateWasCalled = true;
170         }
171 
172         @Override
finish()173         public void finish() {}
174 
175         @Override
getMenu()176         public Menu getMenu() {
177             return null;
178         }
179 
180         @Override
getTitle()181         public CharSequence getTitle() {
182             return null;
183         }
184 
185         @Override
getSubtitle()186         public CharSequence getSubtitle() {
187             return null;
188         }
189 
190         @Override
getCustomView()191         public View getCustomView() {
192             return null;
193         }
194 
195         @Override
getMenuInflater()196         public MenuInflater getMenuInflater() {
197             return null;
198         }
199     }
200 }
201