/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.media.test.client;

import static android.support.mediacompat.testlib.util.IntentUtil.SERVICE_PACKAGE_NAME;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import android.content.Context;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

import androidx.media.MediaController2;
import androidx.media.SessionToken2;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.Executor;

/** Test {@link TestHelper}. */
@RunWith(AndroidJUnit4.class)
public class TestHelperTest {
    private static final int TIME_OUT_MS = 3000;

    private Context mContext;
    private TestHelper mTestHelper;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getTargetContext();
        mTestHelper = new TestHelper(mContext);
        boolean connected = mTestHelper.connect(TIME_OUT_MS);
        if (!connected) {
            fail("Failed to connect to Test helper service.");
        }
    }

    @Test
    @SmallTest
    public void testGettingToken() {
        SessionToken2 token = mTestHelper.getSessionToken2("testGettingToken");
        assertNotNull(token);
        assertEquals(SERVICE_PACKAGE_NAME, token.getPackageName());
    }

    @Test
    @SmallTest
    public void testCreatingController() {
        Looper.prepare();
        SessionToken2 token = mTestHelper.getSessionToken2("testCreatingController");
        assertNotNull(token);
        MediaController2 controller = new MediaController2(mContext, token, new Executor() {
            @Override
            public void execute(Runnable command) {
                command.run();
            }
        }, new MediaController2.ControllerCallback() {});
    }
}

@@ -701,44 +701,285 @@
 lib.
-util.IntentUtil.SERVICE_PACKAGE_NAME
+VersionConstants.KEY_SERVICE_VERSION;%0Aimport static android.support.mediacompat.testlib.VersionConstants.VERSION_TOT;%0Aimport static android.support.mediacompat.testlib.util.IntentUtil.SERVICE_PACKAGE_NAME;%0Aimport static android.support.test.InstrumentationRegistry.getArguments
 ;%0A%0Ai
@@ -1721,16 +1721,52 @@
 tHelper;
+%0A    private String mServiceVersion;
 %0A%0A    @B
@@ -1793,24 +1793,277 @@
 d setUp() %7B%0A
+        // The version of the service app is provided through the instrumentation arguments.%0A        mServiceVersion = getArguments().getString(KEY_SERVICE_VERSION, %22%22);%0A        if (!VERSION_TOT.equals(mServiceVersion)) %7B%0A            return;%0A        %7D%0A%0A
         mCon
@@ -2387,24 +2387,106 @@
 ngToken() %7B%0A
+        if (!VERSION_TOT.equals(mServiceVersion)) %7B%0A            return;%0A        %7D%0A
         Sess
@@ -2675,32 +2675,32 @@
 %0A    @SmallTest%0A
-
     public void 
@@ -2722,24 +2722,106 @@
 troller() %7B%0A
+        if (!VERSION_TOT.equals(mServiceVersion)) %7B%0A            return;%0A        %7D%0A
         Loop

Add version check in TestHelperTest am: ab53d3bc36