• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, 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 package com.android.managedprovisioning.parser;
17 
18 import static com.android.managedprovisioning.common.StoreUtils.DIR_PROVISIONING_PARAMS_FILE_CACHE;
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertNull;
21 import static org.mockito.Matchers.eq;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.net.Uri;
27 import android.support.test.InstrumentationRegistry;
28 import android.support.test.filters.SmallTest;
29 
30 import com.android.managedprovisioning.common.StoreUtils;
31 import com.android.managedprovisioning.testcommon.TestUtils;
32 import com.android.managedprovisioning.tests.R;
33 import java.io.File;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 
41 /**
42  * Test for {@link DeviceAdminIconParser}
43  */
44 @SmallTest
45 public class DeviceAdminIconParserTest {
46     private static final String TEST_FILE_DIRNAME = "DeviceAdminIconParserTest";
47     private static final long TEST_PROVISIONING_ID = 999L;
48     private static File TEST_FILE_DIR;
49     private static File OUTPUT_FILE;
50     private static Uri INPUT_URI;
51     private static String INPUT_CONTENT;
52 
53     @Mock
54     Context mContext;
55 
56     DeviceAdminIconParser mDeviceAdminIconParser;
57 
58     @BeforeClass
setUpClass()59     public static void setUpClass() throws Exception {
60         // Stop the activity from rotating in order to keep hold of the context
61         Context testContext = InstrumentationRegistry.getContext();
62         Context targetContext = InstrumentationRegistry.getTargetContext();
63         ContentResolver cr = targetContext.getContentResolver();
64         TEST_FILE_DIR = new File(targetContext.getFilesDir(), TEST_FILE_DIRNAME);
65         OUTPUT_FILE = new File(new File(TEST_FILE_DIR, DIR_PROVISIONING_PARAMS_FILE_CACHE),
66                 "device_admin_icon_" + TEST_PROVISIONING_ID);
67 
68         INPUT_URI = TestUtils.resourceToUri(testContext, R.raw.android);
69         INPUT_CONTENT = TestUtils.stringFromUri(cr, INPUT_URI);
70     }
71 
72     @Before
setUp()73     public void setUp() throws Exception {
74         MockitoAnnotations.initMocks(this);
75 
76         Context targetContext = InstrumentationRegistry.getTargetContext();
77 
78         TEST_FILE_DIR.mkdir();
79         when(mContext.getFilesDir()).thenReturn(TEST_FILE_DIR);
80         when(mContext.getContentResolver()).thenReturn(targetContext.getContentResolver());
81 
82         mDeviceAdminIconParser = new DeviceAdminIconParser(mContext, TEST_PROVISIONING_ID);
83     }
84 
85     @After
tearDown()86     public void tearDown() {
87         deleteRecursive(TEST_FILE_DIR);
88     }
89 
90     @Test
testNullUri()91     public void testNullUri() {
92         assertNull(mDeviceAdminIconParser.parse(null));
93     }
94 
95     @Test
testUri()96     public void testUri() throws Exception {
97         assertEquals(OUTPUT_FILE.getAbsolutePath(), mDeviceAdminIconParser.parse(INPUT_URI));
98         assertEquals(INPUT_CONTENT, StoreUtils.readString(OUTPUT_FILE));
99     }
100 
deleteRecursive(File fileOrDirectory)101     private static void deleteRecursive(File fileOrDirectory) {
102         if (fileOrDirectory.isDirectory())
103             for (File child : fileOrDirectory.listFiles())
104                 deleteRecursive(child);
105 
106         fileOrDirectory.delete();
107     }
108 }
109