4848public class MainActivityTests {
4949 private IdlingResource m_idlingResource ;
5050 private static final Map <String , File > s_testFiles = new ArrayMap <>();
51+ private static final String EXPECTED_FIRST_WORD_ODT = "This" ;
52+ private static final String EXPECTED_FIRST_WORD_PDF = "Dummy" ;
53+ private static final String EXPECTED_FIRST_WORD_DOCX = "Table" ;
54+ private static final String EXPECTED_FIRST_WORD_PASSWORD_ODT = "Hallo" ;
5155
5256 // Yes, this is ActivityTestRule instead of ActivityScenario, because ActivityScenario does not actually work.
5357 // Issue ID may or may not be added later.
@@ -138,6 +142,7 @@ public void testODT() throws InterruptedException {
138142 PageView pageView = documentFragment .getPageView ();
139143 Assert .assertNotNull (pageView );
140144 Assert .assertTrue ("ODT should load" , waitForPageLoaded (pageView , 10000 ));
145+ assertFirstWord (pageView , EXPECTED_FIRST_WORD_ODT , "ODT" );
141146
142147 String fileType = documentFragment .getLastFileType ();
143148 Assert .assertNotNull (fileType );
@@ -154,6 +159,7 @@ public void testPDF() throws InterruptedException {
154159 PageView pageView = documentFragment .getPageView ();
155160 Assert .assertNotNull (pageView );
156161 Assert .assertTrue ("PDF should load" , waitForPageLoaded (pageView , 10000 ));
162+ assertFirstWord (pageView , EXPECTED_FIRST_WORD_PDF , "PDF" );
157163
158164 String fileType = documentFragment .getLastFileType ();
159165 Assert .assertNotNull (fileType );
@@ -186,6 +192,7 @@ public void testPasswordProtectedODT() throws InterruptedException {
186192 Assert .assertNotNull (pageView );
187193 Assert .assertTrue ("Password-protected ODT should load with correct password" ,
188194 waitForPageLoaded (pageView , 10000 ));
195+ assertFirstWord (pageView , EXPECTED_FIRST_WORD_PASSWORD_ODT , "Password-protected ODT" );
189196 }
190197
191198 @ Test
@@ -194,10 +201,12 @@ public void testODTEditMode() throws InterruptedException {
194201 Assert .assertNotNull (testFile );
195202 MainActivity activity = mainActivityActivityTestRule .getActivity ();
196203 DocumentFragment documentFragment = loadDocument (activity , testFile );
197- enterEditMode (activity , documentFragment );
198204
199205 PageView pageView = documentFragment .getPageView ();
200206 Assert .assertNotNull (pageView );
207+ assertFirstWord (pageView , EXPECTED_FIRST_WORD_ODT , "ODT" );
208+
209+ enterEditMode (activity , documentFragment );
201210 Assert .assertTrue (
202211 "ODT should become editable after entering edit mode" ,
203212 waitForEditableState (pageView , true , 10000 )
@@ -210,11 +219,12 @@ public void testDOCXEditMode() throws InterruptedException {
210219 Assert .assertNotNull (testFile );
211220 MainActivity activity = mainActivityActivityTestRule .getActivity ();
212221 DocumentFragment documentFragment = loadDocument (activity , testFile );
213- enterEditMode (activity , documentFragment );
214222
215223 PageView pageView = documentFragment .getPageView ();
216224 Assert .assertNotNull (pageView );
225+ assertFirstWord (pageView , EXPECTED_FIRST_WORD_DOCX , "DOCX" );
217226
227+ enterEditMode (activity , documentFragment );
218228 Assert .assertTrue (
219229 "DOCX should become editable after entering edit mode" ,
220230 waitForEditableState (pageView , true , 10000 )
@@ -298,6 +308,37 @@ private boolean waitForPageLoaded(PageView pageView, long timeoutMs) throws Inte
298308 return false ;
299309 }
300310
311+ private void assertFirstWord (PageView pageView , String expected , String label ) throws InterruptedException {
312+ String firstWord = waitForFirstWord (pageView , 10000 );
313+ Assert .assertEquals (label + " first word mismatch" , expected , firstWord );
314+ }
315+
316+ private String waitForFirstWord (PageView pageView , long timeoutMs ) throws InterruptedException {
317+ long startMs = SystemClock .elapsedRealtime ();
318+ while (SystemClock .elapsedRealtime () - startMs < timeoutMs ) {
319+ String firstWord = getFirstWord (pageView );
320+ if (!firstWord .isEmpty ()) {
321+ return firstWord ;
322+ }
323+ SystemClock .sleep (250 );
324+ }
325+ return "" ;
326+ }
327+
328+ private String getFirstWord (PageView pageView ) throws InterruptedException {
329+ String result = evaluateJavascript (pageView ,
330+ "(function(){"
331+ + "var text = document.body ? (document.body.innerText || '') : '';"
332+ + "text = text.replace(/\\ s+/g,' ').trim();"
333+ + "if (!text) return '';"
334+ + "return text.split(' ')[0];"
335+ + "})()" );
336+ if (result == null ) {
337+ return "" ;
338+ }
339+ return result .replace ("\" " , "" ).trim ();
340+ }
341+
301342 private String getPageViewUrl (PageView pageView ) throws InterruptedException {
302343 AtomicReference <String > url = new AtomicReference <>();
303344 CountDownLatch latch = new CountDownLatch (1 );
0 commit comments