|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +package com.example.executorchllamademo |
| 10 | + |
| 11 | +import android.content.Context |
| 12 | +import android.util.Log |
| 13 | +import androidx.compose.ui.test.junit4.createAndroidComposeRule |
| 14 | +import androidx.compose.ui.test.onAllNodesWithText |
| 15 | +import androidx.compose.ui.test.onNodeWithTag |
| 16 | +import androidx.compose.ui.test.onNodeWithText |
| 17 | +import androidx.compose.ui.test.performClick |
| 18 | +import androidx.compose.ui.test.performTextInput |
| 19 | +import androidx.test.core.app.ApplicationProvider |
| 20 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 21 | +import androidx.test.filters.LargeTest |
| 22 | +import org.junit.Before |
| 23 | +import org.junit.Rule |
| 24 | +import org.junit.Test |
| 25 | +import org.junit.runner.RunWith |
| 26 | + |
| 27 | +/** |
| 28 | + * Preset model sanity test that validates the preset model UI workflow. |
| 29 | + * |
| 30 | + * This test validates: |
| 31 | + * 1. Navigate from Welcome screen to Preset model screen |
| 32 | + * 2. Load preset config from URL |
| 33 | + * 3. Verify Stories 110M model is displayed |
| 34 | + * |
| 35 | + * Note: Download and chat steps are skipped in CI due to network constraints. |
| 36 | + */ |
| 37 | +@RunWith(AndroidJUnit4::class) |
| 38 | +@LargeTest |
| 39 | +class PresetSanityTest { |
| 40 | + |
| 41 | + companion object { |
| 42 | + private const val TAG = "PresetSanityTest" |
| 43 | + private const val DEFAULT_CONFIG_URL = "https://raw.githubusercontent.com/meta-pytorch/executorch-examples/889ccc6e88813cbf03775889beed29b793d0c8db/llm/android/LlamaDemo/app/src/main/assets/preset_models.json" |
| 44 | + } |
| 45 | + |
| 46 | + @get:Rule |
| 47 | + val composeTestRule = createAndroidComposeRule<WelcomeActivity>() |
| 48 | + |
| 49 | + @Before |
| 50 | + fun setUp() { |
| 51 | + // Clear SharedPreferences before test to ensure a clean state |
| 52 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 53 | + val prefs = context.getSharedPreferences( |
| 54 | + context.getString(R.string.demo_pref_file_key), |
| 55 | + Context.MODE_PRIVATE |
| 56 | + ) |
| 57 | + prefs.edit().clear().commit() |
| 58 | + |
| 59 | + // Also clear the preset config preferences |
| 60 | + val configPrefs = context.getSharedPreferences("preset_config_prefs", Context.MODE_PRIVATE) |
| 61 | + configPrefs.edit().clear().commit() |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Loads the preset config from URL. |
| 66 | + * This is needed because the bundled preset_models.json is empty by default. |
| 67 | + */ |
| 68 | + private fun loadPresetConfigFromUrl() { |
| 69 | + Log.i(TAG, "Loading preset config from URL") |
| 70 | + |
| 71 | + // Type the URL into the config URL field (it's empty by default) |
| 72 | + composeTestRule.onNodeWithTag("config_url_field").performClick() |
| 73 | + composeTestRule.waitForIdle() |
| 74 | + composeTestRule.onNodeWithTag("config_url_field").performTextInput(DEFAULT_CONFIG_URL) |
| 75 | + |
| 76 | + // Small delay to ensure text is entered |
| 77 | + Thread.sleep(500) |
| 78 | + |
| 79 | + // Click the Load button |
| 80 | + composeTestRule.onNodeWithText("Load").performClick() |
| 81 | + |
| 82 | + // Wait for config to load (models should appear) |
| 83 | + // Don't use waitForIdle here as the loading spinner animation keeps Compose busy |
| 84 | + composeTestRule.waitUntil(timeoutMillis = 60000) { |
| 85 | + composeTestRule.onAllNodesWithText("Stories 110M").fetchSemanticsNodes().isNotEmpty() |
| 86 | + } |
| 87 | + Log.i(TAG, "Preset config loaded successfully") |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Tests the preset model UI workflow: |
| 92 | + * 1. From Welcome screen, tap "Preset model" card |
| 93 | + * 2. Load preset config from URL (since bundled JSON is empty) |
| 94 | + * 3. Verify Stories 110M model is displayed |
| 95 | + * |
| 96 | + * Note: Download and chat steps are skipped in CI due to network constraints. |
| 97 | + */ |
| 98 | + @Test |
| 99 | + fun testPresetModelDownloadAndChat() { |
| 100 | + composeTestRule.waitForIdle() |
| 101 | + |
| 102 | + // Step 1: From Welcome screen, tap "Preset model" card |
| 103 | + Log.i(TAG, "Step 1: Navigating to Preset model screen") |
| 104 | + composeTestRule.onNodeWithText("Preset model").performClick() |
| 105 | + composeTestRule.waitUntil(timeoutMillis = 5000) { |
| 106 | + composeTestRule.onAllNodesWithText("Download Preset Model").fetchSemanticsNodes().isNotEmpty() |
| 107 | + } |
| 108 | + |
| 109 | + // Step 2: Load preset config from URL |
| 110 | + Log.i(TAG, "Step 2: Loading preset config from URL") |
| 111 | + loadPresetConfigFromUrl() |
| 112 | + |
| 113 | + // Step 3: Find Stories 110M and verify it exists |
| 114 | + Log.i(TAG, "Step 3: Verifying Stories 110M is displayed") |
| 115 | + composeTestRule.onNodeWithText("Stories 110M").assertExists() |
| 116 | + Log.i(TAG, "Stories 110M found - preset model screen is working correctly") |
| 117 | + |
| 118 | + // Note: Download and chat steps are skipped in CI due to network constraints |
| 119 | + } |
| 120 | +} |
0 commit comments