-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
254 additions
and
154 deletions.
There are no files selected for viewing
This file contains 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
103 changes: 103 additions & 0 deletions
103
stevia/example/ios/RunnerTests/HapticChannelTests.swift
This file contains 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,103 @@ | ||
// This demonstrates a simple unit test of the Swift portion of this plugin's implementation. | ||
// | ||
// See https://developer.apple.com/documentation/xctest for more information about using XCTest. | ||
|
||
import Flutter | ||
import UIKit | ||
import XCTest | ||
|
||
@testable import stevia | ||
|
||
class HapticPluginTests: XCTestCase { | ||
|
||
let plugin = SteviaPlugin() | ||
|
||
func testHandleSuccessful() { | ||
let call = FlutterMethodCall(methodName: "hapticFeedback", arguments: "success") | ||
let expectation = expectation(description: "result block must be called.") | ||
|
||
plugin.handle(call) { result in | ||
XCTAssertNil(result) | ||
expectation.fulfill() | ||
} | ||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testHandleInvalidArgument() { | ||
let call = FlutterMethodCall(methodName: "hapticFeedback", arguments: nil) | ||
let expectation = expectation(description: "result block must be called.") | ||
|
||
plugin.handle(call) { result in | ||
XCTAssertEqual( | ||
result as! FlutterError, | ||
FlutterError(code: HapticPlugin.errorCode, message: "No pattern was specified", details: nil) | ||
) | ||
expectation.fulfill() | ||
} | ||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testHandleUnsupportedMethod() { | ||
let call = FlutterMethodCall(methodName: "invalid", arguments: "success") | ||
let expectation = expectation(description: "result block must be called.") | ||
|
||
plugin.handle(call) { result in | ||
XCTAssertEqual( | ||
result as! NSObject, | ||
FlutterMethodNotImplemented | ||
) | ||
expectation.fulfill() | ||
} | ||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
} | ||
|
||
class HapticFeedbackTests: XCTestCase { | ||
|
||
let feedback = HapticFeedback() | ||
|
||
func testHapticFeedbackPerformSuccess() { | ||
XCTAssertNil(feedback.perform("success")) | ||
} | ||
|
||
func testHapticFeedbackPerformWarning() { | ||
XCTAssertNil(feedback.perform("warning")) | ||
} | ||
|
||
func testHapticFeedbackPerformError() { | ||
XCTAssertNil(feedback.perform("error")) | ||
} | ||
|
||
func testHapticFeedbackPerformSelection() { | ||
XCTAssertNil(feedback.perform("selection")) | ||
} | ||
|
||
func testHapticFeedbackPerformHeavy() { | ||
XCTAssertNil(feedback.perform("heavy")) | ||
} | ||
|
||
func testHapticFeedbackPerformMedium() { | ||
XCTAssertNil(feedback.perform("medium")) | ||
} | ||
|
||
func testHapticFeedbackPerformLight() { | ||
XCTAssertNil(feedback.perform("light")) | ||
} | ||
|
||
func testHapticFeedbackPerformRigid() { | ||
XCTAssertNil(feedback.perform("rigid")) | ||
} | ||
|
||
func testHapticFeedbackPerformSoft() { | ||
XCTAssertNil(feedback.perform("soft")) | ||
} | ||
|
||
func testHapticFeedbackUnspportedPattern() { | ||
XCTAssertEqual( | ||
feedback.perform("invalid") as! FlutterError, | ||
FlutterError(code: HapticPlugin.errorCode, message: "Unsupported pattern: invalid", details: nil) | ||
) | ||
} | ||
|
||
} |
This file contains 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,105 +1,4 @@ | ||
import Flutter | ||
import UIKit | ||
import XCTest | ||
|
||
@testable import stevia | ||
|
||
|
||
|
||
// This demonstrates a simple unit test of the Swift portion of this plugin's implementation. | ||
// | ||
// See https://developer.apple.com/documentation/xctest for more information about using XCTest. | ||
|
||
class HapticPluginTests: XCTestCase { | ||
|
||
let plugin = HapticPlugin() | ||
|
||
func testHandleSuccessful() { | ||
let call = FlutterMethodCall(methodName: "hapticFeedback", arguments: "success") | ||
let expectation = expectation(description: "result block must be called.") | ||
|
||
plugin.handle(call) { result in | ||
XCTAssertNil(result) | ||
expectation.fulfill() | ||
} | ||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testHandleInvalidArgument() { | ||
let call = FlutterMethodCall(methodName: "hapticFeedback", arguments: nil) | ||
let expectation = expectation(description: "result block must be called.") | ||
|
||
plugin.handle(call) { result in | ||
XCTAssertEqual( | ||
result as! FlutterError, | ||
FlutterError(code: HapticPlugin.errorCode, message: "No pattern was specified", details: nil) | ||
) | ||
expectation.fulfill() | ||
} | ||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
func testHandleUnsupportedMethod() { | ||
let call = FlutterMethodCall(methodName: "invalid", arguments: "success") | ||
let expectation = expectation(description: "result block must be called.") | ||
|
||
plugin.handle(call) { result in | ||
XCTAssertEqual( | ||
result as! NSObject, | ||
FlutterMethodNotImplemented | ||
) | ||
expectation.fulfill() | ||
} | ||
waitForExpectations(timeout: 1) | ||
} | ||
|
||
} | ||
|
||
class HapticFeedbackTests: XCTestCase { | ||
|
||
let feedback = HapticFeedback() | ||
|
||
func testHapticFeedbackPerformSuccess() { | ||
XCTAssertNil(feedback.perform("success")) | ||
} | ||
|
||
func testHapticFeedbackPerformWarning() { | ||
XCTAssertNil(feedback.perform("warning")) | ||
} | ||
|
||
func testHapticFeedbackPerformError() { | ||
XCTAssertNil(feedback.perform("error")) | ||
} | ||
|
||
func testHapticFeedbackPerformSelection() { | ||
XCTAssertNil(feedback.perform("selection")) | ||
} | ||
|
||
func testHapticFeedbackPerformHeavy() { | ||
XCTAssertNil(feedback.perform("heavy")) | ||
} | ||
|
||
func testHapticFeedbackPerformMedium() { | ||
XCTAssertNil(feedback.perform("medium")) | ||
} | ||
|
||
func testHapticFeedbackPerformLight() { | ||
XCTAssertNil(feedback.perform("light")) | ||
} | ||
|
||
func testHapticFeedbackPerformRigid() { | ||
XCTAssertNil(feedback.perform("rigid")) | ||
} | ||
|
||
func testHapticFeedbackPerformSoft() { | ||
XCTAssertNil(feedback.perform("soft")) | ||
} | ||
|
||
func testHapticFeedbackUnspportedPattern() { | ||
XCTAssertEqual( | ||
feedback.perform("invalid") as! FlutterError, | ||
FlutterError(code: HapticPlugin.errorCode, message: "Unsupported pattern: invalid", details: nil) | ||
) | ||
} | ||
|
||
} |
This file contains 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,69 @@ | ||
import Flutter | ||
import UIKit | ||
|
||
class HapticChannel { | ||
|
||
private let methodHander = HapticMethodHandler() | ||
private let methodChannel: FlutterMethodChannel | ||
|
||
init(with registrar: FlutterPluginRegistrar) { | ||
methodChannel = FlutterMethodChannel(name: "com.foruslabs.stevia.haptic", binaryMessenger: registrar.messenger()) | ||
methodChannel.setMethodCallHandler(methodHander.handle) | ||
} | ||
|
||
} | ||
|
||
class HapticMethodHandler { | ||
|
||
private let feedback = HapticFeedback() | ||
|
||
func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { | ||
switch (call.method, call.arguments) { | ||
case ("hapticFeedback", let pattern as String): | ||
result(feedback.perform(pattern)) | ||
case ("hapticFeedback", _): | ||
result(FlutterError(code: HapticFeedback.code, message: "No pattern was specified", details: nil)); | ||
|
||
default: | ||
result(FlutterMethodNotImplemented) | ||
} | ||
} | ||
|
||
} | ||
|
||
class HapticFeedback { | ||
static let code = "invalid-haptic-pattern" | ||
|
||
private let notificationFeedback = UINotificationFeedbackGenerator() | ||
private let selectionFeedback = UISelectionFeedbackGenerator() | ||
private let heavyImpactFeedback = UIImpactFeedbackGenerator(style: .heavy) | ||
private let mediumImpactFeedback = UIImpactFeedbackGenerator(style: .medium) | ||
private let lightImpactFeedback = UIImpactFeedbackGenerator(style: .light) | ||
private var rigitImpactFeedback: UIImpactFeedbackGenerator? | ||
private var softImpactFeedback: UIImpactFeedbackGenerator? | ||
|
||
init() { | ||
if #available(iOS 13, *) { | ||
rigitImpactFeedback = UIImpactFeedbackGenerator(style: .rigid) | ||
softImpactFeedback = UIImpactFeedbackGenerator(style: .soft) | ||
} | ||
} | ||
|
||
func perform(_ pattern: String) -> Any? { | ||
switch pattern { | ||
case "success": notificationFeedback.notificationOccurred(.success) | ||
case "warning": notificationFeedback.notificationOccurred(.warning) | ||
case "error": notificationFeedback.notificationOccurred(.error) | ||
case "selection": selectionFeedback.selectionChanged() | ||
case "heavy": heavyImpactFeedback.impactOccurred() | ||
case "medium": mediumImpactFeedback.impactOccurred() | ||
case "light": lightImpactFeedback.impactOccurred() | ||
case "rigid": rigitImpactFeedback?.impactOccurred() | ||
case "soft": softImpactFeedback?.impactOccurred() | ||
default: | ||
return FlutterError(code: HapticFeedback.code, message: "Unsupported pattern: \(pattern)", details: nil) | ||
} | ||
|
||
return nil | ||
} | ||
} |
This file contains 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
Oops, something went wrong.