From 31bff2d00e54f24948c8adc5ffddc7e8eebef562 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 28 Jun 2019 08:39:43 +0200 Subject: [PATCH] [Timeline] Implement tests for request history function. --- lib/src/Room.dart | 19 +++++++++++--- test/FakeMatrixApi.dart | 58 +++++++++++++++++++++++++++++++++++++++++ test/Timeline_test.dart | 12 +++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 9a205ca4..d189a8c7 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -293,7 +293,7 @@ class Room { /// Request more previous events from the server. Future requestHistory({int historyCount = 100}) async { - final dynamic resp = client.connection.jsonRequest( + final dynamic resp = await client.connection.jsonRequest( type: "GET", action: "/client/r0/rooms/$id/messages", data: {"from": prev_batch, "dir": "b", "limit": historyCount}); @@ -305,12 +305,12 @@ class Room { resp["end"] is String)) return; List history = resp["chunk"]; - client.store.transaction(() { + client.store?.transaction(() { for (int i = 0; i < history.length; i++) { EventUpdate eventUpdate = EventUpdate( - eventType: "history", + type: "history", roomID: id, - type: history[i]["type"], + eventType: history[i]["type"], content: history[i], ); client.connection.onEvent.add(eventUpdate); @@ -319,6 +319,17 @@ class Room { "UPDATE Rooms SET prev_batch=? WHERE id=?", [resp["end"], id]); } }); + if (client.store == null) { + for (int i = 0; i < history.length; i++) { + EventUpdate eventUpdate = EventUpdate( + type: "history", + roomID: id, + eventType: history[i]["type"], + content: history[i], + ); + client.connection.onEvent.add(eventUpdate); + } + } } /// Sets this room as a direct chat for this user. diff --git a/test/FakeMatrixApi.dart b/test/FakeMatrixApi.dart index c1dad41d..9e561c43 100644 --- a/test/FakeMatrixApi.dart +++ b/test/FakeMatrixApi.dart @@ -62,6 +62,64 @@ class FakeMatrixApi extends MockClient { static final Map> api = { "GET": { + "/client/r0/rooms/!1234:example.com/messages": (var req) => { + "start": "t47429-4392820_219380_26003_2265", + "end": "t47409-4357353_219380_26003_2265", + "chunk": [ + { + "content": { + "body": "This is an example text message", + "msgtype": "m.text", + "format": "org.matrix.custom.html", + "formatted_body": "This is an example text message" + }, + "type": "m.room.message", + "event_id": "3143273582443PhrSn:example.org", + "room_id": "!1234:example.com", + "sender": "@example:example.org", + "origin_server_ts": 1432735824653, + "unsigned": {"age": 1234} + }, + { + "content": {"name": "The room name"}, + "type": "m.room.name", + "event_id": "2143273582443PhrSn:example.org", + "room_id": "!1234:example.com", + "sender": "@example:example.org", + "origin_server_ts": 1432735824653, + "unsigned": {"age": 1234}, + "state_key": "" + }, + { + "content": { + "body": "Gangnam Style", + "url": "mxc://example.org/a526eYUSFFxlgbQYZmo442", + "info": { + "thumbnail_url": + "mxc://example.org/FHyPlCeYUSFFxlgbQYZmoEoe", + "thumbnail_info": { + "mimetype": "image/jpeg", + "size": 46144, + "w": 300, + "h": 300 + }, + "w": 480, + "h": 320, + "duration": 2140786, + "size": 1563685, + "mimetype": "video/mp4" + }, + "msgtype": "m.video" + }, + "type": "m.room.message", + "event_id": "1143273582443PhrSn:example.org", + "room_id": "!1234:example.com", + "sender": "@example:example.org", + "origin_server_ts": 1432735824653, + "unsigned": {"age": 1234} + } + ] + }, "/client/versions": (var req) => { "versions": ["r0.0.1", "r0.1.0", "r0.2.0", "r0.3.0", "r0.4.0"], "unstable_features": {"m.lazy_load_members": true}, diff --git a/test/Timeline_test.dart b/test/Timeline_test.dart index bd84dc3a..b66ed69f 100644 --- a/test/Timeline_test.dart +++ b/test/Timeline_test.dart @@ -187,5 +187,17 @@ void main() { expect(timeline.events[0].content["txid"], "1234"); expect(timeline.events[0].status, 1); }); + + test("Request history", () async { + await room.requestHistory(); + + await new Future.delayed(new Duration(milliseconds: 50)); + + 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"); + }); }); }