1 /*
2  * Copyright (C) 2013 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 com.android.externalstorage;
18 
19 import static com.android.externalstorage.ExternalStorageProvider.buildUniqueFile;
20 
21 import android.os.FileUtils;
22 import android.provider.DocumentsContract.Document;
23 import android.test.AndroidTestCase;
24 import android.test.suitebuilder.annotation.MediumTest;
25 
26 import java.io.File;
27 
28 @MediumTest
29 public class ExternalStorageProviderTest extends AndroidTestCase {
30 
31     private File mTarget;
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         mTarget = getContext().getFilesDir();
37         FileUtils.deleteContents(mTarget);
38     }
39 
40     @Override
tearDown()41     protected void tearDown() throws Exception {
42         super.tearDown();
43         FileUtils.deleteContents(mTarget);
44     }
45 
testBuildUniqueFile_normal()46     public void testBuildUniqueFile_normal() throws Exception {
47         assertNameEquals("test.jpg", buildUniqueFile(mTarget, "image/jpeg", "test"));
48         assertNameEquals("test.jpg", buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
49         assertNameEquals("test.jpeg", buildUniqueFile(mTarget, "image/jpeg", "test.jpeg"));
50         assertNameEquals("TEst.JPeg", buildUniqueFile(mTarget, "image/jpeg", "TEst.JPeg"));
51         assertNameEquals("test.png.jpg", buildUniqueFile(mTarget, "image/jpeg", "test.png.jpg"));
52         assertNameEquals("test.png.jpg", buildUniqueFile(mTarget, "image/jpeg", "test.png"));
53 
54         assertNameEquals("test.flac", buildUniqueFile(mTarget, "audio/flac", "test"));
55         assertNameEquals("test.flac", buildUniqueFile(mTarget, "audio/flac", "test.flac"));
56         assertNameEquals("test.flac", buildUniqueFile(mTarget, "application/x-flac", "test"));
57         assertNameEquals("test.flac", buildUniqueFile(mTarget, "application/x-flac", "test.flac"));
58     }
59 
testBuildUniqueFile_unknown()60     public void testBuildUniqueFile_unknown() throws Exception {
61         assertNameEquals("test", buildUniqueFile(mTarget, "application/octet-stream", "test"));
62         assertNameEquals("test.jpg", buildUniqueFile(mTarget, "application/octet-stream", "test.jpg"));
63         assertNameEquals(".test", buildUniqueFile(mTarget, "application/octet-stream", ".test"));
64 
65         assertNameEquals("test", buildUniqueFile(mTarget, "lolz/lolz", "test"));
66         assertNameEquals("test.lolz", buildUniqueFile(mTarget, "lolz/lolz", "test.lolz"));
67     }
68 
testBuildUniqueFile_dir()69     public void testBuildUniqueFile_dir() throws Exception {
70         assertNameEquals("test", buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test"));
71         new File(mTarget, "test").mkdir();
72         assertNameEquals("test (1)", buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test"));
73 
74         assertNameEquals("test.jpg", buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test.jpg"));
75         new File(mTarget, "test.jpg").mkdir();
76         assertNameEquals("test.jpg (1)", buildUniqueFile(mTarget, Document.MIME_TYPE_DIR, "test.jpg"));
77     }
78 
testBuildUniqueFile_increment()79     public void testBuildUniqueFile_increment() throws Exception {
80         assertNameEquals("test.jpg", buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
81         new File(mTarget, "test.jpg").createNewFile();
82         assertNameEquals("test (1).jpg", buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
83         new File(mTarget, "test (1).jpg").createNewFile();
84         assertNameEquals("test (2).jpg", buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
85     }
86 
assertNameEquals(String expected, File actual)87     private static void assertNameEquals(String expected, File actual) {
88         assertEquals(expected, actual.getName());
89     }
90 }
91