1 /*
2  * Copyright (C) 2008 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 package android.app.stubs;
17 
18 import android.app.Dialog;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.view.ContextMenu;
22 import android.view.ContextMenu.ContextMenuInfo;
23 import android.view.KeyEvent;
24 import android.view.Menu;
25 import android.view.MenuItem;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import android.view.Window;
29 import android.view.WindowManager.LayoutParams;
30 
31 public class TestDialog extends Dialog {
32     private static final int OPTIONS_MENU_ITEM_0 = Menu.FIRST;
33     private static final int OPTIONS_MENU_ITEM_1 = Menu.FIRST + 1;
34     private static final int OPTIONS_MENU_ITEM_2 = Menu.FIRST + 2;
35     private static final int OPTIONS_MENU_ITEM_3 = Menu.FIRST + 3;
36     private static final int OPTIONS_MENU_ITEM_4 = Menu.FIRST + 4;
37     private static final int OPTIONS_MENU_ITEM_5 = Menu.FIRST + 5;
38     private static final int OPTIONS_MENU_ITEM_6 = Menu.FIRST + 6;
39     private static final int CONTEXT_MENU_ITEM_0 = Menu.FIRST + 7;
40     private static final int CONTEXT_MENU_ITEM_1 = Menu.FIRST + 8;
41     private static final int CONTEXT_MENU_ITEM_2 = Menu.FIRST + 9;
42     public boolean isOnStartCalled;
43     public boolean isOnStopCalled;
44     public boolean isOnCreateCalled;
45     public boolean isRequestWindowFeature;
46     public boolean isOnContentChangedCalled;
47     public boolean isOnWindowFocusChangedCalled;
48     public boolean isOnTouchEventCalled;
49     public boolean isOnTrackballEventCalled;
50     public boolean isOnKeyDownCalled;
51     public boolean isOnKeyUpCalled;
52     public boolean isOnKeyMultipleCalled;
53     public final OrientationTestUtils.Observer onSaveInstanceStateObserver =
54             new OrientationTestUtils.Observer();
55     public final static OrientationTestUtils.Observer onRestoreInstanceStateObserver =
56             new OrientationTestUtils.Observer();
57     public boolean isOnWindowAttributesChangedCalled;
58     public boolean isOnCreatePanelMenuCalled;
59     public boolean isOnCreatePanelViewCalled;
60     public boolean isOnPreparePanelCalled;
61     public boolean isOnMenuOpenedCalled;
62     public boolean isOnMenuItemSelectedCalled;
63     public boolean isOnPanelClosedCalled;
64     public boolean isOnCreateOptionsMenuCalled;
65     public boolean isOnPrepareOptionsMenuCalled;
66     public boolean isOnOptionsItemSelectedCalled;
67     public boolean isOnOptionsMenuClosedCalled;
68     public boolean isOnContextItemSelectedCalled;
69     public boolean isOnContextMenuClosedCalled;
70     public boolean isOnCreateContextMenuCalled;
71     public boolean isOnSearchRequestedCalled;
72     public boolean onKeyDownReturn;
73     public boolean onKeyMultipleReturn;
74     public boolean dispatchTouchEventResult;
75     public boolean dispatchKeyEventResult;
76     public int keyDownCode = -1;
77     public Bundle saveInstanceState;
78     public Bundle savedInstanceState;
79     public KeyEvent keyEvent;
80     public MotionEvent touchEvent;
81     public MotionEvent trackballEvent;
82     public MotionEvent onTrackballEvent;
83     public MotionEvent onTouchEvent;
84     public KeyEvent keyMultipleEvent;
85 
86     private View mEmptyView;
87     private ContextMenu mContextMenu;
88 
TestDialog(Context context)89     public TestDialog(Context context) {
90         super(context);
91     }
92 
TestDialog(Context context, int theme)93     public TestDialog(Context context, int theme) {
94         super(context, theme);
95     }
96 
TestDialog(Context context, boolean cancelable, OnCancelListener cancelListener)97     public TestDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
98         super(context, cancelable, cancelListener);
99     }
100 
101     @Override
onStart()102     protected void onStart() {
103         super.onStart();
104         isOnStartCalled = true;
105     }
106 
107     @Override
onStop()108     protected void onStop() {
109         super.onStop();
110         isOnStopCalled = true;
111     }
112 
113     @Override
onCreate(Bundle savedInstanceState)114     protected void onCreate(Bundle savedInstanceState) {
115         isRequestWindowFeature = requestWindowFeature(Window.FEATURE_LEFT_ICON);
116         super.onCreate(savedInstanceState);
117         isOnCreateCalled = true;
118     }
119 
120     @Override
onContentChanged()121     public void onContentChanged() {
122         super.onContentChanged();
123 
124         isOnContentChangedCalled = true;
125     }
126 
127     @Override
onWindowFocusChanged(boolean hasFocus)128     public void onWindowFocusChanged(boolean hasFocus) {
129         super.onWindowFocusChanged(hasFocus);
130 
131         isOnWindowFocusChangedCalled = true;
132     }
133 
134     @Override
onTouchEvent(MotionEvent event)135     public boolean onTouchEvent(MotionEvent event) {
136         isOnTouchEventCalled = true;
137         onTouchEvent = event;
138         return super.onTouchEvent(event);
139     }
140 
141     @Override
onTrackballEvent(MotionEvent event)142     public boolean onTrackballEvent(MotionEvent event) {
143         isOnTrackballEventCalled = true;
144         onTrackballEvent = event;
145         return super.onTrackballEvent(event);
146     }
147 
148     @Override
onKeyDown(int keyCode, KeyEvent event)149     public boolean onKeyDown(int keyCode, KeyEvent event) {
150         isOnKeyDownCalled = true;
151         keyDownCode = keyCode;
152         onKeyDownReturn = super.onKeyDown(keyCode, event);
153 
154         return onKeyDownReturn;
155     }
156 
157     @Override
onKeyUp(int keyCode, KeyEvent event)158     public boolean onKeyUp(int keyCode, KeyEvent event) {
159         isOnKeyUpCalled = true;
160 
161         return super.onKeyUp(keyCode, event);
162     }
163 
164     @Override
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)165     public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
166         isOnKeyMultipleCalled = true;
167         onKeyMultipleReturn = super.onKeyMultiple(keyCode, repeatCount, event);
168         keyMultipleEvent = event;
169         return onKeyMultipleReturn;
170     }
171 
172     @Override
onSaveInstanceState()173     public Bundle onSaveInstanceState() {
174         saveInstanceState = super.onSaveInstanceState();
175         onSaveInstanceStateObserver.onObserved();
176         return saveInstanceState;
177     }
178 
179     @Override
onRestoreInstanceState(Bundle savedInstanceState)180     public void onRestoreInstanceState(Bundle savedInstanceState) {
181         this.savedInstanceState = savedInstanceState;
182         onRestoreInstanceStateObserver.onObserved();
183         super.onRestoreInstanceState(savedInstanceState);
184     }
185 
186     @Override
onWindowAttributesChanged(LayoutParams params)187     public void onWindowAttributesChanged(LayoutParams params) {
188         isOnWindowAttributesChangedCalled = true;
189         super.onWindowAttributesChanged(params);
190     }
191 
192     @Override
onCreatePanelMenu(int featureId, Menu menu)193     public boolean onCreatePanelMenu(int featureId, Menu menu) {
194         isOnCreatePanelMenuCalled = true;
195         return super.onCreatePanelMenu(featureId, menu);
196     }
197 
198     @Override
onCreatePanelView(int featureId)199     public View onCreatePanelView(int featureId) {
200         isOnCreatePanelViewCalled = true;
201         return super.onCreatePanelView(featureId);
202     }
203 
204     @Override
onPreparePanel(int featureId, View view, Menu menu)205     public boolean onPreparePanel(int featureId, View view, Menu menu) {
206         isOnPreparePanelCalled = true;
207         return super.onPreparePanel(featureId, view, menu);
208     }
209 
210     @Override
onMenuItemSelected(int featureId, MenuItem item)211     public boolean onMenuItemSelected(int featureId, MenuItem item) {
212         isOnMenuItemSelectedCalled = true;
213         return super.onMenuItemSelected(featureId, item);
214     }
215 
216     @Override
onMenuOpened(int featureId, Menu menu)217     public boolean onMenuOpened(int featureId, Menu menu) {
218         isOnMenuOpenedCalled = true;
219         return super.onMenuOpened(featureId, menu);
220     }
221 
222     @Override
onPanelClosed(int featureId, Menu menu)223     public void onPanelClosed(int featureId, Menu menu) {
224         isOnPanelClosedCalled = true;
225         super.onPanelClosed(featureId, menu);
226     }
227 
228     @Override
onPrepareOptionsMenu(Menu menu)229     public boolean onPrepareOptionsMenu(Menu menu) {
230         isOnPrepareOptionsMenuCalled = true;
231         return super.onPrepareOptionsMenu(menu);
232     }
233 
234     @Override
onCreateOptionsMenu(Menu menu)235     public boolean onCreateOptionsMenu(Menu menu) {
236         super.onCreateOptionsMenu(menu);
237         menu.add(0, OPTIONS_MENU_ITEM_0, 0, "OptionsMenuItem0");
238         menu.add(0, OPTIONS_MENU_ITEM_1, 0, "OptionsMenuItem1");
239         menu.add(0, OPTIONS_MENU_ITEM_2, 0, "OptionsMenuItem2");
240         menu.add(0, OPTIONS_MENU_ITEM_3, 0, "OptionsMenuItem3");
241         menu.add(0, OPTIONS_MENU_ITEM_4, 0, "OptionsMenuItem4");
242         menu.add(0, OPTIONS_MENU_ITEM_5, 0, "OptionsMenuItem5");
243         menu.add(0, OPTIONS_MENU_ITEM_6, 0, "OptionsMenuItem6");
244         isOnCreateOptionsMenuCalled = true;
245         return true;
246     }
247 
248     @Override
onOptionsItemSelected(MenuItem item)249     public boolean onOptionsItemSelected(MenuItem item) {
250         isOnOptionsItemSelectedCalled = true;
251         switch (item.getItemId()) {
252             case OPTIONS_MENU_ITEM_0:
253             case OPTIONS_MENU_ITEM_1:
254             case OPTIONS_MENU_ITEM_2:
255             case OPTIONS_MENU_ITEM_3:
256             case OPTIONS_MENU_ITEM_4:
257             case OPTIONS_MENU_ITEM_5:
258             case OPTIONS_MENU_ITEM_6:
259                 return true;
260             default:
261                 return super.onOptionsItemSelected(item);
262         }
263     }
264 
265     @Override
onOptionsMenuClosed(Menu menu)266     public void onOptionsMenuClosed(Menu menu) {
267         isOnOptionsMenuClosedCalled = true;
268         super.onOptionsMenuClosed(menu);
269     }
270 
271     @Override
onContextItemSelected(MenuItem item)272     public boolean onContextItemSelected(MenuItem item) {
273         isOnContextItemSelectedCalled = true;
274         switch (item.getItemId()) {
275             case CONTEXT_MENU_ITEM_0:
276             case CONTEXT_MENU_ITEM_1:
277             case CONTEXT_MENU_ITEM_2:
278                 return true;
279             default:
280                 return super.onContextItemSelected(item);
281         }
282     }
283 
284     @Override
onContextMenuClosed(Menu menu)285     public void onContextMenuClosed(Menu menu) {
286         isOnContextMenuClosedCalled = true;
287         mContextMenu = null;
288         super.onContextMenuClosed(menu);
289     }
290 
291     @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)292     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
293         mContextMenu = menu;
294         menu.add(0, CONTEXT_MENU_ITEM_0, 0, "ContextMenuItem0");
295         menu.add(0, CONTEXT_MENU_ITEM_1, 0, "ContextMenuItem1");
296         menu.add(0, CONTEXT_MENU_ITEM_2, 0, "ContextMenuItem2");
297         mEmptyView = new EmptyView(v.getContext());
298         menu.setHeaderView(mEmptyView);
299         isOnCreateContextMenuCalled = true;
300     }
301 
selectContextMenuItem()302     public void selectContextMenuItem() {
303         if (mContextMenu != null) {
304             mContextMenu.performIdentifierAction(CONTEXT_MENU_ITEM_0, 0);
305         }
306     }
307 
308     @Override
onSearchRequested()309     public boolean onSearchRequested() {
310         isOnSearchRequestedCalled = true;
311         return super.onSearchRequested();
312     }
313 
314     @Override
dispatchKeyEvent(KeyEvent event)315     public boolean dispatchKeyEvent(KeyEvent event) {
316         keyEvent = event;
317         dispatchKeyEventResult = super.dispatchKeyEvent(event);
318         return dispatchKeyEventResult;
319     }
320 
321     @Override
dispatchTrackballEvent(MotionEvent ev)322     public boolean dispatchTrackballEvent(MotionEvent ev) {
323         trackballEvent = ev;
324         return super.dispatchTrackballEvent(ev);
325     }
326 
327     @Override
dispatchTouchEvent(MotionEvent ev)328     public boolean dispatchTouchEvent(MotionEvent ev) {
329         touchEvent = ev;
330         dispatchTouchEventResult = super.dispatchTouchEvent(ev);
331         return dispatchTouchEventResult;
332     }
333 
334     /**
335      * The context menu shows up in a separate window. There isn't a direct way of getting a handle
336      * to that window. However, we must ensure that we wait until that window has focus prior to
337      * injecting key events.
338      * To get around this issue, we add a view to the context menu. Then, we can use
339      * View.hasWindowFocus to assert the focus state of the window containing the context menu.
340      */
contextMenuHasWindowFocus()341     public boolean contextMenuHasWindowFocus() {
342         return mEmptyView.hasWindowFocus();
343     }
344 
345     /**
346      * View that does nothing. Used for obtaining the state of window focus of the parent window.
347      */
348     private static class EmptyView extends View {
EmptyView(Context context)349         EmptyView(Context context) {
350             super(context);
351         }
352     }
353 }
354