Example usage for android.app Instrumentation runOnMainSync

List of usage examples for android.app Instrumentation runOnMainSync

Introduction

In this page you can find the example usage for android.app Instrumentation runOnMainSync.

Prototype

public void runOnMainSync(Runnable runner) 

Source Link

Document

Execute a call on the application's main thread, blocking until it is complete.

Usage

From source file:Main.java

public static <R> R runOnMainSyncAndGetResult(Instrumentation instrumentation, Callable<R> callable)
        throws Throwable {
    FutureTask<R> task = new FutureTask<R>(callable);
    instrumentation.runOnMainSync(task);
    try {//from ww w . j a  v  a 2 s .c  o  m
        return task.get();
    } catch (ExecutionException e) {
        // Unwrap the cause of the exception and re-throw it.
        throw e.getCause();
    }
}

From source file:Main.java

/**
 * Assert run an AbsListView with LayoutAnimationController successfully.
 * @param instrumentation/*from w w w. j  a  va2 s  .  c  o m*/
 * @param view
 * @param controller
 * @param duration
 * @throws InterruptedException
 */
public static void assertRunController(final Instrumentation instrumentation, final ViewGroup view,
        final LayoutAnimationController controller, final long duration) throws InterruptedException {

    instrumentation.runOnMainSync(new Runnable() {
        public void run() {
            view.setLayoutAnimation(controller);
            view.requestLayout();
        }
    });

    // LayoutAnimationController.isDone() always returns true, it's no use for stopping
    // the running, so just using sleeping fixed time instead. we reported issue 1799434 for it.
    Thread.sleep(duration + TIMEOUT_DELTA);
}

From source file:org.onepf.opfiab.opfiab_uitest.tests.ui.UnifiedFragmentHelperTest.java

private static void purchase(Instrumentation instrumentation, IabHelper helper, String sku, long timeout)
        throws InterruptedException {
    instrumentation.runOnMainSync(new PurchaseRunnable(helper, sku));
    Thread.sleep(timeout);/*from  www  .  j  av a  2 s  .c  o m*/
}

From source file:org.onepf.opfiab.opfiab_uitest.tests.ui.UnifiedFragmentHelperTest.java

@SuppressWarnings("PMD.UnusedPrivateMethod")
private static FragmentIabHelper getHelper(final boolean isSupport, final Object fragment,
        final OnPurchaseListener listener, Instrumentation instrumentation) throws InterruptedException {
    final FragmentIabHelper[] helpers = new FragmentIabHelper[1];
    instrumentation.runOnMainSync(new Runnable() {
        @Override//w w w  .  j  a  v  a2  s  . c  o  m
        public void run() {
            if (isSupport) {
                helpers[0] = ((SupportTestFragment) fragment).getIabHelper(listener);
            } else {
                helpers[0] = ((TestFragment) fragment).getIabHelper(listener);
            }
        }
    });
    Thread.sleep(WAIT_INIT);
    return helpers[0];
}

From source file:com.scvngr.levelup.core.test.TestThreadingUtils.java

/**
 * Runs a runnable on the main thread, but also catches any errors thrown on the main thread and
 * re-throws them on the test thread so they can be displayed more easily.
 *
 * @param instrumentation the {@link Instrumentation} for the test.
 * @param activity the {@link Activity} that this this test is running in.
 * @param runnable the runnable to run./*from w ww  .  j a  v a2 s  . c  om*/
 */
public static void runOnMainSync(@NonNull final Instrumentation instrumentation,
        @NonNull final Activity activity, @NonNull final Runnable runnable) {
    if (activity.getMainLooper().equals(Looper.myLooper())) {
        runnable.run();
    } else {
        final FutureAssertionError futureError = new FutureAssertionError();
        instrumentation.runOnMainSync(new Runnable() {

            @Override
            public void run() {
                try {
                    runnable.run();
                } catch (final AssertionError e) {
                    futureError.setAssertionError(e);
                }
            }
        });
        futureError.throwPendingAssertionError();

        instrumentation.waitForIdleSync();
    }
}

From source file:org.onepf.opfiab.opfiab_uitest.tests.ui.UnifiedFragmentHelperTest.java

private static Object createFragment(boolean isSupport, Activity activity, Instrumentation instrumentation,
        int color) throws InterruptedException {
    final Object fragment;
    if (isSupport) {
        fragment = SupportTestFragment.getInstance(color);
        final android.support.v4.app.FragmentManager supportFragmentManager = ((FragmentActivity) activity)
                .getSupportFragmentManager();
        supportFragmentManager.beginTransaction()
                .replace(R.id.content, (android.support.v4.app.Fragment) fragment, FRAGMENT_TAG).commit();
        instrumentation.runOnMainSync(new Runnable() {
            @Override/*  ww w .j a  va2 s.c o m*/
            public void run() {
                supportFragmentManager.executePendingTransactions();
            }
        });
    } else {
        fragment = TestFragment.getInstance(color);
        final FragmentManager fragmentManager = activity.getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content, (Fragment) fragment, FRAGMENT_TAG).commit();
        instrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                fragmentManager.executePendingTransactions();
            }
        });
    }
    Thread.sleep(WAIT_INIT);
    return fragment;
}

From source file:org.onepf.opfiab.opfiab_uitest.tests.ui.UnifiedFragmentHelperTest.java

public static void registerUnregisterHomeButton(Instrumentation instrumentation, final Activity activity,
        UiDevice uiDevice) throws InterruptedException {

    final String providerName = String.format(TEST_PROVIDER_NAME_FMT, "HOME");
    final BillingProvider billingProvider = new MockBillingProviderBuilder().setWillPostSuccess(true)
            .setName(providerName).setIsAvailable(true).setSleepTime(WAIT_BILLING_PROVIDER).build();

    final TestManager testPurchaseManager = new TestManager.Builder()
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .expectEvent(new AlwaysFailValidator()).setTag("Purchase").setSkipWrongEvents(false).build();
    final BillingManagerAdapter purchaseListenerAdapter = new BillingManagerAdapter(testPurchaseManager);

    final TestManager testGlobalListenerManager = new TestManager.Builder()
            .expectEvent(new SetupStartedEventValidator()).expectEvent(new SetupResponseValidator(providerName))
            .expectEvent(new PurchaseRequestValidator(SKU_CONSUMABLE))
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .expectEvent(new PurchaseRequestValidator(SKU_CONSUMABLE))
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .expectEvent(new PurchaseRequestValidator(SKU_CONSUMABLE))
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .setStrategy(TestManager.Strategy.UNORDERED_EVENTS).setTag("Global").build();

    final TestManager[] managers = { testGlobalListenerManager, testPurchaseManager };

    final Configuration configuration = new Configuration.Builder().addBillingProvider(billingProvider)
            .setBillingListener(new BillingManagerAdapter(testGlobalListenerManager)).build();

    instrumentation.runOnMainSync(new Runnable() {
        @Override//from   w w w .  j ava2  s  .c o m
        public void run() {
            OPFIab.init(activity.getApplication(), configuration);
        }
    });
    Thread.sleep(WAIT_INIT);
    final boolean isSupport = activity instanceof FragmentActivity;
    Object fragment = createFragment(isSupport, activity, instrumentation, R.color.blue);

    FragmentIabHelper helper = getHelper(isSupport, fragment, purchaseListenerAdapter, instrumentation);

    purchase(instrumentation, helper, SKU_CONSUMABLE);

    changeToHomeScreen(uiDevice);

    purchase(instrumentation, helper, SKU_CONSUMABLE);

    reopenActivity(instrumentation);

    fragment = getFragment(isSupport);
    helper = getHelper(isSupport, fragment, purchaseListenerAdapter, instrumentation);

    pressBackButton(uiDevice);

    purchase(instrumentation, helper, SKU_CONSUMABLE);

    purchaseListenerAdapter.validateEvent(AlwaysFailValidator.getStopObject());

    for (TestManager manager : managers) {
        Assert.assertTrue(manager.await(WAIT_TEST_MANAGER));
    }
}

From source file:org.onepf.opfiab.opfiab_uitest.tests.ui.UnifiedFragmentHelperTest.java

public static void registerUnregisterFragmentReplace(Instrumentation instrumentation, final Boolean isSupport,
        UiDevice uiDevice) throws InterruptedException {
    final String providerName = String.format(TEST_PROVIDER_NAME_FMT, "FRAGMENT_REPLACE");
    final Activity activity;
    if (isSupport) {
        activity = EmptyFragmentActivity.getLastInstance();
    } else {//from  w w  w .  java  2  s . c o  m
        activity = EmptyActivity.getLastInstance();
    }

    final BillingProvider billingProvider = new MockBillingProviderBuilder().setWillPostSuccess(true)
            .setName(providerName).setIsAvailable(true).setSleepTime(WAIT_BILLING_PROVIDER).build();

    final TestManager testPurchaseManager = new TestManager.Builder()
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .expectEvent(new AlwaysFailValidator())
            .expectEvent(new PurchaseResponseValidator(providerName, true)).setTag("Purchase").build();
    final BillingManagerAdapter purchaseListenerAdapter = new BillingManagerAdapter(testPurchaseManager);

    final TestManager testGlobalListenerManager = new TestManager.Builder()
            .expectEvent(new SetupStartedEventValidator()).expectEvent(new SetupResponseValidator(providerName))
            .expectEvent(new PurchaseRequestValidator(SKU_CONSUMABLE))
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .expectEvent(new PurchaseRequestValidator(SKU_CONSUMABLE))
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .expectEvent(new PurchaseRequestValidator(SKU_CONSUMABLE))
            .expectEvent(new PurchaseResponseValidator(providerName, true))
            .setStrategy(TestManager.Strategy.UNORDERED_EVENTS).setTag("Global").build();

    final Configuration configuration = new Configuration.Builder().addBillingProvider(billingProvider)
            .setBillingListener(new BillingManagerAdapter(testGlobalListenerManager)).build();

    final TestManager[] managers = { testGlobalListenerManager, testPurchaseManager };

    final FragmentIabHelper[] helpers = new FragmentIabHelper[1];
    instrumentation.runOnMainSync(new Runnable() {
        @Override
        public void run() {
            OPFIab.init(activity.getApplication(), configuration);
        }
    });
    Thread.sleep(WAIT_INIT);
    final Object fragment = createFragment(isSupport, activity, instrumentation, R.color.green);
    final FragmentIabHelper helper = getHelper(isSupport, fragment, purchaseListenerAdapter, instrumentation);

    purchase(instrumentation, helper, SKU_CONSUMABLE);
    Thread.sleep(WAIT_PURCHASE);

    replaceFragment(activity);

    purchase(instrumentation, helper, SKU_CONSUMABLE);
    Thread.sleep(WAIT_LAUNCH_SCREEN);

    restoreFragment(activity);

    purchaseListenerAdapter.validateEvent(AlwaysFailValidator.getStopObject());

    purchase(instrumentation, helper, SKU_CONSUMABLE);
    Thread.sleep(WAIT_PURCHASE);

    for (TestManager manager : managers) {
        Assert.assertTrue(manager.await(WAIT_TEST_MANAGER));
    }
}