Merge "Fix test cases broken"

This commit is contained in:
TreeHugger Robot
2018-07-18 16:30:31 +00:00
committed by Android (Google) Code Review
4 changed files with 107 additions and 21 deletions

View File

@@ -1,5 +1,8 @@
<resources>
<style name="Theme.Settings" parent="@android:style/Theme.DeviceDefault.Settings" />
<style name="Theme.Settings" parent="@android:style/Theme.DeviceDefault.Settings">
<!-- For AndroidX AlertDialogs -->
<item name="alertDialogTheme">@style/Theme.AppCompat.DayNight.Dialog.Alert</item>
</style>
<!-- Override the main app's style for ActionPrimaryButton to get around lack of new style
support in robolectric -->

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Google Inc.
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,38 +16,33 @@
package com.android.settings.password;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.app.AlertDialog;
import android.content.Context;
import com.android.settings.R;
import com.android.settings.password.ChooseLockTypeDialogFragment.OnLockTypeSelectedListener;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.testutils.shadow.SettingsShadowResourcesImpl;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settingslib.testutils.FragmentTestUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;
import org.robolectric.shadows.ShadowDialog;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {ShadowUserManager.class, ShadowUtils.class})
@Config(shadows = {SettingsShadowResourcesImpl.class, ShadowAlertDialogCompat.class})
public class ChooseLockTypeDialogFragmentTest {
private Context mContext;
@@ -61,32 +56,36 @@ public class ChooseLockTypeDialogFragmentTest {
}
@Test
@Ignore("b/111247403")
public void testThatDialog_IsShown() {
AlertDialog latestDialog = startLockFragment();
assertNotNull(latestDialog);
ShadowDialog shadowDialog = Shadows.shadowOf(latestDialog);
ShadowAlertDialogCompat shadowAlertDialog = ShadowAlertDialogCompat.shadowOf(
latestDialog);
assertThat(latestDialog).isNotNull();
assertThat(latestDialog.isShowing()).isTrue();
// verify that we are looking at the expected dialog.
assertEquals(shadowDialog.getTitle(),
assertThat(shadowAlertDialog.getTitle()).isEqualTo(
mContext.getString(R.string.setup_lock_settings_options_dialog_title));
}
@Test
@Ignore("b/111247403")
public void testThat_OnClickListener_IsCalled() {
mFragment.mDelegate = mock(OnLockTypeSelectedListener.class);
AlertDialog lockDialog = startLockFragment();
ShadowAlertDialog shadowAlertDialog = Shadows.shadowOf(lockDialog);
ShadowAlertDialogCompat shadowAlertDialog = ShadowAlertDialogCompat.shadowOf(lockDialog);
shadowAlertDialog.clickOnItem(0);
verify(mFragment.mDelegate, times(1)).onLockTypeSelected(any(ScreenLockType.class));
}
@Test
@Ignore("b/111247403")
public void testThat_OnClickListener_IsNotCalledWhenCancelled() {
mFragment.mDelegate = mock(OnLockTypeSelectedListener.class);
AlertDialog lockDialog = startLockFragment();
lockDialog.dismiss();
verify(mFragment.mDelegate, never()).onLockTypeSelected(any(ScreenLockType.class));
}
@@ -94,7 +93,7 @@ public class ChooseLockTypeDialogFragmentTest {
ChooseLockTypeDialogFragment chooseLockTypeDialogFragment =
ChooseLockTypeDialogFragment.newInstance(1234);
chooseLockTypeDialogFragment.show(mFragment.getChildFragmentManager(), null);
return ShadowAlertDialog.getLatestAlertDialog();
return ShadowAlertDialogCompat.getLatestAlertDialog();
}
public static class TestFragment extends Fragment implements OnLockTypeSelectedListener {

View File

@@ -43,7 +43,8 @@ public class SettingsShadowResourcesImpl extends ShadowResourcesImpl {
// that Robolectric isn't yet aware of.
// TODO: Remove this once Robolectric is updated.
if (id == R.drawable.switchbar_background
|| id == R.color.ripple_material_light) {
|| id == R.color.ripple_material_light
|| id == R.color.ripple_material_dark) {
return new ColorDrawable();
} else if (id == R.drawable.ic_launcher_settings) {
// ic_launcher_settings uses adaptive-icon, which is not supported by robolectric,

View File

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.testutils.shadow;
import android.annotation.SuppressLint;
import android.view.View;
import org.robolectric.Shadows;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowDialog;
import org.robolectric.util.ReflectionHelpers;
import javax.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
/* Robolectric shadow for the androidx alert dialog. */
@Implements(AlertDialog.class)
public class ShadowAlertDialogCompat extends ShadowDialog {
@SuppressLint("StaticFieldLeak")
@Nullable
private static ShadowAlertDialogCompat latestSupportAlertDialog;
@RealObject
private AlertDialog realAlertDialog;
@Implementation
public void show() {
super.show();
latestSupportAlertDialog = this;
}
public CharSequence getMessage() {
final Object alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert");
return ReflectionHelpers.getField(alertController, "mMessage");
}
public CharSequence getTitle() {
final Object alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert");
return ReflectionHelpers.getField(alertController, "mTitle");
}
public View getView() {
final Object alertController = ReflectionHelpers.getField(realAlertDialog, "mAlert");
return ReflectionHelpers.getField(alertController, "mView");
}
@Nullable
public static AlertDialog getLatestAlertDialog() {
return latestSupportAlertDialog == null ? null : latestSupportAlertDialog.realAlertDialog;
}
@Resetter
public static void reset() {
latestSupportAlertDialog = null;
}
public static ShadowAlertDialogCompat shadowOf(AlertDialog alertDialog) {
return (ShadowAlertDialogCompat) Shadow.extract(alertDialog);
}
public void clickOnItem(int index) {
Shadows.shadowOf(realAlertDialog.getListView()).performItemClick(index);
}
}