todo-app/test/ui_images/cloud_images_settings_test.dart
Krzysztof kuhy Rudnicki 203e61d6ac
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Text-free sync settings via ssui 0.7.0
Bump sync_settings_ui 0.7.0 and github_device_auth 0.4.0 (glyph +
spoken label). Guard covers the Sync settings screen; degraded sync
gets its own warning glyph; tooltip delegate and copy button tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 01:32:02 +01:00

159 lines
5.2 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:todo/attachments/image_store_api.dart';
import 'package:todo/attachments/image_store_scope.dart';
import 'package:todo/ui/cloud_images_settings.dart';
import '../fake_image_store.dart';
import '../fake_secure_storage.dart';
void main() {
Future<void> pump(
WidgetTester tester, {
ImageStore? store,
http.Client? client,
bool field = true,
}) async {
final app = MaterialApp(
home: CloudImagesScreen(httpClient: client, showPasswordField: field),
);
await tester.pumpWidget(
store == null ? app : ImageStoreScope(store: store, child: app),
);
await tester.pump();
}
MockClient answer(int code) =>
MockClient((_) async => http.Response('', code));
Future<void> saveWith(WidgetTester tester, String password) async {
await tester.enterText(find.byType(TextField), password);
await tester.tap(find.bySemanticsLabel('Save & test'));
await tester.pump();
await tester.pump();
}
testWidgets('the settings tile opens the screen', (tester) async {
installFakeSecureStorage();
await tester.pumpWidget(
const MaterialApp(home: Scaffold(body: CloudImagesTile())),
);
await tester.tap(find.bySemanticsLabel('Cloud images'));
await tester.pumpAndSettle();
expect(find.byType(CloudImagesScreen), findsOneWidget);
});
testWidgets('loads the saved password and the store status', (tester) async {
installFakeSecureStorage(initial: {'dufsImagesPassword': 'saved'});
await pump(tester, store: FakeImageStore());
expect(
tester.widget<TextField>(find.byType(TextField)).controller!.text,
'saved',
);
expect(find.bySemanticsLabel('Login: configured'), findsOneWidget);
expect(find.bySemanticsLabel('Waiting to upload: 0'), findsOneWidget);
});
testWidgets('an accepted password is saved and the queue uploads', (
tester,
) async {
installFakeSecureStorage();
final store = FakeImageStore();
await pump(tester, store: store, client: answer(207));
await saveWith(tester, ' pw ');
expect(
find.bySemanticsLabel('Saved — dufs accepted the login'),
findsOneWidget,
);
expect(store.reconciles.single, isEmpty);
});
testWidgets('a rejected password says so and uploads nothing', (
tester,
) async {
installFakeSecureStorage();
final store = FakeImageStore();
await pump(tester, store: store, client: answer(401));
await saveWith(tester, 'bad');
expect(
find.bySemanticsLabel('Saved, but dufs rejected the password'),
findsOneWidget,
);
expect(store.reconciles, isEmpty);
});
testWidgets('an unreachable dufs is reported', (tester) async {
installFakeSecureStorage();
await pump(
tester,
store: FakeImageStore(),
client: MockClient((_) => throw const SocketException('down')),
);
await saveWith(tester, 'pw');
expect(
find.bySemanticsLabel('Saved — dufs is unreachable right now'),
findsOneWidget,
);
});
testWidgets('no keystore means nothing is saved', (tester) async {
installFakeSecureStorage(throwing: true);
await pump(tester, store: FakeImageStore(), client: answer(207));
await saveWith(tester, 'pw');
expect(
find.bySemanticsLabel('Could not save: the keystore is unavailable'),
findsOneWidget,
);
});
testWidgets('the desktop shows status only, with the setup command', (
tester,
) async {
await pump(tester, field: false);
expect(find.byType(TextField), findsNothing);
expect(
find.bySemanticsLabel('Login: missing — create it on the PC with'),
findsOneWidget,
);
expect(find.bySemanticsLabel('Copy: $kAddLoginCommand'), findsOneWidget);
// The command is copied, not painted: the button puts it on the
// clipboard (stubbed channel; the test host has no clipboard plugin).
final copied = <Object?>[];
final messenger =
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async {
if (call.method == 'Clipboard.setData') copied.add(call.arguments);
return null;
});
addTearDown(
() => messenger.setMockMethodCallHandler(SystemChannels.platform, null),
);
await tester.tap(find.bySemanticsLabel('Copy: $kAddLoginCommand'));
await tester.pump();
expect(copied, [
{'text': kAddLoginCommand},
]);
});
testWidgets('closing the screen mid-test is harmless', (tester) async {
installFakeSecureStorage();
final reply = Completer<http.Response>();
final store = FakeImageStore();
await pump(tester, store: store, client: MockClient((_) => reply.future));
await tester.enterText(find.byType(TextField), 'pw');
await tester.tap(find.bySemanticsLabel('Save & test'));
await tester.pump();
await tester.pumpWidget(const SizedBox());
reply.complete(http.Response('', 207));
await tester.pump();
expect(store.reconciles, isEmpty);
});
}