mirror of
https://github.com/flutter/flutter
synced 2024-08-27 20:10:11 +00:00
Enable Windows platform_channel test (#106973)
This commit is contained in:
parent
5ca39a2054
commit
f3717d70d7
1
.ci.yaml
1
.ci.yaml
|
@ -3994,7 +3994,6 @@ targets:
|
||||||
["devicelab", "hostonly"]
|
["devicelab", "hostonly"]
|
||||||
task_name: platform_channel_sample_test_windows
|
task_name: platform_channel_sample_test_windows
|
||||||
scheduler: luci
|
scheduler: luci
|
||||||
bringup: true
|
|
||||||
|
|
||||||
- name: Windows plugin_dependencies_test
|
- name: Windows plugin_dependencies_test
|
||||||
recipe: devicelab/devicelab_drone
|
recipe: devicelab/devicelab_drone
|
||||||
|
|
|
@ -28,8 +28,12 @@ class _PlatformChannelState extends State<PlatformChannel> {
|
||||||
try {
|
try {
|
||||||
final int? result = await methodChannel.invokeMethod('getBatteryLevel');
|
final int? result = await methodChannel.invokeMethod('getBatteryLevel');
|
||||||
batteryLevel = 'Battery level: $result%.';
|
batteryLevel = 'Battery level: $result%.';
|
||||||
} on PlatformException {
|
} on PlatformException catch (e) {
|
||||||
batteryLevel = 'Failed to get battery level.';
|
if (e.code == 'NO_BATTERY') {
|
||||||
|
batteryLevel = 'No battery.';
|
||||||
|
} else {
|
||||||
|
batteryLevel = 'Failed to get battery level.';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
setState(() {
|
setState(() {
|
||||||
_batteryLevel = batteryLevel;
|
_batteryLevel = batteryLevel;
|
||||||
|
|
|
@ -31,7 +31,11 @@ void main() {
|
||||||
batteryLevel = await driver.getText(batteryLevelLabel);
|
batteryLevel = await driver.getText(batteryLevelLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(batteryLevel.contains('%'), isTrue);
|
// Allow either a battery percentage or "No battery" since it will vary
|
||||||
|
// by device; either indicates that a known response came from the host
|
||||||
|
// implementation.
|
||||||
|
expect(batteryLevel.contains('%') || batteryLevel.contains('No battery'),
|
||||||
|
isTrue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,10 +16,17 @@
|
||||||
|
|
||||||
#include "flutter/generated_plugin_registrant.h"
|
#include "flutter/generated_plugin_registrant.h"
|
||||||
|
|
||||||
|
static constexpr int kBatteryError = -1;
|
||||||
|
static constexpr int kNoBattery = -2;
|
||||||
|
|
||||||
static int GetBatteryLevel() {
|
static int GetBatteryLevel() {
|
||||||
SYSTEM_POWER_STATUS status;
|
SYSTEM_POWER_STATUS status;
|
||||||
if (GetSystemPowerStatus(&status) == 0 || status.BatteryLifePercent == 255) {
|
if (GetSystemPowerStatus(&status) == 0) {
|
||||||
return -1;
|
return kBatteryError;
|
||||||
|
} else if (status.BatteryFlag == 128) {
|
||||||
|
return kNoBattery;
|
||||||
|
} else if (status.BatteryLifePercent == 255) {
|
||||||
|
return kBatteryError;
|
||||||
}
|
}
|
||||||
return status.BatteryLifePercent;
|
return status.BatteryLifePercent;
|
||||||
}
|
}
|
||||||
|
@ -59,10 +66,12 @@ bool FlutterWindow::OnCreate() {
|
||||||
if (call.method_name() == "getBatteryLevel") {
|
if (call.method_name() == "getBatteryLevel") {
|
||||||
int battery_level = GetBatteryLevel();
|
int battery_level = GetBatteryLevel();
|
||||||
|
|
||||||
if (battery_level != -1) {
|
if (battery_level == kBatteryError) {
|
||||||
result->Success(battery_level);
|
|
||||||
} else {
|
|
||||||
result->Error("UNAVAILABLE", "Battery level not available.");
|
result->Error("UNAVAILABLE", "Battery level not available.");
|
||||||
|
} else if (battery_level == kNoBattery) {
|
||||||
|
result->Error("NO_BATTERY", "Device does not have a battery.");
|
||||||
|
} else {
|
||||||
|
result->Success(battery_level);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result->NotImplemented();
|
result->NotImplemented();
|
||||||
|
|
Loading…
Reference in a new issue