[Events] Add more EventTypes and tests

This commit is contained in:
Christian Pauly 2019-07-22 11:54:06 +02:00
parent ed6f502a83
commit df7adbe5b6
2 changed files with 124 additions and 28 deletions

View file

@ -108,6 +108,10 @@ class Event {
return EventTypes.RoomMember;
case "m.room.power_levels":
return EventTypes.RoomPowerLevels;
case "m.room.guest_access":
return EventTypes.GuestAccess;
case "m.room.history_visibility":
return EventTypes.HistoryVisibility;
case "m.room.message":
switch (content["msgtype"] ?? "m.text") {
case "m.text":
@ -131,7 +135,7 @@ class Event {
return EventTypes.Location;
}
}
return EventTypes.Text;
return EventTypes.Unknown;
}
/// Generate a new Event object from a json string, mostly a table row.
@ -218,6 +222,9 @@ enum EventTypes {
RoomName,
RoomTopic,
RoomAvatar,
GuestAccess,
HistoryVisibility,
Unknown,
}
final Map<String, int> StatusTypes = {

View file

@ -21,6 +21,8 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:convert';
import 'package:famedlysdk/src/Event.dart';
import 'package:famedlysdk/src/User.dart';
import 'package:flutter_test/flutter_test.dart';
@ -28,34 +30,34 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
/// All Tests related to the Event
group("Event", () {
final int timestamp = DateTime.now().millisecondsSinceEpoch;
final String id = "!4fsdfjisjf:server.abc";
final String senderID = "@alice:server.abc";
final String senderDisplayname = "Alice";
final String empty = "";
final Membership membership = Membership.join;
final String type = "m.room.message";
final String msgtype = "m.text";
final String body = "Hello World";
final String formatted_body = "<b>Hello</b> World";
final String contentJson =
'{"msgtype":"$msgtype","body":"$body","formatted_body":"$formatted_body"}';
Map<String, dynamic> jsonObj = {
"event_id": id,
"matrix_id": senderID,
"displayname": senderDisplayname,
"avatar_url": empty,
"membership": membership.toString().split('.').last,
"origin_server_ts": timestamp,
"state_key": empty,
"type": type,
"content_json": contentJson,
};
test("Create from json", () async {
final int timestamp = DateTime.now().millisecondsSinceEpoch;
final String id = "!4fsdfjisjf:server.abc";
final String senderID = "@alice:server.abc";
final String senderDisplayname = "Alice";
final String empty = "";
final Membership membership = Membership.join;
final String type = "m.room.message";
final String msgtype = "m.text";
final String body = "Hello World";
final String formatted_body = "<b>Hello</b> World";
final String contentJson =
'{"msgtype":"$msgtype","body":"$body","formatted_body":"$formatted_body"}';
Map<String, dynamic> json = {
"event_id": id,
"matrix_id": senderID,
"displayname": senderDisplayname,
"avatar_url": empty,
"membership": membership.toString().split('.').last,
"origin_server_ts": timestamp,
"state_key": empty,
"type": type,
"content_json": contentJson,
};
Event event = Event.fromJson(json, null);
Event event = Event.fromJson(jsonObj, null);
expect(event.id, id);
expect(event.sender.id, senderID);
@ -68,5 +70,92 @@ void main() {
expect(event.getBody(), body);
expect(event.type, EventTypes.Text);
});
test("Test all EventTypes", () async {
Event event;
jsonObj["type"] = "m.room.avatar";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomAvatar);
jsonObj["type"] = "m.room.name";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomName);
jsonObj["type"] = "m.room.topic";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomTopic);
jsonObj["type"] = "m.room.Aliases";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomAliases);
jsonObj["type"] = "m.room.canonical_alias";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomCanonicalAlias);
jsonObj["type"] = "m.room.create";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomCreate);
jsonObj["type"] = "m.room.join_rules";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomJoinRules);
jsonObj["type"] = "m.room.member";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomMember);
jsonObj["type"] = "m.room.power_levels";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomPowerLevels);
jsonObj["type"] = "m.room.guest_access";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.GuestAccess);
jsonObj["type"] = "m.room.history_visibility";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.HistoryVisibility);
jsonObj["type"] = "m.room.message";
jsonObj["content"] = json.decode(jsonObj["content_json"]);
jsonObj["content"]["msgtype"] = "m.notice";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Notice);
jsonObj["content"]["msgtype"] = "m.emote";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Emote);
jsonObj["content"]["msgtype"] = "m.image";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Image);
jsonObj["content"]["msgtype"] = "m.video";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Video);
jsonObj["content"]["msgtype"] = "m.audio";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Audio);
jsonObj["content"]["msgtype"] = "m.file";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.File);
jsonObj["content"]["msgtype"] = "m.location";
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Location);
jsonObj["type"] = "m.room.message";
jsonObj["content"]["msgtype"] = "m.text";
jsonObj["content"]["m.relates_to"] = {};
jsonObj["content"]["m.relates_to"]["m.in_reply_to"] = {
"event_id": "1234",
};
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.Reply);
});
});
}