Skip to content

Commit

Permalink
Update sample app with Today button and add test for month listener o…
Browse files Browse the repository at this point in the history
…n scrollToDate()
  • Loading branch information
Ben Loe authored and kizitonwose committed Aug 16, 2020
1 parent f45fc0f commit c9e18d3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CalenderViewTests {
val homeScreenRule = ActivityTestRule(HomeActivity::class.java, true, false)

private val currentMonth = YearMonth.now()
private val today = LocalDate.now()

@Before
fun setup() {
Expand Down Expand Up @@ -224,6 +225,28 @@ class CalenderViewTests {
assertEquals(targetCalMonth?.yearMonth, targetMonth)
}

@Test
fun monthScrollListenerIsCalledWhenScrolledToDate() {
onView(withId(R.id.examplesRv)).perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click()))

val calendarView = findFragment<Example1Fragment>().findViewById<CalendarView>(R.id.exOneCalendar)

val targetDay = today.plusDays(60)

var targetCalMonth: CalendarMonth? = null
calendarView.monthScrollListener = { month ->
targetCalMonth = month
}

homeScreenRule.runOnUiThread {
calendarView.scrollToDate(targetDay)
}

sleep(5000) // Enough time for smooth scrolling animation.

assertEquals(targetDay.yearMonth, targetCalMonth?.yearMonth)
}

@Test
fun findVisibleDaysAndMonthsWorksOnVerticalOrientation() {
onView(withId(R.id.examplesRv)).perform(actionOnItemAtPosition<RecyclerView.ViewHolder>(1, click()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ class Example1Fragment : BaseFragment(R.layout.example_1_fragment), HasToolbar {
animator.duration = 250
animator.start()
}

binding.todayButton.setOnClickListener {
binding.exOneCalendar.scrollToDate(LocalDate.now())
}
}

override fun onStart() {
Expand Down
10 changes: 10 additions & 0 deletions sample/src/main/res/layout/example_1_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@
android:textSize="18sp"
android:layout_gravity="bottom|center_horizontal" />

<Button
android:id="@+id/todayButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/today"
android:textSize="18sp"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="86dp"
style="@style/Widget.AppCompat.Button.Borderless"/>

</FrameLayout>


Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<string name="end_date">End date</string>
<string name="clear">Clear</string>
<string name="week_mode">Week mode</string>
<string name="today">Today</string>

</resources>

0 comments on commit c9e18d3

Please sign in to comment.