Skip to content

Commit

Permalink
Update ShadowDatePickerDialog to use reflector
Browse files Browse the repository at this point in the history
Make getYear(), getMonthOfYear(), getDayOfMonth() and
getOnDateSetListenerCallback() use the "real" values instead of saving
everything in the shadow.

PiperOrigin-RevId: 408807209
  • Loading branch information
Googler authored and hoisie committed Nov 20, 2021
1 parent 0cb21aa commit ec38884
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
@@ -1,6 +1,8 @@
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.N;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.robolectric.Shadows.shadowOf;

import android.app.DatePickerDialog;
Expand All @@ -9,12 +11,13 @@
import java.util.Locale;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(AndroidJUnit4.class)
public class ShadowDatePickerDialogTest {

@Test
public void returnsTheInitialYearMonthAndDayPassedIntoTheDatePickerDialog() {
public void testGettersReturnInitialConstructorValues() {
Locale.setDefault(Locale.US);
DatePickerDialog datePickerDialog =
new DatePickerDialog(ApplicationProvider.getApplicationContext(), null, 2012, 6, 7);
Expand All @@ -23,6 +26,33 @@ public void returnsTheInitialYearMonthAndDayPassedIntoTheDatePickerDialog() {
assertThat(shadowOf(datePickerDialog).getDayOfMonth()).isEqualTo(7);
}

@Test
public void updateDate_shouldUpdateYearMonthAndDay() {
Locale.setDefault(Locale.US);
DatePickerDialog datePickerDialog =
new DatePickerDialog(ApplicationProvider.getApplicationContext(), null, 2012, 6, 7);
datePickerDialog.updateDate(2021, 11, 10);

assertThat(shadowOf(datePickerDialog).getYear()).isEqualTo(2021);
assertThat(shadowOf(datePickerDialog).getMonthOfYear()).isEqualTo(11);
assertThat(shadowOf(datePickerDialog).getDayOfMonth()).isEqualTo(10);
}

@Test
@Config(minSdk = N)
public void updateListener_shouldUpdateTheListenerPassedInto() {
DatePickerDialog.OnDateSetListener mockCallBack =
mock(DatePickerDialog.OnDateSetListener.class);
DatePickerDialog datePickerDialog =
new DatePickerDialog(ApplicationProvider.getApplicationContext(), null, 2012, 6, 7);
assertThat(shadowOf(datePickerDialog).getOnDateSetListenerCallback()).isNull();

// setOnDateSetListener added in Android Nougat
datePickerDialog.setOnDateSetListener(mockCallBack);

assertThat(shadowOf(datePickerDialog).getOnDateSetListenerCallback()).isEqualTo(mockCallBack);
}

@Test
public void savesTheCallback() {
DatePickerDialog.OnDateSetListener expectedDateSetListener =
Expand All @@ -37,4 +67,5 @@ public void savesTheCallback() {
ShadowDatePickerDialog shadowDatePickerDialog = shadowOf(datePickerDialog);
assertThat(shadowDatePickerDialog.getOnDateSetListenerCallback()).isEqualTo(expectedDateSetListener);
}

}
@@ -1,26 +1,31 @@
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.KITKAT;
import static android.os.Build.VERSION_CODES.KITKAT_WATCH;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.N;
import static org.robolectric.shadow.api.Shadow.invokeConstructor;
import static org.robolectric.util.ReflectionHelpers.ClassParameter;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import androidx.annotation.RequiresApi;
import java.util.Calendar;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.ForType;

@Implements(DatePickerDialog.class)
public class ShadowDatePickerDialog extends ShadowAlertDialog {

@RealObject protected DatePickerDialog realDatePickerDialog;
private Calendar calendar;
private int year;
private int monthOfYear;
private int dayOfMonth;
private DatePickerDialog.OnDateSetListener callBack;

@Implementation(maxSdk = M)
protected void __constructor__(
Expand All @@ -30,10 +35,6 @@ protected void __constructor__(
int year,
int monthOfYear,
int dayOfMonth) {
this.year = year;
this.monthOfYear = monthOfYear;
this.dayOfMonth = dayOfMonth;
this.callBack = callBack;

invokeConstructor(DatePickerDialog.class, realDatePickerDialog,
ClassParameter.from(Context.class, context),
Expand All @@ -54,10 +55,6 @@ protected void __constructor__(
int monthOfYear,
int dayOfMonth) {
this.calendar = calendar;
this.year = year;
this.monthOfYear = monthOfYear;
this.dayOfMonth = dayOfMonth;
this.callBack = callBack;

invokeConstructor(DatePickerDialog.class, realDatePickerDialog,
ClassParameter.from(Context.class, context),
Expand All @@ -74,18 +71,36 @@ public Calendar getCalendar() {
}

public int getYear() {
return year;
return realDatePickerDialog.getDatePicker().getYear();
}

public int getMonthOfYear() {
return monthOfYear;
return realDatePickerDialog.getDatePicker().getMonth();
}

public int getDayOfMonth() {
return dayOfMonth;
return realDatePickerDialog.getDatePicker().getDayOfMonth();
}

public DatePickerDialog.OnDateSetListener getOnDateSetListenerCallback() {
return this.callBack;
if (RuntimeEnvironment.getApiLevel() <= KITKAT) {
return reflector(DatePickerDialogReflector.class, realDatePickerDialog).getCallback();
} else {
return reflector(DatePickerDialogReflector.class, realDatePickerDialog).getDateSetListener();
}
}

@ForType(DatePickerDialog.class)
interface DatePickerDialogReflector {

/** For sdk version at least {@link KITKAT_WATCH} */
@RequiresApi(KITKAT_WATCH)
@Accessor("mDateSetListener")
OnDateSetListener getDateSetListener();

/** For sdk version is equals to {@link KITKAT} */
@TargetApi(KITKAT)
@Accessor("mCallBack")
OnDateSetListener getCallback();
}
}

0 comments on commit ec38884

Please sign in to comment.