OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.test.util.browser.contextmenu; | 5 package org.chromium.chrome.test.util.browser.contextmenu; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.app.Instrumentation; | 8 import android.app.Instrumentation; |
9 import android.text.TextUtils; | 9 import android.text.TextUtils; |
10 import android.view.ContextMenu; | 10 import android.view.ContextMenu; |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 80 |
81 helper.waitForCallback(callCount); | 81 helper.waitForCallback(callCount); |
82 return helper.getContextMenu(); | 82 return helper.getContextMenu(); |
83 } | 83 } |
84 | 84 |
85 /** | 85 /** |
86 * Opens and selects an item from a context menu. | 86 * Opens and selects an item from a context menu. |
87 * @param tab The tab to open a context menu for. | 87 * @param tab The tab to open a context menu for. |
88 * @param openerDOMNodeId The DOM node to long press to open the conte
xt menu for. | 88 * @param openerDOMNodeId The DOM node to long press to open the conte
xt menu for. |
89 * @param itemId The context menu item ID to select. | 89 * @param itemId The context menu item ID to select. |
| 90 * @param activity The activity to assert for gaining focus aft
er click or null. |
90 * @throws InterruptedException | 91 * @throws InterruptedException |
91 * @throws TimeoutException | 92 * @throws TimeoutException |
92 */ | 93 */ |
93 public static void selectContextMenuItem(Instrumentation instrumentation, Ac
tivity activity, | 94 public static void selectContextMenuItem(Instrumentation instrumentation, Ac
tivity activity, |
94 Tab tab, String openerDOMNodeId, final int itemId) | 95 Tab tab, String openerDOMNodeId, final int itemId) |
95 throws InterruptedException, TimeoutException { | 96 throws InterruptedException, TimeoutException { |
96 String jsCode = "document.getElementById('" + openerDOMNodeId + "')"; | 97 String jsCode = "document.getElementById('" + openerDOMNodeId + "')"; |
97 selectContextMenuItemByJs(instrumentation, activity, tab, jsCode, itemId
); | 98 selectContextMenuItemByJs(instrumentation, activity, tab, jsCode, itemId
); |
98 } | 99 } |
99 | 100 |
100 /** | 101 /** |
101 * Long presses to open and selects an item from a context menu. | 102 * Long presses to open and selects an item from a context menu. |
102 * @param tab The tab to open a context menu for. | 103 * @param tab The tab to open a context menu for. |
103 * @param jsCode The javascript to get the DOM node to long p
ress | 104 * @param jsCode The javascript to get the DOM node to long p
ress |
104 * to open the context menu for. | 105 * to open the context menu for. |
105 * @param itemId The context menu item ID to select. | 106 * @param itemId The context menu item ID to select. |
| 107 * @param activity The activity to assert for gaining focus aft
er click or null. |
106 * @throws InterruptedException | 108 * @throws InterruptedException |
107 * @throws TimeoutException | 109 * @throws TimeoutException |
108 */ | 110 */ |
109 public static void selectContextMenuItemByJs(Instrumentation instrumentation
, Activity activity, | 111 public static void selectContextMenuItemByJs(Instrumentation instrumentation
, Activity activity, |
110 Tab tab, String jsCode, final int itemId) | 112 Tab tab, String jsCode, final int itemId) |
111 throws InterruptedException, TimeoutException { | 113 throws InterruptedException, TimeoutException { |
112 ContextMenu menu = openContextMenuByJs(tab, jsCode); | 114 ContextMenu menu = openContextMenuByJs(tab, jsCode); |
113 Assert.assertNotNull("Failed to open context menu", menu); | 115 Assert.assertNotNull("Failed to open context menu", menu); |
114 | 116 |
115 selectOpenContextMenuItem(instrumentation, activity, menu, itemId); | 117 selectOpenContextMenuItem(instrumentation, activity, menu, itemId); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 Assert.assertTrue("'" + itemId + "' is not enabled", item.isEnabled()); | 152 Assert.assertTrue("'" + itemId + "' is not enabled", item.isEnabled()); |
151 | 153 |
152 instrumentation.runOnMainSync(new Runnable() { | 154 instrumentation.runOnMainSync(new Runnable() { |
153 @Override | 155 @Override |
154 public void run() { | 156 public void run() { |
155 boolean activated = menu.performIdentifierAction(itemId, 0); | 157 boolean activated = menu.performIdentifierAction(itemId, 0); |
156 Assert.assertTrue("Failed to activate '" + itemId + "' in menu",
activated); | 158 Assert.assertTrue("Failed to activate '" + itemId + "' in menu",
activated); |
157 } | 159 } |
158 }); | 160 }); |
159 | 161 |
160 CriteriaHelper.pollInstrumentationThread(new Criteria("Activity did not
regain focus.") { | 162 if (activity != null) { |
161 @Override | 163 CriteriaHelper.pollInstrumentationThread( |
162 public boolean isSatisfied() { | 164 new Criteria("Activity did not regain focus.") { |
163 return activity.hasWindowFocus(); | 165 @Override |
164 } | 166 public boolean isSatisfied() { |
165 }); | 167 return activity.hasWindowFocus(); |
| 168 } |
| 169 }); |
| 170 } |
166 } | 171 } |
167 } | 172 } |
OLD | NEW |