Convert ViewInfoActivityTest to a unit test.
The instrumentation test runner is deprecated.
Rewriting the tests paves the way to migrating to the support library,
which the new UX relies on.
Bug: 30046624
Test: runtest --path packages/apps/EmergencyInfo/tests
Change-Id: I7b3ede2be5cd4a61fb8b7a4f2c234328b50a945f
diff --git a/tests/instrumentation/src/com/android/emergency/view/ViewInfoActivityTest.java b/tests/instrumentation/src/com/android/emergency/view/ViewInfoActivityTest.java
deleted file mode 100644
index f54a5a2..0000000
--- a/tests/instrumentation/src/com/android/emergency/view/ViewInfoActivityTest.java
+++ /dev/null
@@ -1,217 +0,0 @@
-/*
- * Copyright (C) 2016 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 com.android.emergency.view;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import android.app.Fragment;
-import android.app.Instrumentation;
-import android.content.ComponentName;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.content.pm.PackageManager;
-import android.preference.PreferenceManager;
-import android.test.ActivityInstrumentationTestCase2;
-import android.test.suitebuilder.annotation.LargeTest;
-import android.util.Pair;
-import android.view.KeyEvent;
-import android.view.View;
-import android.widget.LinearLayout;
-import android.widget.TextView;
-import android.widget.ViewFlipper;
-
-import com.android.emergency.ContactTestUtils;
-import com.android.emergency.PreferenceKeys;
-import com.android.emergency.R;
-import com.android.emergency.edit.EditInfoActivity;
-
-import java.util.ArrayList;
-import java.util.Calendar;
-
-/**
- * Tests for {@link ViewInfoActivity}.
- */
-@LargeTest
-public class ViewInfoActivityTest extends ActivityInstrumentationTestCase2<ViewInfoActivity> {
- private ArrayList<Pair<String, Fragment>> mFragments;
- private LinearLayout mPersonalCard;
- private TextView mPersonalCardLargeItem;
- private ViewFlipper mViewFlipper;
- private int mNoInfoIndex;
- private int mTabsIndex;
-
- public ViewInfoActivityTest() {
- super(ViewInfoActivity.class);
- }
-
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- mPersonalCard = (LinearLayout) getActivity().findViewById(R.id.name_and_dob_linear_layout);
- mPersonalCardLargeItem = (TextView) getActivity().findViewById(R.id.personal_card_large);
- mViewFlipper = (ViewFlipper) getActivity().findViewById(R.id.view_flipper);
- mNoInfoIndex = mViewFlipper
- .indexOfChild(getActivity().findViewById(R.id.no_info));
- mTabsIndex = mViewFlipper.indexOfChild(getActivity().findViewById(R.id.tabs));
-
- PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit();
- }
-
- @Override
- protected void tearDown() throws Exception {
- PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().clear().commit();
- super.tearDown();
- }
-
- public void testInitialState() throws Throwable {
- onPause();
- onResume();
-
- mFragments = getActivity().getFragments();
- assertThat(mFragments).isEmpty();
- assertThat(mPersonalCard.getVisibility()).isEqualTo(View.GONE);
- assertThat(getActivity().getTabLayout().getVisibility()).isEqualTo(View.GONE);
- assertThat(mViewFlipper.getDisplayedChild()).isEqualTo(mNoInfoIndex);
- }
-
- public void testNameSet() throws Throwable {
- onPause();
-
- final String name = "John";
- PreferenceManager.getDefaultSharedPreferences(getActivity())
- .edit().putString(PreferenceKeys.KEY_NAME, name).commit();
-
- onResume();
-
- mFragments = getActivity().getFragments();
- assertThat(mFragments).isEmpty();
- assertThat(getActivity().getTabLayout().getVisibility()).isEqualTo(View.GONE);
- assertThat(getActivity().findViewById(R.id.no_info).getVisibility())
- .isEqualTo(View.VISIBLE);
- assertThat(mViewFlipper.getDisplayedChild()).isEqualTo(mNoInfoIndex);
- assertThat(mPersonalCardLargeItem.getVisibility()).isEqualTo(View.VISIBLE);
- assertThat(mPersonalCardLargeItem.getText()).isEqualTo(name);
- }
-
- public void testEmergencyInfoSet() throws Throwable {
- onPause();
-
- final String allergies = "Peanuts";
- PreferenceManager.getDefaultSharedPreferences(getActivity())
- .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit();
-
- onResume();
-
- mFragments = getActivity().getFragments();
- assertThat(getActivity().getTabLayout().getVisibility()).isEqualTo(View.GONE);
- assertThat(mViewFlipper.getDisplayedChild()).isEqualTo(mTabsIndex);
- assertThat(mFragments.size()).isEqualTo(1);
- ViewEmergencyInfoFragment viewEmergencyInfoFragment =
- (ViewEmergencyInfoFragment) mFragments.get(0).second;
- assertThat(viewEmergencyInfoFragment).isNotNull();
- }
-
- public void testEmergencyContactSet() throws Throwable {
- onPause();
-
- final String emergencyContact =
- ContactTestUtils.createContact(getActivity().getContentResolver(),
- "John", "123").toString();
- PreferenceManager.getDefaultSharedPreferences(getActivity())
- .edit().putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit();
-
- onResume();
-
- mFragments = getActivity().getFragments();
- assertThat(mViewFlipper.getDisplayedChild()).isEqualTo(mTabsIndex);
- assertThat(mFragments.size()).isEqualTo(1);
- assertThat(getActivity().getTabLayout().getVisibility()).isEqualTo(View.GONE);
- ViewEmergencyContactsFragment viewEmergencyContactsFragment =
- (ViewEmergencyContactsFragment) mFragments.get(0).second;
- assertThat(viewEmergencyContactsFragment).isNotNull();
-
- assertThat(
- ContactTestUtils.deleteContact(getActivity().getContentResolver(), "John", "123"))
- .isTrue();
- }
-
- public void testInfoAndEmergencyContactsSet() throws Throwable {
- onPause();
-
- final String emergencyContact =
- ContactTestUtils.createContact(getActivity().getContentResolver(),
- "John", "123").toString();
- PreferenceManager.getDefaultSharedPreferences(getActivity()).edit()
- .putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit();
-
- final String allergies = "Peanuts";
- PreferenceManager.getDefaultSharedPreferences(getActivity())
- .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit();
-
- onResume();
-
- mFragments = getActivity().getFragments();
- assertThat(mViewFlipper.getDisplayedChild()).isEqualTo(mTabsIndex);
- assertThat(mFragments.size()).isEqualTo(2);
- assertThat(getActivity().getTabLayout().getVisibility()).isEqualTo(View.VISIBLE);
- ViewEmergencyInfoFragment viewEmergencyInfoFragment =
- (ViewEmergencyInfoFragment) mFragments.get(0).second;
- assertThat(viewEmergencyInfoFragment).isNotNull();
- ViewEmergencyContactsFragment viewEmergencyContactsFragment =
- (ViewEmergencyContactsFragment) mFragments.get(1).second;
- assertThat(viewEmergencyContactsFragment).isNotNull();
-
- assertThat(
- ContactTestUtils.deleteContact(getActivity().getContentResolver(), "John", "123"))
- .isTrue();
- }
-
- public void testCanGoToEditInfoActivityFromMenu() {
- final ViewInfoActivity activity = getActivity();
-
- Instrumentation.ActivityMonitor activityMonitor =
- getInstrumentation().addMonitor(EditInfoActivity.class.getName(),
- null /* result */, false /* block */);
-
- activity.getMenu().performIdentifierAction(R.id.action_edit, 0 /* flags */);
-
- EditInfoActivity editInfoActivity = (EditInfoActivity)
- getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 1000 /* timeOut */);
- assertThat(editInfoActivity).isNotNull();
- assertThat(getInstrumentation().checkMonitorHit(activityMonitor, 1 /* minHits */)).isTrue();
- editInfoActivity.finish();
- }
-
- private void onPause() throws Throwable {
- runTestOnUiThread(new Runnable() {
- @Override
- public void run() {
- getInstrumentation().callActivityOnPause(getActivity());
- }
- });
- }
-
- private void onResume() throws Throwable {
- runTestOnUiThread(new Runnable() {
- @Override
- public void run() {
- getInstrumentation().callActivityOnResume(getActivity());
- }
- });
- getInstrumentation().waitForIdleSync();
- }
-}
-
diff --git a/tests/unit/Android.mk b/tests/unit/Android.mk
new file mode 100644
index 0000000..45d6fed
--- /dev/null
+++ b/tests/unit/Android.mk
@@ -0,0 +1,36 @@
+# Copyright (C) 2017 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.
+
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_JAVA_LIBRARIES := android.test.runner
+
+LOCAL_STATIC_JAVA_LIBRARIES := \
+ android-support-test \
+ emergencyinfo-test-common \
+ legacy-android-test \
+ truth-prebuilt
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := EmergencyInfoUnitTests
+LOCAL_COMPATIBILITY_SUITE := device-tests
+LOCAL_CERTIFICATE := platform
+
+LOCAL_INSTRUMENTATION_FOR := EmergencyInfo
+
+include $(BUILD_PACKAGE)
diff --git a/tests/unit/AndroidManifest.xml b/tests/unit/AndroidManifest.xml
new file mode 100644
index 0000000..1f88b22
--- /dev/null
+++ b/tests/unit/AndroidManifest.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2017 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.emergency.tests.unit">
+ <uses-permission android:name="android.permission.WRITE_CONTACTS" />
+ <uses-permission android:name="android.permission.READ_CONTACTS" />
+
+ <application>
+ <uses-library android:name="android.test.runner" />
+ </application>
+
+ <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+ android:targetPackage="com.android.emergency"
+ android:label="Emergency Info Test Cases">
+ </instrumentation>
+
+</manifest>
diff --git a/tests/unit/src/com/android/emergency/view/ViewInfoActivityTest.java b/tests/unit/src/com/android/emergency/view/ViewInfoActivityTest.java
new file mode 100644
index 0000000..e27e526
--- /dev/null
+++ b/tests/unit/src/com/android/emergency/view/ViewInfoActivityTest.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright (C) 2016 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 com.android.emergency.view;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.app.Fragment;
+import android.app.Instrumentation;
+import android.content.Context;
+import android.content.Intent;
+import android.preference.PreferenceManager;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.util.Pair;
+import android.view.View;
+import android.widget.TextView;
+import android.widget.ViewFlipper;
+
+import com.android.emergency.ContactTestUtils;
+import com.android.emergency.PreferenceKeys;
+import com.android.emergency.R;
+import com.android.emergency.edit.EditInfoActivity;
+
+import java.util.ArrayList;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/** Unit tests for {@link ViewInfoActivity}. */
+@RunWith(AndroidJUnit4.class)
+public class ViewInfoActivityTest {
+ private Instrumentation mInstrumentation;
+ private Context mTargetContext;
+ private ViewInfoActivity mActivity;
+
+ @Before
+ public void setUp() {
+ mInstrumentation = InstrumentationRegistry.getInstrumentation();
+ mTargetContext = mInstrumentation.getTargetContext();
+ }
+
+ @After
+ public void tearDown() {
+ PreferenceManager.getDefaultSharedPreferences(mTargetContext).edit().clear().commit();
+ }
+
+ @Test
+ public void testInitialState() {
+ ViewInfoActivity activity = startViewInfoActivity();
+
+ assertThat(activity.getFragments()).isEmpty();
+ assertThat(activity.findViewById(R.id.name_and_dob_linear_layout).getVisibility())
+ .isEqualTo(View.GONE);
+ assertThat(activity.getTabLayout().getVisibility()).isEqualTo(View.GONE);
+
+ ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
+ int noInfoIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.no_info));
+ assertThat(viewFlipper.getDisplayedChild()).isEqualTo(noInfoIndex);
+ }
+
+ @Test
+ public void testNameSet() {
+ final String name = "John";
+ PreferenceManager.getDefaultSharedPreferences(mTargetContext)
+ .edit().putString(PreferenceKeys.KEY_NAME, name).commit();
+
+ ViewInfoActivity activity = startViewInfoActivity();
+
+ assertThat(activity.getFragments()).isEmpty();
+ assertThat(activity.getTabLayout().getVisibility()).isEqualTo(View.GONE);
+ assertThat(activity.findViewById(R.id.no_info).getVisibility())
+ .isEqualTo(View.VISIBLE);
+
+ ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
+ int noInfoIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.no_info));
+ assertThat(viewFlipper.getDisplayedChild()).isEqualTo(noInfoIndex);
+
+ TextView personalCardLargeItem = (TextView) activity.findViewById(R.id.personal_card_large);
+ assertThat(personalCardLargeItem.getVisibility()).isEqualTo(View.VISIBLE);
+ assertThat(personalCardLargeItem.getText()).isEqualTo(name);
+ }
+
+ @Test
+ public void testMedicalInfoSet() {
+ final String allergies = "Peanuts";
+ PreferenceManager.getDefaultSharedPreferences(mTargetContext)
+ .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit();
+
+ ViewInfoActivity activity = startViewInfoActivity();
+
+ assertThat(activity.getTabLayout().getVisibility()).isEqualTo(View.GONE);
+ ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
+ int tabsIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.tabs));
+ assertThat(viewFlipper.getDisplayedChild()).isEqualTo(tabsIndex);
+
+ ArrayList<Pair<String, Fragment>> fragments = activity.getFragments();
+ assertThat(fragments).hasSize(1);
+ ViewEmergencyInfoFragment viewEmergencyInfoFragment =
+ (ViewEmergencyInfoFragment) fragments.get(0).second;
+ assertThat(viewEmergencyInfoFragment).isNotNull();
+ }
+
+ @Test
+ public void testEmergencyContactsSet() {
+ final String emergencyContact =
+ ContactTestUtils.createContact(mTargetContext.getContentResolver(),
+ "John", "123").toString();
+ PreferenceManager.getDefaultSharedPreferences(mTargetContext)
+ .edit().putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit();
+
+ ViewInfoActivity activity = startViewInfoActivity();
+
+ ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
+ int tabsIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.tabs));
+ assertThat(viewFlipper.getDisplayedChild()).isEqualTo(tabsIndex);
+
+ ArrayList<Pair<String, Fragment>> fragments = activity.getFragments();
+ assertThat(fragments).hasSize(1);
+ ViewEmergencyContactsFragment viewEmergencyContactsFragment =
+ (ViewEmergencyContactsFragment) fragments.get(0).second;
+ assertThat(viewEmergencyContactsFragment).isNotNull();
+
+ assertThat(
+ ContactTestUtils.deleteContact(mTargetContext.getContentResolver(), "John", "123"))
+ .isTrue();
+ }
+
+ @Test
+ public void testMedicalInfoAndEmergencyContactsSet() {
+ final String emergencyContact =
+ ContactTestUtils.createContact(mTargetContext.getContentResolver(),
+ "John", "123").toString();
+ PreferenceManager.getDefaultSharedPreferences(mTargetContext)
+ .edit().putString(PreferenceKeys.KEY_EMERGENCY_CONTACTS, emergencyContact).commit();
+ final String allergies = "Peanuts";
+ PreferenceManager.getDefaultSharedPreferences(mTargetContext)
+ .edit().putString(PreferenceKeys.KEY_ALLERGIES, allergies).commit();
+
+ ViewInfoActivity activity = startViewInfoActivity();
+
+ ViewFlipper viewFlipper = (ViewFlipper) activity.findViewById(R.id.view_flipper);
+ int tabsIndex = viewFlipper.indexOfChild(activity.findViewById(R.id.tabs));
+ assertThat(viewFlipper.getDisplayedChild()).isEqualTo(tabsIndex);
+
+ ArrayList<Pair<String, Fragment>> fragments = activity.getFragments();
+ assertThat(fragments).hasSize(2);
+ ViewEmergencyInfoFragment viewEmergencyInfoFragment =
+ (ViewEmergencyInfoFragment) fragments.get(0).second;
+ assertThat(viewEmergencyInfoFragment).isNotNull();
+ ViewEmergencyContactsFragment viewEmergencyContactsFragment =
+ (ViewEmergencyContactsFragment) fragments.get(1).second;
+ assertThat(viewEmergencyContactsFragment).isNotNull();
+
+ assertThat(
+ ContactTestUtils.deleteContact(mTargetContext.getContentResolver(), "John", "123"))
+ .isTrue();
+ }
+
+ @Test
+ public void testCanGoToEditInfoActivityFromMenu() {
+ ViewInfoActivity activity = startViewInfoActivity();
+
+ Instrumentation.ActivityMonitor activityMonitor =
+ mInstrumentation.addMonitor(EditInfoActivity.class.getName(),
+ null /* result */, false /* block */);
+
+ activity.getMenu().performIdentifierAction(R.id.action_edit, 0 /* flags */);
+
+ EditInfoActivity editInfoActivity = (EditInfoActivity)
+ mInstrumentation.waitForMonitorWithTimeout(activityMonitor, 1000 /* timeOut */);
+ assertThat(editInfoActivity).isNotNull();
+ assertThat(mInstrumentation.checkMonitorHit(activityMonitor, 1 /* minHits */)).isTrue();
+ editInfoActivity.finish();
+ }
+
+ private ViewInfoActivity startViewInfoActivity() {
+ final Intent viewActivityIntent = new Intent(mTargetContext, ViewInfoActivity.class);
+ return (ViewInfoActivity) mInstrumentation.startActivitySync(viewActivityIntent);
+ }
+}