From b4ae8b47f8dc73c9acb1a0cf45c67b6d6f43be75 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Thu, 8 Aug 2019 11:54:39 +0200 Subject: [PATCH] [Tests] Fix user tests --- lib/src/RawEvent.dart | 2 +- lib/src/Room.dart | 4 ++-- lib/src/State.dart | 4 ++-- lib/src/User.dart | 23 ++++++++++++++++++++++- test/Client_test.dart | 4 ++-- test/Room_test.dart | 26 +++++++++++++------------- test/User_test.dart | 24 +++++++++++++++--------- 7 files changed, 57 insertions(+), 30 deletions(-) diff --git a/lib/src/RawEvent.dart b/lib/src/RawEvent.dart index 8deff6f..c4bf7b5 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: senderId); + User get sender => room.states[senderId] ?? User(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 630aa49..07b0f0d 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -76,7 +76,7 @@ class Room { for (int i = 0; i < mHeroes.length; i++) { User hero = states[mHeroes[i]] != null ? states[mHeroes[i]].asUser - : User(stateKey: mHeroes[i]); + : User(mHeroes[i]); displayname += hero.calcDisplayname() + ", "; } return displayname.substring(0, displayname.length - 2); @@ -155,7 +155,7 @@ class Room { if (mHeroes.length > 0) { String displayname = ""; for (int i = 0; i < mHeroes.length; i++) - displayname += User(senderId: mHeroes[i]).calcDisplayname() + ", "; + displayname += User(mHeroes[i]).calcDisplayname() + ", "; return displayname.substring(0, displayname.length - 2); } return "Empty chat"; diff --git a/lib/src/State.dart b/lib/src/State.dart index 52dee1f..bf083fc 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(senderId: stateKey); + User get stateKeyUser => room.states[stateKey] ?? User(stateKey); State( {this.prevContent, @@ -96,7 +96,7 @@ class State extends RawEvent { /// stateKey. Otherwise it will be the [type] as a string. String get key => stateKey == null || stateKey.isEmpty ? typeKey : stateKey; - User get asUser => User( + User get asUser => User.fromState( stateKey: stateKey, prevContent: prevContent, content: content, diff --git a/lib/src/User.dart b/lib/src/User.dart index e664bd0..11b5206 100644 --- a/lib/src/User.dart +++ b/lib/src/User.dart @@ -33,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( + factory User( + String id, { + String membership, + String displayName, + String avatarUrl, + Room room, + }) { + Map content = {}; + if (membership != null) content["membership"] = membership; + if (displayName != null) content["displayname"] = displayName; + if (avatarUrl != null) content["avatar_url"] = avatarUrl; + return User.fromState( + stateKey: id, + content: content, + typeKey: "m.room.member", + roomId: room?.id, + room: room, + time: ChatTime.now(), + ); + } + + User.fromState( {dynamic prevContent, String stateKey, dynamic content, diff --git a/test/Client_test.dart b/test/Client_test.dart index 855da00..8239e0b 100644 --- a/test/Client_test.dart +++ b/test/Client_test.dart @@ -263,8 +263,8 @@ void main() { test('createGroup', () async { final List users = [ - User(senderId: "@alice:fakeServer.notExisting"), - User(senderId: "@bob:fakeServer.notExisting") + User("@alice:fakeServer.notExisting"), + User("@bob:fakeServer.notExisting") ]; final String newID = await matrix.createGroup(users); expect(newID, "!1234:fakeServer.notExisting"); diff --git a/test/Room_test.dart b/test/Room_test.dart index 066b575..0b251a1 100644 --- a/test/Room_test.dart +++ b/test/Room_test.dart @@ -229,21 +229,21 @@ void main() { final dynamic resp = await room.addToDirectChat("Testname"); expect(resp, {}); }); - }); - test("getTimeline", () async { - final Timeline timeline = await room.getTimeline(); - expect(timeline.events, []); - }); + test("getTimeline", () async { + final Timeline timeline = await room.getTimeline(); + expect(timeline.events, []); + }); - test("loadEvents", () async { - final List events = await room.loadEvents(); - expect(events, []); - }); + test("loadEvents", () async { + final List events = await room.loadEvents(); + expect(events, []); + }); - test("getUserByMXID", () async { - final User user = await room.getUserByMXID("@getme:example.com"); - expect(user.stateKey, "@getme:example.com"); - expect(user.calcDisplayname(), "You got me"); + test("getUserByMXID", () async { + final User user = await room.getUserByMXID("@getme:example.com"); + expect(user.stateKey, "@getme:example.com"); + expect(user.calcDisplayname(), "You got me"); + }); }); } diff --git a/test/User_test.dart b/test/User_test.dart index 4d5d641..7e192c2 100644 --- a/test/User_test.dart +++ b/test/User_test.dart @@ -33,14 +33,20 @@ void main() { final Membership membership = Membership.join; final String displayName = "Alice"; final String avatarUrl = ""; - final int powerLevel = 50; final Map jsonObj = { - "matrix_id": id, - "displayname": displayName, - "avatar_url": avatarUrl, - "membership": membership.toString().split('.').last, - "power_level": powerLevel, + "content": { + "membership": "join", + "avatar_url": avatarUrl, + "displayname": displayName + }, + "type": "m.room.member", + "event_id": "143273582443PhrSn:example.org", + "room_id": "!636q39766251:example.com", + "sender": id, + "origin_server_ts": 1432735824653, + "unsigned": {"age": 1234}, + "state_key": id }; User user = State.fromJson(jsonObj, null).asUser; @@ -53,9 +59,9 @@ void main() { }); test("calcDisplayname", () async { - final User user1 = User(senderId: "@alice:example.com"); - final User user2 = User(senderId: "@alice:example.com"); - final User user3 = User(senderId: "@alice:example.com"); + final User user1 = User("@alice:example.com"); + final User user2 = User("@SuperAlice:example.com"); + final User user3 = User("@alice:example.com"); expect(user1.calcDisplayname(), "alice"); expect(user2.calcDisplayname(), "SuperAlice"); expect(user3.calcDisplayname(), "alice");