From df0cc1d273c177b92b7df479667eb2b0e1f5bc4a Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 8 Aug 2019 10:31:39 +0200 Subject: [PATCH] [Tests] Refactoring --- lib/src/Client.dart | 13 ++++++- lib/src/RawEvent.dart | 2 +- lib/src/Room.dart | 25 ++++++++++--- lib/src/RoomList.dart | 3 -- lib/src/State.dart | 14 ++++++- lib/src/User.dart | 24 +++++++++++- lib/src/utils/ChatTime.dart | 1 - test/Client_test.dart | 73 +++++++++++++++++++++++-------------- test/FakeMatrixApi.dart | 2 +- test/Room_test.dart | 6 +-- test/Timeline_test.dart | 13 +++---- test/User_test.dart | 10 ++--- 12 files changed, 128 insertions(+), 58 deletions(-) diff --git a/lib/src/Client.dart b/lib/src/Client.dart index c25f59d0..90561afc 100644 --- a/lib/src/Client.dart +++ b/lib/src/Client.dart @@ -99,14 +99,22 @@ class Client { /// Presences of users by a given matrix ID Map presences = {}; + /// Callback will be called on account data updates. + AccountDataEventCB onAccountData; + + /// Callback will be called on presences. + PresenceCB onPresence; + void handleUserUpdate(UserUpdate userUpdate) { if (userUpdate.type == "account_data") { AccountData newAccountData = AccountData.fromJson(userUpdate.content); accountData[newAccountData.typeKey] = newAccountData; + if (onAccountData != null) onAccountData(newAccountData); } if (userUpdate.type == "presence") { Presence newPresence = Presence.fromJson(userUpdate.content); - presences[newPresence.typeKey] = newPresence; + presences[newPresence.sender] = newPresence; + if (onPresence != null) onPresence(newPresence); } } @@ -320,3 +328,6 @@ class Client { return resp; } } + +typedef AccountDataEventCB = void Function(AccountData accountData); +typedef PresenceCB = void Function(Presence presence); diff --git a/lib/src/RawEvent.dart b/lib/src/RawEvent.dart index c4bf7b5f..8deff6f4 100644 --- a/lib/src/RawEvent.dart +++ b/lib/src/RawEvent.dart @@ -44,7 +44,7 @@ class RawEvent { /// The user who has sent this event if it is not a global account data event. final String senderId; - User get sender => room.states[senderId] ?? User(senderId); + User get sender => room.states[senderId] ?? User(senderId: senderId); /// The time this event has received at the server. May be null for events like /// account data. diff --git a/lib/src/Room.dart b/lib/src/Room.dart index f9de44bc..ee376147 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -74,7 +74,7 @@ class Room { if (mHeroes.length > 0) { String displayname = ""; for (int i = 0; i < mHeroes.length; i++) - displayname += User(mHeroes[i]).calcDisplayname() + ", "; + displayname += User(senderId: mHeroes[i]).calcDisplayname() + ", "; return displayname.substring(0, displayname.length - 2); } return "Empty chat"; @@ -99,8 +99,23 @@ class Room { ? states["m.room.canonical_alias"].content["alias"] : ""; - /// If this room is a direct chat, this is the matrix ID of the user - String get directChatMatrixID => ""; // TODO: Needs account_data in client + /// If this room is a direct chat, this is the matrix ID of the user. + /// Returns null otherwise. + String get directChatMatrixID { + String returnUserId = null; + if (client.directChats is Map) { + client.directChats.forEach((String userId, dynamic roomIds) { + if (roomIds is List) { + for (int i = 0; i < roomIds.length; i++) + if (roomIds[i] == this.id) { + returnUserId = userId; + break; + } + } + }); + } + return returnUserId; + } /// Must be one of [all, mention] String notificationSettings; @@ -136,7 +151,7 @@ class Room { if (mHeroes.length > 0) { String displayname = ""; for (int i = 0; i < mHeroes.length; i++) - displayname += User(mHeroes[i]).calcDisplayname() + ", "; + displayname += User(senderId: mHeroes[i]).calcDisplayname() + ", "; return displayname.substring(0, displayname.length - 2); } return "Empty chat"; @@ -467,7 +482,7 @@ class Room { return participants; for (num i = 0; i < res["chunk"].length; i++) { - User newUser = State.fromJson(res["chunk"][i], this) as User; + User newUser = State.fromJson(res["chunk"][i], this).asUser; if (newUser.membership != Membership.leave) participants.add(newUser); } diff --git a/lib/src/RoomList.dart b/lib/src/RoomList.dart index b8079556..6096dd44 100644 --- a/lib/src/RoomList.dart +++ b/lib/src/RoomList.dart @@ -27,13 +27,10 @@ import 'dart:core'; import 'package:famedlysdk/src/State.dart'; import 'Client.dart'; -import 'Event.dart'; import 'Room.dart'; import 'User.dart'; import 'sync/EventUpdate.dart'; import 'sync/RoomUpdate.dart'; -import 'utils/ChatTime.dart'; -import 'utils/MxContent.dart'; /// Represents a list of rooms for this client, which will automatically update /// itself and call the [onUpdate], [onInsert] and [onDelete] callbacks. To get diff --git a/lib/src/State.dart b/lib/src/State.dart index 15b8eef1..52dee1f0 100644 --- a/lib/src/State.dart +++ b/lib/src/State.dart @@ -36,7 +36,7 @@ class State extends RawEvent { /// the overwriting semantics for this piece of room state. final String stateKey; - User get stateKeyUser => room.states[stateKey] ?? User(stateKey); + User get stateKeyUser => room.states[stateKey] ?? User(senderId: stateKey); State( {this.prevContent, @@ -95,4 +95,16 @@ class State extends RawEvent { /// The unique key of this event. For events with a [stateKey], it will be the /// stateKey. Otherwise it will be the [type] as a string. String get key => stateKey == null || stateKey.isEmpty ? typeKey : stateKey; + + User get asUser => User( + stateKey: stateKey, + prevContent: prevContent, + content: content, + typeKey: typeKey, + eventId: eventId, + roomId: roomId, + senderId: senderId, + time: time, + unsigned: unsigned, + room: room); } diff --git a/lib/src/User.dart b/lib/src/User.dart index 7a10ab0b..835a982c 100644 --- a/lib/src/User.dart +++ b/lib/src/User.dart @@ -24,6 +24,7 @@ import 'package:famedlysdk/src/Room.dart'; import 'package:famedlysdk/src/State.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart'; +import 'package:famedlysdk/src/utils/ChatTime.dart'; import 'package:famedlysdk/src/utils/MxContent.dart'; import 'Connection.dart'; @@ -32,7 +33,28 @@ enum Membership { join, invite, leave, ban } /// Represents a Matrix User which may be a participant in a Matrix Room. class User extends State { - User(String userId) : super(senderId: userId); + User( + {dynamic prevContent, + String stateKey, + dynamic content, + String typeKey, + String eventId, + String roomId, + String senderId, + ChatTime time, + dynamic unsigned, + Room room}) + : super( + stateKey: stateKey, + prevContent: prevContent, + content: content, + typeKey: typeKey, + eventId: eventId, + roomId: roomId, + senderId: senderId, + time: time, + unsigned: unsigned, + room: room); /// The full qualified Matrix ID in the format @username:server.abc. String get id => stateKey; diff --git a/lib/src/utils/ChatTime.dart b/lib/src/utils/ChatTime.dart index bd77d6d1..f53c0581 100644 --- a/lib/src/utils/ChatTime.dart +++ b/lib/src/utils/ChatTime.dart @@ -59,7 +59,6 @@ class ChatTime { return toTimeString(); } else if (sameWeek) { switch (dateTime.weekday) { - // TODO: Needs localization case 1: return "Montag"; case 2: diff --git a/test/Client_test.dart b/test/Client_test.dart index 11183ae5..855da000 100644 --- a/test/Client_test.dart +++ b/test/Client_test.dart @@ -23,8 +23,10 @@ import 'dart:async'; +import 'package:famedlysdk/src/AccountData.dart'; import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Connection.dart'; +import 'package:famedlysdk/src/Presence.dart'; import 'package:famedlysdk/src/User.dart'; import 'package:famedlysdk/src/requests/SetPushersRequest.dart'; import 'package:famedlysdk/src/responses/ErrorResponse.dart'; @@ -61,6 +63,15 @@ void main() { Future errorFuture = matrix.connection.onError.stream.first; + int presenceCounter = 0; + int accountDataCounter = 0; + matrix.onPresence = (Presence data) { + presenceCounter++; + }; + matrix.onAccountData = (AccountData data) { + accountDataCounter++; + }; + final bool checkResp1 = await matrix.checkServer("https://fakeserver.wrongaddress"); final bool checkResp2 = @@ -110,7 +121,8 @@ void main() { expect(matrix.accountData.length, 2); expect(matrix.getDirectChatFromUserId("@bob:example.com"), - "!abcdefgh:example.com"); + "!726s6s6q:example.com"); + expect(matrix.roomList.rooms[1].directChatMatrixID, "@bob:example.com"); expect(matrix.directChats, matrix.accountData["m.direct"].content); expect(matrix.presences.length, 1); expect(matrix.roomList.rooms.length, 2); @@ -118,7 +130,11 @@ void main() { "#famedlyContactDiscovery:${matrix.userID.split(":")[1]}"); final List contacts = await matrix.loadFamedlyContacts(); expect(contacts.length, 1); - expect(contacts[0].senderId, "@alice:example.com"); + expect(contacts[0].senderId, "@alice:example.org"); + expect( + matrix.presences["@alice:example.com"].content["presence"], "online"); + expect(presenceCounter, 1); + expect(accountDataCounter, 2); }); test('Try to get ErrorResponse', () async { @@ -184,36 +200,39 @@ void main() { List eventUpdateList = await eventUpdateListFuture; - expect(eventUpdateList.length, 7); + expect(eventUpdateList.length, 8); - expect(eventUpdateList[0].eventType == "m.room.member", true); - expect(eventUpdateList[0].roomID == "!726s6s6q:example.com", true); - expect(eventUpdateList[0].type == "state", true); + expect(eventUpdateList[0].eventType, "m.room.member"); + expect(eventUpdateList[0].roomID, "!726s6s6q:example.com"); + expect(eventUpdateList[0].type, "state"); - expect(eventUpdateList[1].eventType == "m.room.member", true); - expect(eventUpdateList[1].roomID == "!726s6s6q:example.com", true); - expect(eventUpdateList[1].type == "timeline", true); + expect(eventUpdateList[1].eventType, "m.room.canonical_alias"); + expect(eventUpdateList[1].roomID, "!726s6s6q:example.com"); + expect(eventUpdateList[1].type, "state"); - expect(eventUpdateList[2].eventType == "m.room.message", true); - expect(eventUpdateList[2].roomID == "!726s6s6q:example.com", true); - expect(eventUpdateList[2].type == "timeline", true); + expect(eventUpdateList[2].eventType, "m.room.member"); + expect(eventUpdateList[2].roomID, "!726s6s6q:example.com"); + expect(eventUpdateList[2].type, "timeline"); - expect(eventUpdateList[3].eventType == "m.tag", true); - expect(eventUpdateList[3].roomID == "!726s6s6q:example.com", true); - expect(eventUpdateList[3].type == "account_data", true); + expect(eventUpdateList[3].eventType, "m.room.message"); + expect(eventUpdateList[3].roomID, "!726s6s6q:example.com"); + expect(eventUpdateList[3].type, "timeline"); - expect(eventUpdateList[4].eventType == "org.example.custom.room.config", - true); - expect(eventUpdateList[4].roomID == "!726s6s6q:example.com", true); - expect(eventUpdateList[4].type == "account_data", true); + expect(eventUpdateList[4].eventType, "m.tag"); + expect(eventUpdateList[4].roomID, "!726s6s6q:example.com"); + expect(eventUpdateList[4].type, "account_data"); - expect(eventUpdateList[5].eventType == "m.room.name", true); - expect(eventUpdateList[5].roomID == "!696r7674:example.com", true); - expect(eventUpdateList[5].type == "invite_state", true); + expect(eventUpdateList[5].eventType, "org.example.custom.room.config"); + expect(eventUpdateList[5].roomID, "!726s6s6q:example.com"); + expect(eventUpdateList[5].type, "account_data"); - expect(eventUpdateList[6].eventType == "m.room.member", true); - expect(eventUpdateList[6].roomID == "!696r7674:example.com", true); - expect(eventUpdateList[6].type == "invite_state", true); + expect(eventUpdateList[6].eventType, "m.room.name"); + expect(eventUpdateList[6].roomID, "!696r7674:example.com"); + expect(eventUpdateList[6].type, "invite_state"); + + expect(eventUpdateList[7].eventType, "m.room.member"); + expect(eventUpdateList[7].roomID, "!696r7674:example.com"); + expect(eventUpdateList[7].type, "invite_state"); }); test('User Update Test', () async { @@ -244,8 +263,8 @@ void main() { test('createGroup', () async { final List users = [ - User("@alice:fakeServer.notExisting"), - User("@bob:fakeServer.notExisting") + User(senderId: "@alice:fakeServer.notExisting"), + User(senderId: "@bob:fakeServer.notExisting") ]; final String newID = await matrix.createGroup(users); expect(newID, "!1234:fakeServer.notExisting"); diff --git a/test/FakeMatrixApi.dart b/test/FakeMatrixApi.dart index bda93ad8..f63f445a 100644 --- a/test/FakeMatrixApi.dart +++ b/test/FakeMatrixApi.dart @@ -355,7 +355,7 @@ class FakeMatrixApi extends MockClient { { "content": { "@bob:example.com": [ - "!abcdefgh:example.com", + "!726s6s6q:example.com", "!hgfedcba:example.com" ] }, diff --git a/test/Room_test.dart b/test/Room_test.dart index b70be7fe..897d1192 100644 --- a/test/Room_test.dart +++ b/test/Room_test.dart @@ -125,12 +125,8 @@ void main() { expect(room.fullyRead, fullyRead); expect(room.notificationSettings, notificationSettings); expect(room.directChatMatrixID, ""); - expect(room.draft, ""); expect(room.canonicalAlias, canonicalAlias); expect(room.prev_batch, ""); - expect(room.guestAccess, guestAccess); - expect(room.historyVisibility, historyVisibility); - expect(room.joinRules, joinRules); expect(room.lastMessage, body); expect(room.timeCreated.toTimeStamp() >= now, true); room.powerLevels.forEach((String key, int value) { @@ -167,7 +163,7 @@ void main() { test("getEventByID", () async { final Event event = await room.getEventById("1234"); - expect(event.id, "143273582443PhrSn:example.org"); + expect(event.eventId, "143273582443PhrSn:example.org"); }); }); } diff --git a/test/Timeline_test.dart b/test/Timeline_test.dart index b99c6494..3b37c6e7 100644 --- a/test/Timeline_test.dart +++ b/test/Timeline_test.dart @@ -87,10 +87,9 @@ void main() { expect(insertList, [0, 0]); expect(insertList.length, timeline.events.length); expect(timeline.events.length, 2); - expect(timeline.events[0].id, "1"); + expect(timeline.events[0].eventId, "1"); expect(timeline.events[0].sender.id, "@alice:example.com"); expect(timeline.events[0].time.toTimeStamp(), testTimeStamp); - expect(timeline.events[0].environment, "m.room.message"); expect(timeline.events[0].getBody(), "Testcase"); expect(timeline.events[0].time > timeline.events[1].time, true); }); @@ -103,7 +102,7 @@ void main() { expect(updateCount, 4); expect(insertList, [0, 0, 0]); expect(insertList.length, timeline.events.length); - expect(timeline.events[0].id, "42"); + expect(timeline.events[0].eventId, "42"); expect(timeline.events[0].status, 1); client.connection.onEvent.add(EventUpdate( @@ -125,7 +124,7 @@ void main() { expect(updateCount, 5); expect(insertList, [0, 0, 0]); expect(insertList.length, timeline.events.length); - expect(timeline.events[0].id, "42"); + expect(timeline.events[0].eventId, "42"); expect(timeline.events[0].status, 2); }); @@ -189,9 +188,9 @@ void main() { expect(updateCount, 19); expect(timeline.events.length, 9); - expect(timeline.events[6].id, "1143273582443PhrSn:example.org"); - expect(timeline.events[7].id, "2143273582443PhrSn:example.org"); - expect(timeline.events[8].id, "3143273582443PhrSn:example.org"); + expect(timeline.events[6].eventId, "1143273582443PhrSn:example.org"); + expect(timeline.events[7].eventId, "2143273582443PhrSn:example.org"); + expect(timeline.events[8].eventId, "3143273582443PhrSn:example.org"); expect(room.prev_batch, "t47409-4357353_219380_26003_2265"); }); }); diff --git a/test/User_test.dart b/test/User_test.dart index dcd8b594..4d5d6416 100644 --- a/test/User_test.dart +++ b/test/User_test.dart @@ -21,6 +21,7 @@ * along with famedlysdk. If not, see . */ +import 'package:famedlysdk/src/State.dart'; import 'package:famedlysdk/src/User.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -42,20 +43,19 @@ void main() { "power_level": powerLevel, }; - User user = User.fromJson(jsonObj, null); + User user = State.fromJson(jsonObj, null).asUser; expect(user.id, id); expect(user.membership, membership); expect(user.displayName, displayName); expect(user.avatarUrl.mxc, avatarUrl); - expect(user.powerLevel, powerLevel); expect(user.calcDisplayname(), displayName); }); test("calcDisplayname", () async { - final User user1 = User("@alice:example.com"); - final User user2 = User("@alice:example.com", displayName: "SuperAlice"); - final User user3 = User("@alice:example.com", displayName: ""); + final User user1 = User(senderId: "@alice:example.com"); + final User user2 = User(senderId: "@alice:example.com"); + final User user3 = User(senderId: "@alice:example.com"); expect(user1.calcDisplayname(), "alice"); expect(user2.calcDisplayname(), "SuperAlice"); expect(user3.calcDisplayname(), "alice");