Home | History | Annotate | Download | only in managedprovisioning
      1 /*
      2  * Copyright (C) 2012 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 
     17 package com.android.cts.verifier.managedprovisioning;
     18 
     19 import android.app.KeyguardManager;
     20 import android.app.admin.DevicePolicyManager;
     21 import android.content.ActivityNotFoundException;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.pm.PackageManager;
     26 import android.net.ConnectivityManager;
     27 import android.os.Bundle;
     28 import android.provider.Settings;
     29 import android.util.Log;
     30 import android.view.View;
     31 import android.view.View.OnClickListener;
     32 import android.widget.Toast;
     33 
     34 import com.android.cts.verifier.ArrayTestListAdapter;
     35 import com.android.cts.verifier.DialogTestListActivity;
     36 import com.android.cts.verifier.R;
     37 import com.android.cts.verifier.TestListActivity;
     38 import com.android.cts.verifier.TestListAdapter.TestListItem;
     39 import com.android.cts.verifier.TestResult;
     40 import com.android.cts.verifier.location.LocationListenerActivity;
     41 
     42 /**
     43  * CTS verifier test for BYOD managed provisioning flow
     44  *
     45  * This activity is responsible for starting the managed provisioning flow and verify the outcome of
     46  * provisioning. It performs the following verifications:
     47  *   Full disk encryption is enabled.
     48  *   Profile owner is correctly installed.
     49  *   Profile owner shows up in the Settings app.
     50  *   Badged work apps show up in launcher.
     51  * The first two verifications are performed automatically, by interacting with profile owner using
     52  * cross-profile intents, while the last two are carried out manually by the user.
     53  */
     54 public class ByodFlowTestActivity extends DialogTestListActivity {
     55 
     56     private static final String TAG = "ByodFlowTestActivity";
     57     private static ConnectivityManager mCm;
     58     private static final int REQUEST_MANAGED_PROVISIONING = 0;
     59     private static final int REQUEST_PROFILE_OWNER_STATUS = 1;
     60     private static final int REQUEST_INTENT_FILTERS_STATUS = 2;
     61     private static final int REQUEST_CHECK_DISK_ENCRYPTION = 3;
     62     private static final int REQUEST_SET_LOCK_FOR_ENCRYPTION = 4;
     63 
     64     private ComponentName mAdminReceiverComponent;
     65     private KeyguardManager mKeyguardManager;
     66     private ByodFlowTestHelper mByodFlowTestHelper;
     67 
     68     private DialogTestListItem mProfileOwnerInstalled;
     69     private DialogTestListItem mDiskEncryptionTest;
     70     private DialogTestListItem mProfileAccountVisibleTest;
     71     private DialogTestListItem mDeviceAdminVisibleTest;
     72     private DialogTestListItem mWorkAppVisibleTest;
     73     private DialogTestListItem mCrossProfileIntentFiltersTestFromPersonal;
     74     private DialogTestListItem mCrossProfileIntentFiltersTestFromWork;
     75     private DialogTestListItem mAppLinkingTest;
     76     private DialogTestListItem mDisableNonMarketTest;
     77     private DialogTestListItem mEnableNonMarketTest;
     78     private DialogTestListItem mWorkNotificationBadgedTest;
     79     private DialogTestListItem mWorkStatusBarIconTest;
     80     private DialogTestListItem mWorkStatusBarToastTest;
     81     private DialogTestListItem mUserSettingsVisibleTest;
     82     private DialogTestListItem mAppSettingsVisibleTest;
     83     private DialogTestListItem mLocationSettingsVisibleTest;
     84     private DialogTestListItem mWiFiDataUsageSettingsVisibleTest;
     85     private DialogTestListItem mCellularDataUsageSettingsVisibleTest;
     86     private DialogTestListItem mCredSettingsVisibleTest;
     87     private DialogTestListItem mPrintSettingsVisibleTest;
     88     private DialogTestListItem mIntentFiltersTest;
     89     private DialogTestListItem mPermissionLockdownTest;
     90     private DialogTestListItem mCrossProfileImageCaptureSupportTest;
     91     private DialogTestListItem mCrossProfileVideoCaptureWithExtraOutputSupportTest;
     92     private DialogTestListItem mCrossProfileVideoCaptureWithoutExtraOutputSupportTest;
     93     private DialogTestListItem mCrossProfileAudioCaptureSupportTest;
     94     private TestListItem mKeyguardDisabledFeaturesTest;
     95     private DialogTestListItem mDisableNfcBeamTest;
     96     private TestListItem mAuthenticationBoundKeyTest;
     97     private DialogTestListItem mEnableLocationModeTest;
     98     private DialogTestListItem mDisableLocationModeThroughMainSwitchTest;
     99     private DialogTestListItem mDisableLocationModeThroughWorkSwitchTest;
    100     private DialogTestListItem mPrimaryLocationWhenWorkDisabledTest;
    101     private DialogTestListItem mSelectWorkChallenge;
    102     private DialogTestListItem mConfirmWorkCredentials;
    103     private DialogTestListItem mParentProfilePassword;
    104     private TestListItem mVpnTest;
    105     private TestListItem mKeyChainTest;
    106     private TestListItem mAlwaysOnVpnSettingsTest;
    107     private TestListItem mRecentsTest;
    108     private TestListItem mDisallowAppsControlTest;
    109     private TestListItem mOrganizationInfoTest;
    110     private TestListItem mPolicyTransparencyTest;
    111     private TestListItem mTurnOffWorkFeaturesTest;
    112     private TestListItem mWidgetTest;
    113 
    114     public ByodFlowTestActivity() {
    115         super(R.layout.provisioning_byod,
    116                 R.string.provisioning_byod, R.string.provisioning_byod_info,
    117                 R.string.provisioning_byod_instructions);
    118     }
    119 
    120     @Override
    121     protected void onCreate(Bundle savedInstanceState) {
    122         super.onCreate(savedInstanceState);
    123         mByodFlowTestHelper = new ByodFlowTestHelper(this);
    124         mAdminReceiverComponent = new ComponentName(this, DeviceAdminTestReceiver.class.getName());
    125         mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    126 
    127         mByodFlowTestHelper.setup();
    128 
    129         mPrepareTestButton.setText(R.string.provisioning_byod_start);
    130         mPrepareTestButton.setOnClickListener(new OnClickListener() {
    131             @Override
    132             public void onClick(View v) {
    133                 Utils.provisionManagedProfile(ByodFlowTestActivity.this, mAdminReceiverComponent,
    134                         REQUEST_MANAGED_PROVISIONING);
    135             }
    136         });
    137 
    138         // If we are started by managed provisioning (fresh managed provisioning after encryption
    139         // reboot), redirect the user back to the main test list. This is because the test result
    140         // is only saved by the parent TestListActivity, and if we did allow the user to proceed
    141         // here, the test result would be lost when this activity finishes.
    142         if (ByodHelperActivity.ACTION_PROFILE_OWNER_STATUS.equals(getIntent().getAction())) {
    143             startActivity(new Intent(this, TestListActivity.class));
    144             // Calling super.finish() because we delete managed profile in our overridden of finish(),
    145             // which is not what we want to do here.
    146             super.finish();
    147         } else {
    148             queryProfileOwner(false);
    149         }
    150     }
    151 
    152     @Override
    153     protected void onNewIntent(Intent intent) {
    154         // This is called when managed provisioning completes successfully without reboot.
    155         super.onNewIntent(intent);
    156         if (ByodHelperActivity.ACTION_PROFILE_OWNER_STATUS.equals(intent.getAction())) {
    157             handleStatusUpdate(RESULT_OK, intent);
    158         }
    159     }
    160 
    161     @Override
    162     protected void handleActivityResult(int requestCode, int resultCode, Intent data) {
    163         switch (requestCode) {
    164             case REQUEST_MANAGED_PROVISIONING:
    165                 return;
    166             case REQUEST_PROFILE_OWNER_STATUS:
    167                 // Called after queryProfileOwner()
    168                 handleStatusUpdate(resultCode, data);
    169                 break;
    170             case REQUEST_CHECK_DISK_ENCRYPTION:
    171                 // Called after checkDiskEncryption()
    172                 handleDiskEncryptionStatus(resultCode, data);
    173                 break;
    174             case REQUEST_SET_LOCK_FOR_ENCRYPTION:
    175                 // Called after handleDiskEncryptionStatus() to set screen lock if necessary
    176                 handleSetLockForEncryption();
    177                 break;
    178             case REQUEST_INTENT_FILTERS_STATUS:
    179                 // Called after checkIntentFilters()
    180                 handleIntentFiltersStatus(resultCode);
    181                 break;
    182             default:
    183                 super.handleActivityResult(requestCode, resultCode, data);
    184         }
    185     }
    186 
    187     private void handleStatusUpdate(int resultCode, Intent data) {
    188         boolean provisioned = data != null &&
    189                 data.getBooleanExtra(ByodHelperActivity.EXTRA_PROVISIONED, false);
    190         setTestResult(mProfileOwnerInstalled, (provisioned && resultCode == RESULT_OK) ?
    191                 TestResult.TEST_RESULT_PASSED : TestResult.TEST_RESULT_FAILED);
    192     }
    193 
    194     @Override
    195     public void finish() {
    196         // Pass and fail buttons are known to call finish() when clicked, and this is when we want to
    197         // clean up the provisioned profile.
    198         mByodFlowTestHelper.tearDown();
    199         super.finish();
    200     }
    201 
    202     @Override
    203     protected void setupTests(ArrayTestListAdapter adapter) {
    204         mProfileOwnerInstalled = new DialogTestListItem(this,
    205                 R.string.provisioning_byod_profileowner,
    206                 "BYOD_ProfileOwnerInstalled") {
    207             @Override
    208             public void performTest(DialogTestListActivity activity) {
    209                 queryProfileOwner(true);
    210             }
    211         };
    212 
    213         mDiskEncryptionTest = new DialogTestListItem(this,
    214                 R.string.provisioning_byod_disk_encryption,
    215                 "BYOD_DiskEncryptionTest") {
    216             @Override
    217             public void performTest(DialogTestListActivity activity) {
    218                 checkDiskEncryption();
    219             }
    220         };
    221 
    222         /*
    223          * To keep the image in this test up to date, use the instructions in
    224          * {@link ByodIconSamplerActivity}.
    225          */
    226 
    227         if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
    228             mWorkAppVisibleTest = new DialogTestListItemWithIcon(this,
    229                     R.string.provisioning_byod_workapps_visible,
    230                     "BYOD_WorkAppVisibleTest",
    231                     R.string.provisioning_byod_workapps_visible_instruction,
    232                     new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME),
    233                     R.drawable.badged_icon);
    234 
    235             mConfirmWorkCredentials = new DialogTestListItem(this,
    236                     R.string.provisioning_byod_confirm_work_credentials,
    237                     "BYOD_ConfirmWorkCredentials",
    238                     R.string.provisioning_byod_confirm_work_credentials_description,
    239                     new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
    240 
    241             mWiFiDataUsageSettingsVisibleTest = new DialogTestListItem(this,
    242                     R.string.provisioning_byod_wifi_data_usage_settings,
    243                     "BYOD_WiFiDataUsageSettingsVisibleTest",
    244                     R.string.provisioning_byod_wifi_data_usage_settings_instruction,
    245                     new Intent(Settings.ACTION_SETTINGS));
    246         }
    247 
    248         mWorkNotificationBadgedTest = new DialogTestListItemWithIcon(this,
    249                 R.string.provisioning_byod_work_notification,
    250                 "BYOD_WorkNotificationBadgedTest",
    251                 R.string.provisioning_byod_work_notification_instruction,
    252                 new Intent(ByodHelperActivity.ACTION_NOTIFICATION),
    253                 R.drawable.ic_corp_icon);
    254 
    255         Intent workStatusIcon = new Intent(WorkStatusTestActivity.ACTION_WORK_STATUS_ICON);
    256         workStatusIcon.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    257         mWorkStatusBarIconTest = new DialogTestListItemWithIcon(this,
    258                 R.string.provisioning_byod_work_status_icon,
    259                 "BYOD_WorkStatusBarIconTest",
    260                 R.string.provisioning_byod_work_status_icon_instruction,
    261                 workStatusIcon,
    262                 R.drawable.stat_sys_managed_profile_status);
    263 
    264         Intent workStatusToast = new Intent(WorkStatusTestActivity.ACTION_WORK_STATUS_TOAST);
    265         workStatusToast.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    266         mWorkStatusBarToastTest = new DialogTestListItem(this,
    267                 R.string.provisioning_byod_work_status_toast,
    268                 "BYOD_WorkStatusBarToastTest",
    269                 R.string.provisioning_byod_work_status_toast_instruction,
    270                 workStatusToast);
    271 
    272         mDisableNonMarketTest = new DialogTestListItem(this,
    273                 R.string.provisioning_byod_nonmarket_deny,
    274                 "BYOD_DisableNonMarketTest",
    275                 R.string.provisioning_byod_nonmarket_deny_info,
    276                 new Intent(ByodHelperActivity.ACTION_INSTALL_APK)
    277                         .putExtra(ByodHelperActivity.EXTRA_ALLOW_NON_MARKET_APPS, false));
    278 
    279         mEnableNonMarketTest = new DialogTestListItem(this,
    280                 R.string.provisioning_byod_nonmarket_allow,
    281                 "BYOD_EnableNonMarketTest",
    282                 R.string.provisioning_byod_nonmarket_allow_info,
    283                 new Intent(ByodHelperActivity.ACTION_INSTALL_APK)
    284                         .putExtra(ByodHelperActivity.EXTRA_ALLOW_NON_MARKET_APPS, true));
    285 
    286         mProfileAccountVisibleTest = new DialogTestListItem(this,
    287                 R.string.provisioning_byod_profile_visible,
    288                 "BYOD_ProfileAccountVisibleTest",
    289                 R.string.provisioning_byod_profile_visible_instruction,
    290                 new Intent(Settings.ACTION_SETTINGS));
    291 
    292         mUserSettingsVisibleTest = new DialogTestListItem(this,
    293             R.string.provisioning_byod_user_settings,
    294             "BYOD_UserSettingsVisibleTest",
    295             R.string.provisioning_byod_user_settings_instruction,
    296             new Intent(Settings.ACTION_SETTINGS));
    297 
    298         mAppSettingsVisibleTest = new DialogTestListItem(this,
    299                 R.string.provisioning_byod_app_settings,
    300                 "BYOD_AppSettingsVisibleTest",
    301                 R.string.provisioning_byod_app_settings_instruction,
    302                 new Intent(Settings.ACTION_APPLICATION_SETTINGS));
    303 
    304         mDeviceAdminVisibleTest = new DialogTestListItem(this,
    305                 R.string.provisioning_byod_admin_visible,
    306                 "BYOD_DeviceAdminVisibleTest",
    307                 R.string.provisioning_byod_admin_visible_instruction,
    308                 new Intent(Settings.ACTION_SECURITY_SETTINGS));
    309 
    310         mCredSettingsVisibleTest = new DialogTestListItem(this,
    311                 R.string.provisioning_byod_cred_settings,
    312                 "BYOD_CredSettingsVisibleTest",
    313                 R.string.provisioning_byod_cred_settings_instruction,
    314                 new Intent(Settings.ACTION_SECURITY_SETTINGS));
    315 
    316         mLocationSettingsVisibleTest = new DialogTestListItem(this,
    317                 R.string.provisioning_byod_location_settings,
    318                 "BYOD_LocationSettingsVisibleTest",
    319                 R.string.provisioning_byod_location_settings_instruction,
    320                 new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
    321 
    322         mCellularDataUsageSettingsVisibleTest = new DialogTestListItem(this,
    323                 R.string.provisioning_byod_cellular_data_usage_settings,
    324                 "BYOD_CellularDataUsageSettingsVisibleTest",
    325                 R.string.provisioning_byod_cellular_data_usage_settings_instruction,
    326                 new Intent(Settings.ACTION_SETTINGS));
    327 
    328         mPrintSettingsVisibleTest = new DialogTestListItem(this,
    329                 R.string.provisioning_byod_print_settings,
    330                 "BYOD_PrintSettingsVisibleTest",
    331                 R.string.provisioning_byod_print_settings_instruction,
    332                 new Intent(Settings.ACTION_PRINT_SETTINGS));
    333 
    334         Intent intent = new Intent(CrossProfileTestActivity.ACTION_CROSS_PROFILE_TO_WORK);
    335         intent.putExtra(CrossProfileTestActivity.EXTRA_STARTED_FROM_WORK, false);
    336         Intent chooser = Intent.createChooser(intent,
    337                 getResources().getString(R.string.provisioning_cross_profile_chooser));
    338         mCrossProfileIntentFiltersTestFromPersonal = new DialogTestListItem(this,
    339                 R.string.provisioning_byod_cross_profile_from_personal,
    340                 "BYOD_CrossProfileIntentFiltersTestFromPersonal",
    341                 R.string.provisioning_byod_cross_profile_from_personal_instruction,
    342                 chooser);
    343 
    344         mCrossProfileIntentFiltersTestFromWork = new DialogTestListItem(this,
    345                 R.string.provisioning_byod_cross_profile_from_work,
    346                 "BYOD_CrossProfileIntentFiltersTestFromWork",
    347                 R.string.provisioning_byod_cross_profile_from_work_instruction,
    348                 new Intent(ByodHelperActivity.ACTION_TEST_CROSS_PROFILE_INTENTS_DIALOG));
    349 
    350         /* Disable due to b/33571176
    351         mAppLinkingTest = new DialogTestListItem(this,
    352                 R.string.provisioning_app_linking,
    353                 "BYOD_AppLinking",
    354                 R.string.provisioning_byod_app_linking_instruction,
    355                 new Intent(ByodHelperActivity.ACTION_TEST_APP_LINKING_DIALOG));
    356         */
    357 
    358         mKeyguardDisabledFeaturesTest = TestListItem.newTest(this,
    359                 R.string.provisioning_byod_keyguard_disabled_features,
    360                 KeyguardDisabledFeaturesActivity.class.getName(),
    361                 new Intent(this, KeyguardDisabledFeaturesActivity.class), null);
    362 
    363         mAuthenticationBoundKeyTest = TestListItem.newTest(this,
    364                 R.string.provisioning_byod_auth_bound_key,
    365                 AuthenticationBoundKeyTestActivity.class.getName(),
    366                 new Intent(AuthenticationBoundKeyTestActivity.ACTION_AUTH_BOUND_KEY_TEST),
    367                 null);
    368 
    369         mVpnTest = TestListItem.newTest(this,
    370                 R.string.provisioning_byod_vpn,
    371                 VpnTestActivity.class.getName(),
    372                 new Intent(VpnTestActivity.ACTION_VPN),
    373                 null);
    374 
    375         mAlwaysOnVpnSettingsTest = TestListItem.newTest(this,
    376                 R.string.provisioning_byod_always_on_vpn,
    377                 AlwaysOnVpnSettingsTestActivity.class.getName(),
    378                 new Intent(AlwaysOnVpnSettingsTestActivity.ACTION_ALWAYS_ON_VPN_SETTINGS_TEST),
    379                 null);
    380 
    381         mDisallowAppsControlTest = TestListItem.newTest(this,
    382                 R.string.provisioning_byod_disallow_apps_control,
    383                 DisallowAppsControlActivity.class.getName(),
    384                 new Intent(this, DisallowAppsControlActivity.class), null);
    385 
    386         // Test for checking if the required intent filters are set during managed provisioning.
    387         mIntentFiltersTest = new DialogTestListItem(this,
    388                 R.string.provisioning_byod_cross_profile_intent_filters,
    389                 "BYOD_IntentFiltersTest") {
    390             @Override
    391             public void performTest(DialogTestListActivity activity) {
    392                 checkIntentFilters();
    393             }
    394         };
    395 
    396         mTurnOffWorkFeaturesTest = TestListItem.newTest(this,
    397                 R.string.provisioning_byod_turn_off_work,
    398                 TurnOffWorkActivity.class.getName(),
    399                 new Intent(this, TurnOffWorkActivity.class), null);
    400 
    401         Intent permissionCheckIntent = new Intent(
    402                 PermissionLockdownTestActivity.ACTION_MANAGED_PROFILE_CHECK_PERMISSION_LOCKDOWN);
    403         mPermissionLockdownTest = new DialogTestListItem(this,
    404                 R.string.device_profile_owner_permission_lockdown_test,
    405                 "BYOD_PermissionLockdownTest",
    406                 R.string.profile_owner_permission_lockdown_test_info,
    407                 permissionCheckIntent);
    408 
    409         mSelectWorkChallenge = new DialogTestListItem(this,
    410                 R.string.provisioning_byod_select_work_challenge,
    411                 "BYOD_SelectWorkChallenge",
    412                 R.string.provisioning_byod_select_work_challenge_description,
    413                 new Intent(ByodHelperActivity.ACTION_TEST_SELECT_WORK_CHALLENGE));
    414 
    415         mRecentsTest = TestListItem.newTest(this,
    416                 R.string.provisioning_byod_recents,
    417                 RecentsRedactionActivity.class.getName(),
    418                 new Intent(RecentsRedactionActivity.ACTION_RECENTS),
    419                 null);
    420 
    421         mOrganizationInfoTest = TestListItem.newTest(this,
    422                 R.string.provisioning_byod_organization_info,
    423                 OrganizationInfoTestActivity.class.getName(),
    424                 new Intent(this, OrganizationInfoTestActivity.class),
    425                 null);
    426 
    427         mKeyChainTest = TestListItem.newTest(this,
    428                 R.string.provisioning_byod_keychain,
    429                 KeyChainTestActivity.class.getName(),
    430                 new Intent(KeyChainTestActivity.ACTION_KEYCHAIN),
    431                 null);
    432 
    433         mParentProfilePassword = new DialogTestListItem(this,
    434                 R.string.provisioning_byod_parent_profile_password,
    435                 "BYOD_ParentProfilePasswordTest",
    436                 R.string.provisioning_byod_parent_profile_password_description,
    437                 new Intent(ByodHelperActivity.ACTION_TEST_PARENT_PROFILE_PASSWORD));
    438 
    439         final Intent policyTransparencyTestIntent = new Intent(this,
    440                 PolicyTransparencyTestListActivity.class);
    441         policyTransparencyTestIntent.putExtra(
    442                 PolicyTransparencyTestListActivity.EXTRA_MODE,
    443                 PolicyTransparencyTestListActivity.MODE_MANAGED_PROFILE);
    444         policyTransparencyTestIntent.putExtra(
    445                 PolicyTransparencyTestActivity.EXTRA_TEST_ID, "BYOD_PolicyTransparency");
    446         mPolicyTransparencyTest = TestListItem.newTest(this,
    447                 R.string.device_profile_owner_policy_transparency_test,
    448                 "BYOD_PolicyTransparency",
    449                 policyTransparencyTestIntent, null);
    450 
    451         adapter.add(mProfileOwnerInstalled);
    452         adapter.add(mDiskEncryptionTest);
    453 
    454         // Badge related tests
    455         if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
    456             adapter.add(mWorkAppVisibleTest);
    457         }
    458 
    459         adapter.add(mWorkNotificationBadgedTest);
    460         adapter.add(mWorkStatusBarIconTest);
    461         adapter.add(mWorkStatusBarToastTest);
    462 
    463         // Settings related tests.
    464         adapter.add(mProfileAccountVisibleTest);
    465         adapter.add(mDeviceAdminVisibleTest);
    466         adapter.add(mCredSettingsVisibleTest);
    467         adapter.add(mUserSettingsVisibleTest);
    468         adapter.add(mAppSettingsVisibleTest);
    469         adapter.add(mLocationSettingsVisibleTest);
    470         adapter.add(mPrintSettingsVisibleTest);
    471 
    472         adapter.add(mCrossProfileIntentFiltersTestFromPersonal);
    473         adapter.add(mCrossProfileIntentFiltersTestFromWork);
    474         /* Disable due to b/33571176
    475         adapter.add(mAppLinkingTest);
    476         */
    477         adapter.add(mDisableNonMarketTest);
    478         adapter.add(mEnableNonMarketTest);
    479         adapter.add(mIntentFiltersTest);
    480         adapter.add(mPermissionLockdownTest);
    481         adapter.add(mKeyguardDisabledFeaturesTest);
    482         adapter.add(mAuthenticationBoundKeyTest);
    483         adapter.add(mVpnTest);
    484         adapter.add(mAlwaysOnVpnSettingsTest);
    485         adapter.add(mTurnOffWorkFeaturesTest);
    486         adapter.add(mSelectWorkChallenge);
    487         if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
    488             adapter.add(mConfirmWorkCredentials);
    489         }
    490         adapter.add(mRecentsTest);
    491         adapter.add(mOrganizationInfoTest);
    492         adapter.add(mParentProfilePassword);
    493         adapter.add(mPolicyTransparencyTest);
    494 
    495         if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
    496             if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
    497                 adapter.add(mWiFiDataUsageSettingsVisibleTest);
    498             }
    499         }
    500 
    501         mCm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    502         if(mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null) {
    503             adapter.add(mCellularDataUsageSettingsVisibleTest);
    504         }
    505 
    506         if (canResolveIntent(new Intent(Settings.ACTION_APPLICATION_SETTINGS))) {
    507             adapter.add(mDisallowAppsControlTest);
    508         }
    509 
    510         /* If there is an application that handles ACTION_IMAGE_CAPTURE, test that it handles it
    511          * well.
    512          */
    513         if (canResolveIntent(ByodHelperActivity.getCaptureImageIntent())) {
    514             // Capture image intent can be resolved in primary profile, so test.
    515             mCrossProfileImageCaptureSupportTest = new DialogTestListItem(this,
    516                     R.string.provisioning_byod_capture_image_support,
    517                     "BYOD_CrossProfileImageCaptureSupportTest",
    518                     R.string.provisioning_byod_capture_image_support_info,
    519                     new Intent(ByodHelperActivity.ACTION_CAPTURE_AND_CHECK_IMAGE));
    520             adapter.add(mCrossProfileImageCaptureSupportTest);
    521         } else {
    522             // Capture image intent cannot be resolved in primary profile, so skip test.
    523             Toast.makeText(ByodFlowTestActivity.this,
    524                     R.string.provisioning_byod_no_image_capture_resolver, Toast.LENGTH_SHORT)
    525                     .show();
    526         }
    527 
    528         /* If there is an application that handles ACTION_VIDEO_CAPTURE, test that it handles it
    529          * well.
    530          */
    531         if (canResolveIntent(ByodHelperActivity.getCaptureVideoIntent())) {
    532             // Capture video intent can be resolved in primary profile, so test.
    533             mCrossProfileVideoCaptureWithExtraOutputSupportTest = new DialogTestListItem(this,
    534                     R.string.provisioning_byod_capture_video_support_with_extra_output,
    535                     "BYOD_CrossProfileVideoCaptureWithExtraOutputSupportTest",
    536                     R.string.provisioning_byod_capture_video_support_info,
    537                     new Intent(ByodHelperActivity.ACTION_CAPTURE_AND_CHECK_VIDEO_WITH_EXTRA_OUTPUT));
    538             adapter.add(mCrossProfileVideoCaptureWithExtraOutputSupportTest);
    539             mCrossProfileVideoCaptureWithoutExtraOutputSupportTest = new DialogTestListItem(this,
    540                     R.string.provisioning_byod_capture_video_support_without_extra_output,
    541                     "BYOD_CrossProfileVideoCaptureWithoutExtraOutputSupportTest",
    542                     R.string.provisioning_byod_capture_video_support_info,
    543                     new Intent(ByodHelperActivity.ACTION_CAPTURE_AND_CHECK_VIDEO_WITHOUT_EXTRA_OUTPUT));
    544             adapter.add(mCrossProfileVideoCaptureWithoutExtraOutputSupportTest);
    545         } else {
    546             // Capture video intent cannot be resolved in primary profile, so skip test.
    547             Toast.makeText(ByodFlowTestActivity.this,
    548                     R.string.provisioning_byod_no_video_capture_resolver, Toast.LENGTH_SHORT)
    549                     .show();
    550         }
    551 
    552         if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
    553             mDisableNfcBeamTest = new DialogTestListItem(this, R.string.provisioning_byod_nfc_beam,
    554                     "BYOD_DisableNfcBeamTest",
    555                     R.string.provisioning_byod_nfc_beam_allowed_instruction,
    556                     new Intent(ByodHelperActivity.ACTION_TEST_NFC_BEAM)) {
    557                 @Override
    558                 public void performTest(final DialogTestListActivity activity) {
    559                     activity.showManualTestDialog(mDisableNfcBeamTest,
    560                             new DefaultTestCallback(mDisableNfcBeamTest) {
    561                         @Override
    562                         public void onPass() {
    563                             // Start a second test with beam disallowed by policy.
    564                             Intent testNfcBeamIntent = new Intent(
    565                                     ByodHelperActivity.ACTION_TEST_NFC_BEAM);
    566                             testNfcBeamIntent.putExtra(NfcTestActivity.EXTRA_DISALLOW_BY_POLICY,
    567                                     true);
    568                             DialogTestListItem disableNfcBeamTest2 =
    569                                     new DialogTestListItem(activity,
    570                                     R.string.provisioning_byod_nfc_beam,
    571                                     "BYOD_DisableNfcBeamTest",
    572                                     R.string.provisioning_byod_nfc_beam_disallowed_instruction,
    573                                     testNfcBeamIntent);
    574                             // The result should be reflected on the original test.
    575                             activity.showManualTestDialog(disableNfcBeamTest2,
    576                                     new DefaultTestCallback(mDisableNfcBeamTest));
    577                         }
    578                     });
    579                 }
    580             };
    581             adapter.add(mDisableNfcBeamTest);
    582             adapter.add(mKeyChainTest);
    583         }
    584 
    585         /* If there is an application that handles RECORD_SOUND_ACTION, test that it handles it
    586          * well.
    587          */
    588         if (canResolveIntent(ByodHelperActivity.getCaptureAudioIntent())) {
    589             // Capture audio intent can be resolved in primary profile, so test.
    590             mCrossProfileAudioCaptureSupportTest = new DialogTestListItem(this,
    591                     R.string.provisioning_byod_capture_audio_support,
    592                     "BYOD_CrossProfileAudioCaptureSupportTest",
    593                     R.string.provisioning_byod_capture_audio_support_info,
    594                     new Intent(ByodHelperActivity.ACTION_CAPTURE_AND_CHECK_AUDIO));
    595             adapter.add(mCrossProfileAudioCaptureSupportTest);
    596         } else {
    597             // Capture audio intent cannot be resolved in primary profile, so skip test.
    598             Toast.makeText(ByodFlowTestActivity.this,
    599                     R.string.provisioning_byod_no_audio_capture_resolver, Toast.LENGTH_SHORT)
    600                     .show();
    601         }
    602 
    603         if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)) {
    604             mEnableLocationModeTest = new DialogTestListItem(this,
    605                     R.string.provisioning_byod_location_mode_enable,
    606                     "BYOD_LocationModeEnableTest",
    607                     R.string.provisioning_byod_location_mode_enable_instruction,
    608                     new Intent(ByodHelperActivity.ACTION_BYOD_SET_LOCATION_AND_CHECK_UPDATES));
    609             mDisableLocationModeThroughMainSwitchTest = new DialogTestListItem(this,
    610                     R.string.provisioning_byod_location_mode_disable,
    611                     "BYOD_LocationModeDisableMainTest",
    612                     R.string.provisioning_byod_location_mode_disable_instruction,
    613                     new Intent(ByodHelperActivity.ACTION_BYOD_SET_LOCATION_AND_CHECK_UPDATES));
    614             mDisableLocationModeThroughWorkSwitchTest = new DialogTestListItem(this,
    615                     R.string.provisioning_byod_work_location_mode_disable,
    616                     "BYOD_LocationModeDisableWorkTest",
    617                     R.string.provisioning_byod_work_location_mode_disable_instruction,
    618                     new Intent(ByodHelperActivity.ACTION_BYOD_SET_LOCATION_AND_CHECK_UPDATES));
    619             mPrimaryLocationWhenWorkDisabledTest = new DialogTestListItem(this,
    620                     R.string.provisioning_byod_primary_location_when_work_disabled,
    621                     "BYOD_PrimaryLocationWhenWorkDisabled",
    622                     R.string.provisioning_byod_primary_location_when_work_disabled_instruction,
    623                     new Intent(LocationListenerActivity.ACTION_SET_LOCATION_AND_CHECK_UPDATES));
    624             adapter.add(mEnableLocationModeTest);
    625             adapter.add(mDisableLocationModeThroughMainSwitchTest);
    626             adapter.add(mDisableLocationModeThroughWorkSwitchTest);
    627             adapter.add(mPrimaryLocationWhenWorkDisabledTest);
    628         } else {
    629             // The system does not support GPS feature, so skip test.
    630             Toast.makeText(ByodFlowTestActivity.this,
    631                     R.string.provisioning_byod_no_gps_location_feature, Toast.LENGTH_SHORT)
    632                     .show();
    633         }
    634 
    635         mWidgetTest = TestListItem.newTest(this,
    636                 R.string.provisioning_byod_work_profile_widget,
    637                 WorkProfileWidgetActivity.class.getName(),
    638                 new Intent(WorkProfileWidgetActivity.ACTION_TEST_WORK_PROFILE_WIDGET),
    639                 new String[] {PackageManager.FEATURE_APP_WIDGETS});
    640         adapter.add(mWidgetTest);
    641 
    642     }
    643 
    644     // Return whether the intent can be resolved in the current profile
    645     private boolean canResolveIntent(Intent intent) {
    646         return intent.resolveActivity(getPackageManager()) != null;
    647     }
    648 
    649     @Override
    650     protected void clearRemainingState(final DialogTestListItem test) {
    651         super.clearRemainingState(test);
    652         if (ByodHelperActivity.ACTION_NOTIFICATION.equals(
    653                 test.getManualTestIntent().getAction())) {
    654             try {
    655                 startActivity(new Intent(
    656                         ByodHelperActivity.ACTION_CLEAR_NOTIFICATION));
    657             } catch (ActivityNotFoundException e) {
    658                 // User shouldn't run this test before work profile is set up.
    659             }
    660         }
    661     }
    662 
    663     private void queryProfileOwner(boolean showToast) {
    664         try {
    665             Intent intent = new Intent(ByodHelperActivity.ACTION_QUERY_PROFILE_OWNER);
    666             startActivityForResult(intent, REQUEST_PROFILE_OWNER_STATUS);
    667         }
    668         catch (ActivityNotFoundException e) {
    669             Log.d(TAG, "queryProfileOwner: ActivityNotFoundException", e);
    670             setTestResult(mProfileOwnerInstalled, TestResult.TEST_RESULT_FAILED);
    671             if (showToast) {
    672                 Utils.showToast(this, R.string.provisioning_byod_no_activity);
    673             }
    674         }
    675     }
    676 
    677     private void checkDiskEncryption() {
    678         try {
    679             Intent intent = new Intent(ByodHelperActivity.ACTION_CHECK_DISK_ENCRYPTION);
    680             startActivityForResult(intent, REQUEST_CHECK_DISK_ENCRYPTION);
    681         } catch (ActivityNotFoundException e) {
    682             Log.d(TAG, "checkDiskEncryption: ActivityNotFoundException", e);
    683             setTestResult(mDiskEncryptionTest, TestResult.TEST_RESULT_FAILED);
    684             Utils.showToast(this, R.string.provisioning_byod_no_activity);
    685         }
    686     }
    687 
    688     private void handleDiskEncryptionStatus(int resultCode, Intent data) {
    689         if (resultCode != RESULT_OK || data == null) {
    690             Log.e(TAG, "Failed to get result for disk encryption, result code: " + resultCode);
    691             setTestResult(mDiskEncryptionTest, TestResult.TEST_RESULT_FAILED);
    692             return;
    693         }
    694 
    695         final int status = data.getIntExtra(ByodHelperActivity.EXTRA_ENCRYPTION_STATUS,
    696                 DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED);
    697         switch (status) {
    698             case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE:
    699             case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_PER_USER:
    700                 setTestResult(mDiskEncryptionTest, TestResult.TEST_RESULT_PASSED);
    701                 break;
    702             case DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE_DEFAULT_KEY:
    703                 if (!mKeyguardManager.isDeviceSecure()) {
    704                     Utils.setScreenLock(this, REQUEST_SET_LOCK_FOR_ENCRYPTION);
    705                     return;
    706                 }
    707                 Log.e(TAG, "Disk encryption key is not entangled with lock screen credentials");
    708                 Toast.makeText(this, R.string.provisioning_byod_disk_encryption_default_key_toast,
    709                         Toast.LENGTH_LONG).show();
    710                 // fall through
    711             default:
    712                 setTestResult(mDiskEncryptionTest, TestResult.TEST_RESULT_FAILED);
    713         }
    714 
    715         if (mKeyguardManager.isDeviceSecure()) {
    716             Utils.removeScreenLock(this);
    717         }
    718     }
    719 
    720     private void handleSetLockForEncryption() {
    721         if (mKeyguardManager.isDeviceSecure()) {
    722             checkDiskEncryption();
    723         } else {
    724             setTestResult(mDiskEncryptionTest, TestResult.TEST_RESULT_FAILED);
    725             Toast.makeText(this, R.string.provisioning_byod_disk_encryption_no_pin_toast,
    726                     Toast.LENGTH_LONG).show();
    727         }
    728     }
    729 
    730     private void checkIntentFilters() {
    731         try {
    732             // Enable component HandleIntentActivity before intent filters are checked.
    733             setHandleIntentActivityEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
    734             // We disable the ByodHelperActivity in the primary profile. So, this intent
    735             // will be handled by the ByodHelperActivity in the managed profile.
    736             Intent intent = new Intent(ByodHelperActivity.ACTION_CHECK_INTENT_FILTERS);
    737             startActivityForResult(intent, REQUEST_INTENT_FILTERS_STATUS);
    738         } catch (ActivityNotFoundException e) {
    739             // Disable component HandleIntentActivity if intent filters check fails.
    740             setHandleIntentActivityEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    741             Log.d(TAG, "checkIntentFilters: ActivityNotFoundException", e);
    742             setTestResult(mIntentFiltersTest, TestResult.TEST_RESULT_FAILED);
    743             Utils.showToast(this, R.string.provisioning_byod_no_activity);
    744         }
    745     }
    746 
    747     private void handleIntentFiltersStatus(int resultCode) {
    748         // Disable component HandleIntentActivity after intent filters are checked.
    749         setHandleIntentActivityEnabledSetting(PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    750         // we use the resultCode from ByodHelperActivity in the managed profile to know if certain
    751         // intents fired from the managed profile are forwarded.
    752         final boolean intentFiltersSetForManagedIntents = (resultCode == RESULT_OK);
    753         // Since the ByodFlowTestActivity is running in the primary profile, we directly use
    754         // the IntentFiltersTestHelper to know if certain intents fired from the primary profile
    755         // are forwarded.
    756         final boolean intentFiltersSetForPrimaryIntents =
    757                 new IntentFiltersTestHelper(this).checkCrossProfileIntentFilters(
    758                         IntentFiltersTestHelper.FLAG_INTENTS_FROM_PRIMARY);
    759         final boolean intentFiltersSet =
    760                 intentFiltersSetForPrimaryIntents & intentFiltersSetForManagedIntents;
    761         setTestResult(mIntentFiltersTest,
    762                 intentFiltersSet ? TestResult.TEST_RESULT_PASSED : TestResult.TEST_RESULT_FAILED);
    763     }
    764 
    765     private void setHandleIntentActivityEnabledSetting(final int enableState) {
    766         getPackageManager().setComponentEnabledSetting(
    767             new ComponentName(ByodFlowTestActivity.this, HandleIntentActivity.class.getName()),
    768             enableState, PackageManager.DONT_KILL_APP);
    769     }
    770 }
    771