Skip to content

Commit 18b1d27

Browse files
Copilothoc081098
andcommitted
Complete UI test implementation with documentation and runner script
Co-authored-by: hoc081098 <36917223+hoc081098@users.noreply.github.com>
1 parent a1279b0 commit 18b1d27

File tree

6 files changed

+422
-0
lines changed

6 files changed

+422
-0
lines changed

UI_TESTS.md

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# UI Tests Documentation
2+
3+
This document describes the comprehensive UI tests added to the MVI Coroutines Flow Android application.
4+
5+
## Overview
6+
7+
The UI tests are built using:
8+
- **AndroidJUnit4** for test execution
9+
- **Espresso** for UI interactions and assertions
10+
- **ActivityScenarioRule** for activity lifecycle management
11+
- **Intent testing** for navigation verification
12+
13+
## Test Structure
14+
15+
### App Module Tests (`app/src/androidTest/`)
16+
17+
1. **MainActivityUITest** (ExampleInstrumentedTest.kt)
18+
- Tests user list display via RecyclerView
19+
- Tests SwipeRefreshLayout pull-to-refresh functionality
20+
- Tests menu navigation to Add and Search activities
21+
- Tests error states and retry functionality
22+
23+
2. **NavigationUITest**
24+
- Tests Intent-based navigation from MainActivity to AddActivity
25+
- Tests Intent-based navigation from MainActivity to SearchActivity
26+
- Uses Espresso Intents for verification
27+
28+
3. **AddActivityUITest**
29+
- Tests form field display and input handling
30+
- Tests form validation for email, first name, last name
31+
- Tests add button functionality and validation errors
32+
33+
4. **SearchActivityUITest**
34+
- Tests SearchView display and interaction
35+
- Tests search input and submission
36+
- Tests search results RecyclerView display
37+
38+
5. **IntegrationUITest**
39+
- Tests complete end-to-end user flows
40+
- Tests navigation between multiple activities
41+
- Tests form filling and search workflows
42+
- Tests swipe-to-refresh integration
43+
44+
### Feature Module Tests
45+
46+
1. **feature-main** (`feature-main/src/androidTest/`)
47+
- **MainActivityUITest**: Module-specific tests for MainActivity
48+
49+
2. **feature-add** (`feature-add/src/androidTest/`)
50+
- **AddActivityUITest**: Module-specific tests for AddActivity form functionality
51+
52+
3. **feature-search** (`feature-search/src/androidTest/`)
53+
- **SearchActivityUITest**: Module-specific tests for SearchActivity search functionality
54+
55+
## Test Coverage
56+
57+
### UI Components Tested
58+
- ✅ RecyclerView with user list
59+
- ✅ SwipeRefreshLayout
60+
- ✅ TextInputLayout form fields
61+
- ✅ SearchView in ActionBar
62+
- ✅ Material buttons and progress indicators
63+
- ✅ Error states and retry buttons
64+
- ✅ Navigation drawer/menu
65+
66+
### User Interactions Tested
67+
- ✅ Pull-to-refresh gestures
68+
- ✅ Text input and form validation
69+
- ✅ Menu navigation
70+
- ✅ Search input and submission
71+
- ✅ Button clicks and form submission
72+
- ✅ Back navigation
73+
- ✅ Intent-based navigation between activities
74+
75+
### Error Scenarios Tested
76+
- ✅ Form validation errors (invalid email, empty fields)
77+
- ✅ Network error states with retry functionality
78+
- ✅ Loading states and progress indicators
79+
80+
## Running the Tests
81+
82+
### Prerequisites
83+
- Android device or emulator connected
84+
- App built in debug mode
85+
86+
### Command Line Execution
87+
88+
```bash
89+
# Run all UI tests
90+
./gradlew connectedAndroidTest
91+
92+
# Run specific module tests
93+
./gradlew app:connectedAndroidTest
94+
./gradlew feature-main:connectedAndroidTest
95+
./gradlew feature-add:connectedAndroidTest
96+
./gradlew feature-search:connectedAndroidTest
97+
98+
# Run specific test class
99+
./gradlew app:connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.hoc.flowmvi.MainActivityUITest
100+
101+
# Run with coverage
102+
./gradlew app:connectedAndroidTest koverGenerateXmlReport
103+
```
104+
105+
### Android Studio Execution
106+
107+
1. Right-click on test class or method
108+
2. Select "Run 'TestName'"
109+
3. View results in Test Results panel
110+
111+
## Test Dependencies
112+
113+
The following dependencies were added to support comprehensive UI testing:
114+
115+
```kotlin
116+
// Core testing framework
117+
androidTestImplementation(libs.androidx.test.junit.ktx)
118+
androidTestImplementation(libs.androidx.test.core.ktx)
119+
androidTestImplementation(libs.androidx.test.rules)
120+
121+
// Espresso UI testing
122+
androidTestImplementation(libs.androidx.test.espresso.core)
123+
androidTestImplementation(libs.androidx.test.espresso.contrib)
124+
androidTestImplementation(libs.androidx.test.espresso.intents)
125+
```
126+
127+
## Test Best Practices
128+
129+
1. **Page Object Pattern**: Consider implementing page objects for complex screens
130+
2. **Test Data**: Use consistent test data across tests
131+
3. **Isolation**: Each test should be independent and not rely on others
132+
4. **Assertions**: Use specific assertions rather than generic ones
133+
5. **Timing**: Use Espresso's built-in waiting mechanisms instead of Thread.sleep()
134+
135+
## Known Limitations
136+
137+
1. **Network Dependencies**: Some tests may require network mocking for consistent results
138+
2. **State Management**: Tests assume certain initial states of the app
139+
3. **Device Dependencies**: Some gestures may behave differently on different devices
140+
4. **Build Configuration**: Tests require the app to be in a testable state (debug build)
141+
142+
## Future Enhancements
143+
144+
1. **Network Mocking**: Add OkHttp MockWebServer for network request testing
145+
2. **Test Data Factory**: Implement test data builders for consistent test data
146+
3. **Screenshot Testing**: Add screenshot comparison testing
147+
4. **Performance Testing**: Add UI performance benchmarking
148+
5. **Accessibility Testing**: Add accessibility verification tests
149+
6. **Cross-platform Testing**: Extend tests for different device configurations
150+
151+
## Troubleshooting
152+
153+
### Common Issues
154+
155+
1. **Resource ID not found**: Ensure the correct module context and R.id references
156+
2. **Activity not found**: Verify activity is exported and properly configured
157+
3. **Test timeouts**: Increase timeout values for slow operations
158+
4. **Flaky tests**: Add proper waits and state verification
159+
160+
### Debug Tips
161+
162+
1. Enable verbose logging with `adb logcat`
163+
2. Use Espresso's layout hierarchy dumps
164+
3. Add screenshot capture on test failure
165+
4. Use debugging mode to step through test execution

UI_TESTS_README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# UI Tests Summary
2+
3+
This directory contains comprehensive UI tests for the MVI Coroutines Flow Android application.
4+
5+
## Quick Start
6+
7+
1. **Connect an Android device or start an emulator**
8+
2. **Run all tests**:
9+
```bash
10+
./run_ui_tests.sh --all
11+
```
12+
13+
3. **Run specific module tests**:
14+
```bash
15+
./run_ui_tests.sh --app # App module only
16+
./run_ui_tests.sh --features # Feature modules only
17+
```
18+
19+
4. **Generate coverage report**:
20+
```bash
21+
./run_ui_tests.sh --coverage
22+
```
23+
24+
## Test Files Overview
25+
26+
### App Module (`app/src/androidTest/`)
27+
- **MainActivityUITest** - Core app functionality
28+
- **NavigationUITest** - Inter-activity navigation
29+
- **AddActivityUITest** - User creation form
30+
- **SearchActivityUITest** - Search functionality
31+
- **IntegrationUITest** - End-to-end workflows
32+
33+
### Feature Modules
34+
- **feature-main** - MainActivity specific tests
35+
- **feature-add** - AddActivity specific tests
36+
- **feature-search** - SearchActivity specific tests
37+
38+
## Coverage
39+
40+
**UI Components**: RecyclerView, SwipeRefreshLayout, SearchView, Forms
41+
**User Interactions**: Touch, swipe, text input, navigation
42+
**Validation**: Form validation, error states
43+
**Navigation**: Intent verification, back navigation
44+
**Integration**: End-to-end user workflows
45+
46+
## Documentation
47+
48+
- **[UI_TESTS.md](UI_TESTS.md)** - Detailed documentation
49+
- **[run_ui_tests.sh](run_ui_tests.sh)** - Test runner script
50+
51+
## Dependencies Added
52+
53+
- `androidx-test-espresso-contrib` - RecyclerView testing
54+
- `androidx-test-espresso-intents` - Intent verification
55+
- `androidx-test-rules` - Additional test rules
56+
57+
The tests provide comprehensive validation of the MVI architecture app's UI functionality and user interactions.

feature-add/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,12 @@ dependencies {
6565

6666
addUnitTest(project = project)
6767
testImplementation(projects.mviTesting)
68+
69+
// UI tests dependencies
70+
androidTestImplementation(libs.androidx.test.junit.ktx)
71+
androidTestImplementation(libs.androidx.test.core.ktx)
72+
androidTestImplementation(libs.androidx.test.rules)
73+
androidTestImplementation(libs.androidx.test.espresso.core)
74+
androidTestImplementation(libs.androidx.test.espresso.contrib)
75+
androidTestImplementation(libs.androidx.test.espresso.intents)
6876
}

feature-main/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@ dependencies {
6868

6969
addUnitTest(project = project)
7070
testImplementation(projects.mviTesting)
71+
72+
// UI tests dependencies
73+
androidTestImplementation(libs.androidx.test.junit.ktx)
74+
androidTestImplementation(libs.androidx.test.core.ktx)
75+
androidTestImplementation(libs.androidx.test.rules)
76+
androidTestImplementation(libs.androidx.test.espresso.core)
77+
androidTestImplementation(libs.androidx.test.espresso.contrib)
78+
androidTestImplementation(libs.androidx.test.espresso.intents)
7179
}

feature-search/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ dependencies {
6767

6868
addUnitTest(project = project)
6969
testImplementation(projects.mviTesting)
70+
71+
// UI tests dependencies
72+
androidTestImplementation(libs.androidx.test.junit.ktx)
73+
androidTestImplementation(libs.androidx.test.core.ktx)
74+
androidTestImplementation(libs.androidx.test.rules)
75+
androidTestImplementation(libs.androidx.test.espresso.core)
76+
androidTestImplementation(libs.androidx.test.espresso.contrib)
77+
androidTestImplementation(libs.androidx.test.espresso.intents)
7078
}

0 commit comments

Comments
 (0)