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