From 1fc329eb23b0e3053d1ed7676b6d5361f9102704 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 11 Jun 2019 12:21:45 +0200 Subject: [PATCH] Add test for Room --- lib/src/Room.dart | 44 ++++++++++++++----- test/Room_test.dart | 104 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 test/Room_test.dart diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 3ffe5c9..a097390 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -246,25 +246,47 @@ class Room { /// Returns a Room from a json String which comes normally from the store. static Future getRoomFromTableRow( Map row, Client matrix) async { + String name = row["topic"]; - if (name == "") name = await matrix.store.getChatNameFromMemberNames(row["id"]); + if (name == "") name = await matrix.store?.getChatNameFromMemberNames(row["id"]) ?? ""; - String content_body = row["content_body"]; - if (content_body == null || content_body == "") - content_body = "Keine vorhergehenden Nachrichten"; - - String avatarMxcUrl = row["avatar_url"]; - - if (avatarMxcUrl == "") - avatarMxcUrl = await matrix.store.getAvatarFromSingleChat(row["id"]); + if (row["avatar_url"] == "") + row["avatar_url"] = await matrix.store?.getAvatarFromSingleChat(row["id"]) ?? ""; return Room( id: row["id"], name: name, - avatar: MxContent(avatarMxcUrl), + topic: row["description"], + avatar: MxContent(row["avatar_url"]), notificationCount: row["notification_count"], highlightCount: row["highlight_count"], - topic: "", + unread: ChatTime(row["unread"]), + fullyRead: row["fully_read"], + notificationSettings: row["notification_settings"], + directChatMatrixID: row["direct_chat_matrix_id"], + draft: row["draft"], + prev_batch: row["prev_batch"], + + guestAccess: row["guest_access"], + historyVisibility: row["history_visibility"], + joinRules: row["join_rules"], + + powerLevels: { + "power_events_default": row["power_events_default"], + "power_state_default": row["power_state_default"], + "power_redact": row["power_redact"], + "power_invite": row["power_invite"], + "power_ban": row["power_ban"], + "power_kick": row["power_kick"], + "power_user_default": row["power_user_default"], + "power_event_avatar": row["power_event_avatar"], + "power_event_history_visibility": row["power_event_history_visibility"], + "power_event_canonical_alias": row["power_event_canonical_alias"], + "power_event_aliases": row["power_event_aliases"], + "power_event_name": row["power_event_name"], + "power_event_power_levels": row["power_event_power_levels"], + }, + client: matrix, events: [], participants: [], diff --git a/test/Room_test.dart b/test/Room_test.dart new file mode 100644 index 0000000..0798231 --- /dev/null +++ b/test/Room_test.dart @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * This file is part of famedlysdk. + * + * famedlysdk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * famedlysdk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Foobar. If not, see . + */ + +import 'package:flutter_test/flutter_test.dart'; +import 'package:famedlysdk/src/Room.dart'; +import 'package:famedlysdk/src/Client.dart'; +import 'dart:async'; +import 'FakeMatrixApi.dart'; + +void main() { + /// All Tests related to the Event + group("Room", () { + test("Create from json", () async { + Client matrix = Client("testclient"); + matrix.connection.httpClient = FakeMatrixApi(); + matrix.homeserver = "https://fakeServer.notExisting"; + + final String id = "!jf983jjf:server.abc"; + final String name = "My Room"; + final String topic = "This is my own room"; + final int unread = DateTime.now().millisecondsSinceEpoch; + final int notificationCount = 2; + final int highlightCount = 1; + final String fullyRead = "fjh82jdjifd:server.abc"; + final String notificationSettings = "all"; + final String guestAccess = "forbidden"; + final String historyVisibility = "invite"; + final String joinRules = "invite"; + + final Map jsonObj = { + "id": id, + "topic": name, + "description": topic, + "avatar_url": "", + "notification_count": notificationCount, + "highlight_count": highlightCount, + "unread": unread, + "fully_read": fullyRead, + "notification_settings": notificationSettings, + "direct_chat_matrix_id": "", + "draft": "", + "prev_batch": "", + "guest_access": guestAccess, + "history_visibility": historyVisibility, + "join_rules": joinRules, + "power_events_default": 0, + "power_state_default": 0, + "power_redact": 0, + "power_invite": 0, + "power_ban": 0, + "power_kick": 0, + "power_user_default": 0, + "power_event_avatar": 0, + "power_event_history_visibility": 0, + "power_event_canonical_alias": 0, + "power_event_aliases": 0, + "power_event_name": 0, + "power_event_power_levels": 0, + }; + + Room room = await Room.getRoomFromTableRow(jsonObj, matrix); + + expect(room.id,id); + expect(room.name,name); + expect(room.topic,topic); + expect(room.avatar.mxc,""); + expect(room.notificationCount,notificationCount); + expect(room.highlightCount,highlightCount); + expect(room.unread.toTimeStamp(),unread); + expect(room.fullyRead,fullyRead); + expect(room.notificationSettings,notificationSettings); + expect(room.directChatMatrixID,""); + expect(room.draft,""); + expect(room.prev_batch,""); + expect(room.guestAccess,guestAccess); + expect(room.historyVisibility,historyVisibility); + expect(room.joinRules,joinRules); + room.powerLevels.forEach((String key, int value) { + expect(value, 0); + }); + + }); + }); +}