Skip to content

Commit

Permalink
Merge pull request #163 from google/issue_156
Browse files Browse the repository at this point in the history
issue 156
  • Loading branch information
rundong08 authored Aug 31, 2023
2 parents 775e1c2 + a94e416 commit 0e9176f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
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("");
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);
// });
});
}

0 comments on commit 0e9176f

Please sign in to comment.