Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #156 from Vacxe/improvemet/time-n-date-pickers
Browse files Browse the repository at this point in the history
Add Time and Date pickers
  • Loading branch information
Vacxe committed Jul 10, 2019
2 parents ad62d20 + a27e322 commit bf40f77
Show file tree
Hide file tree
Showing 15 changed files with 465 additions and 0 deletions.
@@ -0,0 +1,19 @@
package com.agoda.kakao.picker.date

import androidx.test.espresso.contrib.PickerActions
import com.agoda.kakao.common.actions.BaseActions

/**
* Provides actions for date picker
*/
interface DatePickerAction : BaseActions {
/**
* Set date to picker dialog
* Month number will be normalized
*
* @param year year
* @param monthOfYear month
* @param day day
*/
fun setDate(year: Int, monthOfYear: Int, day: Int) = view.perform(PickerActions.setDate(year, monthOfYear, day))
}
@@ -0,0 +1,80 @@
package com.agoda.kakao.picker.date

import android.widget.DatePicker
import androidx.test.espresso.ViewAssertion
import com.agoda.kakao.common.actions.BaseActions

/**
* Provides assertions for date picker
*/
interface DatePickerAssertion : BaseActions {

/**
* Check if picker dialog contain selected date
* Month number will be normalized
*
* @param year year
* @param monthOfYear month
* @param day day
*/
fun hasDate(year: Int, monthOfYear: Int, day: Int) {
hasYear(year)
hasMonth(monthOfYear)
hasDay(day)
}

/**
* Check if picker dialog contain selected day
*
* @param day day
*/
fun hasDay(day: Int) {
view.check(ViewAssertion { view, notFoundException ->
if (view is DatePicker) {
if (day != view.dayOfMonth) {
throw AssertionError("Expected day is $day," +
" but actual is ${view.dayOfMonth}")
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
})
}

/**
* Check if picker dialog contain selected month
*
* @param monthOfYear month
*/
fun hasMonth(monthOfYear: Int) {
val normalizedMonthOfYear = monthOfYear - 1
view.check(ViewAssertion { view, notFoundException ->
if (view is DatePicker) {
if (normalizedMonthOfYear != view.month) {
throw AssertionError("Expected month is $normalizedMonthOfYear," +
" but actual is ${view.month}")
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
})
}

/**
* Check if picker dialog contain selected year
*
* @param year year
*/
fun hasYear(year: Int) {
view.check(ViewAssertion { view, notFoundException ->
if (view is DatePicker) {
if (year != view.year) {
throw AssertionError("Expected year is $year," +
" but actual is ${view.year}")
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
})
}
}
19 changes: 19 additions & 0 deletions kakao/src/main/kotlin/com/agoda/kakao/picker/date/KDatePicker.kt
@@ -0,0 +1,19 @@
package com.agoda.kakao.picker.date

import android.view.View
import android.widget.DatePicker
import androidx.test.espresso.DataInteraction
import com.agoda.kakao.common.builders.ViewBuilder
import com.agoda.kakao.common.views.KBaseView
import org.hamcrest.Matcher

/**
* View for interact with default date picker
*
* @see DatePicker
*/
class KDatePicker : KBaseView<KDatePicker>, DatePickerAction, DatePickerAssertion {
constructor(function: ViewBuilder.() -> Unit) : super(function)
constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function)
constructor(parent: DataInteraction, function: ViewBuilder.() -> Unit) : super(parent, function)
}
@@ -0,0 +1,27 @@
package com.agoda.kakao.picker.date

import android.app.DatePickerDialog
import android.widget.DatePicker
import com.agoda.kakao.common.views.KBaseView
import com.agoda.kakao.text.KButton

/**
* View for interact with default date picker dialog
*
* @see DatePickerDialog
*/
class KDatePickerDialog : KBaseView<KDatePickerDialog>({ isRoot() }) {

init {
inRoot { isDialog() }
}

val datePicker = KDatePicker { isInstanceOf(DatePicker::class.java) }
.also { it.inRoot { isDialog() } }

val okButton = KButton { withId(android.R.id.button1) }
.also { it.inRoot { isDialog() } }

val cancelButton = KButton { withId(android.R.id.button2) }
.also { it.inRoot { isDialog() } }
}
19 changes: 19 additions & 0 deletions kakao/src/main/kotlin/com/agoda/kakao/picker/time/KTimePicker.kt
@@ -0,0 +1,19 @@
package com.agoda.kakao.picker.time

import android.view.View
import android.widget.TimePicker
import androidx.test.espresso.DataInteraction
import com.agoda.kakao.common.builders.ViewBuilder
import com.agoda.kakao.common.views.KBaseView
import org.hamcrest.Matcher

/**
* View for interact with default time picker
*
* @see TimePicker
*/
class KTimePicker : KBaseView<KTimePicker>, TimePickerAction, TimePickerAssertion {
constructor(function: ViewBuilder.() -> Unit) : super(function)
constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function)
constructor(parent: DataInteraction, function: ViewBuilder.() -> Unit) : super(parent, function)
}
@@ -0,0 +1,28 @@
package com.agoda.kakao.picker.time

import android.app.TimePickerDialog
import android.widget.TimePicker
import com.agoda.kakao.common.views.KBaseView
import com.agoda.kakao.text.KButton

/**
* View for interact with default date picker dialog
*
* @see TimePickerDialog
*/
class KTimePickerDialog : KBaseView<KTimePickerDialog>({ isRoot() }) {

init {
inRoot { isDialog() }
}

val timePicker = KTimePicker { isInstanceOf(TimePicker::class.java) }.also {
it.inRoot { isDialog() }
}

val okButton = KButton { withId(android.R.id.button1) }
.also { it.inRoot { isDialog() } }

val cancelButton = KButton { withId(android.R.id.button2) }
.also { it.inRoot { isDialog() } }
}
@@ -0,0 +1,16 @@
package com.agoda.kakao.picker.time

import androidx.test.espresso.contrib.PickerActions
import com.agoda.kakao.common.actions.BaseActions

/**
* Provides actions for time picker
*/
interface TimePickerAction : BaseActions {
/**
* Set time to picker dialog
*
* @param
*/
fun setTime(hour: Int, minutes: Int) = view.perform(PickerActions.setTime(hour, minutes))
}
@@ -0,0 +1,58 @@
package com.agoda.kakao.picker.time

import android.widget.TimePicker
import androidx.test.espresso.ViewAssertion
import com.agoda.kakao.common.actions.BaseActions

/**
* Provides assertions for time picker
*/
interface TimePickerAssertion : BaseActions {

/**
* Check if picker dialog contain selected time
*
* @param hour hour
* @param minute monute
*/
fun hasTime(hour: Int, minute: Int) {
hasHour(hour)
hasMinute(minute)
}

/**
* Check if picker dialog contain selected hour
*
* @param hour hour
*/
fun hasHour(hour: Int) {
view.check(ViewAssertion { view, notFoundException ->
if (view is TimePicker) {
if (hour != view.currentHour) {
throw AssertionError("Expected hour is $hour," +
" but actual is ${view.currentHour}")
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
})
}

/**
* Check if picker dialog contain selected minute
*
* @param minute month
*/
fun hasMinute(minute: Int) {
view.check(ViewAssertion { view, notFoundException ->
if (view is TimePicker) {
if (minute != view.currentMinute) {
throw AssertionError("Expected minutes is $minute," +
" but actual is ${view.currentMinute}")
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
})
}
}
93 changes: 93 additions & 0 deletions sample/src/androidTest/kotlin/com/agoda/sample/PickersTest.kt
@@ -0,0 +1,93 @@
package com.agoda.sample

import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import androidx.test.rule.ActivityTestRule
import com.agoda.kakao.screen.Screen
import com.agoda.sample.screen.PickersActivityScreen
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4ClassRunner::class)
class PickersTest {
@Rule
@JvmField
val rule = ActivityTestRule(PickersActivity::class.java)

@Test
fun testDatePickerDialog() {
Screen.onScreen<PickersActivityScreen> {
selectDateButton {
click()
}

datePickerDialog {
datePicker {
setDate(1955, 11, 12)
hasDate(1955, 11, 12)
}
cancelButton {
click()
}
}

selectDateButton {
click()
}

datePickerDialog {
datePicker {
setDate(1955, 11, 12)
}
okButton {
click()
}
}

dateText {
hasText("12 11 1955")
}
}
}

@Test
fun testTimePickerDialog() {
Screen.onScreen<PickersActivityScreen> {
selectTimeButton {
click()
}

timePickerDialog {
timePicker {
setTime(22, 4)
hasTime(22, 4)
}
cancelButton {
click()
}
}

selectTimeButton {
click()
}

timePickerDialog {
timePicker {
setTime(22, 4)
}
okButton {
click()
}
}

timeText {
hasText("22:4")
}

selectDateButton {
click()
}
}
}

}
@@ -0,0 +1,25 @@
package com.agoda.sample.screen

import com.agoda.kakao.common.views.KView
import com.agoda.kakao.picker.date.KDatePickerDialog
import com.agoda.kakao.picker.time.KTimePickerDialog

import com.agoda.kakao.screen.Screen
import com.agoda.kakao.text.KTextView
import com.agoda.sample.R

open class PickersActivityScreen : Screen<PickersActivityScreen>() {
val selectDateButton: KView = KView { withId(R.id.select_date) }
val selectTimeButton: KView = KView { withId(R.id.select_time) }

val dateText: KTextView = KTextView {
withId(R.id.date_field)
}

val timeText: KTextView = KTextView {
withId(R.id.time_field)
}

val datePickerDialog: KDatePickerDialog = KDatePickerDialog()
val timePickerDialog: KTimePickerDialog = KTimePickerDialog()
}

0 comments on commit bf40f77

Please sign in to comment.