@@ -61,12 +61,14 @@ public class MainActivityTests {
6161
6262 // Yes, this is ActivityTestRule instead of ActivityScenario, because ActivityScenario does not actually work.
6363 // Issue ID may or may not be added later.
64+ // Launch activity manually to ensure complete restart between tests
6465 @ Rule
65- public ActivityTestRule <MainActivity > mainActivityActivityTestRule = new ActivityTestRule <>(MainActivity .class );
66+ public ActivityTestRule <MainActivity > mainActivityActivityTestRule = new ActivityTestRule <>(MainActivity .class , false , false );
6667
6768 @ Before
6869 public void setUp () {
69- MainActivity mainActivity = mainActivityActivityTestRule .getActivity ();
70+ // Launch a fresh activity for each test
71+ MainActivity mainActivity = mainActivityActivityTestRule .launchActivity (null );
7072
7173 m_idlingResource = mainActivity .getOpenFileIdlingResource ();
7274 IdlingRegistry .getInstance ().register (m_idlingResource );
@@ -91,8 +93,14 @@ public void tearDown() {
9193 IdlingRegistry .getInstance ().unregister (m_idlingResource );
9294 }
9395
94- // Ensure activity is finished to reset state between tests
95- mainActivityActivityTestRule .getActivity ().finish ();
96+ // Finish and wait for activity to be destroyed
97+ MainActivity activity = mainActivityActivityTestRule .getActivity ();
98+ if (activity != null ) {
99+ mainActivityActivityTestRule .finishActivity ();
100+
101+ // Use Instrumentation to wait until activity is destroyed
102+ InstrumentationRegistry .getInstrumentation ().waitForIdleSync ();
103+ }
96104 }
97105
98106 private static void copy (InputStream src , File dst ) throws IOException {
@@ -135,7 +143,7 @@ public static void cleanupTestFiles() {
135143 }
136144
137145 @ Test
138- public void testODT () {
146+ public void testXODT () {
139147 File testFile = s_testFiles .get ("test.odt" );
140148 Assert .assertNotNull (testFile );
141149 Context appCtx = InstrumentationRegistry .getInstrumentation ().getTargetContext ();
@@ -149,21 +157,21 @@ public void testODT() {
149157 );
150158
151159 onView (allOf (withId (R .id .menu_open ), withContentDescription ("Open document" ), isDisplayed ()))
152- .perform (click ());
160+ .perform (click ());
153161
154162 // The menu item could be either Documents or Files.
155163 onView (allOf (withId (android .R .id .text1 ), anyOf (withText ("Documents" ), withText ("Files" )), isDisplayed ()))
156164 .perform (click ());
157165
158166 // next onView will be blocked until m_idlingResource is idle.
159167 onView (allOf (withId (R .id .menu_edit ), withContentDescription ("Edit document" ), isEnabled ()))
160- .withFailureHandler ((error , viewMatcher ) -> {
161- // fails on small screens, try again with overflow menu
162- onView (allOf (withContentDescription ("More options" ), isDisplayed ())).perform (click ());
168+ .withFailureHandler ((error , viewMatcher ) -> {
169+ // fails on small screens, try again with overflow menu
170+ onView (allOf (withContentDescription ("More options" ), isDisplayed ())).perform (click ());
163171
164- onView (allOf (withId (R .id .menu_edit ), withContentDescription ("Edit document" ), isDisplayed ()))
165- .perform (click ());
166- });
172+ onView (allOf (withId (R .id .menu_edit ), withContentDescription ("Edit document" ), isDisplayed ()))
173+ .perform (click ());
174+ });
167175 }
168176
169177 @ Test
@@ -203,19 +211,19 @@ public void testPDF() {
203211 public void testPasswordProtectedODT () {
204212 File testFile = s_testFiles .get ("password-test.odt" );
205213 Assert .assertNotNull (testFile );
206-
214+
207215 // Check if the file exists and is readable
208216 Assert .assertTrue ("Password test file does not exist: " + testFile .getAbsolutePath (), testFile .exists ());
209217 Assert .assertTrue ("Password test file is not readable: " + testFile .getAbsolutePath (), testFile .canRead ());
210-
218+
211219 // Log file info for debugging CI issues
212220 Log .d ("MainActivityTests" , "Password test file path: " + testFile .getAbsolutePath ());
213221 Log .d ("MainActivityTests" , "Password test file size: " + testFile .length ());
214222 Log .d ("MainActivityTests" , "All test files: " + s_testFiles .keySet ());
215-
223+
216224 // Double-check we're using the right file
217225 Assert .assertEquals ("password-test.odt file size mismatch" , 12671L , testFile .length ());
218-
226+
219227 Context appCtx = InstrumentationRegistry .getInstrumentation ().getTargetContext ();
220228 Uri testFileUri = FileProvider .getUriForFile (appCtx , appCtx .getPackageName () + ".provider" , testFile );
221229 Intents .intending (hasAction (Intent .ACTION_OPEN_DOCUMENT )).respondWith (
@@ -227,7 +235,7 @@ public void testPasswordProtectedODT() {
227235 );
228236
229237 onView (allOf (withId (R .id .menu_open ), withContentDescription ("Open document" ), isDisplayed ()))
230- .perform (click ());
238+ .perform (click ());
231239
232240 onView (allOf (withId (android .R .id .text1 ), anyOf (withText ("Documents" ), withText ("Files" )), isDisplayed ()))
233241 .perform (click ());
@@ -256,11 +264,11 @@ public void testPasswordProtectedODT() {
256264
257265 // Check if edit button becomes available (indicating successful load)
258266 onView (allOf (withId (R .id .menu_edit ), withContentDescription ("Edit document" ), isEnabled ()))
259- .withFailureHandler ((error , viewMatcher ) -> {
260- onView (allOf (withContentDescription ("More options" ), isDisplayed ())).perform (click ());
267+ .withFailureHandler ((error , viewMatcher ) -> {
268+ onView (allOf (withContentDescription ("More options" ), isDisplayed ())).perform (click ());
261269
262- onView (allOf (withId (R .id .menu_edit ), withContentDescription ("Edit document" ), isDisplayed ()))
263- .perform (click ());
264- });
270+ onView (allOf (withId (R .id .menu_edit ), withContentDescription ("Edit document" ), isDisplayed ()))
271+ .perform (click ());
272+ });
265273 }
266274}
0 commit comments