Flutter integration test, how to ensure user is signed out? (flushed state) - flutter

I'm writing my first Flutter integrations tests, and noticed that when the app starts up, the user is already signed in and lands on the page after sign in. I don't know why this happens, maybe because the app has been running previously on the same device in development mode with a signed in user?
Is there a way to start the tests with no previous state, no sessions, etc? I can't find anything in the documentation for this.

Use setUp and tearDown to set/clear state before/after each test.
setUp(() async {
// TODO
});
tearDown(() async {
// TODO
});

Related

How to get a Cloud Functions Callback URL to use during an API callback after it completes a money transaction, Flutter App

I need a callback URL so that when a mobile money API completes a payment transaction, my app can receive a message about the transaction status. I'm building a flutter app. I have seen that cloud functions may be the answer. So, I need help on how to get started on creating this callback URL.
I'm yet to try anything because all the material I have seen talks about JavaScript and websites. I need material on doing this in the flutter mobile app.
look at this snippet I hope it helps
// await for your first function t
await moneyFuntion()
.whenComplete(() async => await anotherAPIFuntion())
.onError((error, stackTrace) {
log("$error");

Flutter Web Firebase Auth's persistence doesn't work on PWA

I have developed a Flutter Web app that uses Firebase Authentication in order to sign in users to the app.
I've declared the Firebase Authentication persistence field so that the app will remember and auto-login the user when he revisits the Flutter Web app's URL, and won't be required to re-login every time he launches the URL.
It all works fine on a regular browser, but when the user generates a PWA (for example, clicking "Add to Home Screen" on iOS devices to save the website as PWA), the persistence feature stops working, and the user is required to re-login every time he opens the PWA.
Is there a way to add Firebase Authentication's persistence feature to a PWA? And if not, is there a way to prevent generating a PWA (and saving the Flutter Web app as a regular browser URL when clicking "Add to Home Screen" button on iOS, for example)?
Thank you!
To solve the persistence problem, add a listener:
FirebaseAuth.instance.idTokenChanges().listen((User? user) async {
if (user == null) {
// Function for user not logged in here. Do not write function to change page here.
} else {
// As it's a Future it will take a while to process the user's information, so it
will call the function after it's done.
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (_) => Home()));
}
}
This is an example I made and it worked, use controllers to change the status, put some function to wait for the information to be processed.
Hope this helps. Any questions, at your disposal.

Flutter integration test firebase auth (email link or google sign in)

I'm adding integration testing (using the integration_test package) to my app but I am running into a problem.
Let me explain. The first step when my app launch is authentication for which I have 3 options: firebase email link, firebase google sign in, and firebase facebook sign in.
What is blocking me is that all these sign in methods require actions outside of the main app dart code and thus are not accessible by flutter driver.
Am I missing something here? And if not how should that case be handled?
Cheers!
To make the test less flaky i would recommend to not rely on an internet connection or a third party (like Firebase or Google login).
I would advise to use a Mock for this. So when you try to login to your test you send a fake response, and in that way you can continue using the app.
The following article explains how to use a mock:
https://medium.com/stuart-engineering/mocking-integration-tests-with-flutter-af3b6ba846c7
You can use Patrol – it lets you interact with native system UI from within your Flutter integration tests. Example:
import 'package:flutter_test/flutter_test.dart';
import 'package:patrol/patrol.dart';
void main() {
patrolTest(
'signs in',
nativeAutomation: true,
(PatrolTester $) async {
await $.native.enterText(
Selector(textContains: 'Email'),
text: 'tester#awesomeapp.pl'),
);
await $.native.enterText(
Selector(textContains: 'Password'),
text: 'ny4ncat'),
);
await $.native.tap(Selector(text: 'Continue'));
// you should be signed in
});
}
You can add the fourth way of signing in - using username and password. Firebase should support this kind of very common situation, so you can do it within lines of code.
If you do not want the end users to login by password, you can simply disable this method in production build and only enable it in debug build.
Another way is to mock your authentication system. In other words, when doing testing, you have a button called "fake sign in", and your integration test driver just click that button.

Why is onTokenRefresh not firing?

I'm new to firebase messaging and flutter. According to the flutter firebase_messaging package docs, onTokenRefresh is fired when a new FCM token is generated. And according to Google's firebase docs there are two scenarios that triggers token generation:
When a new token is generated on initial app startup
Whenever an existing token is changed
Here is a simplified version of the main function of my application. After each execution, I delete the app from the emulator and the displayed token does indeed change. Despite this, onTokenRefresh is never fired and it should if my understanding of the documentation is correct.
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.instance.onTokenRefresh.listen((String token) {
print("New token: $token");
});
String token = await FirebaseMessaging.instance.getToken();
print("Token: $token");
//runApp(MyApp());
}
As I said, I'm new to flutter, dart and firebase messaging, is there something I'm fundamentally misunderstanding? Thanks.
So I think I figured it out. I noticed that sometimes, the onTokenRefresh does indeed fire. And I was wondering if it had something to do with how the flutter application is launched onto the emulator, in the sense that there is a race condition between when the token is generated and the listener attached.
To get the app to appear to start for the first time, I wiped the app data. Unfortunately this causes the flutter to automatically disconnect from the app which means I won't see the output of the print statement. So instead of trying to print when the token generation occurs, I assigned a value from the onTokenRefresh listener to a variable. I then updated a text widget with the value of the variable. And onTokenRefresh does indeed fire each time at start up if the app data has previously been wiped.

Selecting Email Account from google_sign_in during Flutter integration testing

I was trying to test sign-in to my home screen for my flutter app. I am using the google-signin ( https://pub.dev/packages/google_sign_in) package from Flutter to perform login and authentication. So once the login button is pressed, a popup appears asking the user to select the user account. At that point, I am unable to control the tap as this dialog screen is generated by the plugin. How do I implement selection of the user account in this case ?
test('Test Login', () async {
final Timeline timeline = await driver.traceAction(() async {
await driver.tap(find.byValueKey('GoogleLogin'));
await driver.tap(find.text('myemail#gmail.com')); // This will not work !!!
});
TimelineSummary.summarize(timeline)
..writeSummaryToFile('home_scroll_perf', pretty: true)
..writeTimelineToFile('home_scroll_perf', pretty: true);
});
In my opinion, this could be a problem when testing with any third party plugin. Please help.
It seems that the issue is still existing. There is no available workaround yet. Keep an eye on this GitHub post as this is closely monitored by the Flutter team.
Try Patrol, it lets you very easily interact with native UIs such as the Google sign-in dialog.
Below is a rough example of how I'd approach this. I'm going to assume that you're already signed in to a Google account, so the only thing you have to do is to select an email from the dialog.
// integration_test/sign_in_test.dart
void main() {
patrolTest(
'signs in with Google',
nativeAutomation: true,
(PatrolTester $) async {
await $.pumpWidgeAndSettle(YourAppWidget());
await $('Sign in with Google').tap();
await $.native.tap(Selector(text: 'your.email#gmail.com'));
await $.pumpAndSettle();
// should be signed in now
},
);
}
Remember: that integration tests has to be run with patrol drive, not flutter test integration_test.