@@ -356,6 +356,89 @@ public void testStopGeneration() throws Exception {
356356 }
357357 }
358358
359+ /**
360+ * Tests that trying to send a new message during generation is not possible:
361+ * 1. Load model
362+ * 2. Send a message to start generation
363+ * 3. Wait for generation to start
364+ * 4. Verify input field is cleared
365+ * 5. Type new text in input field
366+ * 6. Verify send button shows stop icon (not send) - button is enabled for stopping
367+ * 7. Stop generation and verify button returns to send mode
368+ */
369+ @ Test
370+ public void testSendDuringGeneration () throws Exception {
371+ try (ActivityScenario <MainActivity > scenario = ActivityScenario .launch (MainActivity .class )) {
372+ // Wait for activity to fully load
373+ Thread .sleep (1000 );
374+
375+ // Dismiss the "Please Select a Model" dialog
376+ onView (withText (android .R .string .ok )).inRoot (isDialog ()).perform (click ());
377+
378+ // --- Load model ---
379+ onView (withId (R .id .settings )).perform (click ());
380+ Thread .sleep (500 );
381+
382+ // Select model
383+ onView (withId (R .id .modelImageButton )).perform (click ());
384+ Thread .sleep (300 );
385+ onData (hasToString (endsWith (modelFile ))).inRoot (isDialog ()).perform (click ());
386+ Thread .sleep (300 );
387+
388+ // Select tokenizer
389+ onView (withId (R .id .tokenizerImageButton )).perform (click ());
390+ Thread .sleep (300 );
391+ onData (hasToString (endsWith (tokenizerFile ))).inRoot (isDialog ()).perform (click ());
392+ Thread .sleep (300 );
393+
394+ // Load model
395+ onView (withId (R .id .loadModelButton )).perform (click ());
396+ onView (withText (android .R .string .yes )).inRoot (isDialog ()).perform (click ());
397+
398+ // Wait for model to load
399+ boolean modelLoaded = waitForModelLoaded (scenario , 60000 );
400+ assertTrue ("Model should be loaded successfully" , modelLoaded );
401+
402+ // --- Send a message to start generation (use a longer prompt for slower generation) ---
403+ onView (withId (R .id .editTextMessage )).perform (typeText ("Write a very long detailed story about a brave knight who goes on an adventure" ), ViewActions .closeSoftKeyboard ());
404+ onView (withId (R .id .sendButton )).perform (click ());
405+
406+ // --- Wait for generation to start ---
407+ boolean generationStarted = waitForResponseStarted (scenario , 30000 );
408+ assertTrue ("Generation should start" , generationStarted );
409+
410+ // --- Verify input field is cleared after sending ---
411+ onView (withId (R .id .editTextMessage )).check (matches (withText ("" )));
412+
413+ // --- Check if still generating (button enabled means stop mode) ---
414+ AtomicBoolean isStillGenerating = new AtomicBoolean (false );
415+ scenario .onActivity (activity -> {
416+ ImageButton sendButton = activity .findViewById (R .id .sendButton );
417+ if (sendButton != null ) {
418+ isStillGenerating .set (sendButton .isEnabled ());
419+ }
420+ });
421+
422+ // If generation already completed, skip the during-generation checks
423+ if (!isStillGenerating .get ()) {
424+ android .util .Log .i ("UIWorkflowTest" , "Generation completed quickly, skipping during-generation checks" );
425+ return ;
426+ }
427+
428+ // --- Type new text during generation ---
429+ onView (withId (R .id .editTextMessage )).perform (typeText ("Another message" ), ViewActions .closeSoftKeyboard ());
430+
431+ // --- Stop generation ---
432+ onView (withId (R .id .sendButton )).perform (click ());
433+ Thread .sleep (1000 );
434+
435+ // --- Verify generation stopped and we can now send ---
436+ // After stopping, the input still has text, so send button should be enabled
437+ onView (withId (R .id .editTextMessage )).check (matches (withText ("Another message" )));
438+ onView (withId (R .id .sendButton )).check (matches (isEnabled ()));
439+ }
440+ }
441+
359442 /**
360443 * Waits for generation to start by checking for model response text.
361444 *
0 commit comments