@Before
    public void setup() {
        // Bind to PostMessageService only after CustomTabsService sends the callback to do so. This
        // callback is sent after requestPostMessageChannel is called.
        mCallback = new TestCustomTabsCallback() {
            @Override
            public void extraCallback(String callbackName, Bundle args) {
                if (TestCustomTabsService.CALLBACK_BIND_TO_POST_MESSAGE.equals(callbackName)) {
                    // This gets run on the UI thread, where mServiceRule.bindService will not work.
                    AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> {
                        try {
                            Intent postMessageServiceIntent = new Intent();
                            postMessageServiceIntent.setClassName(mContext.getPackageName(),
                                    PostMessageService.class.getName());
                            mServiceRule.bindService(postMessageServiceIntent,
                                    mPostMessageServiceConnection, Context.BIND_AUTO_CREATE);
                        } catch (TimeoutException e) {
                            fail();
                        }
                    });
                }
            }
        };
        mContext = mActivityTestRule.getActivity();
        mCustomTabsServiceConnection = new CustomTabsServiceConnection() {
            @Override
            public void onCustomTabsServiceConnected(ComponentName name, CustomTabsClient client) {
                mCustomTabsServiceConnected = true;
                mSession = client.newSession(mCallback);
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                mCustomTabsServiceConnected = false;
            }
        };
        mPostMessageServiceConnection = new PostMessageServiceConnection(
                new CustomTabsSessionToken(mCallback.getStub(), null)) {
            @Override
            public void onPostMessageServiceConnected() {
                mPostMessageServiceConnected = true;
            }

            @Override
            public void onPostMessageServiceDisconnected() {
                mPostMessageServiceConnected = false;
            }
        };
        Intent customTabsServiceIntent = new Intent();
        customTabsServiceIntent.setClassName(
                mContext.getPackageName(), TestCustomTabsService.class.getName());
        try {
            mServiceRule.bindService(customTabsServiceIntent,
                    mCustomTabsServiceConnection, Context.BIND_AUTO_CREATE);
        } catch (TimeoutException e) {
            fail();
        }
    }
Tests for a complete loop between a browser side {@link CustomTabsService}
and a client side {@link PostMessageService}. Both services are bound to through
{@link ServiceTestRule}, but {@link CustomTabsCallback#extraCallback} is used to link browser
side actions.
customtabs/tests/src/android/support/customtabs/PostMessageTest.java