如何测试首选屏幕方向

发布于 2025-01-18 15:29:37 字数 795 浏览 2 评论 0原文

我正在使用一个(简化的)小部件,该小部件在启动时将屏幕方向设置为“横向”,并在处理时将其重置为“纵向”:

class LandScapeWidget extends StatefulWidget {
  const LandScapeWidget({Key? key}) : super(key: key);

  @override
  State<LandScapeWidget> createState() => _LandScapeWidgetState();
}

class _LandScapeWidgetState extends State<LandScapeWidget> {
  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
    ]);
  }

  @override
  void dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return const SizedBox.shrink();
  }
}

我如何编写对此行为的测试?

I'm working with a (simplified) widget that sets the screen orientation to "landscape" when initiated and resets it to "portrait" when disposed:

class LandScapeWidget extends StatefulWidget {
  const LandScapeWidget({Key? key}) : super(key: key);

  @override
  State<LandScapeWidget> createState() => _LandScapeWidgetState();
}

class _LandScapeWidgetState extends State<LandScapeWidget> {
  @override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
    ]);
  }

  @override
  void dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return const SizedBox.shrink();
  }
}

How can I write a test on this behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长途伴 2025-01-25 15:29:37

I got the inspiration from this扑打测试

void main() {
 testWidgets('It should be in the landscape view', (tester) async {
    const widget = LandScapeWidget();

    final logs = [];

    tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, (methodCall) async {
      if (methodCall.method == 'SystemChrome.setPreferredOrientations') {
        logs.add((methodCall.arguments as List)[0]);
      }
      return null;
    });

    expect(logs.length, 0);

    await tester.pumpWidget(
      const MaterialApp(
        home: widget,
      ),
    );

    expect(logs.length, 1, reason: 'It should have pushed a log after the initState');
    expect(logs.first, 'DeviceOrientation.landscapeLeft', reason: 'It should be in the landscape view after the initState');

    await tester.pumpWidget(const SizedBox()); // Disposes the widget
    expect(logs.length, 2, reason: 'It should have added a log after the dispose');
    expect(logs.last, 'DeviceOrientation.portraitUp', reason: 'It should be in the landscape view after the dispose');
  });

I got the inspiration from this flutter test.

void main() {
 testWidgets('It should be in the landscape view', (tester) async {
    const widget = LandScapeWidget();

    final logs = [];

    tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(SystemChannels.platform, (methodCall) async {
      if (methodCall.method == 'SystemChrome.setPreferredOrientations') {
        logs.add((methodCall.arguments as List)[0]);
      }
      return null;
    });

    expect(logs.length, 0);

    await tester.pumpWidget(
      const MaterialApp(
        home: widget,
      ),
    );

    expect(logs.length, 1, reason: 'It should have pushed a log after the initState');
    expect(logs.first, 'DeviceOrientation.landscapeLeft', reason: 'It should be in the landscape view after the initState');

    await tester.pumpWidget(const SizedBox()); // Disposes the widget
    expect(logs.length, 2, reason: 'It should have added a log after the dispose');
    expect(logs.last, 'DeviceOrientation.portraitUp', reason: 'It should be in the landscape view after the dispose');
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文