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