1 /*
2  * Copyright 2024 The Android Open Source Project
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 
17 package androidx.xr.runtime.testing
18 
19 import androidx.annotation.RestrictTo
20 import androidx.xr.runtime.internal.Anchor
21 import androidx.xr.runtime.internal.Hand
22 import androidx.xr.runtime.internal.HitResult
23 import androidx.xr.runtime.internal.PerceptionManager
24 import androidx.xr.runtime.internal.Trackable
25 import androidx.xr.runtime.math.Pose
26 import androidx.xr.runtime.math.Ray
27 import java.util.UUID
28 
29 /** Test-only implementation of [PerceptionManager] used to validate state transitions. */
30 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
31 public class FakePerceptionManager : PerceptionManager, AnchorHolder {
32 
33     public val anchors: MutableList<Anchor> = mutableListOf<Anchor>()
34     override val trackables: MutableList<Trackable> = mutableListOf<Trackable>()
35 
36     override val leftHand: Hand? = FakeRuntimeHand()
37     override val rightHand: Hand? = FakeRuntimeHand()
38 
39     private val hitResults = mutableListOf<HitResult>()
40     private val anchorUuids = mutableListOf<UUID>()
41 
42     /** Flag to represent available tracking state of the camera. */
43     public var isTrackingAvailable: Boolean = true
44 
createAnchornull45     override fun createAnchor(pose: Pose): Anchor {
46         // TODO: b/349862231 - Modify it once detach is implemented.
47         val anchor = FakeRuntimeAnchor(pose, this, isTrackingAvailable)
48         anchors.add(anchor)
49         return anchor
50     }
51 
hitTestnull52     override fun hitTest(ray: Ray): MutableList<HitResult> = hitResults
53 
54     override fun getPersistedAnchorUuids(): List<UUID> = anchorUuids
55 
56     override fun loadAnchor(uuid: UUID): Anchor {
57         check(anchorUuids.contains(uuid)) { "Anchor is not persisted." }
58         return FakeRuntimeAnchor(Pose(), this)
59     }
60 
unpersistAnchornull61     override fun unpersistAnchor(uuid: UUID) {
62         anchorUuids.remove(uuid)
63     }
64 
persistAnchornull65     override fun persistAnchor(anchor: Anchor) {
66         anchorUuids.add(anchor.uuid!!)
67     }
68 
loadAnchorFromNativePointernull69     override fun loadAnchorFromNativePointer(nativePointer: Long): Anchor {
70         return FakeRuntimeAnchor(Pose(), this)
71     }
72 
detachAnchornull73     override fun detachAnchor(anchor: Anchor) {
74         anchors.remove(anchor)
75         anchor.uuid?.let { anchorUuids.remove(it) }
76     }
77 
78     /** Adds a [HitResult] to the list that is returned when calling [hitTest] with any pose. */
addHitResultnull79     public fun addHitResult(hitResult: HitResult) {
80         hitResults.add(hitResult)
81     }
82 
83     /** Removes all [HitResult] instances passed to [addHitResult]. */
clearHitResultsnull84     public fun clearHitResults() {
85         hitResults.clear()
86     }
87 
88     /** Adds a [Trackable] to the list that is returned when calling [trackables]. */
addTrackablenull89     public fun addTrackable(trackable: Trackable) {
90         trackables.add(trackable)
91     }
92 
93     /** Removes all [Trackable] instances passed to [addTrackable]. */
clearTrackablesnull94     public fun clearTrackables() {
95         trackables.clear()
96     }
97 }
98