Fix test flakiness caused by not cleaning up test accounts
Test: ran GoogleContactsTests
Change-Id: I3bfc9e3789a421f7ff8df8373c8c443c02fa407a
diff --git a/tests/src/com/android/contacts/activities/SimImportActivityTest.java b/tests/src/com/android/contacts/activities/SimImportActivityTest.java
index 64b86e2..46b5f40 100644
--- a/tests/src/com/android/contacts/activities/SimImportActivityTest.java
+++ b/tests/src/com/android/contacts/activities/SimImportActivityTest.java
@@ -68,6 +68,7 @@
import com.google.common.util.concurrent.SettableFuture;
import org.junit.After;
+import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -120,6 +121,12 @@
}
}
+ @AfterClass
+ public static void tearDownClass() {
+ AccountsTestHelper.removeAccountsWithPrefix(
+ InstrumentationRegistry.getTargetContext(), "SimImportActivity");
+ }
+
@Test
public void shouldDisplaySimContacts() {
mDao.addSim(someSimCard(),
@@ -214,7 +221,7 @@
AccountTypeManager.setInstanceForTest(null);
final AccountWithDataSet targetAccount = mAccountHelper.addTestAccount(
- mAccountHelper.generateAccountName("SimImportActivity_target_"));
+ mAccountHelper.generateAccountName("SimImportActivity0_targetAccount_"));
final MockContentProvider iccProvider = new MockContentProvider();
iccProvider.expect(MockContentProvider.Query.forAnyUri())
@@ -252,7 +259,7 @@
assertTrue(mDevice.wait(Until.hasObject(By.desc("Show more")), TIMEOUT));
mDevice.findObject(By.desc("Show more")).clickAndWait(Until.newWindow(), TIMEOUT);
- mDevice.findObject(By.textStartsWith("SimImportActivity_target_")).click();
+ mDevice.findObject(By.textContains("_targetAccount_")).click();
assertTrue(mDevice.wait(Until.hasObject(By.text("Skip Two")), TIMEOUT));
diff --git a/tests/src/com/android/contacts/tests/AccountsTestHelper.java b/tests/src/com/android/contacts/tests/AccountsTestHelper.java
index be0a985..2b2c16e 100644
--- a/tests/src/com/android/contacts/tests/AccountsTestHelper.java
+++ b/tests/src/com/android/contacts/tests/AccountsTestHelper.java
@@ -45,7 +45,6 @@
private final ContentResolver mResolver;
private List<Account> mAddedAccounts;
- private Account mTestAccount;
public AccountsTestHelper() {
// Use context instead of target context because the test package has the permissions needed
@@ -61,9 +60,9 @@
}
public void addTestAccount(AccountWithDataSet account) {
- mTestAccount = new Account(account.name, TEST_ACCOUNT_TYPE);
- assertTrue(mAccountManager.addAccountExplicitly(mTestAccount, null, null));
- mAddedAccounts.add(mTestAccount);
+ Account newAccount = new Account(account.name, account.type);
+ assertTrue(mAccountManager.addAccountExplicitly(newAccount, null, null));
+ mAddedAccounts.add(newAccount);
}
public AccountWithDataSet addTestAccount() {
@@ -104,11 +103,6 @@
return accounts.contains(new Account(name, TEST_ACCOUNT_TYPE));
}
- public void removeContactsForAccount() {
- removeContactsForAccount(
- new AccountWithDataSet(mTestAccount.name, mTestAccount.type, null));
- }
-
public void removeContactsForAccount(AccountWithDataSet account) {
mResolver.delete(RawContacts.CONTENT_URI,
RawContacts.ACCOUNT_NAME + "=? AND " + RawContacts.ACCOUNT_TYPE + "=?",
@@ -117,8 +111,6 @@
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
public void cleanup() {
- assertNotNull(mTestAccount);
-
// Note that we don't need to cleanup up the contact data associated with the account.
// CP2 will eventually do that automatically so as long as we're using unique account
// names we should be safe. Note that cleanup is not done synchronously when the account
@@ -129,11 +121,19 @@
mAccountManager.removeAccountExplicitly(account);
}
mAddedAccounts.clear();
-
- mTestAccount = null;
}
- private AccountWithDataSet convertTestAccount() {
- return new AccountWithDataSet(mTestAccount.name, mTestAccount.type, null);
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
+ public static void removeAccountsWithPrefix(Context context, String prefix) {
+ final AccountManager accountManager =
+ (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
+ final Account[] accounts = accountManager.getAccountsByType(TEST_ACCOUNT_TYPE);
+ for (Account account : accounts) {
+ if (account.name.startsWith(prefix)) {
+ accountManager.removeAccountExplicitly(account);
+ }
+ }
+
+
}
}