1 /* 2 * Copyright (C) 2017 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.settings.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.pm.PackageManager; 27 import android.os.Bundle; 28 29 import androidx.preference.Preference; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.shadows.ShadowApplication; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class BluetoothFilesPreferenceControllerTest { 42 43 private Context mContext; 44 private BluetoothFilesPreferenceController mController; 45 private Preference mPreference; 46 @Mock 47 private PackageManager mPackageManager; 48 49 @Before setUp()50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 mContext = spy(RuntimeEnvironment.application); 53 mController = new BluetoothFilesPreferenceController(mContext); 54 mPreference = new Preference(mContext); 55 mPreference.setKey(BluetoothFilesPreferenceController.KEY_RECEIVED_FILES); 56 doReturn(mPackageManager).when(mContext).getPackageManager(); 57 doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); 58 } 59 60 @Test testHandlePreferenceTreeClick_sendBroadcast()61 public void testHandlePreferenceTreeClick_sendBroadcast() { 62 mController.handlePreferenceTreeClick(mPreference); 63 64 final Intent intent = ShadowApplication.getInstance().getNextStartedActivity(); 65 assertThat(intent).isNotNull(); 66 assertThat(intent.getAction()) 67 .isEqualTo(BluetoothFilesPreferenceController.ACTION_OPEN_FILES); 68 69 final Bundle bundle = intent.getExtras(); 70 assertThat(bundle.getInt(BluetoothFilesPreferenceController.EXTRA_DIRECTION)).isEqualTo(1); 71 assertThat(bundle.getBoolean(BluetoothFilesPreferenceController.EXTRA_SHOW_ALL_FILES)) 72 .isTrue(); 73 } 74 } 75