Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine methods: getYear(), getMonthOfYear(), getDayOfMonth() and getOnDateSetListenerCallback(). #6841

Merged
merged 1 commit into from Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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();
}
}