dimanche 1 novembre 2020

testing receiving intents with flutter widget tests

I have a small flutter app that shows incoming share actions a.k.a. intents in text widgets. The re ceiving part is like so:

class MyAppState extends State<MyApp> {

  @override
  void initState() {

     ReceiveSharingIntent.getInitialText().then((String value) {
        if (value == null) return;
        print("got intent " + value);
        add_to_widget (value) ; 
     });

     ReceiveSharingIntent.getTextStream().listen((String value) {
        print("got intent " + value);
        add_to_widget (value) ; 
     });

}

When I run the code on an android virtual device, the intent is gathered correctly and the text shows up in the widget so I guess I have my AndroidManifest.xml right:

<intent-filter>
  <action android:name="android.intent.action.MAIN"/>
  <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>

<intent-filter>
  <action android:name="android.intent.action.SEND" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:mimeType="text/*" />
</intent-filter>

But I want to test that automatically with the widget tester in flutter_test like so :


void main() {
  testWidgets('receive intent ', (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());

    // the missing bit
    await fire_share_action ('hello_intent') ; 


    expect (find.text('hello_intent'), findsOneWidget) ;
  });
}

As you see, the missing part is the content of fire_share_action. I tried firing an intent like in https://pub.dev/packages/intent but, although there was no error nor exception whatsoever, the intent is not printed like it was when I was running in the avd, nor did expect pass.

I have the feeling I miss something. Testing the reception of intents should be a basic thing, right?

I'm quite sure putting android code into fire_share_action (like I did) is the wrong way, but - how else can I do that? How can I send an Intent to my app from inside 'testWidgets'? Or do I have to create a mock receiver to do sandwich testing? I donnot want to go into flutter_driver and also not into unit_test; widget tests is my way to go here since they provide a middle ground between fast turnaround and reliability.

I messed up some days of coding and am very frustrated. And although there is tons of literature about sending and receiving Intents in a flutter app, I found nothing about testing the reception of intents, not to speak from testing sending intents...

Any idea?

Aucun commentaire:

Enregistrer un commentaire