Skip to content

Commit

Permalink
test wip
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan James Li <[email protected]>
  • Loading branch information
PIG208 committed Dec 24, 2024
1 parent b8a77e9 commit fd9f2b6
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions test/widgets/compose_box_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:checks/checks.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter_checks/flutter_checks.dart';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -582,6 +584,110 @@ void main() {

// TODO test what happens when capturing/uploading fails
});

group('attach from keyboard', () {
Future<void> testAttachFromKeyboard(WidgetTester tester, {
required List<int> data,
required String attachedFileUrl,
required String mimeType,
required String expectedFilename,
}) async {
TypingNotifier.debugEnable = false;
addTearDown(TypingNotifier.debugReset);

final channel = eg.stream();
await prepareComposeBox(tester,
narrow: TopicNarrow(channel.streamId, 'topic'), streams: [channel]);

await tester.enterText(contentInputFinder, 'some content: ');
await tester.pump();
checkAppearsLoading(tester, false);

final uploadUrl = '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/$expectedFilename';
connection.prepare(delay: const Duration(seconds: 1), json:
UploadFileResult(uri: uploadUrl).toJson());

// Setup adapted from:
// https://github.com/flutter/flutter/blob/0ffc4ce00ea7bb912e379adf39354644eab2c17e/packages/flutter/test/widgets/editable_text_test.dart#L724-L740
// This is later used to invoke [EditableText]'s implementation of
// [TextInputClient.performAction] on the content [TextField],
// which did not expose an API accessible during testing.
final ByteData? messageBytes = const JSONMessageCodec().encodeMessage({
'args': [
-1,
'TextInputAction.commitContent',
{
"mimeType": mimeType,
"data": data,
"uri": attachedFileUrl,
},
],
'method': 'TextInputClient.performAction',
});
await tester.binding.defaultBinaryMessenger.handlePlatformMessage(
'flutter/textinput',
messageBytes,
(ByteData? _) {},
);
await tester.pump();

check(controller!.content.text)
.equals('some content: [Uploading $expectedFilename…]()\n\n');
// (the request is checked more thoroughly in API tests)
check(connection.lastRequest!).isA<http.MultipartRequest>()
..method.equals('POST')
..files.single.which((it) => it
..field.equals('file')
..length.equals(data.length)
..filename.equals(expectedFilename)
..contentType.asString.equals(mimeType)
..has<Future<List<int>>>((f) => f.finalize().toBytes(), 'contents')
.completes((it) => it.deepEquals(data))
);
checkAppearsLoading(tester, true);

await tester.pump(const Duration(seconds: 1));
check(controller!.content.text)
.equals('some content: [$expectedFilename]($uploadUrl)\n\n');
checkAppearsLoading(tester, false);
}

final fileContent = [1, 0, 1, 0, 0];

testWidgets('empty uri', (tester) async {
await testAttachFromKeyboard(tester,
data: fileContent,
attachedFileUrl: '',
mimeType: 'image/jpeg',
expectedFilename: 'file');
});

testWidgets('empty file', (tester) async {
await testAttachFromKeyboard(tester,
data: [],
attachedFileUrl: '',
mimeType: 'image/jpeg',
expectedFilename: 'file');
});

testWidgets('no name', (tester) async {
await testAttachFromKeyboard(tester,
data: fileContent,
attachedFileUrl: 'content://com.zulip.android.zulipboard.provider',
mimeType: 'image/jpeg',
expectedFilename: 'file');
});

testWidgets('name', (tester) async {
await testAttachFromKeyboard(tester,
data: fileContent,
attachedFileUrl:
'content://com.samsung.android.zulipboard.provider'
'/root/com.zulip.android.zulipboard/candidate_temp/test.gif',
mimeType: 'image/gif',
expectedFilename: 'test.gif');
});
});
});

group('error banner', () {
Expand Down

0 comments on commit fd9f2b6

Please sign in to comment.