gpt4 book ai didi

android - Instrumentation.invokeMenuActionSync() 不适用于旧版 Android 设备 (API10)

转载 作者:太空狗 更新时间:2023-10-29 15:04:51 26 4
gpt4 key购买 nike

我在我的 Android 应用程序上有一些 intrumentation 测试,我想测试我的 Activity 的 onOptionsItemSelected() 方法。

为此,我使用 invokeMenuActionSync检测方法:

这在较新版本的 Android 上运行良好,但似乎不会导致在旧版本(例如 API10)上调用 onOptionsItemSelected() 方法。

我尝试结合其他方法:

if (getInstrumentation().invokeMenuActionSync(activity, R.id.menu_refresh, 0 /* flags */) ||
getInstrumentation().invokeContextMenuAction(activity, R.id.menu_refresh, 0 /* flags */)) {
// verify
}

没有变化。

我正在使用 AppCompat ActionBar。

是否有更好的方法始终调用它,或者我可以在较旧的 API 级别上使用并在两者之间切换的方法?

最佳答案

我发现用仪表类调用支持菜单的唯一方法是使用丑陋的反射代码(“init”部分的某些字段和类可能不需要,抱歉,我也将它们用于其他代码)

// support part
private static Class<?> actionBarActivitySupportClass;
private static Field mImplField;
private static Class<?> actionBarActivityDelegateBaseClass;
private static Field mActionModeSupportField;
private static Class<?> actionModeSupportImplClass;
private static Field mMenuActionModeSupportField;
private static Class<?> menuBuilderSupportClass;
private static Field mVisibleItemsSupportField;
private static Field mCallbackActionModeSupportField;
private static Class<?> callbackWrapperSupportClass;
private static Field mWrappedSupportField;
private static Method onMenuItemSelectedSupportmethod;
private static Field mActionBarSupportView;
private static Class<?> actionBarViewSupportClass;
private static Field mOptionMenuSupportField;
private static Method dispatchMenuItemSelected;

static {
init();
}

private static void init() {
try {
actionBarActivitySupportClass = Class.forName("android.support.v7.app.ActionBarActivity"); //$NON-NLS-1$
mImplField = actionBarActivitySupportClass.getDeclaredField("mImpl"); //$NON-NLS-1$
mImplField.setAccessible(true);
actionBarActivityDelegateBaseClass = Class.forName("android.support.v7.app.ActionBarActivityDelegateBase"); //$NON-NLS-1$
mActionModeSupportField = actionBarActivityDelegateBaseClass.getDeclaredField("mActionMode"); //$NON-NLS-1$
mActionModeSupportField.setAccessible(true);
actionModeSupportImplClass = Class.forName("android.support.v7.app.ActionBarImplBase$ActionModeImpl"); //$NON-NLS-1$
mMenuActionModeSupportField = actionModeSupportImplClass.getDeclaredField("mMenu"); //$NON-NLS-1$
mMenuActionModeSupportField.setAccessible(true);
menuBuilderSupportClass = Class.forName("android.support.v7.internal.view.menu.MenuBuilder"); //$NON-NLS-1$
mVisibleItemsSupportField = menuBuilderSupportClass.getDeclaredField("mVisibleItems"); //$NON-NLS-1$
mVisibleItemsSupportField.setAccessible(true);
mCallbackActionModeSupportField = actionModeSupportImplClass.getDeclaredField("mCallback"); //$NON-NLS-1$
mCallbackActionModeSupportField.setAccessible(true);
callbackWrapperSupportClass = Class.forName("android.support.v7.app.ActionBarActivityDelegateBase$ActionModeCallbackWrapper"); //$NON-NLS-1$
mWrappedSupportField = callbackWrapperSupportClass.getDeclaredField("mWrapped"); //$NON-NLS-1$
mWrappedSupportField.setAccessible(true);
onMenuItemSelectedSupportmethod = actionModeSupportImplClass.getDeclaredMethod("onMenuItemSelected", new Class[]{menuBuilderSupportClass,MenuItem.class}); //$NON-NLS-1$
onMenuItemSelectedSupportmethod.setAccessible(true);
mActionBarSupportView = actionBarActivityDelegateBaseClass.getDeclaredField("mActionBarView"); //$NON-NLS-1$
mActionBarSupportView.setAccessible(true);
actionBarViewSupportClass = Class.forName("android.support.v7.internal.widget.ActionBarView"); //$NON-NLS-1$
mOptionMenuSupportField = actionBarViewSupportClass.getDeclaredField("mOptionsMenu"); //$NON-NLS-1$
mOptionMenuSupportField.setAccessible(true);
dispatchMenuItemSelected = menuBuilderSupportClass.getDeclaredMethod("dispatchMenuItemSelected", new Class[]{menuBuilderSupportClass,MenuItem.class}); //$NON-NLS-1$
dispatchMenuItemSelected.setAccessible(true);
} catch (ClassNotFoundException e) {
System.out.println("support class not loaded "+e.getMessage()); //$NON-NLS-1$
} catch (SecurityException e) {
System.out.println("support class not loaded "+e.getMessage()); //$NON-NLS-1$
} catch (NoSuchFieldException e) {
System.out.println("support class not loaded "+e.getMessage()); //$NON-NLS-1$
} catch (NoSuchMethodException e) {
System.out.println("support class not loaded "+e.getMessage()); //$NON-NLS-1$
}

public static void invokeMenu(Instrumentation instrumentation,final Activity activity,final Object item) {
if ((mImplField != null) && (actionBarActivitySupportClass != null) && (actionBarActivitySupportClass.isInstance(activity))
&& (mActionBarSupportView != null) && (mOptionMenuSupportField != null) && (dispatchMenuItemSelected != null)) {
activity.runInUiThread(new Runnable() {
@Override
public void run() {
try {
Object actionBarActivityDelegateBase = mImplField.get(activity);
if (actionBarActivityDelegateBase != null) {
Object actionBarView = mActionBarSupportView.get(actionBarActivityDelegateBase);
if (actionBarView != null) {
Object mOptionMenu = mOptionMenuSupportField.get(actionBarView);
dispatchMenuItemSelected.invoke(mOptionMenu, new Object[]{mOptionMenu,item});
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
});
} else {
// good old classic way
instrumentation.invokeMenuActionSync(activity, ((MenuItem)item).getItemId(), 0);
}

}

关于android - Instrumentation.invokeMenuActionSync() 不适用于旧版 Android 设备 (API10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23030495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com