|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 compose-jindong |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.compose.jindong.core |
| 17 | + |
| 18 | +import io.github.compose.jindong.core.dsl.buildHapticPattern |
| 19 | +import io.github.compose.jindong.core.fake.FakeHapticExecutor |
| 20 | +import io.github.compose.jindong.core.model.HapticIntensity |
| 21 | +import io.kotest.assertions.assertSoftly |
| 22 | +import io.kotest.assertions.throwables.shouldNotThrowAny |
| 23 | +import io.kotest.core.spec.style.FunSpec |
| 24 | +import io.kotest.matchers.collections.shouldBeEmpty |
| 25 | +import io.kotest.matchers.collections.shouldHaveSize |
| 26 | +import io.kotest.matchers.shouldBe |
| 27 | + |
| 28 | +class HapticManagerTest : |
| 29 | + FunSpec({ |
| 30 | + lateinit var fakeExecutor: FakeHapticExecutor |
| 31 | + |
| 32 | + beforeEach { |
| 33 | + fakeExecutor = FakeHapticExecutor() |
| 34 | + HapticManager.setExecutorForTesting(fakeExecutor) |
| 35 | + } |
| 36 | + |
| 37 | + afterEach { |
| 38 | + HapticManager.release() |
| 39 | + } |
| 40 | + |
| 41 | + test("isSupported should return true when executor supports haptics") { |
| 42 | + HapticManager.isSupported shouldBe true |
| 43 | + } |
| 44 | + |
| 45 | + test("isSupported should return false when executor does not support haptics") { |
| 46 | + HapticManager.setExecutorForTesting(FakeHapticExecutor(isSupported = false)) |
| 47 | + |
| 48 | + HapticManager.isSupported shouldBe false |
| 49 | + } |
| 50 | + |
| 51 | + test("execute should execute pattern synchronously") { |
| 52 | + val pattern = buildHapticPattern { |
| 53 | + haptic(100.ms) |
| 54 | + } |
| 55 | + |
| 56 | + HapticManager.execute(pattern) |
| 57 | + |
| 58 | + assertSoftly { |
| 59 | + fakeExecutor.executedPatterns shouldHaveSize 1 |
| 60 | + fakeExecutor.executedPatterns[0] shouldBe pattern |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + test("execute should handle empty pattern") { |
| 65 | + val emptyPattern = buildHapticPattern { } |
| 66 | + |
| 67 | + HapticManager.execute(emptyPattern) |
| 68 | + |
| 69 | + assertSoftly { |
| 70 | + fakeExecutor.executedPatterns shouldHaveSize 1 |
| 71 | + fakeExecutor.executedPatterns[0].events.shouldBeEmpty() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + test("executeAsync should execute pattern and return handle") { |
| 76 | + val pattern = buildHapticPattern { |
| 77 | + haptic(100.ms, HapticIntensity.STRONG) |
| 78 | + } |
| 79 | + |
| 80 | + val handle = HapticManager.executeAsync(pattern) |
| 81 | + |
| 82 | + assertSoftly { |
| 83 | + fakeExecutor.asyncExecutedPatterns shouldHaveSize 1 |
| 84 | + fakeExecutor.asyncExecutedPatterns[0] shouldBe pattern |
| 85 | + handle.isActive shouldBe true |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + test("executeAsync should cancel previous handle when executing new pattern") { |
| 90 | + val pattern1 = buildHapticPattern { haptic(100.ms) } |
| 91 | + val pattern2 = buildHapticPattern { haptic(200.ms) } |
| 92 | + |
| 93 | + val handle1 = HapticManager.executeAsync(pattern1) |
| 94 | + HapticManager.executeAsync(pattern2) |
| 95 | + |
| 96 | + assertSoftly { |
| 97 | + fakeExecutor.asyncExecutedPatterns shouldHaveSize 2 |
| 98 | + handle1.isActive shouldBe false |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + test("cancel should cancel current execution") { |
| 103 | + val pattern = buildHapticPattern { haptic(100.ms) } |
| 104 | + val handle = HapticManager.executeAsync(pattern) |
| 105 | + |
| 106 | + HapticManager.cancel() |
| 107 | + |
| 108 | + handle.isActive shouldBe false |
| 109 | + } |
| 110 | + |
| 111 | + test("cancel should not throw when no active execution exists") { |
| 112 | + shouldNotThrowAny { |
| 113 | + HapticManager.cancel() |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + test("release should release executor resources") { |
| 118 | + HapticManager.release() |
| 119 | + |
| 120 | + fakeExecutor.releaseCalled shouldBe true |
| 121 | + } |
| 122 | + |
| 123 | + test("release should cancel active execution") { |
| 124 | + val pattern = buildHapticPattern { haptic(100.ms) } |
| 125 | + val handle = HapticManager.executeAsync(pattern) |
| 126 | + |
| 127 | + HapticManager.release() |
| 128 | + |
| 129 | + assertSoftly { |
| 130 | + handle.isActive shouldBe false |
| 131 | + fakeExecutor.releaseCalled shouldBe true |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + test("should allow reuse after release") { |
| 136 | + val firstExecutor = FakeHapticExecutor() |
| 137 | + HapticManager.setExecutorForTesting(firstExecutor) |
| 138 | + HapticManager.release() |
| 139 | + |
| 140 | + val secondExecutor = FakeHapticExecutor() |
| 141 | + HapticManager.setExecutorForTesting(secondExecutor) |
| 142 | + val pattern = buildHapticPattern { haptic(100.ms) } |
| 143 | + HapticManager.executeAsync(pattern) |
| 144 | + |
| 145 | + assertSoftly { |
| 146 | + firstExecutor.releaseCalled shouldBe true |
| 147 | + firstExecutor.asyncExecutedPatterns.shouldBeEmpty() |
| 148 | + secondExecutor.asyncExecutedPatterns shouldHaveSize 1 |
| 149 | + } |
| 150 | + } |
| 151 | + }) |
0 commit comments