[Tests] Fix user tests

This commit is contained in:
Christian Pauly 2019-08-08 11:54:39 +02:00
parent 1617a8b7d5
commit b4ae8b47f8
7 changed files with 57 additions and 30 deletions

View file

@ -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.

View file

@ -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";

View file

@ -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,

View file

@ -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<String, String> 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,

View file

@ -263,8 +263,8 @@ void main() {
test('createGroup', () async {
final List<User> 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");

View file

@ -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<Event> events = await room.loadEvents();
expect(events, []);
});
test("loadEvents", () async {
final List<Event> 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");
});
});
}

View file

@ -33,14 +33,20 @@ void main() {
final Membership membership = Membership.join;
final String displayName = "Alice";
final String avatarUrl = "";
final int powerLevel = 50;
final Map<String, dynamic> 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");