Tests for ChatTime and MxContent including bug fixes

This commit is contained in:
Christian 2019-06-10 06:28:21 +00:00
parent cc36108df1
commit 22231f87b7
5 changed files with 163 additions and 10 deletions

View file

@ -38,7 +38,7 @@ class Connection {
Connection(this.client) {
WidgetsBinding.instance
.addObserver(_LifecycleEventHandler(resumeCallBack: () {
?.addObserver(_LifecycleEventHandler(resumeCallBack: () {
_sync();
}));
}

View file

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

View file

@ -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}
enum ThumbnailMethod { crop, scale }

64
test/ChatTime_test.dart Normal file
View file

@ -0,0 +1,64 @@
/*
* 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
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");
});
});
}

45
test/MxContent_test.dart Normal file
View file

@ -0,0 +1,45 @@
/*
* 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
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");
});
});
}