1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.android.managedprovisioning.e2eui;
16
17import android.app.admin.DevicePolicyManager;
18import android.content.Context;
19import android.content.Intent;
20import android.os.PersistableBundle;
21import android.os.UserHandle;
22import android.util.Log;
23
24/**
25 * Utility functions for end to end tests
26 */
27public class E2eUiTestUtils {
28    private static final String TAG = E2eUiTestUtils.class.getSimpleName();
29    private static final String EXTRAS_BUNDLE_TEST_KEY = "extras_bundle_test_key";
30
31    public static Intent insertProvisioningExtras(Intent intent) {
32        PersistableBundle bundle = new PersistableBundle();
33        bundle.putBoolean(EXTRAS_BUNDLE_TEST_KEY, true);
34        intent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, bundle);
35        return intent;
36    }
37
38    public static boolean verifyProvisioningExtras(Intent intent) {
39        PersistableBundle persistableBundle = intent
40                .getParcelableExtra(DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE);
41        return persistableBundle != null && persistableBundle.getBoolean(EXTRAS_BUNDLE_TEST_KEY);
42    }
43
44    public static void sendResult(String action, Context context, boolean result) {
45        Intent resultBroadcast = new Intent(action);
46        resultBroadcast.putExtra(ProvisioningResultListener.EXTRA_RESULT, result);
47        context.sendBroadcastAsUser(resultBroadcast, UserHandle.SYSTEM);
48    }
49
50    public static boolean verifyProfile(Context context, Intent intent,
51            DevicePolicyManager dpm) {
52        dpm.setProfileEnabled(ManagedProfileAdminReceiver.COMPONENT_NAME);
53        final boolean isProfileOwner = dpm.isProfileOwnerApp(context.getPackageName());
54        Log.i(TAG, "isProfileOwner: " + isProfileOwner);
55        final boolean verifyProvisioningExtras = verifyProvisioningExtras(intent);
56        Log.i(TAG, "verifyProvisioningExtras: " + verifyProvisioningExtras);
57        return isProfileOwner && verifyProvisioningExtras;
58    }
59}
60