diff options
author | Chiao Cheng <chiaocheng@google.com> | 2012-11-13 18:38:05 -0800 |
---|---|---|
committer | Chiao Cheng <chiaocheng@google.com> | 2012-11-13 18:38:05 -0800 |
commit | 273399bf829133a8385332ad43add3c34c889102 (patch) | |
tree | ee74b9e3d8d4269818d3db900a70615d2a8d9942 /tests/src/com/android/contacts | |
parent | edefd0315fe9c8e5bfd25f0abb538f861b3d9e39 (diff) | |
download | android_packages_apps_ContactsCommon-273399bf829133a8385332ad43add3c34c889102.tar.gz android_packages_apps_ContactsCommon-273399bf829133a8385332ad43add3c34c889102.tar.bz2 android_packages_apps_ContactsCommon-273399bf829133a8385332ad43add3c34c889102.zip |
Move dependencies of AccountTypeManager into ContactsCommon.
Moving dependencies in preparation to move AccountTypeManager.
Bug: 6993891
Change-Id: I10893209986efd288315dc6b51c7971838ac3923
Diffstat (limited to 'tests/src/com/android/contacts')
8 files changed, 813 insertions, 0 deletions
diff --git a/tests/src/com/android/contacts/common/model/AccountWithDataSetTest.java b/tests/src/com/android/contacts/common/model/AccountWithDataSetTest.java new file mode 100644 index 00000000..e28f09e8 --- /dev/null +++ b/tests/src/com/android/contacts/common/model/AccountWithDataSetTest.java @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2011 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.contacts.common.model; + +import android.os.Bundle; +import android.test.AndroidTestCase; +import android.test.MoreAsserts; +import android.test.suitebuilder.annotation.SmallTest; + +import com.android.contacts.common.model.account.AccountWithDataSet; +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * Test case for {@link AccountWithDataSet}. + * + * adb shell am instrument -w -e class com.android.contacts.model.AccountWithDataSetTest \ + com.android.contacts.tests/android.test.InstrumentationTestRunner + */ +@SmallTest +public class AccountWithDataSetTest extends AndroidTestCase { + public void testStringifyAndUnstringify() { + AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null); + AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null); + AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset"); + + // stringify() & unstringify + AccountWithDataSet a1r = AccountWithDataSet.unstringify(a1.stringify()); + AccountWithDataSet a2r = AccountWithDataSet.unstringify(a2.stringify()); + AccountWithDataSet a3r = AccountWithDataSet.unstringify(a3.stringify()); + + assertEquals(a1, a1r); + assertEquals(a2, a2r); + assertEquals(a3, a3r); + + MoreAsserts.assertNotEqual(a1, a2r); + MoreAsserts.assertNotEqual(a1, a3r); + + MoreAsserts.assertNotEqual(a2, a1r); + MoreAsserts.assertNotEqual(a2, a3r); + + MoreAsserts.assertNotEqual(a3, a1r); + MoreAsserts.assertNotEqual(a3, a2r); + } + + public void testStringifyListAndUnstringify() { + AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null); + AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null); + AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset"); + + // Empty list + assertEquals(0, stringifyListAndUnstringify().size()); + + // 1 element + final List<AccountWithDataSet> listA = stringifyListAndUnstringify(a1); + assertEquals(1, listA.size()); + assertEquals(a1, listA.get(0)); + + // 2 elements + final List<AccountWithDataSet> listB = stringifyListAndUnstringify(a2, a1); + assertEquals(2, listB.size()); + assertEquals(a2, listB.get(0)); + assertEquals(a1, listB.get(1)); + + // 3 elements + final List<AccountWithDataSet> listC = stringifyListAndUnstringify(a3, a2, a1); + assertEquals(3, listC.size()); + assertEquals(a3, listC.get(0)); + assertEquals(a2, listC.get(1)); + assertEquals(a1, listC.get(2)); + } + + private static List<AccountWithDataSet> stringifyListAndUnstringify( + AccountWithDataSet... accounts) { + + List<AccountWithDataSet> list = Lists.newArrayList(accounts); + return AccountWithDataSet.unstringifyList(AccountWithDataSet.stringifyList(list)); + } + + public void testParcelable() { + AccountWithDataSet a1 = new AccountWithDataSet("name1", "typeA", null); + AccountWithDataSet a2 = new AccountWithDataSet("name2", "typeB", null); + AccountWithDataSet a3 = new AccountWithDataSet("name3", "typeB", "dataset"); + + // Parcel them & unpercel. + final Bundle b = new Bundle(); + b.putParcelable("a1", a1); + b.putParcelable("a2", a2); + b.putParcelable("a3", a3); + + AccountWithDataSet a1r = b.getParcelable("a1"); + AccountWithDataSet a2r = b.getParcelable("a2"); + AccountWithDataSet a3r = b.getParcelable("a3"); + + assertEquals(a1, a1r); + assertEquals(a2, a2r); + assertEquals(a3, a3r); + + MoreAsserts.assertNotEqual(a1, a2r); + MoreAsserts.assertNotEqual(a1, a3r); + + MoreAsserts.assertNotEqual(a2, a1r); + MoreAsserts.assertNotEqual(a2, a3r); + + MoreAsserts.assertNotEqual(a3, a1r); + MoreAsserts.assertNotEqual(a3, a2r); + } +} diff --git a/tests/src/com/android/contacts/common/model/account/AccountTypeTest.java b/tests/src/com/android/contacts/common/model/account/AccountTypeTest.java new file mode 100644 index 00000000..4374ad36 --- /dev/null +++ b/tests/src/com/android/contacts/common/model/account/AccountTypeTest.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2011 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.contacts.common.model.account; + +import android.content.Context; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +import com.android.contacts.common.unittest.R; + +/** + * Test case for {@link AccountType}. + * + * adb shell am instrument -w -e class com.android.contacts.model.AccountTypeTest \ + com.android.contacts.tests/android.test.InstrumentationTestRunner + */ +@SmallTest +public class AccountTypeTest extends AndroidTestCase { + public void testGetResourceText() { + // In this test we use the test package itself as an external package. + final String packageName = getTestContext().getPackageName(); + + final Context c = getContext(); + final String DEFAULT = "ABC"; + + // Package name null, resId -1, use the default + assertEquals(DEFAULT, AccountType.getResourceText(c, null, -1, DEFAULT)); + + // Resource ID -1, use the default + assertEquals(DEFAULT, AccountType.getResourceText(c, packageName, -1, DEFAULT)); + + // Load from an external package. (here, we use this test package itself) + final int externalResID = R.string.test_string; + assertEquals(getTestContext().getString(externalResID), + AccountType.getResourceText(c, packageName, externalResID, DEFAULT)); + + // Load from the contacts package itself. + final int internalResId = com.android.contacts.common.R.string.contactsList; + assertEquals(c.getString(internalResId), + AccountType.getResourceText(c, null, internalResId, DEFAULT)); + } + + /** + * Verify if {@link AccountType#getInviteContactActionLabel} correctly gets the resource ID + * from {@link AccountType#getInviteContactActionResId} + */ + public void testGetInviteContactActionLabel() { + final String packageName = getTestContext().getPackageName(); + final Context c = getContext(); + + final int externalResID = R.string.test_string; + + AccountType accountType = new AccountType() { + { + resourcePackageName = packageName; + syncAdapterPackageName = packageName; + } + @Override protected int getInviteContactActionResId() { + return externalResID; + } + + @Override public boolean isGroupMembershipEditable() { + return false; + } + + @Override public boolean areContactsWritable() { + return false; + } + }; + + assertEquals(getTestContext().getString(externalResID), + accountType.getInviteContactActionLabel(c)); + } + + public void testDisplayLabelComparator() { + final AccountTypeForDisplayLabelTest EMPTY = new AccountTypeForDisplayLabelTest(""); + final AccountTypeForDisplayLabelTest NULL = new AccountTypeForDisplayLabelTest(null); + final AccountTypeForDisplayLabelTest AA = new AccountTypeForDisplayLabelTest("aa"); + final AccountTypeForDisplayLabelTest BBB = new AccountTypeForDisplayLabelTest("bbb"); + final AccountTypeForDisplayLabelTest C = new AccountTypeForDisplayLabelTest("c"); + + assertTrue(compareDisplayLabel(AA, BBB) < 0); + assertTrue(compareDisplayLabel(BBB, C) < 0); + assertTrue(compareDisplayLabel(AA, C) < 0); + assertTrue(compareDisplayLabel(AA, AA) == 0); + assertTrue(compareDisplayLabel(BBB, AA) > 0); + + assertTrue(compareDisplayLabel(EMPTY, AA) < 0); + assertTrue(compareDisplayLabel(EMPTY, NULL) == 0); + } + + private int compareDisplayLabel(AccountType lhs, AccountType rhs) { + return new AccountType.DisplayLabelComparator(getContext()).compare(lhs, rhs); + } + + private class AccountTypeForDisplayLabelTest extends AccountType { + private final String mDisplayLabel; + + public AccountTypeForDisplayLabelTest(String displayLabel) { + mDisplayLabel = displayLabel; + } + + @Override + public CharSequence getDisplayLabel(Context context) { + return mDisplayLabel; + } + + @Override + public boolean isGroupMembershipEditable() { + return false; + } + + @Override + public boolean areContactsWritable() { + return false; + } + } +} diff --git a/tests/src/com/android/contacts/common/model/account/ExternalAccountTypeTest.java b/tests/src/com/android/contacts/common/model/account/ExternalAccountTypeTest.java new file mode 100644 index 00000000..56ee8321 --- /dev/null +++ b/tests/src/com/android/contacts/common/model/account/ExternalAccountTypeTest.java @@ -0,0 +1,251 @@ +/* + * Copyright (C) 2011 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.contacts.common.model.account; + +import android.content.Context; +import android.provider.ContactsContract.CommonDataKinds.Email; +import android.provider.ContactsContract.CommonDataKinds.Event; +import android.provider.ContactsContract.CommonDataKinds.Im; +import android.provider.ContactsContract.CommonDataKinds.Note; +import android.provider.ContactsContract.CommonDataKinds.Organization; +import android.provider.ContactsContract.CommonDataKinds.Photo; +import android.provider.ContactsContract.CommonDataKinds.Relation; +import android.provider.ContactsContract.CommonDataKinds.SipAddress; +import android.provider.ContactsContract.CommonDataKinds.StructuredName; +import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; +import android.provider.ContactsContract.CommonDataKinds.Website; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; +import android.util.Log; + +import com.android.contacts.common.model.dataitem.DataKind; +import com.android.contacts.common.unittest.R; +import com.google.common.base.Objects; + +import java.util.List; + +/** + * Test case for {@link com.android.contacts.common.model.account.ExternalAccountType}. + * + * adb shell am instrument -w -e class com.android.contacts.model.ExternalAccountTypeTest \ + com.android.contacts.tests/android.test.InstrumentationTestRunner + */ +@SmallTest +public class ExternalAccountTypeTest extends AndroidTestCase { + public void testResolveExternalResId() { + final Context c = getContext(); + // In this test we use the test package itself as an external package. + final String packageName = getTestContext().getPackageName(); + + // Resource name empty. + assertEquals(-1, ExternalAccountType.resolveExternalResId(c, null, packageName, "")); + assertEquals(-1, ExternalAccountType.resolveExternalResId(c, "", packageName, "")); + + // Name doesn't begin with '@' + assertEquals(-1, ExternalAccountType.resolveExternalResId(c, "x", packageName, "")); + + // Invalid resource name + assertEquals(-1, ExternalAccountType.resolveExternalResId(c, "@", packageName, "")); + assertEquals(-1, ExternalAccountType.resolveExternalResId(c, "@a", packageName, "")); + assertEquals(-1, ExternalAccountType.resolveExternalResId(c, "@a/b", packageName, "")); + + // Valid resource name + assertEquals(R.string.test_string, ExternalAccountType.resolveExternalResId(c, + "@string/test_string", packageName, "")); + } + + /** + * Initialize with an invalid package name and see if type type will *not* be initialized. + */ + public void testNoPackage() { + final ExternalAccountType type = new ExternalAccountType(getContext(), + "!!!no such package name!!!", false); + assertFalse(type.isInitialized()); + } + + /** + * Initialize with the name of an existing package, which has no contacts.xml metadata. + */ + /* + public void testNoMetadata() { + // Use the main application package, which does exist, but has no contacts.xml in it. + String packageName = getContext().getPackageName(); + Log.e("TEST", packageName); + final ExternalAccountType type = new ExternalAccountType(getContext(), + packageName, false); + assertTrue(type.isInitialized()); + } + */ + + /** + * Initialize with the test package itself and see if EditSchema is correctly parsed. + */ + public void testEditSchema() { + final ExternalAccountType type = new ExternalAccountType(getContext(), + getTestContext().getPackageName(), false); + + assertTrue(type.isInitialized()); + + // Let's just check if the DataKinds are registered. + assertNotNull(type.getKindForMimetype(StructuredName.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME)); + assertNotNull(type.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME)); + assertNotNull(type.getKindForMimetype(Email.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(StructuredPostal.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(Im.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(Organization.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(Photo.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(Note.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(Website.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(SipAddress.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(Event.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(Relation.CONTENT_ITEM_TYPE)); + } + + /** + * Initialize with "contacts_fallback.xml" and compare the DataKinds to those of + * {@link com.android.contacts.common.model.account.FallbackAccountType}. + */ + public void testEditSchema_fallback() { + final ExternalAccountType type = new ExternalAccountType(getContext(), + getTestContext().getPackageName(), false, + getTestContext().getResources().getXml(R.xml.contacts_fallback) + ); + + assertTrue(type.isInitialized()); + + // Create a fallback type with the same resource package name, and compare all the data + // kinds to its. + final AccountType reference = FallbackAccountType.createWithPackageNameForTest( + getContext(), type.resourcePackageName); + + assertsDataKindEquals(reference.getSortedDataKinds(), type.getSortedDataKinds()); + } + + public void testEditSchema_mustHaveChecks() { + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_base, true); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_photo, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name_attr1, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name_attr2, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name_attr3, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name_attr4, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name_attr5, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name_attr6, false); + checkEditSchema_mustHaveChecks(R.xml.missing_contacts_name_attr7, false); + } + + private void checkEditSchema_mustHaveChecks(int xmlResId, boolean expectInitialized) { + final ExternalAccountType type = new ExternalAccountType(getContext(), + getTestContext().getPackageName(), false, + getTestContext().getResources().getXml(xmlResId) + ); + + assertEquals(expectInitialized, type.isInitialized()); + } + + /** + * Initialize with "contacts_readonly.xml" and see if all data kinds are correctly registered. + */ + public void testReadOnlyDefinition() { + final ExternalAccountType type = new ExternalAccountType(getContext(), + getTestContext().getPackageName(), false, + getTestContext().getResources().getXml(R.xml.contacts_readonly) + ); + assertTrue(type.isInitialized()); + + // Shouldn't have a "null" mimetype. + assertTrue(type.getKindForMimetype(null) == null); + + // 3 kinds are defined in XML and 4 are added by default. + assertEquals(4 + 3, type.getSortedDataKinds().size()); + + // Check for the default kinds. + assertNotNull(type.getKindForMimetype(StructuredName.CONTENT_ITEM_TYPE)); + assertNotNull(type.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_DISPLAY_NAME)); + assertNotNull(type.getKindForMimetype(DataKind.PSEUDO_MIME_TYPE_PHONETIC_NAME)); + assertNotNull(type.getKindForMimetype(Photo.CONTENT_ITEM_TYPE)); + + // Check for type specific kinds. + DataKind kind = type.getKindForMimetype("vnd.android.cursor.item/a.b.c"); + assertNotNull(kind); + // No check for icon -- we actually just ignore it. + assertEquals("data1", ((BaseAccountType.SimpleInflater) kind.actionHeader) + .getColumnNameForTest()); + assertEquals("data2", ((BaseAccountType.SimpleInflater) kind.actionBody) + .getColumnNameForTest()); + assertEquals(true, kind.actionBodySocial); + + kind = type.getKindForMimetype("vnd.android.cursor.item/d.e.f"); + assertNotNull(kind); + assertEquals("data3", ((BaseAccountType.SimpleInflater) kind.actionHeader) + .getColumnNameForTest()); + assertEquals("data4", ((BaseAccountType.SimpleInflater) kind.actionBody) + .getColumnNameForTest()); + assertEquals(false, kind.actionBodySocial); + + kind = type.getKindForMimetype("vnd.android.cursor.item/xyz"); + assertNotNull(kind); + assertEquals("data5", ((BaseAccountType.SimpleInflater) kind.actionHeader) + .getColumnNameForTest()); + assertEquals("data6", ((BaseAccountType.SimpleInflater) kind.actionBody) + .getColumnNameForTest()); + assertEquals(true, kind.actionBodySocial); + } + + private static void assertsDataKindEquals(List<DataKind> expectedKinds, + List<DataKind> actualKinds) { + final int count = Math.max(actualKinds.size(), expectedKinds.size()); + for (int i = 0; i < count; i++) { + String actual = actualKinds.size() > i ? actualKinds.get(i).toString() : "(n/a)"; + String expected = expectedKinds.size() > i ? expectedKinds.get(i).toString() : "(n/a)"; + + // Because assertEquals()'s output is not very friendly when comparing two similar + // strings, we manually do the check. + if (!Objects.equal(actual, expected)) { + final int commonPrefixEnd = findCommonPrefixEnd(actual, expected); + fail("Kind #" + i + + "\n[Actual]\n" + insertMarkerAt(actual, commonPrefixEnd) + + "\n[Expected]\n" + insertMarkerAt(expected, commonPrefixEnd)); + } + } + } + + private static int findCommonPrefixEnd(String s1, String s2) { + int i = 0; + for (;;) { + final boolean s1End = (s1.length() <= i); + final boolean s2End = (s2.length() <= i); + if (s1End || s2End) { + return i; + } + if (s1.charAt(i) != s2.charAt(i)) { + return i; + } + i++; + } + } + + private static String insertMarkerAt(String s, int position) { + final String MARKER = "***"; + if (position > s.length()) { + return s + MARKER; + } else { + return new StringBuilder(s).insert(position, MARKER).toString(); + } + } +} diff --git a/tests/src/com/android/contacts/common/tests/testauth/TestAuthenticationService.java b/tests/src/com/android/contacts/common/tests/testauth/TestAuthenticationService.java new file mode 100644 index 00000000..93d1f4a9 --- /dev/null +++ b/tests/src/com/android/contacts/common/tests/testauth/TestAuthenticationService.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2011 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.contacts.common.tests.testauth; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; +import android.util.Log; + +public abstract class TestAuthenticationService extends Service { + + private TestAuthenticator mAuthenticator; + + @Override + public void onCreate() { + Log.v(TestauthConstants.LOG_TAG, this + " Service started."); + mAuthenticator = new TestAuthenticator(this); + } + + @Override + public void onDestroy() { + Log.v(TestauthConstants.LOG_TAG, this + " Service stopped."); + } + + @Override + public IBinder onBind(Intent intent) { + Log.v(TestauthConstants.LOG_TAG, this + " getBinder() intent=" + intent); + return mAuthenticator.getIBinder(); + } + + public static class Basic extends TestAuthenticationService { + } +} diff --git a/tests/src/com/android/contacts/common/tests/testauth/TestAuthenticator.java b/tests/src/com/android/contacts/common/tests/testauth/TestAuthenticator.java new file mode 100644 index 00000000..2f676c70 --- /dev/null +++ b/tests/src/com/android/contacts/common/tests/testauth/TestAuthenticator.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2011 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.contacts.common.tests.testauth; + +import android.accounts.AbstractAccountAuthenticator; +import android.accounts.Account; +import android.accounts.AccountAuthenticatorResponse; +import android.accounts.AccountManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.util.Log; + +/** + * Simple authenticator. It has no "login" dialogs/activities. When you add a new account, it'll + * just create a new account with a unique name. + */ +class TestAuthenticator extends AbstractAccountAuthenticator { + private static final String PASSWORD = "xxx"; // any string will do. + + // To remember the last user-ID. + private static final String PREF_KEY_LAST_USER_ID = "TestAuthenticator.PREF_KEY_LAST_USER_ID"; + + private final Context mContext; + + public TestAuthenticator(Context context) { + super(context); + mContext = context.getApplicationContext(); + } + + /** + * @return a new, unique username. + */ + private String newUniqueUserName() { + final SharedPreferences prefs = + PreferenceManager.getDefaultSharedPreferences(mContext); + final int nextId = prefs.getInt(PREF_KEY_LAST_USER_ID, 0) + 1; + prefs.edit().putInt(PREF_KEY_LAST_USER_ID, nextId).apply(); + + return "User-" + nextId; + } + + /** + * Create a new account with the name generated by {@link #newUniqueUserName()}. + */ + @Override + public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, + String authTokenType, String[] requiredFeatures, Bundle options) { + Log.v(TestauthConstants.LOG_TAG, "addAccount() type=" + accountType); + final Bundle bundle = new Bundle(); + + final Account account = new Account(newUniqueUserName(), accountType); + + // Create an account. + AccountManager.get(mContext).addAccountExplicitly(account, PASSWORD, null); + + // And return it. + bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); + bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); + return bundle; + } + + /** + * Just return the user name as the authtoken. + */ + @Override + public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, + String authTokenType, Bundle loginOptions) { + Log.v(TestauthConstants.LOG_TAG, "getAuthToken() account=" + account); + final Bundle bundle = new Bundle(); + bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); + bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); + bundle.putString(AccountManager.KEY_AUTHTOKEN, account.name); + + return bundle; + } + + @Override + public Bundle confirmCredentials( + AccountAuthenticatorResponse response, Account account, Bundle options) { + Log.v(TestauthConstants.LOG_TAG, "confirmCredentials()"); + return null; + } + + @Override + public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) { + Log.v(TestauthConstants.LOG_TAG, "editProperties()"); + throw new UnsupportedOperationException(); + } + + @Override + public String getAuthTokenLabel(String authTokenType) { + // null means we don't support multiple authToken types + Log.v(TestauthConstants.LOG_TAG, "getAuthTokenLabel()"); + return null; + } + + @Override + public Bundle hasFeatures( + AccountAuthenticatorResponse response, Account account, String[] features) { + // This call is used to query whether the Authenticator supports + // specific features. We don't expect to get called, so we always + // return false (no) for any queries. + Log.v(TestauthConstants.LOG_TAG, "hasFeatures()"); + final Bundle result = new Bundle(); + result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false); + return result; + } + + @Override + public Bundle updateCredentials(AccountAuthenticatorResponse response, Account account, + String authTokenType, Bundle loginOptions) { + Log.v(TestauthConstants.LOG_TAG, "updateCredentials()"); + return null; + } +} diff --git a/tests/src/com/android/contacts/common/tests/testauth/TestSyncAdapter.java b/tests/src/com/android/contacts/common/tests/testauth/TestSyncAdapter.java new file mode 100644 index 00000000..a7c0f83c --- /dev/null +++ b/tests/src/com/android/contacts/common/tests/testauth/TestSyncAdapter.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2011 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.contacts.common.tests.testauth; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.content.AbstractThreadedSyncAdapter; +import android.content.ContentProviderClient; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Context; +import android.content.SyncResult; +import android.os.Bundle; +import android.provider.ContactsContract.RawContacts; +import android.util.Log; + +/** + * Simple (minimal) sync adapter. + * + */ +public class TestSyncAdapter extends AbstractThreadedSyncAdapter { + private final AccountManager mAccountManager; + + private final Context mContext; + + public TestSyncAdapter(Context context, boolean autoInitialize) { + super(context, autoInitialize); + mContext = context.getApplicationContext(); + mAccountManager = AccountManager.get(mContext); + } + + /** + * Doesn't actually sync, but sweep up all existing local-only contacts. + */ + @Override + public void onPerformSync(Account account, Bundle extras, String authority, + ContentProviderClient provider, SyncResult syncResult) { + Log.v(TestauthConstants.LOG_TAG, "TestSyncAdapter.onPerformSync() account=" + account); + + // First, claim all local-only contacts, if any. + ContentResolver cr = mContext.getContentResolver(); + ContentValues values = new ContentValues(); + values.put(RawContacts.ACCOUNT_NAME, account.name); + values.put(RawContacts.ACCOUNT_TYPE, account.type); + final int count = cr.update(RawContacts.CONTENT_URI, values, + RawContacts.ACCOUNT_NAME + " IS NULL AND " + RawContacts.ACCOUNT_TYPE + " IS NULL", + null); + if (count > 0) { + Log.v(TestauthConstants.LOG_TAG, "Claimed " + count + " local raw contacts"); + } + + // TODO: Clear isDirty flag + // TODO: Remove isDeleted raw contacts + } +} diff --git a/tests/src/com/android/contacts/common/tests/testauth/TestSyncService.java b/tests/src/com/android/contacts/common/tests/testauth/TestSyncService.java new file mode 100644 index 00000000..3354cb4e --- /dev/null +++ b/tests/src/com/android/contacts/common/tests/testauth/TestSyncService.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2011 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.contacts.common.tests.testauth; + +import android.app.Service; +import android.content.Intent; +import android.os.IBinder; + +public abstract class TestSyncService extends Service { + + private static TestSyncAdapter sSyncAdapter; + + @Override + public void onCreate() { + if (sSyncAdapter == null) { + sSyncAdapter = new TestSyncAdapter(getApplicationContext(), true); + } + } + + @Override + public IBinder onBind(Intent intent) { + return sSyncAdapter.getSyncAdapterBinder(); + } + + public static class Basic extends TestSyncService { + } +} diff --git a/tests/src/com/android/contacts/common/tests/testauth/TestauthConstants.java b/tests/src/com/android/contacts/common/tests/testauth/TestauthConstants.java new file mode 100644 index 00000000..3ce7f5ab --- /dev/null +++ b/tests/src/com/android/contacts/common/tests/testauth/TestauthConstants.java @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2011 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.contacts.common.tests.testauth; + +class TestauthConstants { + public static final String LOG_TAG = "Testauth"; +} |