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.KeyEvent;
23 import android.view.Menu;
24 import android.view.MenuItem;
25 import android.view.MotionEvent;
26 import android.view.View;
27 import android.view.Window;
28 import android.view.ContextMenu.ContextMenuInfo;
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 Window window;
78     public Bundle saveInstanceState;
79     public Bundle savedInstanceState;
80     public KeyEvent keyEvent;
81     public MotionEvent touchEvent;
82     public MotionEvent trackballEvent;
83     public MotionEvent onTrackballEvent;
84     public MotionEvent onTouchEvent;
85     public KeyEvent keyMultipleEvent;
86 
TestDialog(Context context)87     public TestDialog(Context context) {
88         super(context);
89     }
90 
TestDialog(Context context, int theme)91     public TestDialog(Context context, int theme) {
92         super(context, theme);
93     }
94 
TestDialog(Context context, boolean cancelable, OnCancelListener cancelListener)95     public TestDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
96         super(context, cancelable, cancelListener);
97     }
98 
99     @Override
onStart()100     protected void onStart() {
101         super.onStart();
102         isOnStartCalled = true;
103     }
104 
105     @Override
onStop()106     protected void onStop() {
107         super.onStop();
108         isOnStopCalled = true;
109     }
110 
111     @Override
onCreate(Bundle savedInstanceState)112     protected void onCreate(Bundle savedInstanceState) {
113         isRequestWindowFeature = requestWindowFeature(Window.FEATURE_LEFT_ICON);
114         super.onCreate(savedInstanceState);
115         isOnCreateCalled = true;
116     }
117 
118     @Override
onContentChanged()119     public void onContentChanged() {
120         super.onContentChanged();
121 
122         isOnContentChangedCalled = true;
123     }
124 
125     @Override
onWindowFocusChanged(boolean hasFocus)126     public void onWindowFocusChanged(boolean hasFocus) {
127         super.onWindowFocusChanged(hasFocus);
128 
129         isOnWindowFocusChangedCalled = true;
130     }
131 
132     @Override
onTouchEvent(MotionEvent event)133     public boolean onTouchEvent(MotionEvent event) {
134         isOnTouchEventCalled = true;
135         onTouchEvent = event;
136         return super.onTouchEvent(event);
137     }
138 
139     @Override
onTrackballEvent(MotionEvent event)140     public boolean onTrackballEvent(MotionEvent event) {
141         isOnTrackballEventCalled = true;
142         onTrackballEvent = event;
143         return super.onTrackballEvent(event);
144     }
145 
146     @Override
onKeyDown(int keyCode, KeyEvent event)147     public boolean onKeyDown(int keyCode, KeyEvent event) {
148         isOnKeyDownCalled = true;
149         keyDownCode = keyCode;
150         onKeyDownReturn = super.onKeyDown(keyCode, event);
151 
152         return onKeyDownReturn;
153     }
154 
155     @Override
onKeyUp(int keyCode, KeyEvent event)156     public boolean onKeyUp(int keyCode, KeyEvent event) {
157         isOnKeyUpCalled = true;
158 
159         return super.onKeyUp(keyCode, event);
160     }
161 
162     @Override
onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)163     public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
164         isOnKeyMultipleCalled = true;
165         onKeyMultipleReturn = super.onKeyMultiple(keyCode, repeatCount, event);
166         keyMultipleEvent = event;
167         return onKeyMultipleReturn;
168     }
169 
170     @Override
onSaveInstanceState()171     public Bundle onSaveInstanceState() {
172         saveInstanceState = super.onSaveInstanceState();
173         onSaveInstanceStateObserver.onObserved();
174         return saveInstanceState;
175     }
176 
177     @Override
onRestoreInstanceState(Bundle savedInstanceState)178     public void onRestoreInstanceState(Bundle savedInstanceState) {
179         this.savedInstanceState = savedInstanceState;
180         onRestoreInstanceStateObserver.onObserved();
181         super.onRestoreInstanceState(savedInstanceState);
182     }
183 
184     @Override
onWindowAttributesChanged(LayoutParams params)185     public void onWindowAttributesChanged(LayoutParams params) {
186         isOnWindowAttributesChangedCalled = true;
187         super.onWindowAttributesChanged(params);
188     }
189 
190     @Override
onCreatePanelMenu(int featureId, Menu menu)191     public boolean onCreatePanelMenu(int featureId, Menu menu) {
192         isOnCreatePanelMenuCalled = true;
193         return super.onCreatePanelMenu(featureId, menu);
194     }
195 
196     @Override
onCreatePanelView(int featureId)197     public View onCreatePanelView(int featureId) {
198         isOnCreatePanelViewCalled = true;
199         return super.onCreatePanelView(featureId);
200     }
201 
202     @Override
onPreparePanel(int featureId, View view, Menu menu)203     public boolean onPreparePanel(int featureId, View view, Menu menu) {
204         isOnPreparePanelCalled = true;
205         return super.onPreparePanel(featureId, view, menu);
206     }
207 
208     @Override
onMenuItemSelected(int featureId, MenuItem item)209     public boolean onMenuItemSelected(int featureId, MenuItem item) {
210         isOnMenuItemSelectedCalled = true;
211         return super.onMenuItemSelected(featureId, item);
212     }
213 
214     @Override
onMenuOpened(int featureId, Menu menu)215     public boolean onMenuOpened(int featureId, Menu menu) {
216         isOnMenuOpenedCalled = true;
217         return super.onMenuOpened(featureId, menu);
218     }
219 
220     @Override
onPanelClosed(int featureId, Menu menu)221     public void onPanelClosed(int featureId, Menu menu) {
222         isOnPanelClosedCalled = true;
223         super.onPanelClosed(featureId, menu);
224     }
225 
226     @Override
onPrepareOptionsMenu(Menu menu)227     public boolean onPrepareOptionsMenu(Menu menu) {
228         isOnPrepareOptionsMenuCalled = true;
229         return super.onPrepareOptionsMenu(menu);
230     }
231 
232     @Override
onCreateOptionsMenu(Menu menu)233     public boolean onCreateOptionsMenu(Menu menu) {
234         super.onCreateOptionsMenu(menu);
235         menu.add(0, OPTIONS_MENU_ITEM_0, 0, "OptionsMenuItem0");
236         menu.add(0, OPTIONS_MENU_ITEM_1, 0, "OptionsMenuItem1");
237         menu.add(0, OPTIONS_MENU_ITEM_2, 0, "OptionsMenuItem2");
238         menu.add(0, OPTIONS_MENU_ITEM_3, 0, "OptionsMenuItem3");
239         menu.add(0, OPTIONS_MENU_ITEM_4, 0, "OptionsMenuItem4");
240         menu.add(0, OPTIONS_MENU_ITEM_5, 0, "OptionsMenuItem5");
241         menu.add(0, OPTIONS_MENU_ITEM_6, 0, "OptionsMenuItem6");
242         isOnCreateOptionsMenuCalled = true;
243         return true;
244     }
245 
246     @Override
onOptionsItemSelected(MenuItem item)247     public boolean onOptionsItemSelected(MenuItem item) {
248         isOnOptionsItemSelectedCalled = true;
249         switch (item.getItemId()) {
250             case OPTIONS_MENU_ITEM_0:
251             case OPTIONS_MENU_ITEM_1:
252             case OPTIONS_MENU_ITEM_2:
253             case OPTIONS_MENU_ITEM_3:
254             case OPTIONS_MENU_ITEM_4:
255             case OPTIONS_MENU_ITEM_5:
256             case OPTIONS_MENU_ITEM_6:
257                 return true;
258             default:
259                 return super.onOptionsItemSelected(item);
260         }
261     }
262 
263     @Override
onOptionsMenuClosed(Menu menu)264     public void onOptionsMenuClosed(Menu menu) {
265         isOnOptionsMenuClosedCalled = true;
266         super.onOptionsMenuClosed(menu);
267     }
268 
269     @Override
onContextItemSelected(MenuItem item)270     public boolean onContextItemSelected(MenuItem item) {
271         isOnContextItemSelectedCalled = true;
272         switch (item.getItemId()) {
273             case CONTEXT_MENU_ITEM_0:
274             case CONTEXT_MENU_ITEM_1:
275             case CONTEXT_MENU_ITEM_2:
276                 return true;
277             default:
278                 return super.onContextItemSelected(item);
279         }
280     }
281 
282     @Override
onContextMenuClosed(Menu menu)283     public void onContextMenuClosed(Menu menu) {
284         isOnContextMenuClosedCalled = true;
285         super.onContextMenuClosed(menu);
286     }
287 
288     @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)289     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
290         menu.add(0, CONTEXT_MENU_ITEM_0, 0, "ContextMenuItem0");
291         menu.add(0, CONTEXT_MENU_ITEM_1, 0, "ContextMenuItem1");
292         menu.add(0, CONTEXT_MENU_ITEM_2, 0, "ContextMenuItem2");
293         isOnCreateContextMenuCalled = true;
294     }
295 
296     @Override
onSearchRequested()297     public boolean onSearchRequested() {
298         isOnSearchRequestedCalled = true;
299         return super.onSearchRequested();
300     }
301 
302     @Override
dispatchKeyEvent(KeyEvent event)303     public boolean dispatchKeyEvent(KeyEvent event) {
304         keyEvent = event;
305         dispatchKeyEventResult = super.dispatchKeyEvent(event);
306         return dispatchKeyEventResult;
307     }
308 
309     @Override
dispatchTrackballEvent(MotionEvent ev)310     public boolean dispatchTrackballEvent(MotionEvent ev) {
311         trackballEvent = ev;
312         return super.dispatchTrackballEvent(ev);
313     }
314 
315     @Override
dispatchTouchEvent(MotionEvent ev)316     public boolean dispatchTouchEvent(MotionEvent ev) {
317         touchEvent = ev;
318         dispatchTouchEventResult = super.dispatchTouchEvent(ev);
319         return dispatchTouchEventResult;
320     }
321 }
322