This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[android_alarm_manager] Testing and documentation #2283
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 0.4.4+3 | ||
|
|
||
| * Add unit tests and DartDocs. | ||
|
|
||
| ## 0.4.4+2 | ||
|
|
||
| * Remove AndroidX warning. | ||
|
|
||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,18 @@ | ||
| name: android_alarm_manager | ||
| description: Flutter plugin for accessing the Android AlarmManager service, and | ||
| running Dart code in the background when alarms fire. | ||
| version: 0.4.4+2 | ||
| version: 0.4.4+3 | ||
| author: Flutter Team <[email protected]> | ||
| homepage: https://github.com/flutter/plugins/tree/master/packages/android_alarm_manager | ||
|
|
||
| dependencies: | ||
| flutter: | ||
| sdk: flutter | ||
|
|
||
| dev_dependencies: | ||
| flutter_test: | ||
| sdk: flutter | ||
|
|
||
| flutter: | ||
| plugin: | ||
| androidPackage: io.flutter.plugins.androidalarmmanager | ||
|
|
||
201 changes: 201 additions & 0 deletions
201
packages/android_alarm_manager/test/android_alarm_manager_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:ui'; | ||
|
|
||
| import 'package:android_alarm_manager/android_alarm_manager.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
|
|
||
| void main() { | ||
| String invalidCallback(String foo) => foo; | ||
| void validCallback(int id) => null; | ||
|
|
||
| const MethodChannel testChannel = MethodChannel( | ||
| 'plugins.flutter.io/android_alarm_manager', JSONMethodCodec()); | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| setUpAll(() { | ||
| testChannel.setMockMethodCallHandler((MethodCall call) => null); | ||
| }); | ||
|
|
||
| test('${AndroidAlarmManager.initialize}', () async { | ||
| testChannel.setMockMethodCallHandler((MethodCall call) async { | ||
| assert(call.method == 'AlarmService.start'); | ||
| return true; | ||
| }); | ||
|
|
||
| final bool initialized = await AndroidAlarmManager.initialize(); | ||
|
|
||
| expect(initialized, isTrue); | ||
| }); | ||
|
|
||
| group('${AndroidAlarmManager.oneShotAt}', () { | ||
| test('validates input', () async { | ||
| final DateTime validTime = DateTime.utc(1993); | ||
| final int validId = 1; | ||
|
|
||
| // Callback should take a single int param. | ||
| await expectLater( | ||
| () => AndroidAlarmManager.oneShotAt( | ||
| validTime, validId, invalidCallback), | ||
| throwsAssertionError); | ||
|
|
||
| // ID should be less than 32 bits. | ||
| await expectLater( | ||
| () => AndroidAlarmManager.oneShotAt( | ||
| validTime, 2147483648, validCallback), | ||
| throwsAssertionError); | ||
| }); | ||
|
|
||
| test('sends arguments to the platform', () async { | ||
| final DateTime alarm = DateTime(1993); | ||
| const int rawHandle = 4; | ||
| AndroidAlarmManager.setTestOverides( | ||
| getCallbackHandle: (Function _) => | ||
| CallbackHandle.fromRawHandle(rawHandle)); | ||
|
|
||
| final int id = 1; | ||
| final bool alarmClock = true; | ||
| final bool allowWhileIdle = true; | ||
| final bool exact = true; | ||
| final bool wakeup = true; | ||
| final bool rescheduleOnReboot = true; | ||
|
|
||
| testChannel.setMockMethodCallHandler((MethodCall call) async { | ||
| expect(call.method, 'Alarm.oneShotAt'); | ||
| expect(call.arguments[0], id); | ||
| expect(call.arguments[1], alarmClock); | ||
| expect(call.arguments[2], allowWhileIdle); | ||
| expect(call.arguments[3], exact); | ||
| expect(call.arguments[4], wakeup); | ||
| expect(call.arguments[5], alarm.millisecondsSinceEpoch); | ||
| expect(call.arguments[6], rescheduleOnReboot); | ||
| expect(call.arguments[7], rawHandle); | ||
| return true; | ||
| }); | ||
|
|
||
| final bool result = await AndroidAlarmManager.oneShotAt( | ||
| alarm, id, validCallback, | ||
| alarmClock: alarmClock, | ||
| allowWhileIdle: allowWhileIdle, | ||
| exact: exact, | ||
| wakeup: wakeup, | ||
| rescheduleOnReboot: rescheduleOnReboot); | ||
|
|
||
| expect(result, isTrue); | ||
| }); | ||
| }); | ||
|
|
||
| test('${AndroidAlarmManager.oneShot} calls through to oneShotAt', () async { | ||
| final DateTime now = DateTime(1993); | ||
| const int rawHandle = 4; | ||
| AndroidAlarmManager.setTestOverides( | ||
| now: () => now, | ||
| getCallbackHandle: (Function _) => | ||
| CallbackHandle.fromRawHandle(rawHandle)); | ||
|
|
||
| const Duration alarm = Duration(seconds: 1); | ||
| final int id = 1; | ||
| final bool alarmClock = true; | ||
| final bool allowWhileIdle = true; | ||
| final bool exact = true; | ||
| final bool wakeup = true; | ||
| final bool rescheduleOnReboot = true; | ||
|
|
||
| testChannel.setMockMethodCallHandler((MethodCall call) async { | ||
| expect(call.method, 'Alarm.oneShotAt'); | ||
| expect(call.arguments[0], id); | ||
| expect(call.arguments[1], alarmClock); | ||
| expect(call.arguments[2], allowWhileIdle); | ||
| expect(call.arguments[3], exact); | ||
| expect(call.arguments[4], wakeup); | ||
| expect( | ||
| call.arguments[5], now.millisecondsSinceEpoch + alarm.inMilliseconds); | ||
| expect(call.arguments[6], rescheduleOnReboot); | ||
| expect(call.arguments[7], rawHandle); | ||
| return true; | ||
| }); | ||
|
|
||
| final bool result = await AndroidAlarmManager.oneShot( | ||
| alarm, id, validCallback, | ||
| alarmClock: alarmClock, | ||
| allowWhileIdle: allowWhileIdle, | ||
| exact: exact, | ||
| wakeup: wakeup, | ||
| rescheduleOnReboot: rescheduleOnReboot); | ||
|
|
||
| expect(result, isTrue); | ||
| }); | ||
|
|
||
| group('${AndroidAlarmManager.periodic}', () { | ||
| test('validates input', () async { | ||
| const Duration validDuration = Duration(seconds: 0); | ||
| final int validId = 1; | ||
|
|
||
| // Callback should take a single int param. | ||
| await expectLater( | ||
| () => AndroidAlarmManager.periodic( | ||
| validDuration, validId, invalidCallback), | ||
| throwsAssertionError); | ||
|
|
||
| // ID should be less than 32 bits. | ||
| await expectLater( | ||
| () => AndroidAlarmManager.periodic( | ||
| validDuration, 2147483648, validCallback), | ||
| throwsAssertionError); | ||
| }); | ||
|
|
||
| test('sends arguments through to the platform', () async { | ||
| final DateTime now = DateTime(1993); | ||
| const int rawHandle = 4; | ||
| AndroidAlarmManager.setTestOverides( | ||
| now: () => now, | ||
| getCallbackHandle: (Function _) => | ||
| CallbackHandle.fromRawHandle(rawHandle)); | ||
|
|
||
| final int id = 1; | ||
| final bool exact = true; | ||
| final bool wakeup = true; | ||
| final bool rescheduleOnReboot = true; | ||
| const Duration period = Duration(seconds: 1); | ||
|
|
||
| testChannel.setMockMethodCallHandler((MethodCall call) async { | ||
| expect(call.method, 'Alarm.periodic'); | ||
| expect(call.arguments[0], id); | ||
| expect(call.arguments[1], exact); | ||
| expect(call.arguments[2], wakeup); | ||
| expect(call.arguments[3], | ||
| (now.millisecondsSinceEpoch + period.inMilliseconds)); | ||
| expect(call.arguments[4], period.inMilliseconds); | ||
| expect(call.arguments[5], rescheduleOnReboot); | ||
| expect(call.arguments[6], rawHandle); | ||
| return true; | ||
| }); | ||
|
|
||
| final bool result = await AndroidAlarmManager.periodic( | ||
| period, | ||
| id, | ||
| (int id) => null, | ||
mklim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| exact: exact, | ||
| wakeup: wakeup, | ||
| rescheduleOnReboot: rescheduleOnReboot, | ||
| ); | ||
|
|
||
| expect(result, isTrue); | ||
| }); | ||
| }); | ||
|
|
||
| test('${AndroidAlarmManager.cancel}', () async { | ||
| final int id = 1; | ||
| testChannel.setMockMethodCallHandler((MethodCall call) async { | ||
| assert(call.method == 'Alarm.cancel' && call.arguments[0] == id); | ||
| return true; | ||
| }); | ||
|
|
||
| final bool canceled = await AndroidAlarmManager.cancel(id); | ||
|
|
||
| expect(canceled, isTrue); | ||
| }); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.