Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
55 changes: 55 additions & 0 deletions flutter_local_notifications_windows/lib/src/ffi/mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Just a mock, doesn't need real types or safety.
// ignore_for_file: type_annotate_public_apis, always_specify_types

import 'dart:ffi';

import 'package:ffi/ffi.dart';

import 'bindings.dart';

/// Mocked FFI bindings.
class MockBindings implements NotificationsPluginBindings {
Copy link
Owner

Choose a reason for hiding this comment

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

Can you place this in the test folder? It's purely test related and is the approach taken when creating mocks via mocktail or mockito or hand crafted ones. Note this is also currently a stub not a mock. A mock provides the ability to record the interactions that can then be used to verify what took place. Can you also rename the class and adjust the docs accordingly to reflect this?

@override
void cancelAll(_) {}

@override
void cancelNotification(_, int id) {}

@override
Pointer<NativePlugin> createPlugin() => malloc<Int>().cast();

@override
void disposePlugin(_) {}

@override
void freeLaunchDetails(_) {}

@override
void freeDetailsArray(ptr) => malloc.free(ptr);

@override
Pointer<NativeNotificationDetails> getActiveNotifications(_, __) =>
malloc<NativeNotificationDetails>();

@override
Pointer<NativeNotificationDetails> getPendingNotifications(_, __) =>
malloc<NativeNotificationDetails>();

@override
bool hasPackageIdentity() => false;

@override
bool init(_, __, ___, ____, _____, ______) => true;

@override
bool isValidXml(ptr) => true;

@override
bool scheduleNotification(a, b, c, d) => true;

@override
bool showNotification(a, b, c, d) => true;

@override
NativeUpdateResult updateNotification(a, b, c) => NativeUpdateResult.success;
}
11 changes: 11 additions & 0 deletions flutter_local_notifications_windows/lib/src/ffi/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import 'package:flutter_local_notifications_platform_interface/flutter_local_not
import '../details.dart';
import '../plugin/base.dart';
import 'bindings.dart';
import 'mock.dart';

/// Whether unit tests are running and to not use the real DLL files.
bool isUnitTest = false;

/// Gets the correct DLL bindings based on [isUnitTest].
NotificationsPluginBindings getBindings() => isUnitTest
? MockBindings()
: NotificationsPluginBindings(
DynamicLibrary.open('flutter_local_notifications_windows.dll'),
);

/// Helpful methods on native string maps.
extension NativeStringMapUtils on NativeStringMap {
Expand Down
20 changes: 15 additions & 5 deletions flutter_local_notifications_windows/lib/src/plugin/ffi.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:xml/xml.dart';

import '../details.dart';
import '../details/notification_to_xml.dart';
Expand All @@ -22,6 +23,16 @@ extension on String {
this[23] == '-';
}

/// Does a basic syntax check on XML.
bool checkXml(String xml) {
try {
XmlDocument.parse(xml);
return true;
} on XmlFormatException {
return false;
}
}

/// The Windows implementation of `package:flutter_local_notifications`.
class FlutterLocalNotificationsWindows extends WindowsNotificationsBase {
/// Creates an instance of the native plugin.
Expand All @@ -37,11 +48,7 @@ class FlutterLocalNotificationsWindows extends WindowsNotificationsBase {
static FlutterLocalNotificationsWindows? instance;

/// The FFI generated bindings to the native code.
late final NotificationsPluginBindings _bindings =
NotificationsPluginBindings(_library);

final DynamicLibrary _library =
DynamicLibrary.open('flutter_local_notifications_windows.dll');
late final NotificationsPluginBindings _bindings = getBindings();

/// A pointer to the C++ handler class.
late final Pointer<NativePlugin> _plugin;
Expand Down Expand Up @@ -281,6 +288,9 @@ class FlutterLocalNotificationsWindows extends WindowsNotificationsBase {

@override
bool isValidXml(String xml) => using((Arena arena) {
if (!checkXml(xml)) {
return false;
}
final Pointer<Utf8> nativeXml = xml.toNativeUtf8(allocator: arena);
return _bindings.isValidXml(nativeXml);
});
Expand Down
58 changes: 0 additions & 58 deletions flutter_local_notifications_windows/test/bindings_test.dart

This file was deleted.

Loading
Loading