1 /* 2 * Copyright 2016, 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 android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_CONTENT; 19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DISCLAIMER_HEADER; 20 import static com.android.managedprovisioning.common.StoreUtils.DIR_PROVISIONING_PARAMS_FILE_CACHE; 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertNotNull; 23 import static org.junit.Assert.assertNull; 24 import static org.mockito.Mockito.when; 25 26 import android.content.ContentResolver; 27 import android.content.Context; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.support.test.InstrumentationRegistry; 31 import android.support.test.filters.SmallTest; 32 import com.android.managedprovisioning.common.StoreUtils; 33 import com.android.managedprovisioning.model.DisclaimersParam; 34 import com.android.managedprovisioning.testcommon.TestUtils; 35 import com.android.managedprovisioning.tests.R; 36 import java.io.File; 37 import java.io.IOException; 38 import org.junit.After; 39 import org.junit.Before; 40 import org.junit.BeforeClass; 41 import org.junit.Test; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 45 /** 46 * Unit tests for {@link DisclaimersParser}. 47 */ 48 @SmallTest 49 public class DisclaimersParserTest { 50 private static final String TEST_FILE_DIRNAME = "DisclaimersParserTest"; 51 private static final String TEST_PACKAGE = "com.android.managedprovisioning.tests"; 52 private static final long TEST_PROVISIONING_ID = 999L; 53 54 private static File TEST_FILE_DIR; 55 56 private static Uri DISCLAIMER_URI_1; 57 private static final String DISCLAIMER_HEADER_1 = "DISCLAIMER_HEADER_1"; 58 private static String DISCLAIMER_CONTENT_1; 59 private static String DISCLAIMER_FILE_DEST_1; 60 private static Uri DISCLAIMER_URI_2; 61 private static final String DISCLAIMER_HEADER_2 = "DISCLAIMER_HEADER_2"; 62 private static String DISCLAIMER_CONTENT_2; 63 private static String DISCLAIMER_FILE_DEST_2; 64 private static Uri DISCLAIMER_URI_3; 65 private static final String DISCLAIMER_HEADER_3 = "DISCLAIMER_HEADER_3"; 66 private static String DISCLAIMER_CONTENT_3; 67 private static String DISCLAIMER_FILE_DEST_3; 68 69 @Mock 70 Context mContext; 71 72 DisclaimersParser mDisclaimersParser; 73 74 @BeforeClass setUpClass()75 public static void setUpClass() throws Exception { 76 // Stop the activity from rotating in order to keep hold of the context 77 Context testContext = InstrumentationRegistry.getContext(); 78 Context targetContext = InstrumentationRegistry.getTargetContext(); 79 ContentResolver cr = targetContext.getContentResolver(); 80 TEST_FILE_DIR = new File(targetContext.getFilesDir(), TEST_FILE_DIRNAME); 81 82 DISCLAIMER_URI_1 = TestUtils.resourceToUri(testContext, R.raw.test_disclaimer1); 83 DISCLAIMER_CONTENT_1 = TestUtils.stringFromUri(cr, DISCLAIMER_URI_1); 84 DISCLAIMER_FILE_DEST_1 = getDisclaimerPath(1); 85 DISCLAIMER_URI_2 = TestUtils.resourceToUri(testContext, R.raw.test_disclaimer2); 86 DISCLAIMER_CONTENT_2 = TestUtils.stringFromUri(cr, DISCLAIMER_URI_2); 87 DISCLAIMER_FILE_DEST_2 = getDisclaimerPath(2); 88 DISCLAIMER_URI_3 = TestUtils.resourceToUri(testContext, R.raw.test_disclaimer3); 89 DISCLAIMER_CONTENT_3 = TestUtils.stringFromUri(cr, DISCLAIMER_URI_3); 90 DISCLAIMER_FILE_DEST_3 = getDisclaimerPath(3); 91 } 92 93 @Before setUp()94 public void setUp() { 95 MockitoAnnotations.initMocks(this); 96 ContentResolver cr = InstrumentationRegistry.getTargetContext().getContentResolver(); 97 when(mContext.getFilesDir()).thenReturn(TEST_FILE_DIR); 98 when(mContext.getContentResolver()).thenReturn(cr); 99 100 mDisclaimersParser = new DisclaimersParser(mContext, TEST_PROVISIONING_ID); 101 } 102 103 @After tearDown()104 public void tearDown() { 105 TestUtils.deleteRecursive(TEST_FILE_DIR); 106 } 107 108 @Test testEmpty()109 public void testEmpty() { 110 assertNull(mDisclaimersParser.parse(null)); 111 assertNull(mDisclaimersParser.parse(new Bundle[0])); 112 } 113 114 @Test testHeaderOnly()115 public void testHeaderOnly() { 116 Bundle bundle = new Bundle(); 117 bundle.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER, DISCLAIMER_HEADER_1); 118 assertNull(mDisclaimersParser.parse(new Bundle[] { bundle })); 119 } 120 121 @Test testOneParsing()122 public void testOneParsing() throws Exception { 123 Bundle bundle = new Bundle(); 124 bundle.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER, DISCLAIMER_HEADER_1); 125 bundle.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT, DISCLAIMER_URI_1); 126 127 DisclaimersParam disclaimers = mDisclaimersParser.parse(new Bundle[] { bundle }); 128 129 assertNotNull(disclaimers); 130 assertEquals(disclaimers.mDisclaimers.length, 1); 131 assertEquals(disclaimers.mDisclaimers[0].mHeader, DISCLAIMER_HEADER_1); 132 assertEquals(disclaimers.mDisclaimers[0].mContentFilePath, DISCLAIMER_FILE_DEST_1); 133 assertEquals(getDisclaimerContentString(disclaimers.mDisclaimers[0]), 134 DISCLAIMER_CONTENT_1); 135 } 136 137 @Test testThreeParsing()138 public void testThreeParsing() throws Exception { 139 Bundle bundle1 = new Bundle(); 140 bundle1.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER, DISCLAIMER_HEADER_1); 141 bundle1.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT, DISCLAIMER_URI_1); 142 Bundle bundle2 = new Bundle(); 143 bundle2.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER, DISCLAIMER_HEADER_2); 144 bundle2.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT, DISCLAIMER_URI_2); 145 Bundle bundle3 = new Bundle(); 146 bundle3.putString(EXTRA_PROVISIONING_DISCLAIMER_HEADER, DISCLAIMER_HEADER_3); 147 bundle3.putParcelable(EXTRA_PROVISIONING_DISCLAIMER_CONTENT, DISCLAIMER_URI_3); 148 149 DisclaimersParam disclaimers = mDisclaimersParser.parse( 150 new Bundle[] { bundle1, bundle2, bundle3 }); 151 152 // The order of disclaimers must follow the original orders 153 assertNotNull(disclaimers); 154 assertEquals(disclaimers.mDisclaimers.length, 3); 155 assertEquals(disclaimers.mDisclaimers[0].mHeader, DISCLAIMER_HEADER_1); 156 assertEquals(disclaimers.mDisclaimers[0].mContentFilePath, DISCLAIMER_FILE_DEST_1); 157 assertEquals(getDisclaimerContentString(disclaimers.mDisclaimers[0]), 158 DISCLAIMER_CONTENT_1); 159 assertEquals(disclaimers.mDisclaimers[1].mHeader, DISCLAIMER_HEADER_2); 160 assertEquals(disclaimers.mDisclaimers[1].mContentFilePath, DISCLAIMER_FILE_DEST_2); 161 assertEquals(getDisclaimerContentString(disclaimers.mDisclaimers[1]), 162 DISCLAIMER_CONTENT_2); 163 assertEquals(disclaimers.mDisclaimers[2].mHeader, DISCLAIMER_HEADER_3); 164 assertEquals(disclaimers.mDisclaimers[2].mContentFilePath, DISCLAIMER_FILE_DEST_3); 165 assertEquals(getDisclaimerContentString(disclaimers.mDisclaimers[2]), 166 DISCLAIMER_CONTENT_3); 167 } 168 getDisclaimerContentString(DisclaimersParam.Disclaimer disclaimer)169 private String getDisclaimerContentString(DisclaimersParam.Disclaimer disclaimer) 170 throws IOException { 171 return StoreUtils.readString(new File(disclaimer.mContentFilePath)); 172 } 173 getDisclaimerPath(int index)174 private static String getDisclaimerPath(int index) { 175 return new File(new File(TEST_FILE_DIR, DIR_PROVISIONING_PARAMS_FILE_CACHE), 176 "disclaimer_content_" + TEST_PROVISIONING_ID + "_" + ( index - 1 ) + ".txt") 177 .getAbsolutePath(); 178 } 179 } 180