diff options
author | Dave Santoro <dsantoro@google.com> | 2011-07-20 13:44:44 -0700 |
---|---|---|
committer | Dave Santoro <dsantoro@google.com> | 2011-07-20 14:51:14 -0700 |
commit | 0b0a96a01617e0d311431d5f7137b5685ccf73aa (patch) | |
tree | c511b0f56be420bb48502a50ff3dc0656f31b201 | |
parent | 3f0d20aa18e63f9af389216a0265ea4e5e4065cf (diff) | |
download | android_packages_apps_Contacts-0b0a96a01617e0d311431d5f7137b5685ccf73aa.tar.gz android_packages_apps_Contacts-0b0a96a01617e0d311431d5f7137b5685ccf73aa.tar.bz2 android_packages_apps_Contacts-0b0a96a01617e0d311431d5f7137b5685ccf73aa.zip |
Test application for inserting stream items.
This will randomly generate 1-5 status messages for a selected
contact. Each status message has a 30% chance of also having
1-5 random photos associated with it.
This change was split off of the stream items UI change, since
the two do not depend on one another.
Change-Id: Icefec5b186867c5ee0dacecd74efda2ac0bcfb1f
-rw-r--r-- | tests/AndroidManifest.xml | 10 | ||||
-rw-r--r-- | tests/res/drawable/android.jpg | bin | 0 -> 107808 bytes | |||
-rw-r--r-- | tests/res/drawable/goldengate.jpg | bin | 0 -> 113658 bytes | |||
-rw-r--r-- | tests/res/drawable/iceland.jpg | bin | 0 -> 47129 bytes | |||
-rw-r--r-- | tests/res/drawable/japan.jpg | bin | 0 -> 24170 bytes | |||
-rw-r--r-- | tests/res/drawable/sydney.jpg | bin | 0 -> 120497 bytes | |||
-rw-r--r-- | tests/res/drawable/wharf.jpg | bin | 0 -> 48159 bytes | |||
-rw-r--r-- | tests/res/drawable/whiskey.jpg | bin | 0 -> 97123 bytes | |||
-rw-r--r-- | tests/res/layout/stream_item_populator.xml | 37 | ||||
-rw-r--r-- | tests/res/values/donottranslate_strings.xml | 4 | ||||
-rw-r--r-- | tests/src/com/android/contacts/tests/streamitems/StreamItemPopulatorActivity.java | 258 |
11 files changed, 309 insertions, 0 deletions
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml index 1b8df6b90..739f5f0fe 100644 --- a/tests/AndroidManifest.xml +++ b/tests/AndroidManifest.xml @@ -73,6 +73,16 @@ <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> + + <activity android:name=".streamitems.StreamItemPopulatorActivity" + android:label="@string/streamItemPopulator" + > + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.DEFAULT" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" diff --git a/tests/res/drawable/android.jpg b/tests/res/drawable/android.jpg Binary files differnew file mode 100644 index 000000000..95693b2fc --- /dev/null +++ b/tests/res/drawable/android.jpg diff --git a/tests/res/drawable/goldengate.jpg b/tests/res/drawable/goldengate.jpg Binary files differnew file mode 100644 index 000000000..7bd3f6753 --- /dev/null +++ b/tests/res/drawable/goldengate.jpg diff --git a/tests/res/drawable/iceland.jpg b/tests/res/drawable/iceland.jpg Binary files differnew file mode 100644 index 000000000..0ed210e82 --- /dev/null +++ b/tests/res/drawable/iceland.jpg diff --git a/tests/res/drawable/japan.jpg b/tests/res/drawable/japan.jpg Binary files differnew file mode 100644 index 000000000..e39f3877f --- /dev/null +++ b/tests/res/drawable/japan.jpg diff --git a/tests/res/drawable/sydney.jpg b/tests/res/drawable/sydney.jpg Binary files differnew file mode 100644 index 000000000..02b407c3e --- /dev/null +++ b/tests/res/drawable/sydney.jpg diff --git a/tests/res/drawable/wharf.jpg b/tests/res/drawable/wharf.jpg Binary files differnew file mode 100644 index 000000000..fa6b04f9e --- /dev/null +++ b/tests/res/drawable/wharf.jpg diff --git a/tests/res/drawable/whiskey.jpg b/tests/res/drawable/whiskey.jpg Binary files differnew file mode 100644 index 000000000..e8ffb8554 --- /dev/null +++ b/tests/res/drawable/whiskey.jpg diff --git a/tests/res/layout/stream_item_populator.xml b/tests/res/layout/stream_item_populator.xml new file mode 100644 index 000000000..acc46bf46 --- /dev/null +++ b/tests/res/layout/stream_item_populator.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> + +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:gravity="center" +> + <Button + android:id="@+id/add" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/chooseAContactButton" + android:layout_marginBottom="50px" + /> + <Button + android:id="@+id/exit" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/exitButton" + /> +</LinearLayout> diff --git a/tests/res/values/donottranslate_strings.xml b/tests/res/values/donottranslate_strings.xml index 9f9f5a4ec..a2e9640f7 100644 --- a/tests/res/values/donottranslate_strings.xml +++ b/tests/res/values/donottranslate_strings.xml @@ -96,6 +96,10 @@ <string name="addedLogEntriesToast">Added %1$d call log entries.</string> <string name="noLogEntriesToast">No entries in the call log yet.</string> + <string name="chooseAContactButton">Choose a contact to add stream items to</string> + <string name="exitButton">Exit</string> + <string name="streamItemPopulator">Populate stream items</string> + <string-array name="pinnedHeaderUseCases"> <item>One short section - no headers</item> <item>Two short sections with headers</item> diff --git a/tests/src/com/android/contacts/tests/streamitems/StreamItemPopulatorActivity.java b/tests/src/com/android/contacts/tests/streamitems/StreamItemPopulatorActivity.java new file mode 100644 index 000000000..5613bc36f --- /dev/null +++ b/tests/src/com/android/contacts/tests/streamitems/StreamItemPopulatorActivity.java @@ -0,0 +1,258 @@ +/* + * 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.tests.streamitems; + +import com.android.contacts.model.GoogleAccountType; +import com.android.contacts.tests.R; +import com.google.android.collect.Lists; + +import android.app.Activity; +import android.content.ContentProviderOperation; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.ContactsContract; +import android.provider.ContactsContract.RawContacts; +import android.provider.ContactsContract.StreamItemPhotos; +import android.provider.ContactsContract.StreamItems; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Random; + +/** + * Testing activity that will populate stream items and stream item photos to selected + * entries in the user's contacts list. + * + * The contact selected must have at least one raw contact that was provided by Google. + */ +public class StreamItemPopulatorActivity extends Activity { + + // Test data to randomly select from. + private String[] snippetStrings = new String[]{ + "Just got back from a vacation in %1$s - what a great place! Can't wait to go back.", + "If I never see %1$s again it will be too soon.", + "This is a public service announcement. If you were even close to considering visiting" + + " %1$s, I strongly advise you to reconsider. The food was terrible, the people were " + + "rude, the hygiene of the bus and taxi drivers was positively <i>barbaric</i>. I " + + "feared for my life almost the entire time I was there, and feel lucky to be back " + + "<b>home</b>.", + "Check out these pictures! I took them in %1$s" + }; + + private String[] placeNames = new String[]{ + "the Google campus in Mountain View", + "the deserts on Arrakis", + "Iceland", + "Japan", + "Sydney", + "San Francisco", + "Munich", + "Istanbul", + "Tanagra", + "the restricted section of Area 51", + "the middle of nowhere" + }; + + // Photos to randomly select from. + private Integer[] imageIds = new Integer[]{ + R.drawable.android, + R.drawable.goldengate, + R.drawable.iceland, + R.drawable.japan, + R.drawable.sydney, + R.drawable.wharf, + R.drawable.whiskey + }; + + // The contact ID that was picked. + private long mContactId = -1; + + private Random mRandom; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mRandom = new Random(System.currentTimeMillis()); + + setContentView(R.layout.stream_item_populator); + Button pickButton = (Button) findViewById(R.id.add); + pickButton.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View v) { + // Reset the contact ID. + mContactId = -1; + + // Forward the Intent to the picker + final Intent pickerIntent = + new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); + pickerIntent.setFlags( + Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivityForResult(pickerIntent, 0); + } + }); + + Button exitButton = (Button) findViewById(R.id.exit); + exitButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (resultCode == Activity.RESULT_OK) { + Uri contactUri = data.getData(); + mContactId = ContentUris.parseId(contactUri); + } + } + + @Override + protected void onResume() { + super.onResume(); + if (mContactId != -1) { + long rawContactId = -1; + String accountType = null; + String accountName = null; + + // Lookup the com.google raw contact for the contact. + Cursor c = getContentResolver().query(RawContacts.CONTENT_URI, + new String[]{ + RawContacts._ID, + RawContacts.ACCOUNT_TYPE, + RawContacts.ACCOUNT_NAME + }, + RawContacts.CONTACT_ID + "=? AND " + RawContacts.ACCOUNT_TYPE + "=?", + new String[]{String.valueOf(mContactId), GoogleAccountType.ACCOUNT_TYPE}, null); + try { + c.moveToFirst(); + rawContactId = c.getLong(0); + accountType = c.getString(1); + accountName = c.getString(2); + } finally { + c.close(); + } + if (rawContactId != -1) { + addStreamItemsToRawContact(rawContactId, accountType, accountName); + } else { + Toast.makeText(this, + "Failed to find raw contact ID for contact ID " + mContactId, 5).show(); + } + } + } + + protected byte[] loadPhotoFromResource(int resourceId) { + InputStream is = getResources().openRawResource(resourceId); + return readInputStreamFully(is); + } + + protected byte[] readInputStreamFully(InputStream is) { + try { + byte[] buffer = new byte[is.available()]; + is.read(buffer); + is.close(); + return buffer; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void addStreamItemsToRawContact(long rawContactId, String accountType, + String accountName) { + ArrayList<ContentProviderOperation> ops = Lists.newArrayList(); + + // Add from 1-5 stream items. + int itemsToAdd = randInt(5) + 1; + int opCount = 0; + for (int i = 0; i < itemsToAdd; i++) { + ContentValues streamItemValues = buildStreamItemValues(accountType, accountName); + ops.add(ContentProviderOperation.newInsert( + Uri.withAppendedPath( + ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, + rawContactId), + ContactsContract.RawContacts.StreamItems.CONTENT_DIRECTORY)) + .withValues(streamItemValues).build()); + + // Maybe add photos - 30% chance per stream item. + boolean includePhotos = randInt(100) < 30; + if (includePhotos) { + // Add 1-5 photos if we're including any. + int numPhotos = randInt(5) + 1; + for (int j = 0; j < numPhotos; j++) { + ContentValues streamItemPhotoValues = + buildStreamItemPhotoValues(j, accountType, accountName); + ops.add(ContentProviderOperation.newInsert(StreamItems.CONTENT_PHOTO_URI) + .withValues(streamItemPhotoValues) + .withValueBackReference(StreamItemPhotos.STREAM_ITEM_ID, opCount) + .build()); + } + opCount += numPhotos; + } + opCount++; + } + try { + getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); + } catch (Exception e) { + // We don't care. This is just for test purposes. + throw new RuntimeException(e); + } + Toast.makeText(this, "Added " + itemsToAdd + " stream item(s) and " + + (opCount - itemsToAdd) + " photos", 5).show(); + } + + private ContentValues buildStreamItemValues(String accountType, String accountName) { + ContentValues values = new ContentValues(); + values.put(StreamItems.TEXT, + String.format(pickRandom(snippetStrings), pickRandom(placeNames))); + values.put(StreamItems.COMMENTS, ""); + // Set the timestamp to some point in the past. + values.put(StreamItems.TIMESTAMP, + System.currentTimeMillis() - randInt(360000000)); + values.put(RawContacts.ACCOUNT_TYPE, accountType); + values.put(RawContacts.ACCOUNT_NAME, accountName); + return values; + } + + private ContentValues buildStreamItemPhotoValues(int index, String accountType, + String accountName) { + ContentValues values = new ContentValues(); + values.put(StreamItemPhotos.SORT_INDEX, index); + values.put(StreamItemPhotos.PHOTO, loadPhotoFromResource(pickRandom(imageIds))); + values.put(RawContacts.ACCOUNT_TYPE, accountType); + values.put(RawContacts.ACCOUNT_NAME, accountName); + return values; + } + + private <T> T pickRandom(T[] from) { + return from[randInt(from.length)]; + } + + private int randInt(int max) { + return Math.abs(mRandom.nextInt()) % max; + } +} |