1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.content.cts;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.net.Uri;
24 import android.platform.test.annotations.AppModeNonSdkSandbox;
25 import android.provider.MediaStore;
26 import android.test.AndroidTestCase;
27 
28 import java.io.File;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.concurrent.Semaphore;
32 import java.util.concurrent.TimeUnit;
33 
34 @AppModeNonSdkSandbox(reason = "SDK sandboxes cannot use an intent with these intent actions.")
35 public class ImageCaptureUriExtraToClipDataTest extends AndroidTestCase {
36     private static final String FILE_NAME = "testFile.txt";
37     private File mTestFile;
38     private final Semaphore mFileReadySemaphore = new Semaphore(0);
39 
40     public static final String TEST_INPUT = "testString";
41     public static final String TAG = "ImageCaptureUriExtraToClipDataTest";
42 
43     @Override
setUp()44     protected void setUp() throws Exception {
45         super.setUp();
46 
47         assertEquals(0, mFileReadySemaphore.availablePermits());
48 
49         BroadcastReceiver mReceiver = new BroadcastReceiver() {
50                 @Override
51                 public void onReceive(Context context, Intent intent) {
52                     mFileReadySemaphore.release();
53                 }
54             };
55         IntentFilter filter = new IntentFilter();
56         filter.addAction(ImageCaptureActivity.ACTION_FILE_READY);
57         getContext().registerReceiver(mReceiver, filter, Context.RECEIVER_EXPORTED);
58 
59         mTestFile = new File(getContext().getFilesDir() + File.separator + FILE_NAME);
60     }
61 
62     @Override
tearDown()63     protected void tearDown() throws Exception {
64         if (mTestFile.exists()) {
65             assertTrue(mTestFile.delete());
66         }
67         super.tearDown();
68     }
69 
70 
testUriExtraOutputMigratedToClipData_imageCaptureIntent()71     public void testUriExtraOutputMigratedToClipData_imageCaptureIntent() {
72         startActivityWithAction(MediaStore.ACTION_IMAGE_CAPTURE);
73         waitForFileReady();
74         assertFileContents();
75     }
76 
testUriExtraOutputMigratedToClipData_imageCaptureSecureIntent()77     public void testUriExtraOutputMigratedToClipData_imageCaptureSecureIntent() {
78         startActivityWithAction(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
79         waitForFileReady();
80         assertFileContents();
81     }
82 
testUriExtraOutputMigratedToClipData_videoCaptureIntent()83     public void testUriExtraOutputMigratedToClipData_videoCaptureIntent() {
84         startActivityWithAction(MediaStore.ACTION_VIDEO_CAPTURE);
85         waitForFileReady();
86         assertFileContents();
87     }
88 
startActivityWithAction(String action)89     private void startActivityWithAction(String action) {
90         Intent intent = new Intent(action);
91         intent.setComponent(new ComponentName("android.content.cts",
92                         "android.content.cts.ImageCaptureActivity"));
93         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTestFile));
94         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
95         getContext().startActivity(intent);
96     }
97 
waitForFileReady()98     private void waitForFileReady() {
99         try {
100             assertTrue(mFileReadySemaphore.tryAcquire(5, TimeUnit.SECONDS));
101         } catch (InterruptedException e) {
102             fail(e.toString());
103         }
104     }
105 
assertFileContents()106     private void assertFileContents() {
107         char[] buffer = new char[TEST_INPUT.length()];
108         try {
109             FileReader reader = new FileReader(mTestFile);
110             reader.read(buffer);
111             reader.close();
112         } catch (IOException e) {
113             // Problem
114             fail(e.toString());
115         }
116         String fileContents = new String(buffer);
117         assertEquals(TEST_INPUT, fileContents);
118     }
119 }
120