From 22231f87b7dd5b74591360db476fcee9ada84024 Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 10 Jun 2019 06:28:21 +0000 Subject: [PATCH] Tests for ChatTime and MxContent including bug fixes --- lib/src/Connection.dart | 2 +- lib/src/utils/ChatTime.dart | 44 +++++++++++++++++++++++-- lib/src/utils/MxContent.dart | 18 ++++++---- test/ChatTime_test.dart | 64 ++++++++++++++++++++++++++++++++++++ test/MxContent_test.dart | 45 +++++++++++++++++++++++++ 5 files changed, 163 insertions(+), 10 deletions(-) create mode 100644 test/ChatTime_test.dart create mode 100644 test/MxContent_test.dart diff --git a/lib/src/Connection.dart b/lib/src/Connection.dart index 009228f..3c62482 100644 --- a/lib/src/Connection.dart +++ b/lib/src/Connection.dart @@ -38,7 +38,7 @@ class Connection { Connection(this.client) { WidgetsBinding.instance - .addObserver(_LifecycleEventHandler(resumeCallBack: () { + ?.addObserver(_LifecycleEventHandler(resumeCallBack: () { _sync(); })); } diff --git a/lib/src/utils/ChatTime.dart b/lib/src/utils/ChatTime.dart index b3c3c37..bc96296 100644 --- a/lib/src/utils/ChatTime.dart +++ b/lib/src/utils/ChatTime.dart @@ -29,15 +29,20 @@ import 'package:intl/intl.dart'; class ChatTime { DateTime dateTime = DateTime.now(); + /// Insert with a timestamp [ts] which represents the milliseconds since + /// the Unix epoch. ChatTime(num ts) { if (ts != null) - dateTime = DateTime.fromMicrosecondsSinceEpoch(ts * 1000); + dateTime = DateTime.fromMillisecondsSinceEpoch(ts); } + /// Returns a ChatTime object which represents the current time. ChatTime.now() { dateTime = DateTime.now(); } + /// Returns [toTimeString()] if the ChatTime is today, the name of the week + /// day if the ChatTime is this week and a date string else. String toString() { DateTime now = DateTime.now(); @@ -74,18 +79,51 @@ class ChatTime { } } + /// Returns the milliseconds since the Unix epoch. num toTimeStamp() { - return dateTime.microsecondsSinceEpoch; + return dateTime.millisecondsSinceEpoch; } + operator <(ChatTime other) { + return this.toTimeStamp() < other.toTimeStamp(); + } + + operator >(ChatTime other) { + return this.toTimeStamp() > other.toTimeStamp(); + } + + operator >=(ChatTime other) { + return this.toTimeStamp() >= other.toTimeStamp(); + } + + operator <=(ChatTime other) { + return this.toTimeStamp() <= other.toTimeStamp(); + } + + operator ==(dynamic other) { + if (other is ChatTime) + return this.toTimeStamp() == other.toTimeStamp(); + else return false; + } + + /// Two message events can belong to the same environment. That means that they + /// don't need to display the time they were sent because they are close + /// enaugh. + static final minutesBetweenEnvironments = 5; + + /// Checks if two ChatTimes are close enough to belong to the same + /// environment. bool sameEnvironment(ChatTime prevTime) { - return toTimeStamp() - prevTime.toTimeStamp() < 1000*60*5; + return toTimeStamp() - prevTime.toTimeStamp() < 1000*60*minutesBetweenEnvironments; } + /// Returns a simple time String. String toTimeString() { return DateFormat('HH:mm').format(dateTime); } + /// If the ChatTime is today, this returns [toTimeString()], if not it also + /// shows the date. String toEventTimeString() { DateTime now = DateTime.now(); diff --git a/lib/src/utils/MxContent.dart b/lib/src/utils/MxContent.dart index 441ed12..2733a6b 100644 --- a/lib/src/utils/MxContent.dart +++ b/lib/src/utils/MxContent.dart @@ -26,23 +26,29 @@ import 'dart:core'; /// A file in Matrix presented by a mxc:// uri scheme. class MxContent { - final String _mxc; + /// Insert a mxc:// uri here. MxContent(this._mxc); + /// Returns the mxc uri. get mxc => _mxc; - getDownloadLink (Client matrix) => "https://${matrix.homeserver}/_matrix/media/r0/download/${_mxc.replaceFirst("mxc://","")}/"; + /// Returns a download Link to this content. + String getDownloadLink(Client matrix) => + "${matrix.homeserver}/_matrix/media/r0/download/${_mxc.replaceFirst("mxc://", "")}"; - getThumbnail (Client matrix, {num width, num height, ThumbnailMethod method}) { + /// Returns a scaled thumbnail link to this content with the given [width] and + /// [height]. [method] can be [ThumbnailMethod.crop] or + /// [ThumbnailMethod.scale] and defaults to [ThumbnailMethod.scale]. + String getThumbnail(Client matrix, + {num width, num height, ThumbnailMethod method}) { String methodStr = "crop"; if (method == ThumbnailMethod.scale) methodStr = "scale"; width = width.round(); height = height.round(); - return "${matrix.homeserver}/_matrix/media/r0/thumbnail/${_mxc.replaceFirst("mxc://","")}?width=$width&height=$height&method=$methodStr"; + return "${matrix.homeserver}/_matrix/media/r0/thumbnail/${_mxc.replaceFirst("mxc://", "")}?width=$width&height=$height&method=$methodStr"; } - } -enum ThumbnailMethod {crop, scale} \ No newline at end of file +enum ThumbnailMethod { crop, scale } diff --git a/test/ChatTime_test.dart b/test/ChatTime_test.dart new file mode 100644 index 0000000..36075e5 --- /dev/null +++ b/test/ChatTime_test.dart @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 Foobar. If not, see . + */ + +import 'package:flutter_test/flutter_test.dart'; +import 'package:famedlysdk/src/utils/ChatTime.dart'; + +void main() { + + + /// All Tests related to the ChatTime + group("ChatTime", () { + + test("Comparing", () async { + final int originServerTs = DateTime.now().millisecondsSinceEpoch - (ChatTime.minutesBetweenEnvironments-1)*1000*60; + final int oldOriginServerTs = DateTime.now().millisecondsSinceEpoch - (ChatTime.minutesBetweenEnvironments+1)*1000*60; + + final ChatTime chatTime = ChatTime(originServerTs); + final ChatTime oldChatTime = ChatTime(oldOriginServerTs); + final ChatTime nowTime = ChatTime.now(); + + expect(chatTime.toTimeStamp(), originServerTs); + expect(nowTime.toTimeStamp()>chatTime.toTimeStamp(),true); + expect(nowTime.sameEnvironment(chatTime),true); + expect(nowTime.sameEnvironment(oldChatTime),false); + + expect(chatTime > oldChatTime, true); + expect(chatTime < oldChatTime, false); + expect(chatTime >= oldChatTime, true); + expect(chatTime <= oldChatTime, false); + expect(chatTime == chatTime, true); + expect(chatTime == oldChatTime, false); + }); + + test("Formatting", () async { + final int timestamp = 1560144984758; + final ChatTime chatTime = ChatTime(timestamp); + expect(chatTime.toTimeString(),"07:36"); + expect(chatTime.toTimeString(),chatTime.toEventTimeString()); + + final ChatTime oldChatTime = ChatTime(156014498475); + expect(oldChatTime.toString(),"11.12.1974"); + }); + }); +} diff --git a/test/MxContent_test.dart b/test/MxContent_test.dart new file mode 100644 index 0000000..3b2dec4 --- /dev/null +++ b/test/MxContent_test.dart @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 Foobar. If not, see . + */ + +import 'package:flutter_test/flutter_test.dart'; +import 'package:famedlysdk/src/Client.dart'; +import 'package:famedlysdk/src/utils/MxContent.dart'; + +void main() { + + + /// All Tests related to the MxContent + group("MxContent", () { + + test("Formatting", () async { + Client client = Client("testclient"); + client.homeserver = "https://testserver.abc"; + final String mxc = "mxc://exampleserver.abc/abcdefghijklmn"; + final MxContent content = MxContent(mxc); + + expect(content.getDownloadLink(client),"${client.homeserver}/_matrix/media/r0/download/exampleserver.abc/abcdefghijklmn"); + expect(content.getThumbnail(client, width: 50, height: 50),"${client.homeserver}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=crop"); + expect(content.getThumbnail(client, width: 50, height: 50, method: ThumbnailMethod.scale),"${client.homeserver}/_matrix/media/r0/thumbnail/exampleserver.abc/abcdefghijklmn?width=50&height=50&method=scale"); + }); + }); +}