Home | History | Annotate | Download | only in cts
      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.provider.MediaStore;
     25 import android.test.AndroidTestCase;
     26 
     27 import java.io.File;
     28 import java.io.FileReader;
     29 import java.io.IOException;
     30 import java.io.Reader;
     31 import java.util.concurrent.Semaphore;
     32 import java.util.concurrent.TimeUnit;
     33 
     34 public class ImageCaptureUriExtraToClipDataTest extends AndroidTestCase {
     35     private static final String FILE_NAME = "testFile.txt";
     36     private File mTestFile;
     37     private final Semaphore mFileReadySemaphore = new Semaphore(0);
     38 
     39     public static final String TEST_INPUT = "testString";
     40     public static final String TAG = "ImageCaptureUriExtraToClipDataTest";
     41 
     42     @Override
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45 
     46         assertEquals(0, mFileReadySemaphore.availablePermits());
     47 
     48         BroadcastReceiver mReceiver = new BroadcastReceiver() {
     49                 @Override
     50                 public void onReceive(Context context, Intent intent) {
     51                     mFileReadySemaphore.release();
     52                 }
     53             };
     54         IntentFilter filter = new IntentFilter();
     55         filter.addAction(ImageCaptureActivity.ACTION_FILE_READY);
     56         getContext().registerReceiver(mReceiver, filter);
     57 
     58         mTestFile = new File(getContext().getFilesDir() + File.separator + FILE_NAME);
     59     }
     60 
     61     @Override
     62     protected void tearDown() throws Exception {
     63         if (mTestFile.exists()) {
     64             assertTrue(mTestFile.delete());
     65         }
     66         super.tearDown();
     67     }
     68 
     69 
     70     public void testUriExtraOutputMigratedToClipData_imageCaptureIntent() {
     71         startActivityWithAction(MediaStore.ACTION_IMAGE_CAPTURE);
     72         waitForFileReady();
     73         assertFileContents();
     74     }
     75 
     76     public void testUriExtraOutputMigratedToClipData_imageCaptureSecureIntent() {
     77         startActivityWithAction(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
     78         waitForFileReady();
     79         assertFileContents();
     80     }
     81 
     82     public void testUriExtraOutputMigratedToClipData_videoCaptureIntent() {
     83         startActivityWithAction(MediaStore.ACTION_VIDEO_CAPTURE);
     84         waitForFileReady();
     85         assertFileContents();
     86     }
     87 
     88     private void startActivityWithAction(String action) {
     89         Intent intent = new Intent(action);
     90         intent.setComponent(new ComponentName("android.content.cts",
     91                         "android.content.cts.ImageCaptureActivity"));
     92         intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTestFile));
     93         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     94         getContext().startActivity(intent);
     95     }
     96 
     97     private void waitForFileReady() {
     98         try {
     99             assertTrue(mFileReadySemaphore.tryAcquire(5, TimeUnit.SECONDS));
    100         } catch (InterruptedException e) {
    101             fail(e.toString());
    102         }
    103     }
    104 
    105     private void assertFileContents() {
    106         char[] buffer = new char[TEST_INPUT.length()];
    107         try {
    108             FileReader reader = new FileReader(mTestFile);
    109             reader.read(buffer);
    110             reader.close();
    111         } catch (IOException e) {
    112             // Problem
    113             fail(e.toString());
    114         }
    115         String fileContents = new String(buffer);
    116         assertEquals(TEST_INPUT, fileContents);
    117     }
    118 }
    119