Merge "Test migration of EXTRA_OUTPUT to clipdata." into lmp-dev
diff --git a/tests/tests/content/AndroidManifest.xml b/tests/tests/content/AndroidManifest.xml
index b3de29f..a5caba8 100644
--- a/tests/tests/content/AndroidManifest.xml
+++ b/tests/tests/content/AndroidManifest.xml
@@ -177,6 +177,16 @@
<activity android:name="android.content.cts.ClipboardManagerListenerActivity"/>
+ <activity android:name="com.android.cts.content.ImageCaptureActivity"
+ android:exported="true">
+ <intent-filter>
+ <action android:name="android.media.action.IMAGE_CAPTURE" />
+ <action android:name="android.media.action.IMAGE_CAPTURE_SECURE" />
+ <action android:name="android.media.action.VIDEO_CAPTURE" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ </activity>
+
</application>
<instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
diff --git a/tests/tests/content/src/android/content/cts/ImageCaptureActivity.java b/tests/tests/content/src/android/content/cts/ImageCaptureActivity.java
new file mode 100644
index 0000000..71bf250
--- /dev/null
+++ b/tests/tests/content/src/android/content/cts/ImageCaptureActivity.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2014 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.cts.content;
+
+import android.app.Activity;
+import android.content.ClipData;
+import android.content.ClipData.Item;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.MediaStore;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+public class ImageCaptureActivity extends Activity {
+ public static final String ACTION_FILE_READY = "com.android.cts.content.action.file_ready";
+ private static final String TAG = ImageCaptureUriExtraToClipDataTest.TAG;
+
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ Intent intent = getIntent();
+
+ // Check action.
+ String action = intent.getAction();
+ if ((MediaStore.ACTION_IMAGE_CAPTURE.equals(action)
+ || MediaStore.ACTION_IMAGE_CAPTURE_SECURE.equals(action)
+ || MediaStore.ACTION_VIDEO_CAPTURE.equals(action))) {
+ writeToClipDataUri(intent);
+ }
+
+ finish();
+ }
+
+ // Sends ACTION_FILE_READY intent when write to clipdata uri is succesful.
+ private void writeToClipDataUri(Intent intent) {
+ if ((intent.getFlags() & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0) {
+
+ // Note: since this activity is in the same package as the test we can write to the file
+ // regardless of this permission, but in general this permission is required.
+ Log.e(TAG, "Intent.FLAG_GRANT_WRITE_URI_PERMISSION was not granted.");
+ return;
+ }
+
+ File file = getFileFromIntent(intent);
+ if (file == null) {
+ Log.e(TAG, "Could not get file from clipdata.");
+ return;
+ }
+ try {
+ FileWriter writer = new FileWriter(file);
+ writer.write(ImageCaptureUriExtraToClipDataTest.TEST_INPUT);
+ writer.flush();
+ writer.close();
+ } catch (IOException e) {
+ Log.e(TAG, "File IO failure while writing.");
+ return;
+ }
+ Intent fileReady = new Intent(ACTION_FILE_READY);
+ sendBroadcast(fileReady);
+ }
+
+ private File getFileFromIntent(Intent intent) {
+ ClipData clipData = intent.getClipData();
+ if (clipData == null) {
+ Log.e(TAG, "ClipData missing.");
+ return null;
+ }
+ if (clipData.getItemCount() == 0) {
+ Log.e(TAG, "Uri missing in ClipData.");
+ return null;
+ }
+
+ Uri filePath = clipData.getItemAt(0).getUri();
+ if (filePath == null) {
+ Log.e(TAG, "Uri missing in ClipData.");
+ return null;
+ }
+
+ try {
+ return new File(filePath.getPath());
+ } catch (IllegalArgumentException e) {
+ Log.e(TAG, "Cannot get file at Uri.");
+ return null;
+ }
+ }
+}
\ No newline at end of file
diff --git a/tests/tests/content/src/android/content/cts/ImageCaptureUriExtraToClipDataTest.java b/tests/tests/content/src/android/content/cts/ImageCaptureUriExtraToClipDataTest.java
new file mode 100644
index 0000000..bb2a43d
--- /dev/null
+++ b/tests/tests/content/src/android/content/cts/ImageCaptureUriExtraToClipDataTest.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2014 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.cts.content;
+
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.Uri;
+import android.provider.MediaStore;
+import android.test.AndroidTestCase;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+
+public class ImageCaptureUriExtraToClipDataTest extends AndroidTestCase {
+ private static final String FILE_NAME = "testFile.txt";
+ private File mTestFile;
+ private final Semaphore mFileReadySemaphore = new Semaphore(0);
+
+ public static final String TEST_INPUT = "testString";
+ public static final String TAG = "ImageCaptureUriExtraToClipDataTest";
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ assertEquals(0, mFileReadySemaphore.availablePermits());
+
+ BroadcastReceiver mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ mFileReadySemaphore.release();
+ }
+ };
+ IntentFilter filter = new IntentFilter();
+ filter.addAction(ImageCaptureActivity.ACTION_FILE_READY);
+ getContext().registerReceiver(mReceiver, filter);
+
+ mTestFile = new File(getContext().getFilesDir() + File.separator + FILE_NAME);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ if (mTestFile.exists()) {
+ assertTrue(mTestFile.delete());
+ }
+ super.tearDown();
+ }
+
+
+ public void testUriExtraOutputMigratedToClipData_imageCaptureIntent() {
+ startActivityWithAction(MediaStore.ACTION_IMAGE_CAPTURE);
+ waitForFileReady();
+ testFileContents();
+ }
+
+ public void testUriExtraOutputMigratedToClipData_imageCaptureSecureIntent() {
+ startActivityWithAction(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
+ waitForFileReady();
+ testFileContents();
+ }
+
+ public void testUriExtraOutputMigratedToClipData_videoCaptureIntent() {
+ startActivityWithAction(MediaStore.ACTION_VIDEO_CAPTURE);
+ waitForFileReady();
+ testFileContents();
+ }
+
+ private void startActivityWithAction(String action) {
+ Intent intent = new Intent(action);
+ intent.setComponent(new ComponentName("com.android.cts.content",
+ "com.android.cts.content.ImageCaptureActivity"));
+ intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTestFile));
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ getContext().startActivity(intent);
+ }
+
+ private void waitForFileReady() {
+ try {
+ assertTrue(mFileReadySemaphore.tryAcquire(5, TimeUnit.SECONDS));
+ } catch (InterruptedException e) {
+ fail(e.toString());
+ }
+ }
+
+ private void testFileContents() {
+ char[] buffer = new char[TEST_INPUT.length()];
+ try {
+ FileReader reader = new FileReader(mTestFile);
+ reader.read(buffer);
+ reader.close();
+ } catch (IOException e) {
+ // Problem
+ fail(e.toString());
+ }
+ String fileContents = new String(buffer);
+ assertEquals(TEST_INPUT, fileContents);
+ }
+}