Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 156 #163

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions pal_event_server/lib/src/daemon/linux/dbus_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import 'package:logging/logging.dart';

import '../daemon.dart' as daemon;

import 'package:meta/meta.dart';

final _logger = Logger('DbusNotifications');

const _objectPath = '/org/freedesktop/Notifications';
Expand All @@ -44,19 +46,29 @@ const _defaultActions = <String>[
];

// Map between Taqo database notification id and libnotify id
final _notifications = <int, int>{};
@visibleForTesting
var notifications = <int, int>{};

void openSurvey(id) {
daemon.openSurvey(id);
}

void _listen(String event) {
@visibleForTesting
void listen(String event) {
_logger.info('Event:${event}');
final action = _actionPattern.matchAsPrefix(event);
if (action != null) {

if (action != null && notifications.keys.isNotEmpty) {
_logger.info('action: id: ${action[1]} action: ${action[2]}');
if (action.groupCount >= 2 && _defaultActions.contains(action[2])) {
final notifId = int.tryParse(action[1]);
if (notifId != null) {
// Not super efficient, but fine for now
final id =
_notifications.keys.firstWhere((k) => _notifications[k] == notifId);
daemon.openSurvey(id);
notifications.keys.firstWhere((k) => notifications[k] == notifId, orElse: () => null);
if (id != null) {
openSurvey(id);
}
}
}
}
Expand All @@ -69,9 +81,9 @@ void _listen(String event) {
}

Future<void> cancel(int id) async {
final notifId = _notifications[id];
final notifId = notifications[id];
if (notifId == null) return;
_notifications.remove(id);
notifications.remove(id);

await Process.run('gdbus', [
'call', //
Expand All @@ -92,7 +104,7 @@ void monitor() {
]).then((Process process) {
//stdout.addStream(process.stdout);
//stderr.addStream(process.stderr);
Utf8Codec().decoder.bind(process.stdout).listen(_listen);
Utf8Codec().decoder.bind(process.stdout).listen(listen);
});
}

Expand Down Expand Up @@ -151,6 +163,6 @@ Future<int> notify(
final idString = processResult.stdout;
final notifId = int.tryParse(
RegExp(r'\(uint32 (\d+),\)').matchAsPrefix(idString)?.group(1));
_notifications[id] = notifId;
notifications[id] = notifId;
return notifId;
}
49 changes: 49 additions & 0 deletions pal_event_server/test/dbus_notifications_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// @dart=2.9

import 'package:test/test.dart';
import 'package:pal_event_server/src/daemon/linux/dbus_notifications.dart';

void main() {
group('All', ()
{
test('A notification fired with an empty event while no notifications are outstanding should not throw a StateException', () {
notifications = <int, int>{};
listen("");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add some comments here about expected behavior, such as expected to do nothing and generate no errors?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added descriptions. Please take a look. Let me know if this is what you had in mind?

expect(true, true);
});
test('A notification fired with an event while no notifications are outstanding should not throw a StateException', () {
notifications = <int, int>{};
listen("/org/freedesktop/Notifications: org.freedesktop.Notifications.ActionInvoked (uint32 27, 'default')\n" +
"/org/freedesktop/Notifications: org.freedesktop.Notifications.NotificationClosed (uint32 27, uint32 2)");
expect(true, true);
});
test('A notification fired with an unmatched Event while a notification is outstanding should not throw a StateException', () {
notifications = <int, int>{1:1};
listen("/org/freedesktop/Notifications: org.freedesktop.Notifications.ActionInvoked (uint32 27, 'default')\n" +
"/org/freedesktop/Notifications: org.freedesktop.Notifications.NotificationClosed (uint32 27, uint32 2)");
expect(true, true);
});
// commented out since it causes the Taqo graphical client to open
// test('A notification fired with a matched event while a notification is outstanding should not throw a StateException', () {
// sut.notifications = <int, int>{1:27};
// sut.openSurvey = (id) => print("Called");
// sut.listen("/org/freedesktop/Notifications: org.freedesktop.Notifications.ActionInvoked (uint32 27, 'default')\n" +
// "/org/freedesktop/Notifications: org.freedesktop.Notifications.NotificationClosed (uint32 27, uint32 2)");
// expect(true, true);
// });
});
}