2019-06-21 10:18:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Zender & Kurtz GbR.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Christian Pauly <krille@famedly.com>
|
|
|
|
* Marcel Radzio <mtrnord@famedly.com>
|
|
|
|
*
|
|
|
|
* This file is part of famedlysdk.
|
|
|
|
*
|
|
|
|
* famedlysdk is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* famedlysdk is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-10-20 09:44:14 +00:00
|
|
|
import 'package:famedlysdk/src/RoomAccountData.dart';
|
2019-10-04 09:44:32 +00:00
|
|
|
import 'package:test/test.dart';
|
2019-06-21 10:18:54 +00:00
|
|
|
import 'package:famedlysdk/src/Client.dart';
|
|
|
|
import 'package:famedlysdk/src/Room.dart';
|
|
|
|
import 'package:famedlysdk/src/Timeline.dart';
|
|
|
|
import 'package:famedlysdk/src/sync/EventUpdate.dart';
|
|
|
|
import 'package:famedlysdk/src/utils/ChatTime.dart';
|
2019-06-26 14:36:34 +00:00
|
|
|
import 'FakeMatrixApi.dart';
|
2019-06-21 10:18:54 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
/// All Tests related to the MxContent
|
|
|
|
group("Timeline", () {
|
|
|
|
final String roomID = "!1234:example.com";
|
|
|
|
final testTimeStamp = ChatTime.now().toTimeStamp();
|
|
|
|
int updateCount = 0;
|
|
|
|
List<int> insertList = [];
|
|
|
|
|
2019-06-26 14:36:34 +00:00
|
|
|
Client client = Client("testclient", debug: true);
|
|
|
|
client.connection.httpClient = FakeMatrixApi();
|
|
|
|
client.homeserver = "https://fakeServer.notExisting";
|
2019-06-21 10:18:54 +00:00
|
|
|
|
2019-08-08 10:00:24 +00:00
|
|
|
Room room = Room(
|
|
|
|
id: roomID,
|
|
|
|
client: client,
|
|
|
|
prev_batch: "1234",
|
|
|
|
states: {},
|
|
|
|
roomAccountData: {});
|
2019-06-26 14:36:34 +00:00
|
|
|
Timeline timeline = Timeline(
|
|
|
|
room: room,
|
|
|
|
events: [],
|
|
|
|
onUpdate: () {
|
|
|
|
updateCount++;
|
|
|
|
},
|
|
|
|
onInsert: (int insertID) {
|
|
|
|
insertList.add(insertID);
|
|
|
|
});
|
2019-06-21 10:18:54 +00:00
|
|
|
|
2019-06-26 14:36:34 +00:00
|
|
|
test("Create", () async {
|
2019-06-21 10:18:54 +00:00
|
|
|
client.connection.onEvent.add(EventUpdate(
|
|
|
|
type: "timeline",
|
|
|
|
roomID: roomID,
|
|
|
|
eventType: "m.room.message",
|
|
|
|
content: {
|
|
|
|
"type": "m.room.message",
|
|
|
|
"content": {"msgtype": "m.text", "body": "Testcase"},
|
|
|
|
"sender": "@alice:example.com",
|
|
|
|
"status": 2,
|
2019-06-27 07:44:37 +00:00
|
|
|
"event_id": "1",
|
2019-06-21 10:18:54 +00:00
|
|
|
"origin_server_ts": testTimeStamp
|
|
|
|
}));
|
|
|
|
|
2019-06-21 11:30:39 +00:00
|
|
|
client.connection.onEvent.add(EventUpdate(
|
|
|
|
type: "timeline",
|
|
|
|
roomID: roomID,
|
|
|
|
eventType: "m.room.message",
|
|
|
|
content: {
|
|
|
|
"type": "m.room.message",
|
|
|
|
"content": {"msgtype": "m.text", "body": "Testcase"},
|
|
|
|
"sender": "@alice:example.com",
|
|
|
|
"status": 2,
|
2019-06-27 07:44:37 +00:00
|
|
|
"event_id": "2",
|
2019-06-21 11:30:39 +00:00
|
|
|
"origin_server_ts": testTimeStamp - 1000
|
|
|
|
}));
|
|
|
|
|
2019-06-21 10:18:54 +00:00
|
|
|
expect(timeline.sub != null, true);
|
|
|
|
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
2019-06-21 11:30:39 +00:00
|
|
|
expect(updateCount, 2);
|
|
|
|
expect(insertList, [0, 0]);
|
2019-06-26 18:03:20 +00:00
|
|
|
expect(insertList.length, timeline.events.length);
|
2019-06-21 11:30:39 +00:00
|
|
|
expect(timeline.events.length, 2);
|
2019-08-08 08:31:39 +00:00
|
|
|
expect(timeline.events[0].eventId, "1");
|
2019-06-21 10:18:54 +00:00
|
|
|
expect(timeline.events[0].sender.id, "@alice:example.com");
|
|
|
|
expect(timeline.events[0].time.toTimeStamp(), testTimeStamp);
|
|
|
|
expect(timeline.events[0].getBody(), "Testcase");
|
2019-06-21 11:30:39 +00:00
|
|
|
expect(timeline.events[0].time > timeline.events[1].time, true);
|
2019-10-20 09:44:14 +00:00
|
|
|
expect(timeline.events[0].receipts, []);
|
|
|
|
|
|
|
|
room.roomAccountData["m.receipt"] = RoomAccountData.fromJson({
|
|
|
|
"type": "m.receipt",
|
|
|
|
"content": {
|
2019-10-25 08:02:56 +00:00
|
|
|
"@alice:example.com": {
|
|
|
|
"event_id": "1",
|
|
|
|
"ts": 1436451550453,
|
2019-10-20 09:44:14 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"room_id": roomID,
|
|
|
|
}, room);
|
|
|
|
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
|
|
|
expect(timeline.events[0].receipts.length, 1);
|
|
|
|
expect(timeline.events[0].receipts[0].user.id, "@alice:example.com");
|
2019-06-21 10:18:54 +00:00
|
|
|
});
|
2019-06-26 14:36:34 +00:00
|
|
|
|
|
|
|
test("Send message", () async {
|
|
|
|
room.sendTextEvent("test", txid: "1234");
|
|
|
|
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
|
|
|
expect(updateCount, 4);
|
|
|
|
expect(insertList, [0, 0, 0]);
|
2019-06-26 18:03:20 +00:00
|
|
|
expect(insertList.length, timeline.events.length);
|
2019-08-08 08:31:39 +00:00
|
|
|
expect(timeline.events[0].eventId, "42");
|
2019-06-26 14:36:34 +00:00
|
|
|
expect(timeline.events[0].status, 1);
|
|
|
|
|
|
|
|
client.connection.onEvent.add(EventUpdate(
|
|
|
|
type: "timeline",
|
|
|
|
roomID: roomID,
|
|
|
|
eventType: "m.room.message",
|
|
|
|
content: {
|
|
|
|
"type": "m.room.message",
|
|
|
|
"content": {"msgtype": "m.text", "body": "test"},
|
|
|
|
"sender": "@alice:example.com",
|
|
|
|
"status": 2,
|
2019-06-27 07:44:37 +00:00
|
|
|
"event_id": "42",
|
2019-06-26 17:03:15 +00:00
|
|
|
"unsigned": {"transaction_id": "1234"},
|
2019-06-26 14:36:34 +00:00
|
|
|
"origin_server_ts": DateTime.now().millisecondsSinceEpoch
|
|
|
|
}));
|
|
|
|
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
|
|
|
expect(updateCount, 5);
|
|
|
|
expect(insertList, [0, 0, 0]);
|
2019-06-26 18:03:20 +00:00
|
|
|
expect(insertList.length, timeline.events.length);
|
2019-08-08 08:31:39 +00:00
|
|
|
expect(timeline.events[0].eventId, "42");
|
2019-06-26 14:36:34 +00:00
|
|
|
expect(timeline.events[0].status, 2);
|
|
|
|
});
|
2019-06-26 14:37:49 +00:00
|
|
|
|
|
|
|
test("Send message with error", () async {
|
2019-06-26 18:03:20 +00:00
|
|
|
client.connection.onEvent.add(EventUpdate(
|
|
|
|
type: "timeline",
|
|
|
|
roomID: roomID,
|
|
|
|
eventType: "m.room.message",
|
|
|
|
content: {
|
|
|
|
"type": "m.room.message",
|
|
|
|
"content": {"msgtype": "m.text", "body": "Testcase"},
|
|
|
|
"sender": "@alice:example.com",
|
|
|
|
"status": 0,
|
2019-06-27 07:44:37 +00:00
|
|
|
"event_id": "abc",
|
2019-06-26 18:03:20 +00:00
|
|
|
"origin_server_ts": testTimeStamp
|
|
|
|
}));
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
2019-06-26 14:37:49 +00:00
|
|
|
room.sendTextEvent("test", txid: "errortxid");
|
2019-06-26 18:03:20 +00:00
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
room.sendTextEvent("test", txid: "errortxid2");
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
room.sendTextEvent("test", txid: "errortxid3");
|
2019-06-26 14:37:49 +00:00
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
2019-06-26 18:03:20 +00:00
|
|
|
expect(updateCount, 12);
|
|
|
|
expect(insertList, [0, 0, 0, 0, 0, 0, 0]);
|
|
|
|
expect(insertList.length, timeline.events.length);
|
2019-06-26 14:37:49 +00:00
|
|
|
expect(timeline.events[0].status, -1);
|
2019-06-26 18:03:20 +00:00
|
|
|
expect(timeline.events[1].status, -1);
|
|
|
|
expect(timeline.events[2].status, -1);
|
2019-06-26 14:37:49 +00:00
|
|
|
});
|
2019-06-27 08:12:39 +00:00
|
|
|
|
|
|
|
test("Remove message", () async {
|
|
|
|
timeline.events[0].remove();
|
|
|
|
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
|
|
|
expect(updateCount, 13);
|
|
|
|
|
|
|
|
expect(insertList, [0, 0, 0, 0, 0, 0, 0]);
|
|
|
|
expect(timeline.events.length, 6);
|
|
|
|
expect(timeline.events[0].status, -1);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("Resend message", () async {
|
|
|
|
timeline.events[0].sendAgain(txid: "1234");
|
|
|
|
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
|
|
|
expect(updateCount, 16);
|
|
|
|
|
|
|
|
expect(insertList, [0, 0, 0, 0, 0, 0, 0, 0]);
|
|
|
|
expect(timeline.events.length, 6);
|
|
|
|
expect(timeline.events[0].status, 1);
|
|
|
|
});
|
2019-06-28 06:39:43 +00:00
|
|
|
|
|
|
|
test("Request history", () async {
|
|
|
|
await room.requestHistory();
|
|
|
|
|
|
|
|
await new Future.delayed(new Duration(milliseconds: 50));
|
|
|
|
|
|
|
|
expect(updateCount, 19);
|
|
|
|
expect(timeline.events.length, 9);
|
2019-08-08 08:31:39 +00:00
|
|
|
expect(timeline.events[6].eventId, "1143273582443PhrSn:example.org");
|
|
|
|
expect(timeline.events[7].eventId, "2143273582443PhrSn:example.org");
|
|
|
|
expect(timeline.events[8].eventId, "3143273582443PhrSn:example.org");
|
2019-06-28 10:32:33 +00:00
|
|
|
expect(room.prev_batch, "t47409-4357353_219380_26003_2265");
|
2019-06-28 06:39:43 +00:00
|
|
|
});
|
2019-06-21 10:18:54 +00:00
|
|
|
});
|
|
|
|
}
|