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