重置 TestTextInput 状态

概述

Summary

现在 TestTextInput 实例(系统屏幕键盘的存根)的状态会在测试之间重置。

The state of a TestTextInput instance, a stub for the system’s onscreen keyboard, is now reset between tests.

上下文

Context

Flutter 测试框架中使用一个名为 TestTextInput 的类来跟踪和操作 widgets 测试中的编辑状态。个别测试可以通过方法调用来修改此对象的内部状态,有时是间接的(例如通过 SystemChannels.textInput 设置自己的处理程序)。在其之后的的测试可能会检查 WidgetTester.testTextInput 的状态,拿到不符合预期的值。

The Flutter test framework uses a class called TestTextInput to track and manipulate editing state in a widgets test. Individual tests can make calls that modify the internal state of this object, sometimes indirectly (such as by setting their own handlers on SystemChannels.textInput). Subsequent tests might then check the state of WidgetTester.testTextInput and get unexpected values.

更改描述

Description of change

现在 WidgetTester.testTextInput 的状态会在运行 testWidgets 测试之前重置。

The state of WidgetTester.testTextInput is now reset before running a testWidgets test.

迁移指南

Migration guide

之前依赖于 WidgetTester.testTextInput 运行时脏状态的测试必须更新。例如 packages/flutter/test/material/text_field_test.dart 文件中的 'Controller can update server' 测试。在这之前,其它测试使得 WidgetTester.testTextInput 处于脏状态,并且在应该设置状态的时候设置状态失败,所以它通过了测试。

Tests that relied on dirty state from a previously run test must be updated. For example, the following test, from packages/flutter/test/material/text_field_test.dart in the 'Controller can update server' test, previously passed because of a combination of dirty state from previous tests and a failure to actually set state in cases where it should have been set.

迁移前的代码:

Code before migration:

widgetsTest 里,在实际更改文本编辑 widget 上的文本之前,此调用可能已成功:

In a widgetsTest, before actually changing text on a text editing widget, this call might have succeeded:

    expect(tester.testTextInput.editingState['text'], isEmpty);

迁移后的代码:

Code after migration:

要么完全删除该调用,要么考虑使用以下声明来确认状态尚未被修改:

Either remove the call entirely, or consider using the following to assert that the state hasn’t been modified yet:

    expect(tester.testTextInput.editingState, isNull);

时间轴

Timeline

发布于版本:1.16.3
发布于稳定版本:1.17

Landed in version: 1.16.3
In stable release: 1.17

参考文献

References

API 文档:

API documentation:

相关 issues:

Relevant issue:

相关 PR:

Relevant PR: