Make Settings robotest work with tot Robolectric

Bug: 116278591
Test: make -j RunSettingsRoboTests
Change-Id: I32cb2fcf6094675f79d2fc24702ef6298a9691e4
This commit is contained in:
Fan Zhang
2018-10-11 15:37:36 -07:00
parent dd3feeb155
commit cdf284b732
140 changed files with 706 additions and 823 deletions

View File

@@ -26,18 +26,14 @@ import android.content.Context;
import android.content.Intent;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {ShadowBluetoothAdapter.class})
public class AlwaysDiscoverableTest {
@Mock
@@ -45,14 +41,12 @@ public class AlwaysDiscoverableTest {
private AlwaysDiscoverable mAlwaysDiscoverable;
private BluetoothAdapter mBluetoothAdapter;
private ShadowBluetoothAdapter mShadowBluetoothAdapter;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mAlwaysDiscoverable = new AlwaysDiscoverable(mContext);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
@Test
@@ -82,7 +76,7 @@ public class AlwaysDiscoverableTest {
@Test
public void startSetsModeAndRegistersReceiver() {
mShadowBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_NONE);
mBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_NONE);
mAlwaysDiscoverable.start();
assertThat(mBluetoothAdapter.getScanMode())
.isEqualTo(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
@@ -110,7 +104,7 @@ public class AlwaysDiscoverableTest {
}
private void sendScanModeChangedIntent(int newMode, int previousMode) {
mShadowBluetoothAdapter.setScanMode(newMode);
mBluetoothAdapter.setScanMode(newMode);
Intent intent = new Intent(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intent.putExtra(BluetoothAdapter.EXTRA_SCAN_MODE, newMode);
intent.putExtra(BluetoothAdapter.EXTRA_PREVIOUS_SCAN_MODE, previousMode);

View File

@@ -38,6 +38,7 @@ import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.testutils.DrawableTestHelper;
import org.junit.Before;
import org.junit.Test;
@@ -79,7 +80,7 @@ public class BluetoothDevicePreferenceTest {
mPreference.onClicked();
verify(mMetricsFeatureProvider)
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
}
@Test
@@ -90,7 +91,7 @@ public class BluetoothDevicePreferenceTest {
mPreference.onClicked();
verify(mMetricsFeatureProvider)
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
}
@Test
@@ -103,9 +104,10 @@ public class BluetoothDevicePreferenceTest {
mPreference.onClicked();
verify(mMetricsFeatureProvider)
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
verify(mMetricsFeatureProvider, never())
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
.action(mContext,
MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
}
@Test
@@ -118,9 +120,10 @@ public class BluetoothDevicePreferenceTest {
mPreference.onClicked();
verify(mMetricsFeatureProvider)
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
verify(mMetricsFeatureProvider)
.action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
.action(mContext,
MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
}
@Test
@@ -164,13 +167,12 @@ public class BluetoothDevicePreferenceTest {
@Test
public void imagingDeviceIcon_isICSettingsPrint() {
when(mCachedBluetoothDevice.getBatteryLevel())
.thenReturn(BluetoothDevice.BATTERY_LEVEL_UNKNOWN);
.thenReturn(BluetoothDevice.BATTERY_LEVEL_UNKNOWN);
when(mCachedBluetoothDevice.getBtClass())
.thenReturn(new BluetoothClass(BluetoothClass.Device.Major.IMAGING));
.thenReturn(new BluetoothClass(BluetoothClass.Device.Major.IMAGING));
mPreference.onDeviceAttributesChanged();
assertThat(mPreference.getIcon()).isEqualTo(
mContext.getDrawable(R.drawable.ic_settings_print));
DrawableTestHelper.assertDrawableResId(mPreference.getIcon(), R.drawable.ic_settings_print);
}
@Test

View File

@@ -32,6 +32,7 @@ import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.testutils.DrawableTestHelper;
import org.junit.Before;
import org.junit.Test;
@@ -72,10 +73,10 @@ public class BluetoothPairingPreferenceControllerTest {
Preference pref = mController.createBluetoothPairingPreference(ORDER);
assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING);
assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_menu_add));
DrawableTestHelper.assertDrawableResId(pref.getIcon(), R.drawable.ic_menu_add);
assertThat(pref.getOrder()).isEqualTo(ORDER);
assertThat(pref.getTitle())
.isEqualTo(mContext.getString(R.string.bluetooth_pairing_pref_title));
.isEqualTo(mContext.getString(R.string.bluetooth_pairing_pref_title));
}
@Test