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 17package com.android.settings.wifi.p2p; 18 19 20import android.net.wifi.p2p.WifiP2pDevice; 21import android.support.v7.preference.Preference; 22import android.support.v7.preference.PreferenceScreen; 23 24import com.android.settings.testutils.SettingsRobolectricTestRunner; 25import com.android.settings.TestConfig; 26 27import org.junit.Before; 28import org.junit.Test; 29import org.junit.runner.RunWith; 30import org.mockito.Mock; 31import org.mockito.MockitoAnnotations; 32import org.robolectric.RuntimeEnvironment; 33import org.robolectric.annotation.Config; 34 35import static com.google.common.truth.Truth.assertThat; 36import static org.mockito.Matchers.anyString; 37import static org.mockito.Mockito.when; 38 39@RunWith(SettingsRobolectricTestRunner.class) 40@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 41public class P2pThisDevicePreferenceControllerTest { 42 43 @Mock 44 private PreferenceScreen mPreferenceScreen; 45 private Preference mPreference; 46 private P2pThisDevicePreferenceController mController; 47 48 @Before 49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 mPreference = new Preference(RuntimeEnvironment.application); 52 when(mPreferenceScreen.findPreference(anyString())).thenReturn(mPreference); 53 54 mController = new P2pThisDevicePreferenceController(RuntimeEnvironment.application); 55 } 56 57 @Test 58 public void isAlwaysAvailable() { 59 assertThat(mController.isAvailable()).isTrue(); 60 } 61 62 @Test 63 public void updateDeviceName_emptyName_shouldUseIpAddress() { 64 WifiP2pDevice device = new WifiP2pDevice(); 65 device.deviceAddress = "address"; 66 mController.displayPreference(mPreferenceScreen); 67 mController.updateDeviceName(device); 68 69 assertThat(mPreference.getTitle()).isEqualTo(device.deviceAddress); 70 } 71 72 @Test 73 public void updateDeviceName_hasName_shouldUseName() { 74 WifiP2pDevice device = new WifiP2pDevice(); 75 device.deviceAddress = "address"; 76 device.deviceName = "name"; 77 mController.displayPreference(mPreferenceScreen); 78 mController.updateDeviceName(device); 79 80 assertThat(mPreference.getTitle()).isEqualTo(device.deviceName); 81 } 82} 83