Skip to content

Commit

Permalink
Make getYear(), getMonthOfYear(), getDayOfMonth() and getOnDateSetLis…
Browse files Browse the repository at this point in the history
…tenerCallback() use the "real" values instead of saving everything in the shadow.

PiperOrigin-RevId: 408807209
  • Loading branch information
Googler authored and copybara-robolectric committed Nov 12, 2021
1 parent d0d1696 commit 9168d3c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
@@ -1,6 +1,9 @@
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.KITKAT_WATCH;
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 +12,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 @@ -24,6 +28,34 @@ public void returnsTheInitialYearMonthAndDayPassedIntoTheDatePickerDialog() {
}

@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
@Config(minSdk = KITKAT_WATCH)
public void savesTheCallback() {
DatePickerDialog.OnDateSetListener expectedDateSetListener =
(datePicker, i, i1, i2) -> {
Expand Down
@@ -1,26 +1,28 @@
package org.robolectric.shadows;

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.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import androidx.annotation.RequiresApi;
import java.util.Calendar;
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 +32,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 +52,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 +68,26 @@ 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;
return reflector(DatePickerDialogReflector.class, realDatePickerDialog).getDateSetListener();
}

@ForType(DatePickerDialog.class)
interface DatePickerDialogReflector {

@RequiresApi(KITKAT_WATCH)
@Accessor("mDateSetListener")
OnDateSetListener getDateSetListener();
}
}

0 comments on commit 9168d3c

Please sign in to comment.