Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to define a custom TestWidgetsFlutterBinding that overrides the defaultBinaryMessenger #104156

Open
Nobler opened this issue May 19, 2022 · 3 comments
Labels
a: tests "flutter test", flutter_test, or one of our tests c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project team-framework Owned by Framework team triaged-framework Triaged by Framework team

Comments

@Nobler
Copy link

Nobler commented May 19, 2022

Use case

In my case, I override the defaultBinaryMessenger just like TestDefaultBinaryMessengerBinding is doing:

mixin BinaryMessengerBinding on ServicesBinding {
  @override
  void initInstances() {
    super.initInstances();

    _instance = this;
  }

  static BinaryMessengerBinding? get instance => _instance;
  static BinaryMessengerBinding? _instance;

  @override
  BinaryMessenger createBinaryMessenger() => MyCustomBinaryMessenger();
}

When I want to test BinaryMessengerBinding in testWidget(), there is no proper way to define a custom TestWidgetsFlutterBinding(test framework needs), because the definition of TestWidgetsFlutterBinding mixed with TestDefaultBinaryMessengerBinding which needs the subclass/mixin also do the same thing (I guess, but am not sure) like:

mixin BinaryMessengerBinding on TestDefaultBinaryMessengerBinding {
  ...

  @override
  TestDefaultBinaryMessenger createBinaryMessenger() => ...
}

Proposal

So, is there any way to test BinaryMessengerBinding above?
If not, could please provide a way to define a custom TestWidgetsFlutterBinding that overrides the defaultBinaryMessenger but will not break the test behavior.

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label May 19, 2022
@darshankawar
Copy link
Member

@Nobler
Thanks for the report. Does this issue resemble your case ? #94123

If not, have you already tried using WidgetsFlutterBinding.ensureInitialized() as the first code just after main() to see if it helps ?

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 19, 2022
@Nobler
Copy link
Author

Nobler commented May 20, 2022

#94123

Thanks for replying.

Production codes work fine just like you said:

class CustomWidgetFlutterBinding extends WidgetsFlutterBinding
    with BinaryMessengerBinding {
  static WidgetsBinding ensureInitialized() {
    if (WidgetsBinding.instance == null) {
      CustomWidgetFlutterBinding();
    }
    return WidgetsBinding.instance!;
  }
}

void main() {
  CustomWidgetFlutterBinding.ensureInitialized();

  runApp();
}

My question is that the implementation of TestWidgetsFlutterBinding in flutter_test package does not support testing BinaryMessengerBinding.

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 20, 2022
@darshankawar
Copy link
Member

Thanks for the update.

@darshankawar darshankawar added a: tests "flutter test", flutter_test, or one of our tests framework flutter/packages/flutter repository. See also f: labels. c: proposal A detailed proposal for a change to Flutter and removed in triage Presently being triaged by the triage team labels May 20, 2022
@goderbauer goderbauer added the P3 Issues that are less important to the Flutter project label May 25, 2022
@flutter-triage-bot flutter-triage-bot bot added team-framework Owned by Framework team triaged-framework Triaged by Framework team labels Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: tests "flutter test", flutter_test, or one of our tests c: proposal A detailed proposal for a change to Flutter framework flutter/packages/flutter repository. See also f: labels. P3 Issues that are less important to the Flutter project team-framework Owned by Framework team triaged-framework Triaged by Framework team
Projects
None yet
Development

No branches or pull requests

3 participants