Add tests for shared_app_data.#.dart API examples. (#147830)

This PR contributes to https://github.com/flutter/flutter/issues/130459

### Description
- Updates `examples/api/lib/widgets/shared_app_data/shared_app_data.0.dart` to meet latest API examples structure
- Updates `examples/api/lib/widgets/shared_app_data/shared_app_data.1.dart` to meet latest API examples structure
- Adds tests for `examples/api/lib/widgets/shared_app_data/shared_app_data.0.dart`
- Adds tests for `examples/api/lib/widgets/shared_app_data/shared_app_data.1.dart`
This commit is contained in:
Kostia Sokolovskyi
2024-05-20 19:49:04 +02:00
committed by GitHub
parent 6cb9119148
commit d9cf066191
5 changed files with 242 additions and 45 deletions

View File

@@ -0,0 +1,100 @@
// 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/material.dart';
import 'package:flutter_api_samples/widgets/shared_app_data/shared_app_data.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('Verify correct labels are displayed', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SharedAppDataExampleApp(),
);
expect(find.text('SharedAppData Sample'), findsOneWidget);
expect(find.text('foo: initial'), findsOneWidget);
expect(find.text('bar: initial'), findsOneWidget);
expect(find.text('change foo'), findsOneWidget);
expect(find.text('change bar'), findsOneWidget);
});
testWidgets('foo value can be updated', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SharedAppDataExampleApp(),
);
int counter = 0;
while (counter < 10) {
counter++;
await tester.tap(find.ancestor(
of: find.text('change foo'),
matching: find.byType(ElevatedButton),
));
await tester.pump();
expect(find.text('foo: FOO $counter'), findsOneWidget);
}
});
testWidgets('bar value can be updated', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SharedAppDataExampleApp(),
);
int counter = 0;
while (counter < 10) {
counter++;
await tester.tap(find.ancestor(
of: find.text('change bar'),
matching: find.byType(ElevatedButton),
));
await tester.pump();
expect(find.text('bar: BAR $counter'), findsOneWidget);
}
});
testWidgets('foo and bar values update independently of one another', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SharedAppDataExampleApp(),
);
int fooCounter = 0;
int barCounter = 0;
for (int i = 0; i < 20; i++) {
if (i.isEven) {
fooCounter++;
} else {
barCounter++;
}
await tester.tap(
find.ancestor(
of: i.isEven ? find.text('change foo') : find.text('change bar'),
matching: find.byType(ElevatedButton),
),
);
await tester.pump();
expect(
find.text(
'foo: ${fooCounter == 0 ? 'initial' : 'FOO $fooCounter'}',
),
findsOneWidget,
);
expect(
find.text(
'bar: ${barCounter == 0 ? 'initial' : 'BAR $barCounter'}',
),
findsOneWidget,
);
}
});
}

View File

@@ -0,0 +1,48 @@
// 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/material.dart';
import 'package:flutter_api_samples/widgets/shared_app_data/shared_app_data.1.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
example.SharedObject getSharedObject(WidgetTester tester) {
final BuildContext context = tester.element(
find.byType(example.CustomWidget),
);
return example.SharedObject.of(context);
}
testWidgets('Verify correct labels are displayed', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SharedAppDataExampleApp(),
);
final example.SharedObject sharedObject = getSharedObject(tester);
expect(find.text('SharedAppData Sample'), findsOneWidget);
expect(find.text('Replace $sharedObject'), findsOneWidget);
});
testWidgets('Button tap resets SharedObject', (WidgetTester tester) async {
await tester.pumpWidget(
const example.SharedAppDataExampleApp(),
);
for (int i = 0; i < 10; i++) {
final example.SharedObject sharedObject = getSharedObject(tester);
final Finder buttonFinder = find.ancestor(
of: find.text('Replace $sharedObject'),
matching: find.byType(ElevatedButton),
);
expect(buttonFinder, findsOneWidget);
await tester.tap(buttonFinder);
await tester.pump();
}
});
}