[Tests] Refactoring

This commit is contained in:
Christian Pauly 2019-08-08 10:31:39 +02:00
parent bb41db7f14
commit df0cc1d273
12 changed files with 128 additions and 58 deletions

View File

@ -99,14 +99,22 @@ class Client {
/// Presences of users by a given matrix ID
Map<String, Presence> 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);

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);
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.

View File

@ -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<String, dynamic>) {
client.directChats.forEach((String userId, dynamic roomIds) {
if (roomIds is List<dynamic>) {
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);
}

View File

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

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(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);
}

View File

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

View File

@ -59,7 +59,6 @@ class ChatTime {
return toTimeString();
} else if (sameWeek) {
switch (dateTime.weekday) {
// TODO: Needs localization
case 1:
return "Montag";
case 2:

View File

@ -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<ErrorResponse> 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<User> 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<EventUpdate> 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<User> 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");

View File

@ -355,7 +355,7 @@ class FakeMatrixApi extends MockClient {
{
"content": {
"@bob:example.com": [
"!abcdefgh:example.com",
"!726s6s6q:example.com",
"!hgfedcba:example.com"
]
},

View File

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

View File

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

View File

@ -21,6 +21,7 @@
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
*/
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");