Skip to content

Commit

Permalink
TW-1702/shared_mocks.dart added
Browse files Browse the repository at this point in the history
  • Loading branch information
Te-Z committed Jun 3, 2024
1 parent a2d5d29 commit d03b1ec
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 144 deletions.
39 changes: 3 additions & 36 deletions test/mixin/download/download_file_on_mobile_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,9 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:matrix/matrix.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'download_file_on_mobile_mixin_test.mocks.dart';

const fakeEventId = "fakeEventId";
const fakeFilename = "fakeFilename";

class MockRoom extends Mock implements Room {
@override
bool operator ==(Object other) => (other is Room && other.id == id);

@override
int get hashCode => Object.hashAll([id]);

@override
Map<String, MatrixFile> get sendingFilePlaceholders => super.noSuchMethod(
Invocation.getter(#sendingFilePlaceholders),
returnValue: <String, MatrixFile>{},
returnValueForMissingStub: <String, MatrixFile>{},
);
}

class MockEvent extends Mock implements Event {
MockEvent(this.fakeRoom);
final Room fakeRoom;
@override
String get eventId => fakeEventId;

@override
Room get room => fakeRoom;

@override
Map get infoMap => super.noSuchMethod(
Invocation.getter(#infoMap),
returnValue: {},
returnValueForMissingStub: {},
);
}
import 'download_file_on_mobile_mixin_test.mocks.dart';
import '../../utils/shared_mocks.dart';

class DummyWidget extends StatefulWidget {
const DummyWidget({required this.event, super.key});
Expand Down Expand Up @@ -167,7 +134,7 @@ void main() {
final dummyState = DummyWidgetState(fakeEvent);

when(fakeRoom.sendingFilePlaceholders).thenReturn(<String, MatrixFile>{
fakeEventId: MatrixFile(
MockEvent.fakeEventId: MatrixFile(
name: fakeFilename,
filePath: "path/to/file",
),
Expand Down
37 changes: 1 addition & 36 deletions test/mixin/download/download_file_on_web_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:matrix/matrix.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import '../../utils/shared_mocks.dart';
import 'download_file_on_web_mixin_test.mocks.dart';

const fakeEventId = "fakeEventId";
const fakeFilename = "fakeFilename";

class MockRoom extends Mock implements Room {
@override
bool operator ==(Object other) => (other is Room && other.id == id);

@override
int get hashCode => Object.hashAll([id]);

@override
Map<String, MatrixFile> get sendingFilePlaceholders => super.noSuchMethod(
Invocation.getter(#sendingFilePlaceholders),
returnValue: <String, MatrixFile>{},
returnValueForMissingStub: <String, MatrixFile>{},
);
}

class MockEvent extends Mock implements Event {
MockEvent(this.fakeRoom);
final Room fakeRoom;
@override
String get eventId => fakeEventId;

@override
Room get room => fakeRoom;

@override
Map get infoMap => super.noSuchMethod(
Invocation.getter(#infoMap),
returnValue: {},
returnValueForMissingStub: {},
);
}

class DummyWidget extends StatefulWidget {
const DummyWidget({required this.event, super.key});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import 'package:matrix/matrix.dart';
import 'package:matrix_api_lite/fake_matrix_api.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';

import '../../../../utils/shared_mocks.dart';

class MockUpMessageContentBuilder with MessageContentBuilderMixin {}

void main() {
Expand All @@ -29,70 +31,6 @@ void main() {

final client = Client('client', httpClient: FakeMatrixApi());
final room = Room(id: '!room:example.abc', client: client);
final fileEvent = Event(
content: {
'body': 'something-important.doc',
'filename': 'something-important.doc',
'info': {'mimetype': 'application/msword', 'size': 46144},
'msgtype': 'm.file',
'url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1894270481925),
room: room,
);
final imageEvent = Event(
content: {
'body': 'filename.jpg',
'info': {'h': 398, 'mimetype': 'image/jpeg', 'size': 31037, 'w': 394},
'msgtype': 'm.image',
'url': 'mxc://example.org/JWEIFJgwEIhweiWJE',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
room: room,
);
final videoEvent = Event(
content: {
'body': 'Gangnam Style',
'info': {
'duration': 2140786,
'h': 320,
'mimetype': 'video/mp4',
'size': 1563685,
'thumbnail_info': {
'h': 300,
'mimetype': 'image/jpeg',
'size': 46144,
'w': 300,
},
'thumbnail_url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe',
'w': 480,
},
'msgtype': 'm.video',
'url': 'mxc://example.org/a526eYUSFFxlgbQYZmo442',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
room: room,
);
final emptyTextEvent = Event(
content: {
'body': '',
'msgtype': 'm.text',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
room: room,
);

group(
'[MessageContentBuilderMixin] TEST\n',
Expand Down Expand Up @@ -184,7 +122,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: fileEvent,
event: EventGenerator.fileEvent(room),
maxWidth: messageMaxWidthWeb,
);
},
Expand All @@ -195,7 +133,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: imageEvent,
event: EventGenerator.imageEvent(room),
maxWidth: messageMaxWidthWeb,
);
},
Expand All @@ -206,7 +144,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: videoEvent,
event: EventGenerator.videoEvent(room),
maxWidth: messageMaxWidthWeb,
);
},
Expand All @@ -219,7 +157,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: emptyTextEvent,
event: EventGenerator.emptyTextEvent(room),
maxWidth: messageMaxWidthWeb,
);
},
Expand Down Expand Up @@ -439,7 +377,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: fileEvent,
event: EventGenerator.fileEvent(room),
maxWidth: messageMaxWidthMobile,
);
},
Expand All @@ -450,7 +388,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: imageEvent,
event: EventGenerator.imageEvent(room),
maxWidth: messageMaxWidthMobile,
);
},
Expand All @@ -461,7 +399,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: videoEvent,
event: EventGenerator.videoEvent(room),
maxWidth: messageMaxWidthMobile,
);
},
Expand All @@ -474,7 +412,7 @@ void main() {
(WidgetTester tester) async {
await runTest(
tester,
event: emptyTextEvent,
event: EventGenerator.emptyTextEvent(room),
maxWidth: messageMaxWidthMobile,
);
},
Expand Down
108 changes: 108 additions & 0 deletions test/utils/shared_mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import 'package:matrix/matrix.dart';
import 'package:mockito/mockito.dart';

const fakeFilename = "fakeFilename";

class MockRoom extends Mock implements Room {
@override
bool operator ==(Object other) => (other is Room && other.id == id);

@override
int get hashCode => Object.hashAll([id]);

@override
Map<String, MatrixFile> get sendingFilePlaceholders => super.noSuchMethod(
Invocation.getter(#sendingFilePlaceholders),
returnValue: <String, MatrixFile>{},
returnValueForMissingStub: <String, MatrixFile>{},
);
}

class MockEvent extends Mock implements Event {
MockEvent(this.fakeRoom);
final Room fakeRoom;
static const fakeEventId = "fakeEventId";

@override
String get eventId => fakeEventId;

@override
Room get room => fakeRoom;

@override
Map get infoMap => super.noSuchMethod(
Invocation.getter(#infoMap),
returnValue: {},
returnValueForMissingStub: {},
);
}

class EventGenerator {
static Event fileEvent(Room room) => Event(
content: {
'body': 'something-important.doc',
'filename': 'something-important.doc',
'info': {'mimetype': 'application/msword', 'size': 46144},
'msgtype': 'm.file',
'url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1894270481925),
room: room,
);

static Event imageEvent(Room room) => Event(
content: {
'body': 'filename.jpg',
'info': {'h': 398, 'mimetype': 'image/jpeg', 'size': 31037, 'w': 394},
'msgtype': 'm.image',
'url': 'mxc://example.org/JWEIFJgwEIhweiWJE',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
room: room,
);

static Event videoEvent(Room room) => Event(
content: {
'body': 'Gangnam Style',
'info': {
'duration': 2140786,
'h': 320,
'mimetype': 'video/mp4',
'size': 1563685,
'thumbnail_info': {
'h': 300,
'mimetype': 'image/jpeg',
'size': 46144,
'w': 300,
},
'thumbnail_url': 'mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe',
'w': 480,
},
'msgtype': 'm.video',
'url': 'mxc://example.org/a526eYUSFFxlgbQYZmo442',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
room: room,
);

static Event emptyTextEvent(Room room) => Event(
content: {
'body': '',
'msgtype': 'm.text',
},
type: 'm.room.message',
eventId: '\$143273582443PhrSn:example.org',
senderId: '@example:example.org',
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
room: room,
);
}

0 comments on commit d03b1ec

Please sign in to comment.