|
| 1 | +package uk.org.openseizuredetector; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.Intent; |
| 5 | +import android.os.Build; |
| 6 | + |
| 7 | +import androidx.test.core.app.ApplicationProvider; |
| 8 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 9 | +import androidx.test.filters.LargeTest; |
| 10 | +import androidx.test.platform.app.InstrumentationRegistry; |
| 11 | +import androidx.test.uiautomator.By; |
| 12 | +import androidx.test.uiautomator.UiDevice; |
| 13 | +import androidx.test.uiautomator.UiObject2; |
| 14 | +import androidx.test.uiautomator.Until; |
| 15 | + |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.runner.RunWith; |
| 19 | + |
| 20 | +import uk.org.openseizuredetector.activity.onboarding.OnboardingActivity; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertNotNull; |
| 23 | + |
| 24 | +@RunWith(AndroidJUnit4.class) |
| 25 | +@LargeTest |
| 26 | +public class OnboardingTest { |
| 27 | + |
| 28 | + private static final int TIMEOUT_MS = 5000; |
| 29 | + private UiDevice mDevice; |
| 30 | + private Context mContext; |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() { |
| 34 | + // Initialize UiDevice instance |
| 35 | + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); |
| 36 | + mContext = ApplicationProvider.getApplicationContext(); |
| 37 | + |
| 38 | + // Pre-grant runtime permissions to avoid system dialogs |
| 39 | + grantRuntimePermissions(); |
| 40 | + |
| 41 | + // Launch the onboarding activity |
| 42 | + Intent intent = new Intent(mContext, OnboardingActivity.class); |
| 43 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 44 | + mContext.startActivity(intent); |
| 45 | + |
| 46 | + // Wait for the app to appear |
| 47 | + mDevice.wait(Until.hasObject(By.pkg(mContext.getPackageName())), TIMEOUT_MS); |
| 48 | + } |
| 49 | + |
| 50 | + private void grantRuntimePermissions() { |
| 51 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 52 | + String pkg = mContext.getPackageName(); |
| 53 | + InstrumentationRegistry.getInstrumentation().getUiAutomation() |
| 54 | + .executeShellCommand("pm grant " + pkg + " android.permission.BODY_SENSORS"); |
| 55 | + InstrumentationRegistry.getInstrumentation().getUiAutomation() |
| 56 | + .executeShellCommand("pm grant " + pkg + " android.permission.ACTIVITY_RECOGNITION"); |
| 57 | + if (Build.VERSION.SDK_INT >= 33) { |
| 58 | + InstrumentationRegistry.getInstrumentation().getUiAutomation() |
| 59 | + .executeShellCommand("pm grant " + pkg + " android.permission.POST_NOTIFICATIONS"); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testOnboardingWizard() { |
| 66 | + // Wait for and verify the welcome screen |
| 67 | + UiObject2 welcomeTitle = mDevice.wait( |
| 68 | + Until.findObject(By.text("Welcome to OpenSeizureDetector")), |
| 69 | + TIMEOUT_MS); |
| 70 | + assertNotNull("Welcome title should be displayed", welcomeTitle); |
| 71 | + |
| 72 | + // Click "Next" button (by resource ID) to go to DataSource selection |
| 73 | + UiObject2 nextButton = mDevice.wait( |
| 74 | + Until.findObject(By.res(mContext.getPackageName(), "btn_next")), |
| 75 | + TIMEOUT_MS); |
| 76 | + assertNotNull("Next button should be present on welcome screen", nextButton); |
| 77 | + nextButton.click(); |
| 78 | + |
| 79 | + // Wait for transition to complete |
| 80 | + mDevice.waitForIdle(2000); |
| 81 | + |
| 82 | + // Try to dismiss any dialog that might appear (ML configuration dialog) |
| 83 | + dismissDialogIfPresent(); |
| 84 | + |
| 85 | + // Wait for DataSource screen - look for the radio button options or screen title |
| 86 | + mDevice.wait(Until.hasObject(By.res(mContext.getPackageName(), "radio_phone")), TIMEOUT_MS); |
| 87 | + |
| 88 | + // Click "Next" button to go to DataSource Config screen (Phone/Demo Mode info page) |
| 89 | + nextButton = mDevice.wait( |
| 90 | + Until.findObject(By.res(mContext.getPackageName(), "btn_next")), |
| 91 | + TIMEOUT_MS); |
| 92 | + assertNotNull("Next button should be present on DataSource screen", nextButton); |
| 93 | + nextButton.click(); |
| 94 | + |
| 95 | + // Wait for transition to DataSource Config screen |
| 96 | + mDevice.waitForIdle(2000); |
| 97 | + dismissDialogIfPresent(); |
| 98 | + |
| 99 | + // Click "Next" again to proceed from DataSource Config to Algorithms screen |
| 100 | + nextButton = mDevice.wait( |
| 101 | + Until.findObject(By.res(mContext.getPackageName(), "btn_next")), |
| 102 | + TIMEOUT_MS); |
| 103 | + assertNotNull("Next button should be present on DataSource Config screen", nextButton); |
| 104 | + nextButton.click(); |
| 105 | + |
| 106 | + // Wait for transition to Algorithms screen |
| 107 | + mDevice.waitForIdle(2000); |
| 108 | + dismissDialogIfPresent(); |
| 109 | + |
| 110 | + // Wait explicitly for the Algorithms screen to load - look for any checkbox |
| 111 | + mDevice.wait(Until.hasObject(By.res(mContext.getPackageName(), "check_ml_alg")), TIMEOUT_MS); |
| 112 | + |
| 113 | + // Try to find and scroll to the OSD algorithm checkbox |
| 114 | + UiObject2 osdCheckbox = mDevice.findObject(By.res(mContext.getPackageName(), "check_osd_alg")); |
| 115 | + |
| 116 | + // If not found, try scrolling down in case it's off-screen |
| 117 | + if (osdCheckbox == null) { |
| 118 | + // Scroll down to find it |
| 119 | + mDevice.swipe(mDevice.getDisplayWidth() / 2, |
| 120 | + mDevice.getDisplayHeight() * 3 / 4, |
| 121 | + mDevice.getDisplayWidth() / 2, |
| 122 | + mDevice.getDisplayHeight() / 4, |
| 123 | + 20); |
| 124 | + mDevice.waitForIdle(500); |
| 125 | + osdCheckbox = mDevice.wait( |
| 126 | + Until.findObject(By.res(mContext.getPackageName(), "check_osd_alg")), |
| 127 | + TIMEOUT_MS); |
| 128 | + } |
| 129 | + |
| 130 | + assertNotNull("OSD algorithm checkbox should be present", osdCheckbox); |
| 131 | + |
| 132 | + // The OSD checkbox may be checked by default - ensure it's checked |
| 133 | + if (!osdCheckbox.isChecked()) { |
| 134 | + osdCheckbox.click(); |
| 135 | + mDevice.waitForIdle(500); |
| 136 | + } |
| 137 | + |
| 138 | + // Click "Next" to proceed (this triggers the OSD configuration dialog) |
| 139 | + nextButton = mDevice.wait( |
| 140 | + Until.findObject(By.res(mContext.getPackageName(), "btn_next")), |
| 141 | + TIMEOUT_MS); |
| 142 | + assertNotNull("Next button should be present on Algorithms screen", nextButton); |
| 143 | + nextButton.click(); |
| 144 | + |
| 145 | + // Wait for and dismiss the "OSD Algorithm" configuration dialog that appears |
| 146 | + mDevice.waitForIdle(1000); |
| 147 | + UiObject2 okButton = mDevice.wait(Until.findObject(By.text("OK")), TIMEOUT_MS); |
| 148 | + if (okButton != null) { |
| 149 | + System.out.println("Found and clicking OSD configuration dialog OK button"); |
| 150 | + okButton.click(); |
| 151 | + mDevice.waitForIdle(1500); |
| 152 | + } else { |
| 153 | + System.out.println("Warning: OSD configuration dialog OK button not found"); |
| 154 | + } |
| 155 | + |
| 156 | + // Wait for transition to final Complete screen |
| 157 | + mDevice.waitForIdle(1000); |
| 158 | + |
| 159 | + // Click "Get Started" to finish onboarding |
| 160 | + // The btn_next button's text changes to "Get Started" on the final page |
| 161 | + UiObject2 getStartedButton = mDevice.wait( |
| 162 | + Until.findObject(By.res(mContext.getPackageName(), "btn_next")), |
| 163 | + TIMEOUT_MS); |
| 164 | + assertNotNull("Get Started button (btn_next) should be present on Complete screen", getStartedButton); |
| 165 | + getStartedButton.click(); |
| 166 | + mDevice.waitForIdle(2000); |
| 167 | + } |
| 168 | + |
| 169 | + private void dismissDialogIfPresent() { |
| 170 | + // Try to find and dismiss any dialog (MaterialAlertDialog with various button texts) |
| 171 | + // Only search for specific positive button texts to avoid clicking Back/Skip/etc |
| 172 | + String[] buttonTexts = {"OK", "Ok", "ok", "OKAY", "Okay", "YES", "Yes", "CONFIRM", "Confirm"}; |
| 173 | + |
| 174 | + for (String buttonText : buttonTexts) { |
| 175 | + UiObject2 button = mDevice.findObject(By.text(buttonText)); |
| 176 | + if (button != null) { |
| 177 | + System.out.println("Found and clicking dialog button: " + buttonText); |
| 178 | + button.click(); |
| 179 | + mDevice.waitForIdle(500); |
| 180 | + return; // Found and clicked, exit |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments