Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2011 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 
     17 package android.media.cts;
     18 
     19 import android.content.Intent;
     20 import android.content.IntentFilter;
     21 import android.os.Environment;
     22 import android.platform.test.annotations.AppModeFull;
     23 import android.test.AndroidTestCase;
     24 
     25 import java.io.File;
     26 import java.io.FileOutputStream;
     27 import java.io.IOException;
     28 
     29 @AppModeFull(reason = "TODO: evaluate and port to instant")
     30 public class MediaScannerNotificationTest extends AndroidTestCase {
     31 
     32     public void testMediaScannerNotification() throws Exception {
     33         ScannerNotificationReceiver startedReceiver = new ScannerNotificationReceiver(
     34                 Intent.ACTION_MEDIA_SCANNER_STARTED);
     35         ScannerNotificationReceiver finishedReceiver = new ScannerNotificationReceiver(
     36                 Intent.ACTION_MEDIA_SCANNER_FINISHED);
     37 
     38         IntentFilter startedIntentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
     39         startedIntentFilter.addDataScheme("file");
     40         IntentFilter finshedIntentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED);
     41         finshedIntentFilter.addDataScheme("file");
     42 
     43         mContext.registerReceiver(startedReceiver, startedIntentFilter);
     44         mContext.registerReceiver(finishedReceiver, finshedIntentFilter);
     45 
     46         String [] temps = new String[] { "avi", "gif", "jpg", "dat", "mp3", "mp4", "txt" };
     47         String tmpPath = createTempFiles(temps);
     48 
     49         MediaScannerTest.startMediaScan();
     50         startedReceiver.waitForBroadcast();
     51         finishedReceiver.waitForBroadcast();
     52 
     53         checkTempFiles(tmpPath, temps);
     54 
     55         // add .nomedia file and scan again
     56         File noMedia = new File(tmpPath, ".nomedia");
     57         try {
     58             noMedia.createNewFile();
     59         } catch (IOException e) {
     60             fail("couldn't create .nomedia file");
     61         }
     62         startedReceiver.reset();
     63         finishedReceiver.reset();
     64         MediaScannerTest.startMediaScan();
     65         startedReceiver.waitForBroadcast();
     66         finishedReceiver.waitForBroadcast();
     67 
     68         checkTempFiles(tmpPath, temps);
     69         assertTrue(noMedia.delete());
     70         deleteTempFiles(tmpPath, temps);
     71 
     72         // scan one more time just to clean everything up nicely
     73         startedReceiver.reset();
     74         finishedReceiver.reset();
     75         MediaScannerTest.startMediaScan();
     76         startedReceiver.waitForBroadcast();
     77         finishedReceiver.waitForBroadcast();
     78 
     79     }
     80 
     81     String createTempFiles(String [] extensions) {
     82         String externalPath = Environment.getExternalStorageDirectory().getAbsolutePath();
     83         File tmpDir = new File(externalPath, "" + System.nanoTime());
     84         String tmpPath = tmpDir.getAbsolutePath();
     85         assertFalse(tmpPath + " already exists", tmpDir.exists());
     86         assertTrue("failed to create " + tmpDir, tmpDir.mkdirs());
     87 
     88         for (int i = 0; i < extensions.length; i++) {
     89             File foo = new File(tmpPath, "foobar." + extensions[i]);
     90             try {
     91                 // create a non-empty file
     92                 foo.createNewFile();
     93                 FileOutputStream out = new FileOutputStream(foo);
     94                 out.write(0x12);
     95                 out.flush();
     96                 out.close();
     97                 assertTrue(foo.length() != 0);
     98             } catch (IOException e) {
     99                 fail("Error creating " + foo.getAbsolutePath() + ": " + e);
    100             }
    101         }
    102         return tmpPath;
    103     }
    104 
    105     void checkTempFiles(String tmpPath, String [] extensions) {
    106         for (int i = 0; i < extensions.length; i++) {
    107             File foo = new File(tmpPath, "foobar." + extensions[i]);
    108             assertTrue(foo.getAbsolutePath() + " no longer exists or was truncated",
    109                     foo.length() != 0);
    110         }
    111     }
    112 
    113     void deleteTempFiles(String tmpPath, String [] extensions) {
    114         for (int i = 0; i < extensions.length; i++) {
    115             assertTrue(new File(tmpPath, "foobar." + extensions[i]).delete());
    116         }
    117         assertTrue(new File(tmpPath).delete());
    118     }
    119 }
    120