我只想加上“Chantell Osejo”的评论。 某些日期的内容将有所不同:
"Mon, Jan 1, 2024"
"Fri, Dec 29"
"Today Thu, Dec 28"
如果您在选择起算日期时重新使用<条码>MaterialDatePicker.dateRangePicker(>,则选定起算日期的内容说明将如<条码>,Startdate Mon, Jan 1, 2024”。
这里是根据@Chantell Osejo的评论编写的“Matier I ve”。 还需要一些改进,感到可以自由发表意见。
fun withDate(date: Date): Matcher<View> {
fun areDatesEqual(calendar1: Calendar, calendar2: Calendar): Boolean {
return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) &&
calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH) &&
calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
}
fun areYearsEqual(calendar1: Calendar, calendar2: Calendar): Boolean {
return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)
}
val now = Calendar.getInstance()
val selectedDate = Calendar.getInstance().apply {
time = date
}
val isToday = areDatesEqual(now, selectedDate)
val areYearsEqual = areYearsEqual(now, selectedDate)
val dateFormat = if (areYearsEqual) {
SimpleDateFormat("EEE, MMM d")
} else {
SimpleDateFormat("EEE, MMM d, yyyy")
}
val finalDate = if (isToday) {
"Today ${dateFormat.format(date)}"
} else {
dateFormat.format(date)
}
return withContentDescription(finalDate)
}
使用:
val tomorrowCalendar = Calendar.getInstance().apply {
add(Calendar.DAY_OF_MONTH, 1)
}
onView(withDate(tomorrowCalendar.time))
.inRoot(RootMatchers.isDialog())
.perform(click())
onView(withId(com.google.android.material.R.id.confirm_button))
.perform(click())