|
| 1 | +import "package:cloud_firestore/cloud_firestore.dart"; |
| 2 | +import "package:fake_cloud_firestore/fake_cloud_firestore.dart"; |
| 3 | +import "package:firebase_core/firebase_core.dart"; |
| 4 | +import "package:flutter_test/flutter_test.dart"; |
| 5 | +import "package:hooks_riverpod/hooks_riverpod.dart"; |
| 6 | +import "package:integration_test/integration_test.dart"; |
| 7 | +import "package:mockito/mockito.dart"; |
| 8 | +import "package:proxima/main.dart"; |
| 9 | +import "package:proxima/services/database/post_repository_service.dart"; |
| 10 | +import "package:proxima/services/database/user_repository_service.dart"; |
| 11 | +import "package:proxima/services/geolocation_service.dart"; |
| 12 | +import "package:proxima/views/home_content/feed/post_feed.dart"; |
| 13 | +import "package:proxima/views/navigation/leading_back_button/leading_back_button.dart"; |
| 14 | +import "package:proxima/views/pages/create_account_page.dart"; |
| 15 | +import "package:proxima/views/pages/home/home_page.dart"; |
| 16 | +import "package:proxima/views/pages/home/top_bar/app_top_bar.dart"; |
| 17 | +import "package:proxima/views/pages/login/login_button.dart"; |
| 18 | +import "package:proxima/views/pages/login/login_page.dart"; |
| 19 | +import "package:proxima/views/pages/new_post/new_post_form.dart"; |
| 20 | +import "package:proxima/views/pages/profile/profile_page.dart"; |
| 21 | + |
| 22 | +import "../test/services/firebase/setup_firebase_mocks.dart"; |
| 23 | +import "../test/services/firebase/testing_auth_providers.dart"; |
| 24 | +import "../test/services/mock_geo_location_service.dart"; |
| 25 | + |
| 26 | +void main() { |
| 27 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 28 | + |
| 29 | + late FakeFirebaseFirestore fakeFireStore; |
| 30 | + late UserRepositoryService userRepo; |
| 31 | + late PostRepositoryService postRepo; |
| 32 | + |
| 33 | + MockGeoLocationService geoLocationService = MockGeoLocationService(); |
| 34 | + const GeoPoint testLocation = GeoPoint(0, 0); |
| 35 | + |
| 36 | + setUpAll(() async { |
| 37 | + setupFirebaseAuthMocks(); |
| 38 | + await Firebase.initializeApp(); |
| 39 | + fakeFireStore = FakeFirebaseFirestore(); |
| 40 | + userRepo = UserRepositoryService(firestore: fakeFireStore); |
| 41 | + postRepo = PostRepositoryService(firestore: fakeFireStore); |
| 42 | + |
| 43 | + when(geoLocationService.getCurrentPosition()).thenAnswer( |
| 44 | + (_) => Future.value(testLocation), |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + testWidgets("End-to-end test of the app navigation flow", |
| 49 | + (WidgetTester tester) async { |
| 50 | + // Set up the app with mocked providers |
| 51 | + await tester.pumpWidget( |
| 52 | + ProviderScope( |
| 53 | + overrides: [ |
| 54 | + ...firebaseAuthMocksOverrides, |
| 55 | + userRepositoryProvider.overrideWithValue(userRepo), |
| 56 | + geoLocationServiceProvider.overrideWithValue(geoLocationService), |
| 57 | + postRepositoryProvider.overrideWithValue(postRepo), |
| 58 | + ], |
| 59 | + child: const ProximaApp(), |
| 60 | + ), |
| 61 | + ); |
| 62 | + |
| 63 | + await tester.pumpAndSettle(); |
| 64 | + |
| 65 | + await loginToCreateAccount(tester); |
| 66 | + await createAccountToHome(tester); |
| 67 | + await homeToProfilePage(tester); |
| 68 | + await bottomNavigation(tester); |
| 69 | + await createPost(tester); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +/// Navigate to the login page and login |
| 74 | +Future<void> loginToCreateAccount(WidgetTester tester) async { |
| 75 | + expect(find.byType(LoginPage), findsOneWidget); |
| 76 | + |
| 77 | + final loginButton = find.byKey(LoginButton.loginButtonKey); |
| 78 | + await tester.tap(loginButton); |
| 79 | + await tester.pumpAndSettle(); |
| 80 | +} |
| 81 | + |
| 82 | +/// Create an account and navigate to the home page |
| 83 | +Future<void> createAccountToHome(WidgetTester tester) async { |
| 84 | + expect(find.byType(CreateAccountPage), findsOneWidget); |
| 85 | + |
| 86 | + // Enter details in the Create Account Page |
| 87 | + await tester.enterText( |
| 88 | + find.byKey(CreateAccountPage.uniqueUsernameFieldKey), |
| 89 | + "newUsername", |
| 90 | + ); |
| 91 | + await tester.enterText( |
| 92 | + find.byKey(CreateAccountPage.pseudoFieldKey), |
| 93 | + "newPseudo", |
| 94 | + ); |
| 95 | + await tester.pumpAndSettle(); |
| 96 | + |
| 97 | + // Submit the create account form |
| 98 | + await tester.tap(find.byKey(CreateAccountPage.confirmButtonKey)); |
| 99 | + await tester.pumpAndSettle(); // Wait for navigation |
| 100 | + |
| 101 | + expect(find.byType(HomePage), findsOneWidget); |
| 102 | +} |
| 103 | + |
| 104 | +/// Navigate to profile page from home page and go back |
| 105 | +Future<void> homeToProfilePage(WidgetTester tester) async { |
| 106 | + expect(find.byType(HomePage), findsOneWidget); |
| 107 | + |
| 108 | + final profilePicture = find.byKey(AppTopBar.profilePictureKey); |
| 109 | + expect(profilePicture, findsOneWidget); |
| 110 | + await tester.tap(profilePicture); |
| 111 | + await tester.pumpAndSettle(); |
| 112 | + |
| 113 | + // Check that the profile page is displayed |
| 114 | + final profilePage = find.byType(ProfilePage); |
| 115 | + expect(profilePage, findsOneWidget); |
| 116 | + |
| 117 | + // Check that the post tab is displayed |
| 118 | + final postTab = find.byKey(ProfilePage.postTabKey); |
| 119 | + expect(postTab, findsOneWidget); |
| 120 | + |
| 121 | + // Check that the comment tab is displayed |
| 122 | + final commentTab = find.byKey(ProfilePage.commentTabKey); |
| 123 | + expect(commentTab, findsOneWidget); |
| 124 | + |
| 125 | + //Check that post column is displayed |
| 126 | + final postColumn = find.byKey(ProfilePage.postColumnKey); |
| 127 | + expect(postColumn, findsOneWidget); |
| 128 | + |
| 129 | + // Tap on the comment tab |
| 130 | + await tester.tap(commentTab); |
| 131 | + await tester.pumpAndSettle(); |
| 132 | + |
| 133 | + // Check that the comment column is displayed |
| 134 | + final commentColumn = find.byKey(ProfilePage.commentColumnKey); |
| 135 | + expect(commentColumn, findsOneWidget); |
| 136 | + |
| 137 | + // Find arrow back button and go back to home page |
| 138 | + final backButton = find.byType(LeadingBackButton); |
| 139 | + expect(backButton, findsOneWidget); |
| 140 | + await tester.tap(backButton); |
| 141 | + await tester.pumpAndSettle(); |
| 142 | + expect(find.byType(HomePage), findsOneWidget); |
| 143 | +} |
| 144 | + |
| 145 | +/// Navigate to the other pages using bottom navigation bar |
| 146 | +Future<void> bottomNavigation(WidgetTester tester) async { |
| 147 | + expect(find.byType(HomePage), findsOneWidget); |
| 148 | + |
| 149 | + // Challenges |
| 150 | + await tester.tap(find.text("Challenge")); |
| 151 | + await tester.pumpAndSettle(); |
| 152 | + expect(find.text("Challenges"), findsOneWidget); |
| 153 | + |
| 154 | + // Group |
| 155 | + await tester.tap(find.text("Group")); |
| 156 | + await tester.pumpAndSettle(); |
| 157 | + expect(find.text("Proxima"), findsOneWidget); |
| 158 | + |
| 159 | + // Map |
| 160 | + await tester.tap(find.text("Map")); |
| 161 | + await tester.pumpAndSettle(); |
| 162 | + expect(find.text("Proxima"), findsOneWidget); |
| 163 | + |
| 164 | + // New Post |
| 165 | + await tester.tap(find.text("New post")); |
| 166 | + await tester.pumpAndSettle(); |
| 167 | + expect(find.text("Create a new post"), findsOneWidget); |
| 168 | + await tester.tap(find.byKey(LeadingBackButton.leadingBackButtonKey)); |
| 169 | + await tester.pumpAndSettle(); |
| 170 | + |
| 171 | + // Home (Feed) |
| 172 | + await tester.tap(find.text("Feed")); |
| 173 | + await tester.pumpAndSettle(); |
| 174 | + expect(find.byType(HomePage), findsOneWidget); |
| 175 | +} |
| 176 | + |
| 177 | +/// Create a post |
| 178 | +Future<void> createPost(WidgetTester tester) async { |
| 179 | + expect(find.byType(HomePage), findsOneWidget); |
| 180 | + |
| 181 | + // Tap on the new post button |
| 182 | + await tester.tap(find.byKey(PostFeed.newPostButtonTextKey)); |
| 183 | + await tester.pumpAndSettle(); |
| 184 | + |
| 185 | + // Check that the new post page is displayed |
| 186 | + expect(find.byType(NewPostForm), findsOneWidget); |
| 187 | + |
| 188 | + // Enter post details |
| 189 | + const postTitle = "I like turtles"; |
| 190 | + const postDescription = "Look at them go!"; |
| 191 | + |
| 192 | + await tester.enterText( |
| 193 | + find.byKey(NewPostForm.titleFieldKey), |
| 194 | + postTitle, |
| 195 | + ); |
| 196 | + |
| 197 | + await tester.enterText( |
| 198 | + find.byKey(NewPostForm.bodyFieldKey), |
| 199 | + postDescription, |
| 200 | + ); |
| 201 | + |
| 202 | + await tester.pumpAndSettle(); |
| 203 | + |
| 204 | + // Submit the post |
| 205 | + await tester.tap(find.byKey(NewPostForm.postButtonKey)); |
| 206 | + await tester.pumpAndSettle(); |
| 207 | + |
| 208 | + // refresh the page by pulling down |
| 209 | + await tester.drag(find.byType(PostFeed), const Offset(0, 500)); |
| 210 | + await tester.pumpAndSettle(); |
| 211 | + |
| 212 | + final refreshButton = find.byKey(PostFeed.refreshButtonKey); |
| 213 | + await tester.tap(refreshButton); |
| 214 | + await tester.pumpAndSettle(); |
| 215 | + |
| 216 | + // Check that the post is displayed |
| 217 | + expect(find.text(postTitle), findsOneWidget); |
| 218 | + expect(find.text(postDescription), findsOneWidget); |
| 219 | +} |
0 commit comments