Skip to content

Commit

Permalink
Merge branch 'master' into feature/connect
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed Sep 24, 2023
2 parents 2ee6986 + 8a6037f commit febffc3
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 81 deletions.
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: 2 additions & 0 deletions app/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,6 @@
<string name="login_menu_connect_sign_in" cc:translatable="true">Sign up for ConnectID</string>
<string name="login_menu_connect_sign_out" cc:translatable="true">Sign out of ConnectID</string>
<string name="login_menu_connect_forget" cc:translatable="true">Forget ConnectID user</string>

<string name="app_with_id_not_found">Required CommCare App is not installed on device</string>
</resources>
27 changes: 27 additions & 0 deletions app/src/org/commcare/activities/DispatchActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.commcare.activities;

import static org.commcare.commcaresupportlibrary.CommCareLauncher.SESSION_ENDPOINT_APP_ID;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -24,6 +26,8 @@

import androidx.appcompat.app.AppCompatActivity;

import javax.annotation.Nullable;

/**
* Dispatches install, login, and home screen activities.
*
Expand Down Expand Up @@ -179,6 +183,9 @@ private void dispatch() {
}
} else if (!CommCareApplication.instance().getSession().isActive()) {
launchLoginScreen();
} else if (needAnotherAppLogin()){
CommCareApplication.instance().closeUserSession();
launchLoginScreen();
} else if (isExternalLaunch()) {
// CommCare was launched from an external app, with a session descriptor
handleExternalLaunch();
Expand All @@ -195,6 +202,17 @@ private void dispatch() {
}
}

private boolean needAnotherAppLogin() {
String sesssionEndpointAppID = getSessionEndpointAppId();
if (sesssionEndpointAppID != null) {
CommCareApp currentApp = CommCareApplication.instance().getCurrentApp();
if (currentApp != null) {
return !currentApp.getUniqueId().equals(sesssionEndpointAppID);
}
}
return false;
}

private boolean isExternalLaunch() {
return this.getIntent().hasExtra(SESSION_REQUEST) ||
this.getIntent().hasExtra(SESSION_ENDPOINT_ID);
Expand Down Expand Up @@ -259,6 +277,10 @@ private void launchLoginScreen() {
// AMS 06/09/16: This check is needed due to what we believe is a bug in the Android platform
Intent i = new Intent(this, LoginActivity.class);
i.putExtra(LoginActivity.USER_TRIGGERED_LOGOUT, userTriggeredLogout);
String sesssionEndpointAppID = getSessionEndpointAppId();
if (sesssionEndpointAppID != null) {
i.putExtra(LoginActivity.EXTRA_APP_ID, sesssionEndpointAppID);
}
startActivityForResult(i, LOGIN_USER);
waitingForActivityResultFromLogin = true;
} else {
Expand All @@ -269,6 +291,11 @@ private void launchLoginScreen() {
}
}

@Nullable
private String getSessionEndpointAppId() {
return getIntent().getStringExtra(SESSION_ENDPOINT_APP_ID);
}

private void launchHomeScreen() {
Intent i;
if (useRootMenuHomeActivity()) {
Expand Down
45 changes: 30 additions & 15 deletions app/src/org/commcare/activities/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@
public class LoginActivity extends CommCareActivity<LoginActivity>
implements OnItemSelectedListener, DataPullController,
RuntimePermissionRequester, WithUIController, PullTaskResultReceiver {
<<<<<<< HEAD
=======

public static final String EXTRA_APP_ID = "extra_app_id";
>>>>>>> master
private static final String TAG = LoginActivity.class.getSimpleName();

public static final int MENU_DEMO = Menu.FIRST;
Expand Down Expand Up @@ -106,6 +111,7 @@ public class LoginActivity extends CommCareActivity<LoginActivity>

private LoginActivityUiController uiController;
private FormAndDataSyncer formAndDataSyncer;
private String presetAppID;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -122,8 +128,12 @@ protected void onCreate(Bundle savedInstanceState) {
uiController.setupUI();
formAndDataSyncer = new FormAndDataSyncer();

<<<<<<< HEAD
ConnectIdManager.init(this);
updateConnectButton();
=======
presetAppID = getIntent().getStringExtra(EXTRA_APP_ID);
>>>>>>> master

if (savedInstanceState == null) {
// Only restore last user on the initial creation
Expand Down Expand Up @@ -648,23 +658,9 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
// Retrieve the app record corresponding to the app selected
selectedAppIndex = position;
String appId = appIdDropdownList.get(selectedAppIndex);

if (appId.length() > 0) {
uiController.setLoginInputsVisibility(true);

//TODO: Show user/pass fields
boolean selectedNewApp = !appId.equals(
CommCareApplication.instance().getCurrentApp().getUniqueId());
if (selectedNewApp) {
// Set the id of the last selected app
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString(KEY_LAST_APP, appId).commit();

// Launch the activity to seat the new app
Intent i = new Intent(this, SeatAppActivity.class);
i.putExtra(SeatAppActivity.KEY_APP_TO_SEAT, appId);
this.startActivityForResult(i, SEAT_APP_ACTIVITY);
} else {
if(!seatAppIfNeeded(appId)){
checkForSavedCredentials();
}
}
Expand All @@ -673,6 +669,21 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
}
}

protected boolean seatAppIfNeeded(String appId) {
boolean selectedNewApp = !appId.equals(CommCareApplication.instance().getCurrentApp().getUniqueId());
if (selectedNewApp) {
// Set the id of the last selected app
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putString(KEY_LAST_APP, appId).commit();

// Launch the activity to seat the new app
Intent i = new Intent(this, SeatAppActivity.class);
i.putExtra(SeatAppActivity.KEY_APP_TO_SEAT, appId);
this.startActivityForResult(i, SEAT_APP_ACTIVITY);
}
return selectedNewApp;
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
Expand Down Expand Up @@ -858,4 +869,8 @@ private void checkManagedConfiguration() {
initiateLoginAttempt(false);
}
}

protected String getPresetAppID() {
return presetAppID;
}
}
Loading

0 comments on commit febffc3

Please sign in to comment.