测试正在运行的Google Play Services API

时间:2018-01-31 11:45:59

标签: android google-play-services geofencing android-geofence

当更新的代码包含不推荐使用的Android API调用时,每个Android版本的代码都相对简单。例如

@RunWith(AndroidJUnit4.class)
public class UITestPlayground {

    String testUrl = "http://testurl.com/";
    MockWebServer server = new MockWebServer();

    @Rule
    public IntentsTestRule<LoginActivity> mIntentsRule = new IntentsTestRule<>(LoginActivity.class);

    @Before
    public void beforeHelper() throws IOException {
        TestHelper.removeUserAndTokenIfAny(mIntentsRule.getActivity());
        URLS.baseUrl = testUrl;
        server.url(URLS.baseUrl);
        //try to shutting down the server JUT IN CASE...
        server.shutdown();
        server.start();

    }

    @After
    public void afterHelper() throws IOException {
        server.shutdown();
    }


    @Test
    public void invalidLoginDueNotValidJSONResponse() {

        server.enqueue(new MockResponse().setBody("Something not valid JSON response"));

        String emailToBeTyped = "tester@tester.com";
        String passToBeTyped = "passtest";

        ViewActions.closeSoftKeyboard();
        // Type text and then press the button.
        onView(withId(R.id.login_email_edit)).perform(typeText(emailToBeTyped));
        ViewActions.closeSoftKeyboard();
        onView(withId(R.id.login_pass_edit)).perform(typeText(passToBeTyped));
        ViewActions.closeSoftKeyboard();
        onView(withId(R.id.log_in_btn)).perform(click());

        //TODO: check on valid error message appearing

    }
 }

但是,如果在Google Play Services API中弃用某些内容会怎么样?例如, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mTonePlayer = new SoundPool.Builder().build(); } else { mTonePlayer = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); } 最近已被弃用,应由LocationServices.GeofencingAPI替换。

这取决于运行Google Play服务版本11.0.0的目标设备,并且不一定依赖于Android SDK。那我怎么能为两者编码呢?

e.g。

GeofencingClient

1 个答案:

答案 0 :(得分:1)

您可以使用checkApiAvailability()方法。

请注意,因为该方法返回异步检查指定的API是否可用的任务。如果一个或多个不可用,则任务将失败并显示AvailabilityException

否则,您可以isGooglePlayServicesAvailable验证此设备上是否已安装并启用Google Play服务,并且此设备上安装的版本不早于此客户端所需的版本。