-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[ios_platform_images] migrate objC to swift #4847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
ad9ed35
e5545bc
cf21182
008d5d3
e422e78
b0122cf
54b9b92
6c29139
f3ce031
100202a
97dc083
1a28d20
8da12bd
50ce448
59d7b6d
17a369e
cfbf55f
101c190
1d2969b
372ce11
523a277
aef7985
78cbc82
45d185d
a9a4879
364be83
aae7a29
cbbc208
ac54592
3e66718
71f2589
e2d9a9a
3324ccd
e370d61
b25d574
5d03976
eaf7c90
139e125
6ea1fc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>IDEDidComputeMac32BitWarning</key> | ||
|
Mairramer marked this conversation as resolved.
|
||
| <true/> | ||
| </dict> | ||
| </plist> | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright 2013 The Flutter 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 Flutter | ||
| import XCTest | ||
|
|
||
| @testable import ios_platform_images | ||
|
|
||
| class IosPlatformImagesTests: XCTestCase { | ||
|
|
||
| var mockChannel: MockMethodChannel! | ||
|
Mairramer marked this conversation as resolved.
Outdated
|
||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
| mockChannel = MockMethodChannel() | ||
| } | ||
|
|
||
| func testHandleMethodCall_loadImage() { | ||
| let name = "flutter" | ||
|
Mairramer marked this conversation as resolved.
Outdated
|
||
|
|
||
|
Mairramer marked this conversation as resolved.
|
||
| let call = FlutterMethodCall(methodName: "loadImage", arguments: [name]) | ||
|
|
||
| let mockChannel = MockMethodChannel() | ||
|
|
||
| let plugin = IosPlatformImagesPlugin(channel: mockChannel) | ||
|
|
||
| let resultExpectation = expectation(description: "result block must be called.") | ||
|
|
||
| plugin.handle(call) { result in | ||
| let result = result as? [String: Any] | ||
| XCTAssertNotNil(result) | ||
|
|
||
| let scale = result?["scale"] as? CGFloat | ||
| let data = result?["data"] as? FlutterStandardTypedData | ||
|
|
||
| XCTAssertNotNil(scale) | ||
| XCTAssertNotNil(data) | ||
|
|
||
| resultExpectation.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: 2, handler: nil) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flutter style prefers not using timeouts in tests, as they contribute to flake. This should just wait until the expectation is fulfilled. Same in all of the tests below. |
||
| } | ||
|
|
||
| func testHandleMethodCall_loadImage_notFound() { | ||
| let name = "flutterNotFound" | ||
|
|
||
| let call = FlutterMethodCall(methodName: "loadImage", arguments: [name]) | ||
|
|
||
| let mockChannel = MockMethodChannel() | ||
|
|
||
| let plugin = IosPlatformImagesPlugin(channel: mockChannel) | ||
|
|
||
| let resultExpectation = expectation(description: "result block must be called.") | ||
|
|
||
| plugin.handle(call) { result in | ||
| let result = result as? [String: Any] | ||
|
|
||
| XCTAssertNil(result) | ||
| resultExpectation.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: 2, handler: nil) | ||
| } | ||
|
|
||
| func testHandleMethodCall_resolveURL() { | ||
| let name = "textfile" | ||
|
|
||
| let call = FlutterMethodCall(methodName: "resolveURL", arguments: [name]) | ||
|
|
||
| let mockChannel = MockMethodChannel() | ||
|
|
||
| let plugin = IosPlatformImagesPlugin(channel: mockChannel) | ||
|
|
||
| let resultExpectation = expectation(description: "result block must be called.") | ||
|
|
||
| plugin.handle(call) { result in | ||
| let result = result as? String | ||
| XCTAssertNotNil(result) | ||
| XCTAssertEqual(result?.components(separatedBy: "/").last, name) | ||
| resultExpectation.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: 1, handler: nil) | ||
| } | ||
|
|
||
| func testHandleMethodCall_resolveURL_notFound() { | ||
| let name = "notFound" | ||
|
|
||
| let call = FlutterMethodCall(methodName: "resolveURL", arguments: [name]) | ||
|
|
||
| let mockChannel = MockMethodChannel() | ||
|
|
||
| let plugin = IosPlatformImagesPlugin(channel: mockChannel) | ||
|
|
||
| let resultExpectation = expectation(description: "result block must be called.") | ||
|
|
||
| plugin.handle(call) { result in | ||
| XCTAssertNil(result) | ||
| resultExpectation.fulfill() | ||
| } | ||
|
|
||
| waitForExpectations(timeout: 1, handler: nil) | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright 2013 The Flutter 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 Foundation | ||
|
|
||
| @testable import ios_platform_images | ||
|
|
||
| final class MockMethodChannel: MethodChannel { | ||
| var invokeMethodStub: ((_ methods: String, _ arguments: Any?) -> Void)? = nil | ||
| func invokeMethod(_ method: String, arguments: Any?) { | ||
| invokeMethodStub?(method, arguments) | ||
| } | ||
| } |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright 2013 The Flutter 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 Flutter | ||
| import Foundation | ||
|
|
||
| public final class IosPlatformImagesPlugin: NSObject, FlutterPlugin { | ||
|
|
||
| private let channel: MethodChannel | ||
|
Mairramer marked this conversation as resolved.
Outdated
|
||
|
|
||
| init(channel: MethodChannel) { | ||
| self.channel = channel | ||
| } | ||
|
|
||
| public static func register(with registrar: FlutterPluginRegistrar) { | ||
| let channel = FlutterMethodChannel( | ||
| name: "plugins.flutter.io/ios_platform_images", | ||
| binaryMessenger: registrar.messenger()) | ||
|
|
||
| let instance = IosPlatformImagesPlugin(channel: channel) | ||
| registrar.addMethodCallDelegate(instance, channel: channel) | ||
| } | ||
|
|
||
| public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { | ||
| switch call.method { | ||
| case "loadImage": | ||
| loadImage(call, result) | ||
| case "resolveURL": | ||
| resolveURL(call, result) | ||
| default: | ||
| result(FlutterMethodNotImplemented) | ||
| } | ||
| } | ||
|
|
||
| private func loadImage(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { | ||
| guard let arguments = call.arguments as? [Any], | ||
|
Mairramer marked this conversation as resolved.
Outdated
|
||
| let name = arguments.first as? String, | ||
|
Mairramer marked this conversation as resolved.
Outdated
|
||
| let image = UIImage.flutterImage(withName: name) | ||
| else { | ||
| result(nil) | ||
| return | ||
| } | ||
|
|
||
| let data = image.pngData() | ||
|
|
||
| let imageResult: [String: Any] = [ | ||
| "scale": image.scale, | ||
| "data": FlutterStandardTypedData(bytes: data!), | ||
|
Mairramer marked this conversation as resolved.
Outdated
|
||
| ] | ||
|
|
||
| result(imageResult) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the purpose of the intermediate |
||
| } | ||
|
|
||
| private func resolveURL(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { | ||
| if let args = call.arguments as? [String?] { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above, this should not fail silently. |
||
| let name = args[0] | ||
| let extensionName = args.count == 2 ? args[1] : nil | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the same logic as the Obj-C version, and is not correct. There will always be two arguments, but the second may be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I change to avoid
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In what context? This is the call, and it passes two arguments. If you are referring to the unit tests you added, the solution is to make the unit tests match actual usage. Having unit tests that call the method in a way that production code never will, and then changing the implementation to work in the unit test but break production, makes the unit tests actively harmful.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw here in another test the reason for your comment, I will make the appropriate change |
||
|
|
||
| if let url = Bundle.main.url(forResource: name, withExtension: extensionName) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the explicit branching, rather than optional chaining like the Obj-C version? The more |
||
| result(url.absoluteString) | ||
| } else { | ||
| result(nil) | ||
| } | ||
| } else { | ||
| result(nil) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // Copyright 2013 The Flutter 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 Flutter | ||
|
|
||
| /// A channel for platform code to communicate with the Dart code. | ||
| protocol MethodChannel { | ||
|
Mairramer marked this conversation as resolved.
Outdated
|
||
| /// Invokes a method in Dart code. | ||
| /// - Parameter method the method name. | ||
| /// - Parameter arguments the method arguments. | ||
| func invokeMethod(_ method: String, arguments: Any?) | ||
| } | ||
|
|
||
| /// A default implementation of the `MethodChannel` protocol. | ||
| extension FlutterMethodChannel: MethodChannel {} | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.