private static void setComponentEnabled(Class clazz, boolean enabled) {
        Context context = InstrumentationRegistry.getContext();
        PackageManager pm = context.getPackageManager();
        ComponentName name = new ComponentName(context, clazz);

        int newState = enabled
                ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        int flags = PackageManager.DONT_KILL_APP;

        if (pm.getComponentEnabledSetting(name) != newState) {
            pm.setComponentEnabledSetting(name, newState, flags);
        }
    }
Manually enables a component. Will be disabled when test finishes.
customtabs/tests/src/android/support/customtabs/EnableComponentsTestRule.java