From fd7302bcdeb675e55c49e16fbee834816788ab31 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 18 Jun 2019 12:06:55 +0200 Subject: [PATCH] More tests --- lib/src/Connection.dart | 1 + lib/src/Room.dart | 4 ++-- test/FakeMatrixApi.dart | 21 ++++++++++++++++++++- test/Room_test.dart | 42 ++++++++++++++++++++++++++++++++--------- 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/lib/src/Connection.dart b/lib/src/Connection.dart index c6b6bb9c..77d3c25b 100644 --- a/lib/src/Connection.dart +++ b/lib/src/Connection.dart @@ -263,6 +263,7 @@ class Connection { _syncRequest = jsonRequest(type: "GET", action: action); final int hash = _syncRequest.hashCode; final syncResp = await _syncRequest; + if (hash != _syncRequest.hashCode) return; if (syncResp is ErrorResponse) { onError.add(syncResp); await Future.delayed(Duration(seconds: syncErrorTimeoutSec), () {}); diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 9a8b373e..49a1a849 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -437,10 +437,10 @@ class Room { /// Request the full list of participants from the server. The local list /// from the store is not complete if the client uses lazy loading. - Future> requestParticipants(Client matrix) async { + Future> requestParticipants() async { List participants = []; - dynamic res = await matrix.connection + dynamic res = await client.connection .jsonRequest(type: "GET", action: "/client/r0/rooms/${id}/members"); if (res is ErrorResponse || !(res["chunk"] is List)) return participants; diff --git a/test/FakeMatrixApi.dart b/test/FakeMatrixApi.dart index 315be5dc..10e3411b 100644 --- a/test/FakeMatrixApi.dart +++ b/test/FakeMatrixApi.dart @@ -71,6 +71,24 @@ class FakeMatrixApi extends MockClient { {"type": "m.login.password"} ] }, + "/client/r0/rooms/!localpart:server.abc/members": (var req) => { + "chunk": [ + { + "content": { + "membership": "join", + "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF", + "displayname": "Alice Margatroid" + }, + "type": "m.room.member", + "event_id": "§143273582443PhrSn:example.org", + "room_id": "!636q39766251:example.com", + "sender": "@example:example.org", + "origin_server_ts": 1432735824653, + "unsigned": {"age": 1234}, + "state_key": "@alice:example.org" + } + ] + }, "/client/r0/sync": (var req) => { "next_batch": Random().nextDouble().toString(), "presence": { @@ -210,7 +228,8 @@ class FakeMatrixApi extends MockClient { "/client/r0/logout/all": (var reqI) => {}, "/client/r0/createRoom": (var reqI) => { "room_id": "!1234:fakeServer.notExisting", - } + }, + "/client/r0/rooms/!localpart:server.abc/read_markers": (var reqI) => {}, }, "PUT": {}, "DELETE": { diff --git a/test/Room_test.dart b/test/Room_test.dart index 91d2c2bf..2ee48353 100644 --- a/test/Room_test.dart +++ b/test/Room_test.dart @@ -24,22 +24,29 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:famedlysdk/src/Room.dart'; import 'package:famedlysdk/src/Client.dart'; -import 'dart:async'; +import 'package:famedlysdk/src/User.dart'; import 'FakeMatrixApi.dart'; void main() { + Client matrix; + Room room; + /// All Tests related to the Event group("Room", () { - Client matrix = Client("testclient"); - matrix.connection.httpClient = FakeMatrixApi(); - matrix.homeserver = "https://fakeServer.notExisting"; - Room room; + test('Login', () async { + matrix = Client("testclient", debug: true); + matrix.connection.httpClient = FakeMatrixApi(); + + final bool checkResp = + await matrix.checkServer("https://fakeServer.notExisting"); + + final bool loginResp = await matrix.login("test", "1234"); + + expect(checkResp, true); + expect(loginResp, true); + }); test("Create from json", () async { - Client matrix = Client("testclient"); - matrix.connection.httpClient = FakeMatrixApi(); - matrix.homeserver = "https://fakeServer.notExisting"; - final String id = "!localpart:server.abc"; final String name = "My Room"; final String membership = "join"; @@ -116,5 +123,22 @@ void main() { expect(value, 0); }); }); + + test("sendReadReceipt", () async { + final dynamic resp = + await room.sendReadReceipt("§1234:fakeServer.notExisting"); + expect(resp, {}); + }); + + test("requestParticipants", () async { + final List participants = await room.requestParticipants(); + expect(participants.length, 1); + User user = participants[0]; + expect(user.id, "@alice:example.org"); + expect(user.displayName, "Alice Margatroid"); + expect(user.membership, "join"); + expect(user.avatarUrl.mxc, "mxc://example.org/SEsfnsuifSDFSSEF"); + expect(user.room.id, "!localpart:server.abc"); + }); }); }