Why doesn't PackageManager locate Activity from Instrumentation Registry (UI-automator)?

倖福魔咒の 提交于 2020-01-03 17:02:41

问题


Why doesn't PackageManager from Instrumentation Registry (UI-automator) locate the Activity?

Intent { pkg=com.me.Activity1 }

My application has 3 entry points. Three Activities, each represented by an icon.

I am trying to run a ui-automator test to press an icon and launch the corresponding activity.

In Android Studio 2.2, when I select app and press Run, then it runs correctly and launches the Activity. However, in ui-automator it fails both on the Nexus 5 device and in the emulator.

However, following the example provided in the documentation, "Accessing UI Components", the intent was null. I found this answer and tried to modify. Since the intent is explicitly created, it is no longer null, but it does not launch the Activity - the logcat shows that it did not launch.

Debugging shows that resolveInfos.size is zero. I am guessing there is something wrong with the context or the instrumentation registry.

    Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));

I also tried... getTargetContext() instead of getContext(), but it failed.

    Context context = InstrumentationRegistry.getTargetContext();

Any help appreciated.

@Test
public void userLaunchesActivity1() {
    launchApp("com.me.Activity1");
}

   void launchApp(String BASIC_SAMPLE_PACKAGE) {
        Log.d("userLaunchesActivity1", "BASIC_SAMPLE_PACKAGE:"+BASIC_SAMPLE_PACKAGE);
        // Initialize UiDevice instance
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    // Start from the home screen
    mDevice.pressHome();

    // Wait for launcher
    final String launcherPackage = mDevice.getLauncherPackageName();
    assertThat(launcherPackage, notNullValue());
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
            LAUNCH_TIMEOUT);

    // Launch the app
    Context context = InstrumentationRegistry.getContext();
    // ======== example from android.com documentation - intent is null ====
    // Description
   // final Intent intent = context.getPackageManager()
     //       .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
    // Clear out any previous instances
    //intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    //context.startActivity(intent);
    //

    //=======stack overflow answer - does not launch activity====================
    Intent intent = new Intent();
    intent.setPackage(BASIC_SAMPLE_PACKAGE);

    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
    Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));

    //======= this is not entered at all ===================
    if(resolveInfos.size() > 0) {
        ResolveInfo launchable = resolveInfos.get(0);
        ActivityInfo activity = launchable.activityInfo;
        ComponentName name = new ComponentName(activity.applicationInfo.packageName,
                activity.name);
        Intent i = new Intent(Intent.ACTION_MAIN);

        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        i.setComponent(name);

        context.startActivity(i);
    }
    //===========================

    // Wait for the app to appear
    mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
            LAUNCH_TIMEOUT);
}

来源:https://stackoverflow.com/questions/38856092/why-doesnt-packagemanager-locate-activity-from-instrumentation-registry-ui-aut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!