Skip to content

Commit

Permalink
Merge pull request #114 from ProximaEPFL/integration-test
Browse files Browse the repository at this point in the history
Integration test
  • Loading branch information
camillelnne authored Apr 12, 2024
2 parents aab0af9 + 85bbe26 commit 3d9ca2f
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 0 deletions.
219 changes: 219 additions & 0 deletions integration_test/app_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
import "package:cloud_firestore/cloud_firestore.dart";
import "package:fake_cloud_firestore/fake_cloud_firestore.dart";
import "package:firebase_core/firebase_core.dart";
import "package:flutter_test/flutter_test.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:integration_test/integration_test.dart";
import "package:mockito/mockito.dart";
import "package:proxima/main.dart";
import "package:proxima/services/database/post_repository_service.dart";
import "package:proxima/services/database/user_repository_service.dart";
import "package:proxima/services/geolocation_service.dart";
import "package:proxima/views/home_content/feed/post_feed.dart";
import "package:proxima/views/navigation/leading_back_button/leading_back_button.dart";
import "package:proxima/views/pages/create_account_page.dart";
import "package:proxima/views/pages/home/home_page.dart";
import "package:proxima/views/pages/home/top_bar/app_top_bar.dart";
import "package:proxima/views/pages/login/login_button.dart";
import "package:proxima/views/pages/login/login_page.dart";
import "package:proxima/views/pages/new_post/new_post_form.dart";
import "package:proxima/views/pages/profile/profile_page.dart";

import "../test/services/firebase/setup_firebase_mocks.dart";
import "../test/services/firebase/testing_auth_providers.dart";
import "../test/services/mock_geo_location_service.dart";

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

late FakeFirebaseFirestore fakeFireStore;
late UserRepositoryService userRepo;
late PostRepositoryService postRepo;

MockGeoLocationService geoLocationService = MockGeoLocationService();
const GeoPoint testLocation = GeoPoint(0, 0);

setUpAll(() async {
setupFirebaseAuthMocks();
await Firebase.initializeApp();
fakeFireStore = FakeFirebaseFirestore();
userRepo = UserRepositoryService(firestore: fakeFireStore);
postRepo = PostRepositoryService(firestore: fakeFireStore);

when(geoLocationService.getCurrentPosition()).thenAnswer(
(_) => Future.value(testLocation),
);
});

testWidgets("End-to-end test of the app navigation flow",
(WidgetTester tester) async {
// Set up the app with mocked providers
await tester.pumpWidget(
ProviderScope(
overrides: [
...firebaseAuthMocksOverrides,
userRepositoryProvider.overrideWithValue(userRepo),
geoLocationServiceProvider.overrideWithValue(geoLocationService),
postRepositoryProvider.overrideWithValue(postRepo),
],
child: const ProximaApp(),
),
);

await tester.pumpAndSettle();

await loginToCreateAccount(tester);
await createAccountToHome(tester);
await homeToProfilePage(tester);
await bottomNavigation(tester);
await createPost(tester);
});
}

/// Navigate to the login page and login
Future<void> loginToCreateAccount(WidgetTester tester) async {
expect(find.byType(LoginPage), findsOneWidget);

final loginButton = find.byKey(LoginButton.loginButtonKey);
await tester.tap(loginButton);
await tester.pumpAndSettle();
}

/// Create an account and navigate to the home page
Future<void> createAccountToHome(WidgetTester tester) async {
expect(find.byType(CreateAccountPage), findsOneWidget);

// Enter details in the Create Account Page
await tester.enterText(
find.byKey(CreateAccountPage.uniqueUsernameFieldKey),
"newUsername",
);
await tester.enterText(
find.byKey(CreateAccountPage.pseudoFieldKey),
"newPseudo",
);
await tester.pumpAndSettle();

// Submit the create account form
await tester.tap(find.byKey(CreateAccountPage.confirmButtonKey));
await tester.pumpAndSettle(); // Wait for navigation

expect(find.byType(HomePage), findsOneWidget);
}

/// Navigate to profile page from home page and go back
Future<void> homeToProfilePage(WidgetTester tester) async {
expect(find.byType(HomePage), findsOneWidget);

final profilePicture = find.byKey(AppTopBar.profilePictureKey);
expect(profilePicture, findsOneWidget);
await tester.tap(profilePicture);
await tester.pumpAndSettle();

// Check that the profile page is displayed
final profilePage = find.byType(ProfilePage);
expect(profilePage, findsOneWidget);

// Check that the post tab is displayed
final postTab = find.byKey(ProfilePage.postTabKey);
expect(postTab, findsOneWidget);

// Check that the comment tab is displayed
final commentTab = find.byKey(ProfilePage.commentTabKey);
expect(commentTab, findsOneWidget);

//Check that post column is displayed
final postColumn = find.byKey(ProfilePage.postColumnKey);
expect(postColumn, findsOneWidget);

// Tap on the comment tab
await tester.tap(commentTab);
await tester.pumpAndSettle();

// Check that the comment column is displayed
final commentColumn = find.byKey(ProfilePage.commentColumnKey);
expect(commentColumn, findsOneWidget);

// Find arrow back button and go back to home page
final backButton = find.byType(LeadingBackButton);
expect(backButton, findsOneWidget);
await tester.tap(backButton);
await tester.pumpAndSettle();
expect(find.byType(HomePage), findsOneWidget);
}

/// Navigate to the other pages using bottom navigation bar
Future<void> bottomNavigation(WidgetTester tester) async {
expect(find.byType(HomePage), findsOneWidget);

// Challenges
await tester.tap(find.text("Challenge"));
await tester.pumpAndSettle();
expect(find.text("Challenges"), findsOneWidget);

// Group
await tester.tap(find.text("Group"));
await tester.pumpAndSettle();
expect(find.text("Proxima"), findsOneWidget);

// Map
await tester.tap(find.text("Map"));
await tester.pumpAndSettle();
expect(find.text("Proxima"), findsOneWidget);

// New Post
await tester.tap(find.text("New post"));
await tester.pumpAndSettle();
expect(find.text("Create a new post"), findsOneWidget);
await tester.tap(find.byKey(LeadingBackButton.leadingBackButtonKey));
await tester.pumpAndSettle();

// Home (Feed)
await tester.tap(find.text("Feed"));
await tester.pumpAndSettle();
expect(find.byType(HomePage), findsOneWidget);
}

/// Create a post
Future<void> createPost(WidgetTester tester) async {
expect(find.byType(HomePage), findsOneWidget);

// Tap on the new post button
await tester.tap(find.byKey(PostFeed.newPostButtonTextKey));
await tester.pumpAndSettle();

// Check that the new post page is displayed
expect(find.byType(NewPostForm), findsOneWidget);

// Enter post details
const postTitle = "I like turtles";
const postDescription = "Look at them go!";

await tester.enterText(
find.byKey(NewPostForm.titleFieldKey),
postTitle,
);

await tester.enterText(
find.byKey(NewPostForm.bodyFieldKey),
postDescription,
);

await tester.pumpAndSettle();

// Submit the post
await tester.tap(find.byKey(NewPostForm.postButtonKey));
await tester.pumpAndSettle();

// refresh the page by pulling down
await tester.drag(find.byType(PostFeed), const Offset(0, 500));
await tester.pumpAndSettle();

final refreshButton = find.byKey(PostFeed.refreshButtonKey);
await tester.tap(refreshButton);
await tester.pumpAndSettle();

// Check that the post is displayed
expect(find.text(postTitle), findsOneWidget);
expect(find.text(postDescription), findsOneWidget);
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter

flutter_lints: ^3.0.0
riverpod_lint: ^2.3.9
Expand Down

0 comments on commit 3d9ca2f

Please sign in to comment.