Skip to content

Commit ce68d97

Browse files
authored
[macOS] Battery check in platform_channel sample (#123354)
Adds a check for the case where a device has no battery present. The devicelab test expects the text "No battery." in the case where a device has no battery. This is triggered by returning an error with a NO_BATTERY error code. Also applies some cosmetic fixes: * Amends a copyright notice to Flutter instead of com.example * Deletes an unused test. Issue: flutter/flutter#79204
1 parent 6fd5989 commit ce68d97

6 files changed

Lines changed: 14 additions & 152 deletions

File tree

‎examples/platform_channel/macos/Runner.xcodeproj/project.pbxproj‎

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
/* End PBXAggregateTarget section */
2222

2323
/* Begin PBXBuildFile section */
24-
331C80D8294CF71000263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C80D7294CF71000263BE5 /* RunnerTests.swift */; };
2524
335BBD1B22A9A15E00E9071D /* GeneratedPluginRegistrant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335BBD1A22A9A15E00E9071D /* GeneratedPluginRegistrant.swift */; };
2625
33CC10F12044A3C60003C045 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33CC10F02044A3C60003C045 /* AppDelegate.swift */; };
2726
33CC10F32044A3C60003C045 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 33CC10F22044A3C60003C045 /* Assets.xcassets */; };
@@ -31,13 +30,6 @@
3130
/* End PBXBuildFile section */
3231

3332
/* Begin PBXContainerItemProxy section */
34-
331C80D9294CF71000263BE5 /* PBXContainerItemProxy */ = {
35-
isa = PBXContainerItemProxy;
36-
containerPortal = 33CC10E52044A3C60003C045 /* Project object */;
37-
proxyType = 1;
38-
remoteGlobalIDString = 33CC10EC2044A3C60003C045;
39-
remoteInfo = Runner;
40-
};
4133
33CC111F2044C79F0003C045 /* PBXContainerItemProxy */ = {
4234
isa = PBXContainerItemProxy;
4335
containerPortal = 33CC10E52044A3C60003C045 /* Project object */;
@@ -61,8 +53,6 @@
6153
/* End PBXCopyFilesBuildPhase section */
6254

6355
/* Begin PBXFileReference section */
64-
331C80D5294CF71000263BE5 /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
65-
331C80D7294CF71000263BE5 /* RunnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunnerTests.swift; sourceTree = "<group>"; };
6656
333000ED22D3DE5D00554162 /* Warnings.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Warnings.xcconfig; sourceTree = "<group>"; };
6757
335BBD1A22A9A15E00E9071D /* GeneratedPluginRegistrant.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneratedPluginRegistrant.swift; sourceTree = "<group>"; };
6858
33CC10ED2044A3C60003C045 /* platform_channel.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = platform_channel.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -83,13 +73,6 @@
8373
/* End PBXFileReference section */
8474

8575
/* Begin PBXFrameworksBuildPhase section */
86-
331C80D2294CF70F00263BE5 /* Frameworks */ = {
87-
isa = PBXFrameworksBuildPhase;
88-
buildActionMask = 2147483647;
89-
files = (
90-
);
91-
runOnlyForDeploymentPostprocessing = 0;
92-
};
9376
33CC10EA2044A3C60003C045 /* Frameworks */ = {
9477
isa = PBXFrameworksBuildPhase;
9578
buildActionMask = 2147483647;
@@ -100,14 +83,6 @@
10083
/* End PBXFrameworksBuildPhase section */
10184

10285
/* Begin PBXGroup section */
103-
331C80D6294CF71000263BE5 /* RunnerTests */ = {
104-
isa = PBXGroup;
105-
children = (
106-
331C80D7294CF71000263BE5 /* RunnerTests.swift */,
107-
);
108-
path = RunnerTests;
109-
sourceTree = "<group>";
110-
};
11186
33BA886A226E78AF003329D5 /* Configs */ = {
11287
isa = PBXGroup;
11388
children = (
@@ -124,7 +99,6 @@
12499
children = (
125100
33FAB671232836740065AC1E /* Runner */,
126101
33CEB47122A05771004F2AC0 /* Flutter */,
127-
331C80D6294CF71000263BE5 /* RunnerTests */,
128102
33CC10EE2044A3C60003C045 /* Products */,
129103
D73912EC22F37F3D000D13A0 /* Frameworks */,
130104
);
@@ -134,7 +108,6 @@
134108
isa = PBXGroup;
135109
children = (
136110
33CC10ED2044A3C60003C045 /* platform_channel.app */,
137-
331C80D5294CF71000263BE5 /* RunnerTests.xctest */,
138111
);
139112
name = Products;
140113
sourceTree = "<group>";
@@ -185,24 +158,6 @@
185158
/* End PBXGroup section */
186159

187160
/* Begin PBXNativeTarget section */
188-
331C80D4294CF70F00263BE5 /* RunnerTests */ = {
189-
isa = PBXNativeTarget;
190-
buildConfigurationList = 331C80DE294CF71000263BE5 /* Build configuration list for PBXNativeTarget "RunnerTests" */;
191-
buildPhases = (
192-
331C80D1294CF70F00263BE5 /* Sources */,
193-
331C80D2294CF70F00263BE5 /* Frameworks */,
194-
331C80D3294CF70F00263BE5 /* Resources */,
195-
);
196-
buildRules = (
197-
);
198-
dependencies = (
199-
331C80DA294CF71000263BE5 /* PBXTargetDependency */,
200-
);
201-
name = RunnerTests;
202-
productName = RunnerTests;
203-
productReference = 331C80D5294CF71000263BE5 /* RunnerTests.xctest */;
204-
productType = "com.apple.product-type.bundle.unit-test";
205-
};
206161
33CC10EC2044A3C60003C045 /* Runner */ = {
207162
isa = PBXNativeTarget;
208163
buildConfigurationList = 33CC10FB2044A3C60003C045 /* Build configuration list for PBXNativeTarget "Runner" */;
@@ -233,10 +188,6 @@
233188
LastUpgradeCheck = 1300;
234189
ORGANIZATIONNAME = "";
235190
TargetAttributes = {
236-
331C80D4294CF70F00263BE5 = {
237-
CreatedOnToolsVersion = 14.0;
238-
TestTargetID = 33CC10EC2044A3C60003C045;
239-
};
240191
33CC10EC2044A3C60003C045 = {
241192
CreatedOnToolsVersion = 9.2;
242193
LastSwiftMigration = 1420;
@@ -267,20 +218,12 @@
267218
projectRoot = "";
268219
targets = (
269220
33CC10EC2044A3C60003C045 /* Runner */,
270-
331C80D4294CF70F00263BE5 /* RunnerTests */,
271221
33CC111A2044C6BA0003C045 /* Flutter Assemble */,
272222
);
273223
};
274224
/* End PBXProject section */
275225

276226
/* Begin PBXResourcesBuildPhase section */
277-
331C80D3294CF70F00263BE5 /* Resources */ = {
278-
isa = PBXResourcesBuildPhase;
279-
buildActionMask = 2147483647;
280-
files = (
281-
);
282-
runOnlyForDeploymentPostprocessing = 0;
283-
};
284227
33CC10EB2044A3C60003C045 /* Resources */ = {
285228
isa = PBXResourcesBuildPhase;
286229
buildActionMask = 2147483647;
@@ -334,14 +277,6 @@
334277
/* End PBXShellScriptBuildPhase section */
335278

336279
/* Begin PBXSourcesBuildPhase section */
337-
331C80D1294CF70F00263BE5 /* Sources */ = {
338-
isa = PBXSourcesBuildPhase;
339-
buildActionMask = 2147483647;
340-
files = (
341-
331C80D8294CF71000263BE5 /* RunnerTests.swift in Sources */,
342-
);
343-
runOnlyForDeploymentPostprocessing = 0;
344-
};
345280
33CC10E92044A3C60003C045 /* Sources */ = {
346281
isa = PBXSourcesBuildPhase;
347282
buildActionMask = 2147483647;
@@ -356,11 +291,6 @@
356291
/* End PBXSourcesBuildPhase section */
357292

358293
/* Begin PBXTargetDependency section */
359-
331C80DA294CF71000263BE5 /* PBXTargetDependency */ = {
360-
isa = PBXTargetDependency;
361-
target = 33CC10EC2044A3C60003C045 /* Runner */;
362-
targetProxy = 331C80D9294CF71000263BE5 /* PBXContainerItemProxy */;
363-
};
364294
33CC11202044C79F0003C045 /* PBXTargetDependency */ = {
365295
isa = PBXTargetDependency;
366296
target = 33CC111A2044C6BA0003C045 /* Flutter Assemble */;
@@ -381,48 +311,6 @@
381311
/* End PBXVariantGroup section */
382312

383313
/* Begin XCBuildConfiguration section */
384-
331C80DB294CF71000263BE5 /* Debug */ = {
385-
isa = XCBuildConfiguration;
386-
buildSettings = {
387-
BUNDLE_LOADER = "$(TEST_HOST)";
388-
CURRENT_PROJECT_VERSION = 1;
389-
GENERATE_INFOPLIST_FILE = YES;
390-
MARKETING_VERSION = 1.0;
391-
PRODUCT_BUNDLE_IDENTIFIER = com.example.platformChannel.RunnerTests;
392-
PRODUCT_NAME = "$(TARGET_NAME)";
393-
SWIFT_VERSION = 5.0;
394-
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/platform_channel.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/platform_channel";
395-
};
396-
name = Debug;
397-
};
398-
331C80DC294CF71000263BE5 /* Release */ = {
399-
isa = XCBuildConfiguration;
400-
buildSettings = {
401-
BUNDLE_LOADER = "$(TEST_HOST)";
402-
CURRENT_PROJECT_VERSION = 1;
403-
GENERATE_INFOPLIST_FILE = YES;
404-
MARKETING_VERSION = 1.0;
405-
PRODUCT_BUNDLE_IDENTIFIER = com.example.platformChannel.RunnerTests;
406-
PRODUCT_NAME = "$(TARGET_NAME)";
407-
SWIFT_VERSION = 5.0;
408-
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/platform_channel.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/platform_channel";
409-
};
410-
name = Release;
411-
};
412-
331C80DD294CF71000263BE5 /* Profile */ = {
413-
isa = XCBuildConfiguration;
414-
buildSettings = {
415-
BUNDLE_LOADER = "$(TEST_HOST)";
416-
CURRENT_PROJECT_VERSION = 1;
417-
GENERATE_INFOPLIST_FILE = YES;
418-
MARKETING_VERSION = 1.0;
419-
PRODUCT_BUNDLE_IDENTIFIER = com.example.platformChannel.RunnerTests;
420-
PRODUCT_NAME = "$(TARGET_NAME)";
421-
SWIFT_VERSION = 5.0;
422-
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/platform_channel.app/$(BUNDLE_EXECUTABLE_FOLDER_PATH)/platform_channel";
423-
};
424-
name = Profile;
425-
};
426314
338D0CE9231458BD00FA5F75 /* Profile */ = {
427315
isa = XCBuildConfiguration;
428316
baseConfigurationReference = 7AFA3C8E1D35360C0083082E /* Release.xcconfig */;
@@ -653,16 +541,6 @@
653541
/* End XCBuildConfiguration section */
654542

655543
/* Begin XCConfigurationList section */
656-
331C80DE294CF71000263BE5 /* Build configuration list for PBXNativeTarget "RunnerTests" */ = {
657-
isa = XCConfigurationList;
658-
buildConfigurations = (
659-
331C80DB294CF71000263BE5 /* Debug */,
660-
331C80DC294CF71000263BE5 /* Release */,
661-
331C80DD294CF71000263BE5 /* Profile */,
662-
);
663-
defaultConfigurationIsVisible = 0;
664-
defaultConfigurationName = Release;
665-
};
666544
33CC10E82044A3C60003C045 /* Build configuration list for PBXProject "Runner" */ = {
667545
isa = XCConfigurationList;
668546
buildConfigurations = (

‎examples/platform_channel/macos/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@
3737
</BuildableReference>
3838
</MacroExpansion>
3939
<Testables>
40-
<TestableReference
41-
skipped = "NO"
42-
parallelizable = "YES">
43-
<BuildableReference
44-
BuildableIdentifier = "primary"
45-
BlueprintIdentifier = "331C80D4294CF70F00263BE5"
46-
BuildableName = "RunnerTests.xctest"
47-
BlueprintName = "RunnerTests"
48-
ReferencedContainer = "container:Runner.xcodeproj">
49-
</BuildableReference>
50-
</TestableReference>
5140
</Testables>
5241
</TestAction>
5342
<LaunchAction

‎examples/platform_channel/macos/Runner/Configs/AppInfo.xcconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ PRODUCT_NAME = platform_channel
1111
PRODUCT_BUNDLE_IDENTIFIER = com.example.platformChannel
1212

1313
// The copyright displayed in application information
14-
PRODUCT_COPYRIGHT = Copyright © 2023 com.example. All rights reserved.
14+
PRODUCT_COPYRIGHT = Copyright © 2014 The Flutter Authors. All rights reserved.

‎examples/platform_channel/macos/Runner/MainFlutterWindow.swift‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ class MainFlutterWindow: NSWindow, FlutterStreamHandler, PowerSourceStateChangeD
2424
batteryChannel.setMethodCallHandler({ [weak self] (call, result) in
2525
switch call.method {
2626
case "getBatteryLevel":
27+
if self?.powerSource.hasBattery() == false {
28+
result(FlutterError(
29+
code: "NO_BATTERY",
30+
message: "Device does not have a battery",
31+
details: nil))
32+
return
33+
}
2734
let level = self?.powerSource.getCurrentCapacity()
2835
if level == -1 {
2936
result(
3037
FlutterError(
3138
code: "UNAVAILABLE",
3239
message: "Battery info unavailable",
3340
details: nil))
41+
return
3442
}
3543
result(level)
3644
default:

‎examples/platform_channel/macos/Runner/PowerSource.swift‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class PowerSource {
1616
let info = IOPSCopyPowerSourcesInfo().takeRetainedValue()
1717
lazy var sources = IOPSCopyPowerSourcesList(info).takeRetainedValue() as Array
1818

19+
func hasBattery() -> Bool {
20+
return !sources.isEmpty
21+
}
22+
1923
/// Returns the current power source capacity. Apple-defined power sources will return this value
2024
/// as a percentage.
2125
func getCurrentCapacity() -> Int {
@@ -31,8 +35,7 @@ class PowerSource {
3135

3236
/// Returns whether the device is drawing battery power or connected to an external power source.
3337
func getPowerState() -> PowerState {
34-
if !sources.isEmpty {
35-
let source = sources[0]
38+
if let source = sources.first {
3639
let description =
3740
IOPSGetPowerSourceDescription(info, source).takeUnretainedValue() as! [String: AnyObject]
3841
if let state = description[kIOPSPowerSourceStateKey] as? String {

‎examples/platform_channel/macos/RunnerTests/RunnerTests.swift‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)