Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Update generated messages (#115)
Browse files Browse the repository at this point in the history
* Migrate platform_interface to pigeon 0.2.1

* Format

* Fix Analysis

* Update SDK version constraints

* Update generated messages

* Add empty test
  • Loading branch information
creativecreatorormaybenot authored May 14, 2021
1 parent 22a46ba commit 8d6197e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 117 deletions.
6 changes: 6 additions & 0 deletions wakelock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.5.2

* Updated generated message handling.
* Updated generated messages in the platform interface, which fixed calling `setMockMessageHandler`.
* Updated `wakelock_` dependencies with updated Dart SDK constraints (`>=2.12.0`).

## 0.5.1+1

* Added Podspec to the `wakelock` package to avoid build issues on macOS.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// Autogenerated from Pigeon (v0.1.14), do not edit directly.
// Autogenerated from Pigeon (v0.2.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon

package creativemaybeno.wakelock;

import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.StandardMessageCodec;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/** Generated class from Pigeon. */
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "unchecked", "CodeBlock2Expr", "RedundantSuppression"})
public class Messages {

/** Generated class from Pigeon that represents data sent in messages. */
Expand All @@ -19,12 +20,12 @@ public static class ToggleMessage {
public Boolean getEnable() { return enable; }
public void setEnable(Boolean setterArg) { this.enable = setterArg; }

HashMap toMap() {
HashMap<String, Object> toMapResult = new HashMap<>();
Map<String, Object> toMap() {
Map<String, Object> toMapResult = new HashMap<>();
toMapResult.put("enable", enable);
return toMapResult;
}
static ToggleMessage fromMap(HashMap map) {
static ToggleMessage fromMap(Map<String, Object> map) {
ToggleMessage fromMapResult = new ToggleMessage();
Object enable = map.get("enable");
fromMapResult.enable = (Boolean)enable;
Expand All @@ -38,12 +39,12 @@ public static class IsEnabledMessage {
public Boolean getEnabled() { return enabled; }
public void setEnabled(Boolean setterArg) { this.enabled = setterArg; }

HashMap toMap() {
HashMap<String, Object> toMapResult = new HashMap<>();
Map<String, Object> toMap() {
Map<String, Object> toMapResult = new HashMap<>();
toMapResult.put("enabled", enabled);
return toMapResult;
}
static IsEnabledMessage fromMap(HashMap map) {
static IsEnabledMessage fromMap(Map<String, Object> map) {
IsEnabledMessage fromMapResult = new IsEnabledMessage();
Object enabled = map.get("enabled");
fromMapResult.enabled = (Boolean)enabled;
Expand All @@ -56,21 +57,21 @@ public interface WakelockApi {
void toggle(ToggleMessage arg);
IsEnabledMessage isEnabled();

/** Sets up an instance of `WakelockApi` to handle messages through the `binaryMessenger` */
/** Sets up an instance of `WakelockApi` to handle messages through the `binaryMessenger`. */
static void setup(BinaryMessenger binaryMessenger, WakelockApi api) {
{
BasicMessageChannel<Object> channel =
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.WakelockApi.toggle", new StandardMessageCodec());
if (api != null) {
channel.setMessageHandler((message, reply) -> {
HashMap<String, HashMap> wrapped = new HashMap<>();
Map<String, Object> wrapped = new HashMap<>();
try {
@SuppressWarnings("ConstantConditions")
ToggleMessage input = ToggleMessage.fromMap((HashMap)message);
ToggleMessage input = ToggleMessage.fromMap((Map<String, Object>)message);
api.toggle(input);
wrapped.put("result", null);
}
catch (Exception exception) {
catch (Error | RuntimeException exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
Expand All @@ -84,12 +85,12 @@ static void setup(BinaryMessenger binaryMessenger, WakelockApi api) {
new BasicMessageChannel<>(binaryMessenger, "dev.flutter.pigeon.WakelockApi.isEnabled", new StandardMessageCodec());
if (api != null) {
channel.setMessageHandler((message, reply) -> {
HashMap<String, HashMap> wrapped = new HashMap<>();
Map<String, Object> wrapped = new HashMap<>();
try {
IsEnabledMessage output = api.isEnabled();
wrapped.put("result", output.toMap());
}
catch (Exception exception) {
catch (Error | RuntimeException exception) {
wrapped.put("error", wrapError(exception));
}
reply.reply(wrapped);
Expand All @@ -100,8 +101,8 @@ static void setup(BinaryMessenger binaryMessenger, WakelockApi api) {
}
}
}
private static HashMap wrapError(Exception exception) {
HashMap<String, Object> errorMap = new HashMap<>();
private static Map<String, Object> wrapError(Throwable exception) {
Map<String, Object> errorMap = new HashMap<>();
errorMap.put("message", exception.toString());
errorMap.put("code", exception.getClass().getSimpleName());
errorMap.put("details", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class Wakelock {
val activity = this.activity!!
val enabled = this.enabled

if (message.getEnable()!!) {
if (message.enable!!) {
if (!enabled) activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else if (enabled) {
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Expand All @@ -33,7 +33,7 @@ internal class Wakelock {
}

val msg = IsEnabledMessage()
msg.setEnabled(enabled)
msg.enabled = enabled
return msg
}
}
Expand Down
2 changes: 1 addition & 1 deletion wakelock/ios/Classes/messages.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v0.1.14), do not edit directly.
// Autogenerated from Pigeon (v0.2.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon
#import <Foundation/Foundation.h>
@protocol FlutterBinaryMessenger;
Expand Down
24 changes: 12 additions & 12 deletions wakelock/ios/Classes/messages.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v0.1.14), do not edit directly.
// Autogenerated from Pigeon (v0.2.1), do not edit directly.
// See also: https://pub.dev/packages/pigeon
#import "messages.h"
#import <Flutter/Flutter.h>
Expand All @@ -7,19 +7,19 @@
#error File requires ARC to be enabled.
#endif

static NSDictionary* wrapResult(NSDictionary *result, FlutterError *error) {
static NSDictionary<NSString*, id>* wrapResult(NSDictionary *result, FlutterError *error) {
NSDictionary *errorDict = (NSDictionary *)[NSNull null];
if (error) {
errorDict = [NSDictionary dictionaryWithObjectsAndKeys:
(error.code ? error.code : [NSNull null]), @"code",
(error.message ? error.message : [NSNull null]), @"message",
(error.details ? error.details : [NSNull null]), @"details",
nil];
errorDict = @{
@"code": (error.code ? error.code : [NSNull null]),
@"message": (error.message ? error.message : [NSNull null]),
@"details": (error.details ? error.details : [NSNull null]),
};
}
return [NSDictionary dictionaryWithObjectsAndKeys:
(result ? result : [NSNull null]), @"result",
errorDict, @"error",
nil];
return @{
@"result": (result ? result : [NSNull null]),
@"error": errorDict,
};
}

@interface FLTToggleMessage ()
Expand Down Expand Up @@ -67,8 +67,8 @@ void FLTWakelockApiSetup(id<FlutterBinaryMessenger> binaryMessenger, id<FLTWakel
binaryMessenger:binaryMessenger];
if (api) {
[channel setMessageHandler:^(id _Nullable message, FlutterReply callback) {
FlutterError *error;
FLTToggleMessage *input = [FLTToggleMessage fromMap:message];
FlutterError *error;
[api toggle:input error:&error];
callback(wrapResult(nil, error));
}];
Expand Down
16 changes: 9 additions & 7 deletions wakelock/pigeons/messages.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// This is fine because it is a dev dependency.
// See https://github.com/flutter/flutter/issues/71360.
// @dart=2.9
import 'package:pigeon/java_generator.dart';
import 'package:pigeon/objc_generator.dart';
import 'package:pigeon/pigeon.dart';

/// Message for toggling the wakelock on the platform side.
class ToggleMessage {
bool enable;
bool? enable;
}

/// Message for reporting the wakelock state from the platform side.
class IsEnabledMessage {
bool enabled;
bool? enabled;
}

@HostApi(dartHostTestHandler: 'TestWakelockApi')
Expand All @@ -23,9 +22,12 @@ abstract class WakelockApi {
void configurePigeon(PigeonOptions options) {
options
..dartOut = '../wakelock_platform_interface/lib/messages.dart'
..dartTestOut = '../wakelock_platform_interface/test/messages.dart'
..objcHeaderOut = 'ios/Classes/messages.h'
..objcSourceOut = 'ios/Classes/messages.m'
..objcOptions.prefix = 'FLT'
..objcOptions = ObjcOptions()
..objcOptions?.prefix = 'FLT'
..javaOut = 'android/src/main/java/creativemaybeno/wakelock/Messages.java'
..javaOptions.package = 'creativemaybeno.wakelock';
..javaOptions = JavaOptions()
..javaOptions?.package = 'creativemaybeno.wakelock';
}
10 changes: 5 additions & 5 deletions wakelock/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: wakelock
description: >-2
Plugin that allows you to keep the device screen awake, i.e. prevent the screen from sleeping on
Android, iOS, macOS, Windows, and web.
version: 0.5.1+1
version: 0.5.2
homepage: https://github.com/creativecreatorormaybenot/wakelock/tree/master/wakelock

environment:
Expand All @@ -15,17 +15,17 @@ dependencies:

meta: ^1.2.0

wakelock_macos: ^0.1.0
wakelock_platform_interface: ^0.2.0
wakelock_web: ^0.2.0
wakelock_macos: ^0.1.0+1
wakelock_platform_interface: ^0.2.1+1
wakelock_web: ^0.2.0+1
wakelock_windows: ^0.1.0

dev_dependencies:
flutter_test:
sdk: flutter

pedantic: ^1.11.0
pigeon: ^0.1.15
pigeon: ^0.2.1 # flutter pub run pigeon --input "pigeons/messages.dart"

flutter:
plugin:
Expand Down
78 changes: 5 additions & 73 deletions wakelock/test/wakelock_test.dart
Original file line number Diff line number Diff line change
@@ -1,77 +1,9 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:wakelock/wakelock.dart';
import 'package:wakelock_platform_interface/messages.dart';
import 'package:wakelock_platform_interface/wakelock_platform_interface.dart';

void main() {
TestWidgetsFlutterBinding.ensureInitialized();

group('$Wakelock', () {
late FakeWakelockApi fakeWakelock;

setUpAll(() {
if (Platform.isMacOS) {
// See the member documentation for the reason behind this.
// todo(creativecreatorormaybenot): remove this once macOS is migrated
// todo| to pigeon.
wakelockPlatformInstance = WakelockPlatformInterface.instance;
}
fakeWakelock = FakeWakelockApi();
});

test('enable', () async {
await Wakelock.enable();

expect(fakeWakelock.calls.last, 'toggle');
expect(fakeWakelock.toggleMessage.enable, isTrue);
});

test('disable', () async {
await Wakelock.disable();

expect(fakeWakelock.calls.last, 'toggle');
expect(fakeWakelock.toggleMessage.enable, isFalse);
});

test('toggle', () async {
await Wakelock.toggle(enable: false);

expect(fakeWakelock.calls.last, 'toggle');
expect(fakeWakelock.toggleMessage.enable, isFalse);

await Wakelock.toggle(enable: true);

expect(fakeWakelock.calls.last, 'toggle');
expect(fakeWakelock.toggleMessage.enable, isTrue);
});

test('enabled', () async {
expect(Wakelock.enabled, completion(isTrue));
expect(fakeWakelock.calls.last, 'isEnabled');
});
});
}

class FakeWakelockApi extends TestWakelockApi {
FakeWakelockApi() {
TestWakelockApi.setup(this);
}

final calls = <String>[];
late ToggleMessage toggleMessage;

@override
IsEnabledMessage isEnabled() {
calls.add('isEnabled');
return IsEnabledMessage()..enabled = true;
}

@override
void toggle(ToggleMessage message) {
calls.add('toggle');
toggleMessage = message;
;
}
// There are no unit tests here as the API in this package only forwards the
// calls to the platform interface (unit tested) → platform implementations.
// Instead, this is all tested via e2e tests in the integration tests in the
// example app.
test('no unit tests', () {});
}

0 comments on commit 8d6197e

Please sign in to comment.