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.ACTION_PROVISION_MANAGED_DEVICE;
19 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE;
20 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
21 
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.spy;
24 
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.os.UserHandle;
28 import android.test.AndroidTestCase;
29 
30 import androidx.test.filters.SmallTest;
31 
32 import com.android.managedprovisioning.common.SettingsFacade;
33 import com.android.managedprovisioning.common.Utils;
34 
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 
38 /** Tests {@link MessageParser} */
39 @SmallTest
40 public class MessageParserTest extends AndroidTestCase {
41     private static final String TEST_PACKAGE_NAME = "com.afwsamples.testdpc";
42     private static final ComponentName TEST_COMPONENT_NAME =
43             ComponentName.unflattenFromString(
44                     "com.afwsamples.testdpc/com.afwsamples.testdpc.DeviceAdminReceiver");
45 
46     @Mock
47     private Context mContext;
48 
49     private Utils mUtils;
50 
51     private MessageParser mMessageParser;
52 
53     @Override
setUp()54     public void setUp() {
55         // this is necessary for mockito to work
56         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
57 
58         MockitoAnnotations.initMocks(this);
59         mUtils = spy(new Utils());
60         mMessageParser = new MessageParser(
61                 mContext, mUtils, new ParserUtils(), new SettingsFacade());
62     }
63 
test_correctParserUsedToParseOtherSupportedProvisioningIntent()64     public void test_correctParserUsedToParseOtherSupportedProvisioningIntent() throws Exception {
65         // GIVEN the device admin app is installed.
66         doReturn(TEST_COMPONENT_NAME)
67                 .when(mUtils)
68                 .findDeviceAdmin(null, TEST_COMPONENT_NAME, mContext, UserHandle.myUserId());
69         // GIVEN a list of supported provisioning actions, except NFC.
70         String[] supportedProvisioningActions = new String[] {
71                 ACTION_PROVISION_MANAGED_DEVICE,
72                 ACTION_PROVISION_MANAGED_DEVICE_FROM_TRUSTED_SOURCE,
73                 ACTION_PROVISION_MANAGED_PROFILE
74         };
75 
76         for (String provisioningAction : supportedProvisioningActions) {
77             // WHEN the mMessageParser.getParser is invoked.
78             ProvisioningDataParser parser = mMessageParser.getParser();
79 
80             // THEN the extras parser is returned.
81             assertTrue(parser instanceof ExtrasProvisioningDataParser);
82         }
83     }
84 }
85