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