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 0ef5179 commit 89446ec
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions test/widgets/compose_box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';

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 @@ -575,6 +576,113 @@ void main() {

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

group('attach from keyboard', () {
Future<void> insertContentFromKeyboard(WidgetTester tester, {
required List<int> data,
required String attachedFileUrl,
required String mimeType,
}) async {
// 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? _) {},
);
}

testWidgets('empty file', (tester) async {
await prepare(tester);
await insertContentFromKeyboard(tester,
data: [],
attachedFileUrl:
'content://com.samsung.android.zulipboard.provider'
'/root/com.zulip.android.zulipboard/candidate_temp/test.gif',
mimeType: 'image/jpeg');

await tester.pump();
check(controller!.content.text)
.equals('see image: ');
check(connection.takeRequests()).isEmpty();
checkAppearsLoading(tester, false);
});

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

testWidgets('no name', (tester) async {
await prepare(tester);
const uploadUrl = '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/file';
connection.prepare(json: UploadFileResult(uri: uploadUrl).toJson());
await insertContentFromKeyboard(tester,
data: fileContent,
attachedFileUrl: 'content://com.zulip.android.zulipboard.provider',
mimeType: 'image/jpeg');

await tester.pump();
check(controller!.content.text)
.equals('see image: [Uploading file…]()\n\n');
check(connection.lastRequest!).isA<http.MultipartRequest>()
..method.equals('POST')
..files.single.which((it) => it
..field.equals('file')
..length.equals(fileContent.length)
..filename.equals('file')
..contentType.asString.equals('image/jpeg')
..has<Future<List<int>>>((f) => f.finalize().toBytes(), 'contents')
.completes((it) => it.deepEquals(fileContent))
);

await tester.pump(Duration.zero);
check(controller!.content.text)
.equals('see image: [file]($uploadUrl)\n\n');
});

testWidgets('name', (tester) async {
await prepare(tester);
const uploadUrl = '/user_uploads/1/4e/m2A3MSqFnWRLUf9SaPzQ0Up_/test.gif';
connection.prepare(json: UploadFileResult(uri: uploadUrl).toJson());
await insertContentFromKeyboard(tester,
data: fileContent,
attachedFileUrl:
'content://com.samsung.android.zulipboard.provider'
'/root/com.zulip.android.zulipboard/candidate_temp/test.gif',
mimeType: 'image/gif');

await tester.pump();
check(controller!.content.text)
.equals('see image: [Uploading test.gif…]()\n\n');
check(connection.lastRequest!).isA<http.MultipartRequest>()
..method.equals('POST')
..files.single.which((it) => it
..field.equals('file')
..length.equals(fileContent.length)
..filename.equals('test.gif')
..contentType.asString.equals('image/gif')
..has<Future<List<int>>>((f) => f.finalize().toBytes(), 'contents')
.completes((it) => it.deepEquals(fileContent))
);

await tester.pump(Duration.zero);
check(controller!.content.text)
.equals('see image: [test.gif]($uploadUrl)\n\n');
});
});
});

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

0 comments on commit 89446ec

Please sign in to comment.