Marcus Hagerott | a7d3c00 | 2016-12-05 15:56:10 -0800 | [diff] [blame] | 1 | package com.android.contacts.activities; |
| 2 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 3 | import static com.android.contacts.tests.ContactsMatchers.DataCursor.hasMimeType; |
| 4 | import static com.android.contacts.tests.ContactsMatchers.hasRowMatching; |
| 5 | import static com.android.contacts.tests.ContactsMatchers.hasValueForColumn; |
| 6 | |
| 7 | import static org.hamcrest.MatcherAssert.assertThat; |
| 8 | import static org.hamcrest.Matchers.allOf; |
| 9 | import static org.junit.Assert.assertTrue; |
| 10 | |
Marcus Hagerott | a7d3c00 | 2016-12-05 15:56:10 -0800 | [diff] [blame] | 11 | import android.annotation.TargetApi; |
| 12 | import android.app.Activity; |
| 13 | import android.app.Instrumentation; |
| 14 | import android.content.Context; |
| 15 | import android.content.Intent; |
| 16 | import android.content.pm.ActivityInfo; |
| 17 | import android.database.Cursor; |
| 18 | import android.os.Build; |
| 19 | import android.provider.ContactsContract; |
| 20 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 21 | import android.provider.ContactsContract.Data; |
| 22 | import android.support.test.InstrumentationRegistry; |
| 23 | import android.support.test.filters.MediumTest; |
| 24 | import android.support.test.filters.SdkSuppress; |
| 25 | import android.support.test.filters.Suppress; |
| 26 | import android.support.test.runner.AndroidJUnit4; |
| 27 | import android.support.test.uiautomator.By; |
| 28 | import android.support.test.uiautomator.UiDevice; |
| 29 | import android.support.test.uiautomator.Until; |
| 30 | |
| 31 | import com.android.contacts.database.SimContactDao; |
| 32 | import com.android.contacts.model.AccountTypeManager; |
| 33 | import com.android.contacts.model.SimCard; |
| 34 | import com.android.contacts.model.SimContact; |
| 35 | import com.android.contacts.model.account.AccountWithDataSet; |
| 36 | import com.android.contacts.tests.AccountsTestHelper; |
| 37 | import com.android.contacts.tests.ContactsMatchers; |
| 38 | import com.android.contacts.tests.FakeSimContactDao; |
| 39 | import com.android.contacts.tests.StringableCursor; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 40 | |
Marcus Hagerott | a7d3c00 | 2016-12-05 15:56:10 -0800 | [diff] [blame] | 41 | import com.google.common.base.Functions; |
| 42 | |
| 43 | import org.hamcrest.BaseMatcher; |
| 44 | import org.hamcrest.Description; |
| 45 | import org.hamcrest.Matcher; |
| 46 | import org.junit.After; |
| 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
| 49 | import org.junit.runner.RunWith; |
| 50 | |
Marcus Hagerott | a7d3c00 | 2016-12-05 15:56:10 -0800 | [diff] [blame] | 51 | /** |
| 52 | * UI Tests for {@link SimImportActivity} |
| 53 | * |
| 54 | * These should probably be converted to espresso tests because espresso does a better job of |
| 55 | * waiting for the app to be idle once espresso library is added |
| 56 | */ |
| 57 | @MediumTest |
| 58 | @RunWith(AndroidJUnit4.class) |
| 59 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.M) |
| 60 | @TargetApi(Build.VERSION_CODES.M) |
| 61 | public class SimImportActivityTest { |
| 62 | |
| 63 | public static final int TIMEOUT = 1000; |
| 64 | private Context mContext; |
| 65 | private UiDevice mDevice; |
| 66 | private Instrumentation mInstrumentation; |
| 67 | private FakeSimContactDao mDao; |
| 68 | private AccountsTestHelper mAccountHelper; |
| 69 | |
| 70 | @Before |
| 71 | public void setUp() throws Exception { |
| 72 | mContext = InstrumentationRegistry.getTargetContext(); |
| 73 | mInstrumentation = InstrumentationRegistry.getInstrumentation(); |
| 74 | mDao = new FakeSimContactDao(); |
| 75 | SimContactDao.setFactoryForTest(Functions.<SimContactDao>constant(mDao)); |
| 76 | mDevice = UiDevice.getInstance(mInstrumentation); |
| 77 | |
| 78 | // Add some test accounts so that account picker is exercised |
| 79 | mAccountHelper = new AccountsTestHelper(); |
| 80 | mAccountHelper.addTestAccount(mAccountHelper.generateAccountName("SimImportActivity1_")); |
| 81 | mAccountHelper.addTestAccount(mAccountHelper.generateAccountName("SimImportActivity2_")); |
| 82 | mAccountHelper.addTestAccount(mAccountHelper.generateAccountName("SimImportActivity3_")); |
| 83 | } |
| 84 | |
| 85 | @After |
| 86 | public void tearDown() throws Exception { |
| 87 | SimContactDao.setFactoryForTest(SimContactDao.DEFAULT_FACTORY); |
| 88 | mAccountHelper.cleanup(); |
| 89 | AccountTypeManager.setInstanceForTest(null); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void shouldDisplaySimContacts() { |
| 94 | mDao.addSim(someSimCard(), |
| 95 | new SimContact(1, "Sim One", "5550101"), |
| 96 | new SimContact(2, "Sim Two", null), |
| 97 | new SimContact(3, null, "5550103") |
| 98 | ); |
| 99 | mInstrumentation.startActivitySync(new Intent(mContext, SimImportActivity.class) |
| 100 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 101 | |
| 102 | mDevice.waitForIdle(); |
| 103 | |
| 104 | assertTrue(mDevice.hasObject(By.text("Sim One"))); |
| 105 | assertTrue(mDevice.hasObject(By.text("Sim Two"))); |
| 106 | assertTrue(mDevice.hasObject(By.text("5550103"))); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void shouldHaveEmptyState() { |
| 111 | mDao.addSim(someSimCard()); |
| 112 | |
| 113 | mInstrumentation.startActivitySync(new Intent(mContext, SimImportActivity.class) |
| 114 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 115 | |
| 116 | mDevice.waitForIdle(); |
| 117 | |
| 118 | assertTrue(mDevice.hasObject(By.textStartsWith("No contacts"))); |
| 119 | } |
| 120 | |
| 121 | @Test |
| 122 | public void smokeRotateInEmptyState() { |
| 123 | mDao.addSim(someSimCard()); |
| 124 | |
| 125 | final Activity activity = mInstrumentation.startActivitySync( |
| 126 | new Intent(mContext, SimImportActivity.class) |
| 127 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 128 | |
| 129 | assertTrue(mDevice.wait(Until.hasObject(By.textStartsWith("No contacts")), TIMEOUT)); |
| 130 | |
| 131 | activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
| 132 | |
| 133 | mDevice.waitForIdle(); |
| 134 | |
| 135 | assertTrue(mDevice.hasObject(By.textStartsWith("No contacts"))); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void smokeRotateInNonEmptyState() throws Exception { |
| 140 | mDao.addSim(someSimCard(), new SimContact(1, "Name One", "5550101"), |
| 141 | new SimContact(2, "Name Two", "5550102")); |
| 142 | |
| 143 | final Activity activity = mInstrumentation.startActivitySync( |
| 144 | new Intent(mContext, SimImportActivity.class) |
| 145 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 146 | |
| 147 | assertTrue(mDevice.wait(Until.hasObject(By.textStartsWith("Name One")), TIMEOUT)); |
| 148 | |
| 149 | activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
| 150 | |
| 151 | mDevice.waitForIdle(); |
| 152 | |
| 153 | assertTrue(mDevice.hasObject(By.textStartsWith("Name One"))); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | // TODO: fix this test. This doesn't work because AccountTypeManager returns a stale account |
| 158 | // list (it doesn't contain the accounts added during the current test run). |
| 159 | // Could use MockAccountTypeManager but probably ought to look at improving how |
| 160 | // AccountTypeManager updates it's account list. |
| 161 | @Suppress |
| 162 | @Test |
| 163 | public void selectionsAreImportedAndDisabledOnSubsequentViews() throws Exception { |
| 164 | // Clear out the instance so that it will have the most recent accounts when reloaded |
| 165 | AccountTypeManager.setInstanceForTest(null); |
| 166 | |
| 167 | final AccountWithDataSet targetAccount = mAccountHelper.addTestAccount( |
| 168 | mAccountHelper.generateAccountName("SimImportActivity_target_")); |
| 169 | |
| 170 | mDao.addSim(someSimCard(), |
| 171 | new SimContact(1, "Import One", "5550101"), |
| 172 | new SimContact(2, "Skip Two", "5550102"), |
| 173 | new SimContact(3, "Import Three", "5550103"), |
| 174 | new SimContact(4, "Skip Four", "5550104"), |
| 175 | new SimContact(5, "Skip Five", "5550105"), |
| 176 | new SimContact(6, "Import Six", "5550106")); |
| 177 | |
| 178 | final Activity activity = mInstrumentation.startActivitySync( |
| 179 | new Intent(mContext, SimImportActivity.class) |
| 180 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 181 | |
| 182 | assertTrue(mDevice.wait(Until.hasObject(By.desc("Show more")), TIMEOUT)); |
| 183 | |
| 184 | mDevice.findObject(By.desc("Show more")).clickAndWait(Until.newWindow(), TIMEOUT); |
| 185 | mDevice.findObject(By.textStartsWith("SimImportActivity_target_")).click(); |
| 186 | mDevice.waitForIdle(); |
| 187 | |
| 188 | mDevice.findObject(By.text("Skip Two")).click(); |
| 189 | mDevice.findObject(By.text("Skip Five")).click(); |
| 190 | |
| 191 | assertTrue(mDevice.hasObject(By.text("Skip Two").checked(false))); |
| 192 | assertTrue(mDevice.hasObject(By.text("Skip Five").checked(false))); |
| 193 | |
| 194 | activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
| 195 | mDevice.wait(Until.hasObject(By.text("Import One")), TIMEOUT); |
| 196 | activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); |
| 197 | mDevice.wait(Until.hasObject(By.text("Import One")), TIMEOUT); |
| 198 | |
| 199 | mDevice.findObject(By.text("IMPORT").clickable(true)).click(); |
| 200 | mDevice.waitForIdle(); |
| 201 | |
| 202 | final Cursor cursor = new StringableCursor( |
| 203 | mContext.getContentResolver().query(Data.CONTENT_URI, null, |
| 204 | ContactsContract.RawContacts.ACCOUNT_NAME + "=? AND " + |
| 205 | ContactsContract.RawContacts.ACCOUNT_TYPE+ "=?", |
| 206 | new String[] { |
| 207 | targetAccount.name, |
| 208 | targetAccount.type |
| 209 | }, null)); |
| 210 | // 3 contacts imported with one row for name and one for phone |
| 211 | assertThat(cursor, ContactsMatchers.hasCount(3 * 2)); |
| 212 | |
| 213 | assertThat(cursor, hasRowMatching(allOf( |
| 214 | hasMimeType(Phone.CONTENT_ITEM_TYPE), |
| 215 | hasValueForColumn(Phone.DISPLAY_NAME, "Import One"), |
| 216 | hasValueForColumn(Phone.NUMBER, "5550101") |
| 217 | ))); |
| 218 | assertThat(cursor, hasRowMatching(allOf( |
| 219 | hasMimeType(Phone.CONTENT_ITEM_TYPE), |
| 220 | hasValueForColumn(Phone.DISPLAY_NAME, "Import Three"), |
| 221 | hasValueForColumn(Phone.NUMBER, "5550103") |
| 222 | ))); |
| 223 | assertThat(cursor, hasRowMatching(allOf( |
| 224 | hasMimeType(Phone.CONTENT_ITEM_TYPE), |
| 225 | hasValueForColumn(Phone.DISPLAY_NAME, "Import Six"), |
| 226 | hasValueForColumn(Phone.NUMBER, "5550106") |
| 227 | ))); |
| 228 | |
| 229 | cursor.close(); |
| 230 | |
| 231 | |
| 232 | mInstrumentation.startActivitySync( |
| 233 | new Intent(mContext, SimImportActivity.class) |
| 234 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 235 | |
| 236 | assertTrue(mDevice.wait(Until.hasObject(By.text("Import One")), TIMEOUT)); |
| 237 | |
| 238 | mDevice.findObject(By.descStartsWith("Saving to")).clickAndWait(Until.newWindow(), TIMEOUT); |
| 239 | mDevice.findObject(By.textContains(targetAccount.name)).click(); |
| 240 | mDevice.waitForIdle(); |
| 241 | |
| 242 | assertTrue(mDevice.hasObject(By.text("Import One").checked(false).enabled(false))); |
| 243 | assertTrue(mDevice.hasObject(By.text("Import Three").checked(false).enabled(false))); |
| 244 | assertTrue(mDevice.hasObject(By.text("Import Six").checked(false).enabled(false))); |
| 245 | } |
| 246 | |
| 247 | private SimCard someSimCard() { |
| 248 | return new SimCard("id", 1, "Carrier", "SIM", "18005550101", "us"); |
| 249 | } |
| 250 | |
| 251 | private Matcher<SimContact> withContactId(final long id) { |
| 252 | return new BaseMatcher<SimContact>() { |
| 253 | @Override |
| 254 | public boolean matches(Object o) { |
| 255 | return (o instanceof SimContact) && ((SimContact) o).getId() == id; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | @Override |
| 260 | public void describeTo(Description description) { |
| 261 | description.appendText("Expected SimContact with id=" + id); |
| 262 | } |
| 263 | }; |
| 264 | } |
| 265 | } |