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

Android 13 Tests Updates #2709

Merged
merged 4 commits into from
Sep 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions app/instrumentation-tests/src/org/commcare/androidTests/BaseTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.commcare.androidTests

import android.Manifest
import android.os.Build
import androidx.test.espresso.Espresso
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand All @@ -22,16 +23,7 @@ abstract class BaseTest {

@Rule
@JvmField
var permissionRule: GrantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA
)
var permissionRule: GrantPermissionRule = GrantPermissionRule.grant(*getAppPermissions())

protected open fun installApp(appName: String, ccz: String, force: Boolean = false) {
if (CommCareApplication.instance().currentApp == null) {
Expand All @@ -44,4 +36,27 @@ abstract class BaseTest {
Espresso.pressBack()
}
}

private fun getAppPermissions(): Array<String> {
var appPermissions = mutableListOf(
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.CAMERA)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
appPermissions.addAll(arrayOf(
Manifest.permission.READ_MEDIA_IMAGES,
Manifest.permission.READ_MEDIA_AUDIO,
Manifest.permission.READ_MEDIA_VIDEO,
Manifest.permission.POST_NOTIFICATIONS))
} else {
appPermissions.addAll(arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE))
}
return appPermissions.toTypedArray()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ import java.util.*
import kotlin.math.absoluteValue



@RunWith(AndroidJUnit4::class)
@LargeTest
@BrowserstackTests
class DateWidgetsTests: BaseTest() {
class DateWidgetsTests : BaseTest() {
companion object {
const val CCZ_NAME = "date_widgets_tests.ccz"
const val APP_NAME = "Date Widgets"
val listOfNepaliMonths = listOf("Baishakh","Jestha","Ashadh","Shrawan",
"Bhadra","Ashwin","Kartik","Mangsir","Poush","Magh",
"Falgun","Chaitra")
val listOfEthiopianMonths = listOf("Säne","Hämle","Nähäse","P’agume",
"Mäskäräm","T’ïk’ïmt","Hïdar","Tahsas","T’ïr","Yäkatit",
"Mägabit","Miyaziya","Gïnbot")

val listOfNepaliMonths = listOf("Baishakh", "Jestha", "Ashadh", "Shrawan",
"Bhadra", "Ashwin", "Kartik", "Mangsir", "Poush", "Magh",
"Falgun", "Chaitra")
val listOfEthiopianMonths = listOf("Säne", "Hämle", "Nähäse", "P’agume",
"Mäskäräm", "T’ïk’ïmt", "Hïdar", "Tahsas", "T’ïr", "Yäkatit",
"Mägabit", "Miyaziya", "Gïnbot")
}

@Before
Expand All @@ -53,34 +51,46 @@ class DateWidgetsTests: BaseTest() {
/**
* Adding the teardown method so the tests doesnot fail after only thefirst execution, when executed as a whole.
*/

@After
fun teardown(){
fun teardown() {
InstrumentationUtility.logout()
}

@Test
fun testDateWidgets(){
fun testDateWidgets() {
testDateWidgetMonths(listOfNepaliMonths, 0)
testDateWidgetMonths(listOfEthiopianMonths, 1)
}

@Test
fun testStandardWidget() {
InstrumentationUtility.openModule(4)
InstrumentationUtility.rotateLeft()
val date = setDateTo(-10)
val dateFormates = getDateInDifferentFormats(date)
InstrumentationUtility.rotatePortrait()
InstrumentationUtility.nextPage()
for (formats in dateFormates) {
assertTrue(onView(allOf(withSubstring(formats))).isPresent())
}
InstrumentationUtility.submitForm()
assertTrue(onView(ViewMatchers.withText("1 form sent to server!")).isPresent())
}

fun testDateWidgetMonths(listOfMonths: List<String>, module: Int){

private fun testDateWidgetMonths(listOfMonths: List<String>, module: Int) {
InstrumentationUtility.openModule(module)
InstrumentationUtility.nextPage()
InstrumentationUtility.nextPage()

// reads the current month, gets the current index in the list, rotates the list starting from current month
// reads the current month, gets the current index in the list, rotates the list starting from current month
val monthText = InstrumentationUtility.getText(onView(withId(R.id.monthtxt)))
listOfMonths.toMutableList()
val index = listOfMonths.indexOf(monthText)
assertTrue(index != -1)
Collections.rotate(listOfMonths, -index)

//asserts if months are present in order
for (listItem in listOfMonths){
//asserts if months are present in order
for (listItem in listOfMonths) {
assertTrue(onView(withText(listItem)).isPresent())
onView(withId(R.id.monthupbtn)).perform(ViewActions.click())
}
Expand All @@ -96,38 +106,22 @@ class DateWidgetsTests: BaseTest() {
assertTrue(onView(ViewMatchers.withText("1 form sent to server!")).isPresent())
}

@Test
fun testStandardWidget(){
InstrumentationUtility.openModule(4)
InstrumentationUtility.rotateLeft()
val date = setDateTo(-10)
val dateFormates = getDateInDifferentFormats(date)
InstrumentationUtility.rotatePortrait()
InstrumentationUtility.nextPage()
for (formats in dateFormates){
assertTrue(onView(allOf(withSubstring(formats))).isPresent())
}
InstrumentationUtility.submitForm()
assertTrue(onView(ViewMatchers.withText("1 form sent to server!")).isPresent())

}

/**
* function to set date in the date picker
*/
fun setDateTo(days: Int) : Date {
private fun setDateTo(days: Int): Date {
val calendar = Calendar.getInstance()
calendar.add(Calendar.DATE, +days)

val year: Int = calendar.get(Calendar.YEAR)
val month: Int = calendar.get(Calendar.MONTH) + 1
val day: Int = calendar.get(Calendar.DAY_OF_MONTH)
onView(
ViewMatchers.withClassName(
Matchers.equalTo(
DatePicker::class.java.name
ViewMatchers.withClassName(
Matchers.equalTo(
DatePicker::class.java.name
)
)
)
).perform(PickerActions.setDate(year, month, day))

return calendar.time
Expand All @@ -136,33 +130,33 @@ class DateWidgetsTests: BaseTest() {
/**
* function returns different format of the selected date
*/
fun getDateInDifferentFormats(date : Date) : Array<String>{
val longFormat = SimpleDateFormat("EE, MMM d, yyyy").format(date)
private fun getDateInDifferentFormats(date: Date): Array<String> {
val longFormat = SimpleDateFormat("EE, MMM d, yyyy", Locale.ENGLISH).format(date)
val shortFormat = SimpleDateFormat("d/M/yy").format(date)
val unformattedDate = SimpleDateFormat("yyyy-MM-dd").format(date)
return arrayOf(longFormat.toString(),shortFormat.toString(),unformattedDate.toString())
return arrayOf(longFormat.toString(), shortFormat.toString(), unformattedDate.toString())
}

/**
* function to set date in the date universal picker
*/
fun setDateToUniversalCalender(days: Int) : String{

var dayChangeViewInteraction = if(days > 0) onView(withId(R.id.dayupbtn)) else onView(withId(R.id. daydownbtn))
for (day in 1..days.absoluteValue){
private fun setDateToUniversalCalender(days: Int): String {
var dayChangeViewInteraction = if (days > 0) onView(withId(R.id.dayupbtn)) else onView(withId(R.id.daydownbtn))
for (day in 1..days.absoluteValue) {
dayChangeViewInteraction.perform(ViewActions.click())
}
var selectedDay = InstrumentationUtility.getText(onView(withId(R.id.daytxt)))
val selectedMonth = InstrumentationUtility.getText(onView(withId(R.id.monthtxt)))
val selectedYear = InstrumentationUtility.getText(onView(withId(R.id.yeartxt)))
selectedDay = selectedDay.replaceFirst("0","")
val selectedDate = selectedDay+" "+selectedMonth+" "+selectedYear
selectedDay = selectedDay.replaceFirst("0", "")
val selectedDate = selectedDay + " " + selectedMonth + " " + selectedYear
return selectedDate
}

/**
* format the gregorian date
*/
fun formatGregorianDate(date: String) : String{
private fun formatGregorianDate(date: String): String {
val formatter = DateTimeFormatter.ofPattern("d MMMM yyyy")
val date = LocalDate.parse(date, formatter)
val newDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(date)
Expand Down
2 changes: 1 addition & 1 deletion commcare-support-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}