Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
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
4 changes: 4 additions & 0 deletions packages/battery/battery/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.5

* Ported to use platform interface.

## 1.0.4+1

* Moved everything from battery to battery/battery
Expand Down
67 changes: 7 additions & 60 deletions packages/battery/battery/lib/battery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,19 @@

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:meta/meta.dart' show visibleForTesting;
import 'package:battery_platform_interface/battery_platform_interface.dart';

/// Indicates the current battery state.
enum BatteryState {
/// The battery is completely full of energy.
full,

/// The battery is currently storing energy.
charging,

/// The battery is currently losing energy.
discharging
}
export 'package:battery_platform_interface/battery_platform_interface.dart'
show BatteryState;

/// API for accessing information about the battery of the device the Flutter
/// app is currently running on.
class Battery {
/// Initializes the plugin and starts listening for potential platform events.
factory Battery() {
if (_instance == null) {
final MethodChannel methodChannel =
const MethodChannel('plugins.flutter.io/battery');
final EventChannel eventChannel =
const EventChannel('plugins.flutter.io/charging');
_instance = Battery.private(methodChannel, eventChannel);
}
return _instance;
}

/// This constructor is only used for testing and shouldn't be accessed by
/// users of the plugin. It may break or change at any time.
@visibleForTesting
Battery.private(this._methodChannel, this._eventChannel);

static Battery _instance;

final MethodChannel _methodChannel;
final EventChannel _eventChannel;
Stream<BatteryState> _onBatteryStateChanged;

/// Returns the current battery level in percent.
Future<int> get batteryLevel => _methodChannel
.invokeMethod<int>('getBatteryLevel')
.then<int>((dynamic result) => result);
Future<int> get batteryLevel async =>
await BatteryPlatform.instance.batteryLevel();

/// Fires whenever the battery state changes.
Stream<BatteryState> get onBatteryStateChanged {
if (_onBatteryStateChanged == null) {
_onBatteryStateChanged = _eventChannel
.receiveBroadcastStream()
.map((dynamic event) => _parseBatteryState(event));
}
return _onBatteryStateChanged;
}
}

BatteryState _parseBatteryState(String state) {
switch (state) {
case 'full':
return BatteryState.full;
case 'charging':
return BatteryState.charging;
case 'discharging':
return BatteryState.discharging;
default:
throw ArgumentError('$state is not a valid BatteryState.');
}
Stream<BatteryState> get onBatteryStateChanged =>
BatteryPlatform.instance.onBatteryStateChanged();
}
6 changes: 4 additions & 2 deletions packages/battery/battery/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: battery
description: Flutter plugin for accessing information about the battery state
(full, charging, discharging) on Android and iOS.
homepage: https://github.com/flutter/plugins/tree/master/packages/battery/battery
version: 1.0.4+1
version: 1.0.5

flutter:
plugin:
Expand All @@ -17,13 +17,15 @@ dependencies:
flutter:
sdk: flutter
meta: ^1.0.5
battery_platform_interface: ^1.0.0

dev_dependencies:
async: ^2.0.8
test: ^1.3.0
mockito: 3.0.0
mockito: ^4.1.1
flutter_test:
sdk: flutter
plugin_platform_interface: ^1.0.0
integration_test:
path: ../../integration_test
pedantic: ^1.8.0
Expand Down
82 changes: 28 additions & 54 deletions packages/battery/battery/test/battery_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,42 @@

import 'dart:async';

import 'package:async/async.dart';
import 'package:flutter/services.dart';
import 'package:battery_platform_interface/battery_platform_interface.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:test/test.dart';
import 'package:battery/battery.dart';
import 'package:mockito/mockito.dart';

void main() {
MockMethodChannel methodChannel;
MockEventChannel eventChannel;
Battery battery;

setUp(() {
methodChannel = MockMethodChannel();
eventChannel = MockEventChannel();
battery = Battery.private(methodChannel, eventChannel);
});

test('batteryLevel', () async {
when(methodChannel.invokeMethod<int>('getBatteryLevel'))
.thenAnswer((Invocation invoke) => Future<int>.value(42));
expect(await battery.batteryLevel, 42);
});

group('battery state', () {
StreamController<String> controller;

setUp(() {
controller = StreamController<String>();
when(eventChannel.receiveBroadcastStream())
.thenAnswer((Invocation invoke) => controller.stream);
});

tearDown(() {
controller.close();
group('battery', () {
Battery battery;
MockBatteryPlatform fakePlatform;
setUp(() async {
fakePlatform = MockBatteryPlatform();
BatteryPlatform.instance = fakePlatform;
battery = Battery();
});

test('calls receiveBroadcastStream once', () {
battery.onBatteryStateChanged;
battery.onBatteryStateChanged;
battery.onBatteryStateChanged;
verify(eventChannel.receiveBroadcastStream()).called(1);
test('batteryLevel', () async {
int result = await battery.batteryLevel;
expect(result, 42);
});

test('receive values', () async {
final StreamQueue<BatteryState> queue =
StreamQueue<BatteryState>(battery.onBatteryStateChanged);

controller.add("full");
expect(await queue.next, BatteryState.full);

controller.add("discharging");
expect(await queue.next, BatteryState.discharging);

controller.add("charging");
expect(await queue.next, BatteryState.charging);

controller.add("illegal");
expect(queue.next, throwsArgumentError);
test('onBatteryStateChanged', () async {
BatteryState result = await battery.onBatteryStateChanged.first;
expect(result, BatteryState.full);
});
});
}

class MockMethodChannel extends Mock implements MethodChannel {}

class MockEventChannel extends Mock implements EventChannel {}
class MockBatteryPlatform extends Mock
with MockPlatformInterfaceMixin
implements BatteryPlatform {
Future<int> batteryLevel() async {
return 42;
}

Stream<BatteryState> onBatteryStateChanged() {
StreamController<BatteryState> result = StreamController<BatteryState>();
result.add(BatteryState.full);
return result.stream;
}
}