flutter_flutter/packages/flutter/test/widgets/shared_app_data_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

222 lines
7.4 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('SharedAppData basics', (WidgetTester tester) async {
var columnBuildCount = 0;
var child1BuildCount = 0;
var child2BuildCount = 0;
late void Function(BuildContext context) setSharedAppDataValue;
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: SharedAppData(
child: Builder(
builder: (BuildContext context) {
columnBuildCount += 1;
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
setSharedAppDataValue.call(context);
},
child: Column(
children: <Widget>[
Builder(
builder: (BuildContext context) {
child1BuildCount += 1;
return Text(
SharedAppData.getValue<String, String>(
context,
'child1Text',
() => 'null',
),
);
},
),
Builder(
builder: (BuildContext context) {
child2BuildCount += 1;
return Text(
SharedAppData.getValue<String, String>(
context,
'child2Text',
() => 'null',
),
);
},
),
],
),
);
},
),
),
),
);
expect(columnBuildCount, 1);
expect(child1BuildCount, 1);
expect(child2BuildCount, 1);
expect(find.text('null').evaluate().length, 2);
// SharedAppData.setValue<String, String>(context, 'child1Text', 'child1')
// causes the first Text widget to be rebuilt with its text to be
// set to 'child1'. Nothing else is rebuilt.
setSharedAppDataValue = (BuildContext context) {
SharedAppData.setValue<String, String>(context, 'child1Text', 'child1');
};
await tester.tap(find.byType(GestureDetector));
await tester.pump();
expect(columnBuildCount, 1);
expect(child1BuildCount, 2);
expect(child2BuildCount, 1);
expect(find.text('child1'), findsOneWidget);
expect(find.text('null'), findsOneWidget);
// SharedAppData.setValue<String, String>(context, 'child2Text', 'child1')
// causes the second Text widget to be rebuilt with its text to be
// set to 'child2'. Nothing else is rebuilt.
setSharedAppDataValue = (BuildContext context) {
SharedAppData.setValue<String, String>(context, 'child2Text', 'child2');
};
await tester.tap(find.byType(GestureDetector));
await tester.pump();
expect(columnBuildCount, 1);
expect(child1BuildCount, 2);
expect(child2BuildCount, 2);
expect(find.text('child1'), findsOneWidget);
expect(find.text('child2'), findsOneWidget);
// Resetting a key's value to the same value does not
// cause any widgets to be rebuilt.
setSharedAppDataValue = (BuildContext context) {
SharedAppData.setValue<String, String>(context, 'child1Text', 'child1');
SharedAppData.setValue<String, String>(context, 'child2Text', 'child2');
};
await tester.tap(find.byType(GestureDetector));
await tester.pump();
expect(columnBuildCount, 1);
expect(child1BuildCount, 2);
expect(child2BuildCount, 2);
// More of the same, resetting the values to null..
setSharedAppDataValue = (BuildContext context) {
SharedAppData.setValue<String, String>(context, 'child1Text', 'null');
};
await tester.tap(find.byType(GestureDetector));
await tester.pump();
expect(columnBuildCount, 1);
expect(child1BuildCount, 3);
expect(child2BuildCount, 2);
expect(find.text('null'), findsOneWidget);
expect(find.text('child2'), findsOneWidget);
setSharedAppDataValue = (BuildContext context) {
SharedAppData.setValue<String, String>(context, 'child2Text', 'null');
};
await tester.tap(find.byType(GestureDetector));
await tester.pump();
expect(columnBuildCount, 1);
expect(child1BuildCount, 3);
expect(child2BuildCount, 3);
expect(find.text('null').evaluate().length, 2);
});
testWidgets('WidgetsApp SharedAppData ', (WidgetTester tester) async {
var parentBuildCount = 0;
var childBuildCount = 0;
await tester.pumpWidget(
WidgetsApp(
color: const Color(0xff00ff00),
builder: (BuildContext context, Widget? child) {
parentBuildCount += 1;
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
SharedAppData.setValue<String, String>(context, 'childText', 'child');
},
child: Center(
child: Builder(
builder: (BuildContext context) {
childBuildCount += 1;
return Text(
SharedAppData.getValue<String, String>(context, 'childText', () => 'null'),
);
},
),
),
);
},
),
);
expect(find.text('null'), findsOneWidget);
expect(parentBuildCount, 1);
expect(childBuildCount, 1);
await tester.tap(find.byType(GestureDetector));
await tester.pump();
expect(parentBuildCount, 1);
expect(childBuildCount, 2);
expect(find.text('child'), findsOneWidget);
});
testWidgets('WidgetsApp SharedAppData Shadowing', (WidgetTester tester) async {
var innerTapCount = 0;
var outerTapCount = 0;
await tester.pumpWidget(
WidgetsApp(
color: const Color(0xff00ff00),
builder: (BuildContext context, Widget? child) {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
outerTapCount += 1;
SharedAppData.setValue<String, String>(context, 'childText', 'child');
},
child: Center(
child: SharedAppData(
child: Builder(
builder: (BuildContext context) {
return GestureDetector(
onTap: () {
innerTapCount += 1;
SharedAppData.setValue<String, String>(context, 'childText', 'child');
},
child: Text(
SharedAppData.getValue<String, String>(context, 'childText', () => 'null'),
),
);
},
),
),
),
);
},
),
);
expect(find.text('null'), findsOneWidget);
await tester.tapAt(const Offset(10, 10));
await tester.pump();
expect(outerTapCount, 1);
expect(innerTapCount, 0);
expect(find.text('null'), findsOneWidget);
await tester.tap(find.text('null'));
await tester.pump();
expect(outerTapCount, 1);
expect(innerTapCount, 1);
expect(find.text('child'), findsOneWidget);
});
}