mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
Add VITURE XR controls, head tracking, and SteamVR (#732)
* Add VITURE glasses support: hardware control, 3D, head tracking Glasses get their own controller over the MCU (permission, 120 Hz default, brightness, volume, sunblock, 2D/3D) and the game moves onto them when plugged in. The IMU drives Cardboard head tracking and the SteamVR driver. 3D falls back to one eye's view on phones that can't send the 3840x1080 signal. Options open from a glasses button in game and on the Steam page. * Drop the glasses settings like Steam's: Play-style button, Settings lip, tabs * Hang the glasses Settings lip over the top of the button * Space glasses MCU commands so the IMU rate sticks, fit fixed resolutions to the glasses * Keep Steam alive with SteamVR registered, run SteamVR at the glasses' rate, keep the glasses lip on top * Fit VR games on the glasses: full view, seated origin at the head, pad hands reach with the d-pad * Let the pad hands reach a metre out so buttons with deep colliders press * Show every VR frame in order at 120 fps: wait for SteamVR's GPU work, present only finished copies, skip the grace on idle shared counters * Glasses: SteamVR starts and stops with them, closer view with a field of view setting, Recenter on the pad * Glasses: SteamVR field of view defaults to 90° * VR pad hands: sticks move and aim the hands, left stick click switches the sticks to the game, d-pad pulls a held hand * Glasses: Play SteamVR beside the glasses button instead of starting SteamVR with them, pad hands drawn as Index controllers and off while other VR controllers are connected, glasses sheet tabs sized like Steam's * Track glasses mode, refresh-aware glasses window, align Play SteamVR with Play Steam * Fix 3D on the glasses: full-res SteamVR presenter, revert VR 3D at session end, coalesce vsync ticks, no session restart after end * Address review: glasses output after installs and session end, VR revert retry, display fallback, presenter swapchain retry, agent hex checks, drawer bumpers, glasses translations, docs * Harden VITURE stereo negotiation and output refresh tracking * Localize stereo recovery feedback and record Astra reconnect tests * Improve test failure diagnostics and refresh Astra firmware findings * Handle VITURE docks separately and reject truncated USB frames * Glasses: remember a 3D fallback per model, docks as display only, VR timing fixes - A 3D attempt that ends at the 640x480 fallback is remembered for that glasses model, so Play SteamVR no longer blacks out the glasses after every restart; Retry clears it and "3D for VR games" stays reachable - A VITURE Mobile Dock on its own no longer counts as glasses; through a dock Play SteamVR is hidden and the sheet shows the output and why there is no control - HDR is probed on the glasses when the session starts there - Glasses frames with a mismatched checksum are marked in the status frame log - SteamVR layer and driver vblank counters stay monotonic across refresh changes; one-eye render scale applies from activation and on mono/3D switches - Compositor binds announce the refresh clients were last told; the presenter retries a failed first swapchain - Dead strings and helpers removed; docs updated * Glasses: drop USB frames with a bad checksum * Glasses: count frames dropped for a bad checksum * Feat/viture xr (#907) * Add RayNeo Air and GT support to the XR glasses pipeline * Add RayNeo Air and GT support to the XR glasses pipeline * Checkpoint glasses tracking, calibration, and SteamVR launch integration * Checkpoint glasses tracking, calibration, and SteamVR launch integration * Add XREAL 1S support and correct GT and Pro 2 tracking * Add XREAL 1S support and correct GT and Pro 2 tracking * Fix mirrored glasses tracking and recover SteamVR after device loss * Fix mirrored glasses tracking and recover SteamVR after device loss * Document confirmed XREAL 1S brightness control * Document confirmed XREAL 1S brightness control * Fix SteamVR glasses defaults and rendering controls * Fix SteamVR glasses defaults and rendering controls * chore: keep only the 3 newest session log folders (#852) * chore: add agent comment rules (#748) Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> * Fix SteamVR polling, VITURE checksums, and fence tracking Keep manual SteamVR launches polled, discard corrupt VITURE packets, and retire Vulkan fences out of order without blocking submissions at a fixed capacity. Preserve the contiguous completion watermark used by the presenter. Fix optional Windows launcher configuration and audited helper staging. Add Android, Windows, and native tracker regression coverage. * Fix SteamVR polling, VITURE checksums, and fence tracking Keep manual SteamVR launches polled, discard corrupt VITURE packets, and retire Vulkan fences out of order without blocking submissions at a fixed capacity. Preserve the contiguous completion watermark used by the presenter. Fix optional Windows launcher configuration and audited helper staging. Add Android, Windows, and native tracker regression coverage. * fix: wireless ADB page names the child-process switch the way Android shows it (#848) * l10n: refresh stale and untranslated strings in es, fr, ko, ru (#850) * l10n: refresh stale and untranslated strings in es, fr, ko, ru - mode_added_art_hint: now mentions Steam then SteamGridDB (all four locales). - wincomp_needs_installer / wincomp_installers_note: es and ko still described the old "coming soon" behaviour; align with the current English text. - summary_remove_game: was still English in all four locales. Machine-assisted translation; native-speaker review is welcome. * l10n(es): say registro, not grabación, for the missing installer record --------- Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com> * fix: a failed cloud save recovery no longer ends the app (#846) The cloud-recovery thread started at every app start had nothing to catch an error, so one went to CrashHandler, which halts the process with status 2. In unit tests the thread can outlive the test that started it, fail on the torn-down preferences, and take the whole test run down. The error is now logged and the thread ends. Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com> * fix: an interrupted Linux runtime download resumes instead of starting again (#847) * fix: an interrupted Linux runtime download resumes instead of starting again installOnce() asked the downloader to resume but always deleted the archive in its finally block, so the next attempt began at zero. The archive is now named after the release version and checksum, a failed download keeps what it fetched, and archives of other releases (and the old fixed name) are removed. A checksum or extraction failure still deletes the file. * test: Downloader resumes a cut-off download from the bytes it kept Starts a socket server that promises the whole file, hangs up part way and then answers a Range request, and checks that the second downloadFile call resumes with the right offset and ends with identical bytes. * fix: a resumed download discards a 206 that does not start where the kept bytes end Downloader appended every 206 response to the partial file without looking at Content-Range. A server or proxy that answers a Range request with a different offset would leave a corrupt file. Check that the range starts at the kept length; otherwise delete the partial file and start again without a Range header. Adds a test with a server that answers the Range request from offset 0. --------- Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com> * l10n: German, French, Spanish, Italian, Portuguese and Russian for the zRAM, reset-settings and store free-space strings (#849) * l10n: German, French, Spanish, Italian, Portuguese and Russian for the zRAM, reset-settings and store free-space strings Adds the strings values-de, values-it and values-pt were missing (14 each) and the six that values-fr, values-es and values-ru were missing. Machine-assisted drafts that follow each file's existing register and terms; native review is welcome. * l10n: correct missing active swap translations --------- Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com> Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com> * fix: droiddeckctl reaches variant-package builds (recover #842) (#854) fix: droiddeckctl reaches builds shipped under a variant package The provider authority and the start Activity's component were fixed to com.droiddeck.launcher, so droiddeckctl could not talk to the antutu, ludashi or pubg builds from tools/release/variants.txt. DROIDDECK_PACKAGE now picks the package; the Activity's class name stays the same in every variant. Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com> * feat: droiddeckctl reports on-screen pacing (recover #843) (#855) feat: droiddeckctl pacing reports the frames that reached the screen Reads SurfaceFlinger's timestats for the game's layer (or the session's SurfaceView) over a capture: frames on screen, fps on screen, the present-to-present histogram and how many intervals were off the usual cadence. Records split by a refresh-rate change are summed. A game's own fps counter counts frames handed over, so it can't show dropped or doubled frames. Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com> * fix: Steam can restart more than twice in a session (#754) * fix: Steam can restart more than twice in a session Every exit 42 counted toward the cap that stops an update which will not install, and the count was never cleared, so the third restart of a session ended it however far apart they were. A client that ran for a minute before asking is not that loop: the count starts again. * fix: measure the client's run time as it exits The time spent collecting logs and checking FEX after the client exits counted toward its run, so a slow cleanup could make a quick exit 42 look like a long one and clear the count. --------- Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com> Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> * fix: the front end keeps its page when the Fold is folded or unfolded (#832) * fix: the front end keeps its page when the Fold is folded or unfolded MainActivity, FileManagerActivity, FilePickerActivity and StoreLoginActivity did not declare the screen-size configuration changes, so folding or unfolding a foldable recreated them and dropped their in-memory state. They now handle orientation, screenSize, screenLayout, smallestScreenSize and density themselves, like SessionActivity already does. uiMode is left out on purpose. * fix: store login keeps rebuilding its toolbar when the display density changes StoreLoginActivity builds its toolbar in code with dp-to-pixel values taken at creation time and has no onConfigurationChanged. It no longer declares density in configChanges, so a density change recreates it as before this PR. Size and layout changes are still handled in place. --------- Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com> * Release pointer buttons without replaying stale motion (#803) * Fix cursor warping on native touch down * Preserve input cleanup without replaying stale pointer motion --------- Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com> * l10n: translate the strings added since #284 into zh-CN, zh-HK, zh-TW and ja (#539) * l10n: translate the 184 strings added since #284 (zh-CN/HK/TW, ja) The stores, cloud saves, Windows components, Steam touch, desktop removal and background-downloads strings landed after #284's translation snapshot, so every one of them fell back to English. Translate all 184 into Simplified Chinese, Traditional Chinese (Taiwan and Hong Kong) and Japanese, keeping each locale file's own vocabulary (工作階段/會話, 解除安裝/卸載, 元件/組件, 設定). Also fix four Hong Kong strings that kept the simplified 着色器. * l10n(zh): fix stale and ambiguous strings found in review Severe: summary_remove_game was untranslated English in all three files; mode_added_art_hint, wincomp_installers_note and wincomp_needs_installer still described the pre-#480/#492 English. Also: distinguish the Setup wizard from Android system settings in zh-HK/zh-TW (初始設定 / 系統設定 / Android 設定), unify library as 收藏庫, component as 組件 in zh-HK, session as 會話 in zh-HK, account as 帳戶 in zh-HK, Trending as 趋势/趨勢, Forget as 清除, reword the re-applied-at-launch, action-failed and already-in-Games strings, and play as 執行/開啟遊戲 in zh-TW/zh-HK. * l10n(ja): fix stale and ambiguous strings found in review --------- Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com> Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com> * Rebuild and verify Windows VR runtime components * Rebuild and verify Windows VR runtime components * Add top, bottom, and centered picture positions for Steam sessions (#871) * fix: dependency scan ignores folders above the game folder (#859) * DroidDeck-Proton: get releases from the Droid-Deck org repo DroidDeck-Proton is now built and released on Droid-Deck/DroidDeck-Proton (11-9.1 is Latest there), so the Components page, the update check and the guest installer read releases from the org repo instead of The412Banner's. * feat: low-latency controller and touch toggles in the session drawer (#808) * fix: SteamGridDB lookups no longer stick as "no match" after a failure; add a search to the art chooser (#876) * feat: Steam home wall fills from the signed-in library instead of store picks (#878) * test: cover GPU family and support classification (#880) * Fix local builds on Linux failing after the first Comet build (#866) * feat: sort the store library and installed tabs, and switch between card and list views (#798) * feat: three-finger tap toggles Steam's on-screen keyboard in game mode (#782) * fix: keep Quest-patched Turnip as the auto default (#853) * Fix deploy_local.sh and droiddeckctl not finding a phone connected over wireless debugging (#864) * fix: keep proot sessions alive when the kernel loses fork events (#457) * fix(quest): correct desktop and Steam display corruption across sessions (#733) fix(quest): stabilize desktop and Steam layer presentation Keep Quest SDR scenes and the black backdrop off the Android Vulkan swapchain regardless of session order or HDR preferences. Only report the first frame after client content is submitted. Add backdrop lifecycle and first-frame regression coverage. Record the user's successful Quest 3 Test 2 result. Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com> * fix: keep SteamVR syscall hooks and report Steam crashes (#905) * fix: preserve SteamVR syscall hooks * fix: report Steam crashes through gamescope * fix: honor glasses pacing and resolution in SteamVR launches --------- Co-authored-by: connor g <connorsspam1992@gmail.com> Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Co-authored-by: Yassine Gherbi <y.gherbi.dev@gmail.com> Co-authored-by: Resul Övür <92828257+ovurrsl@users.noreply.github.com> Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com> Co-authored-by: Kirk <kirtoy.junsay@gmail.com> Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com> Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com> Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com> Co-authored-by: asdf17128 <asdf17128@gmail.com> Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com> Co-authored-by: Dantalian <wsu091227@qq.com> Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com> Co-authored-by: The412Banner <the412banner@users.noreply.github.com> Co-authored-by: The412Banner <205237651+The412Banner@users.noreply.github.com> Co-authored-by: FX1922 <120828496+FX192@users.noreply.github.com> Co-authored-by: Muaz Kadan <32595066+muazkadan@users.noreply.github.com> Co-authored-by: kingokksa <47888451+kingokksa@users.noreply.github.com> Co-authored-by: Jamie Schenker <jam1esch3nk3r@gmail.com> Co-authored-by: Jagger15 <jagger2333@live.com> * Glasses: keep a pending 2D restore and the SteamVR recovery offer, show Play SteamVR only with a picture * Translate the glasses calibration, XREAL and SteamVR recovery strings * SteamVR recovery: give a finished teardown time to collect logs before restarting * Credit the projects the XR glasses support builds on * Joy-Cons: SteamVR hands from their gyro while SteamVR runs, one combined pad otherwise * Headsets: settings badge on Play SteamVR instead of a top-right button, both shown only once connected * Hand tracking: camera-tracked Joy-Cons and bare hands in SteamVR, drawn as white gloves - Joy-Con movement from the accelerometer (report reordering, per-Joy-Con calibration, zero-velocity updates) - MediaPipe pose and hand landmarks from the device camera, placement check, per-hand Kalman filter with the Joy-Con - No controller connected: bare camera hands with Quest-style gestures, 1€ filtered, rendered as a white glove model built from a distance field - SteamVR safe mode cleared before each start so one crash no longer blocks the driver --------- Co-authored-by: MaxsTechReview <6025299+maxjivi05@users.noreply.github.com> Co-authored-by: informalTechCode <informaltechyt@gmail.com> Co-authored-by: connor g <connorsspam1992@gmail.com> Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Co-authored-by: Yassine Gherbi <y.gherbi.dev@gmail.com> Co-authored-by: Resul Övür <92828257+ovurrsl@users.noreply.github.com> Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com> Co-authored-by: Kirk <kirtoy.junsay@gmail.com> Co-authored-by: Kirk <12691869+KirkPatrickJunsay@users.noreply.github.com> Co-authored-by: xXJsonDeruloXx <danielhimebauch@gmail.com> Co-authored-by: angelrenefm <17342133+angelrenefm@users.noreply.github.com> Co-authored-by: asdf17128 <asdf17128@gmail.com> Co-authored-by: siyuan.qsy <siyuan.qsy@alibaba-inc.com> Co-authored-by: Dantalian <wsu091227@qq.com> Co-authored-by: Dantalian <Dantalian-dev@users.noreply.github.com> Co-authored-by: The412Banner <the412banner@users.noreply.github.com> Co-authored-by: The412Banner <205237651+The412Banner@users.noreply.github.com> Co-authored-by: FX1922 <120828496+FX192@users.noreply.github.com> Co-authored-by: Muaz Kadan <32595066+muazkadan@users.noreply.github.com> Co-authored-by: kingokksa <47888451+kingokksa@users.noreply.github.com> Co-authored-by: Jamie Schenker <jam1esch3nk3r@gmail.com> Co-authored-by: Jagger15 <jagger2333@live.com>
This commit is contained in:
221 files changed
+24369
-491
No files matched your search
@@ -0,0 +1,21 @@
|
||||
// Compile with the installed SDK's headers to verify the two mode vocabularies.
|
||||
#include "viture_protocol_public.h"
|
||||
#include "../../main/cpp/viture/display_modes.h"
|
||||
using namespace droiddeck::viture;
|
||||
|
||||
#define CHECK_MODE(standard, native) \
|
||||
static_assert(to_native_mode(standard) == native); \
|
||||
static_assert(from_native_mode(native) == standard)
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_1920_1080_60HZ, VITURE_NATIVE_DISPLAY_MODE_1920_1080_60HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_1920_1080_90HZ, VITURE_NATIVE_DISPLAY_MODE_1920_1080_90HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_1920_1080_120HZ, VITURE_NATIVE_DISPLAY_MODE_1920_1080_120HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_3840_1080_60HZ, VITURE_NATIVE_DISPLAY_MODE_3D_SBS_3840_1080_60HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_3840_1080_90HZ, VITURE_NATIVE_DISPLAY_MODE_3D_SBS_3840_1080_90HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_1920_1200_60HZ, VITURE_NATIVE_DISPLAY_MODE_1920_1200_60HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_1920_1200_90HZ, VITURE_NATIVE_DISPLAY_MODE_1920_1200_90HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_1920_1200_120HZ, VITURE_NATIVE_DISPLAY_MODE_1920_1200_120HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_3840_1200_60HZ, VITURE_NATIVE_DISPLAY_MODE_3D_SBS_3840_1200_60HZ);
|
||||
CHECK_MODE(VITURE_DISPLAY_MODE_3840_1200_90HZ, VITURE_NATIVE_DISPLAY_MODE_3D_SBS_3840_1200_90HZ);
|
||||
static_assert(from_native_mode(VITURE_NATIVE_DISPLAY_MODE_ULTRAWIDE_3840_1080_60HZ) < 0);
|
||||
static_assert(from_native_mode(VITURE_NATIVE_DISPLAY_MODE_ULTRAWIDE_3840_1200_90HZ) < 0);
|
||||
static_assert(to_native_mode(0) < 0);
|
||||
@@ -0,0 +1,235 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class GlassesCallbacksTest {
|
||||
private fun field(name: String) = Glasses::class.java.getDeclaredField(name).apply { isAccessible = true }
|
||||
private fun generation(value: Long) = field("linkGeneration").setLong(null, value)
|
||||
private fun listener(generation: Long): VitureLink.Listener =
|
||||
Glasses::class.java.getDeclaredMethod("linkListener", Long::class.javaPrimitiveType).apply { isAccessible = true }
|
||||
.invoke(Glasses, generation) as VitureLink.Listener
|
||||
|
||||
@Before fun prepare() {
|
||||
field("handler").set(null, Handler(Looper.getMainLooper()))
|
||||
generation(1)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
|
||||
state.value = GlassesState(connected = true, brightness = 2, displayId = 2, width = 640, height = 480, noSignal = true)
|
||||
field("imuPackets").setLong(null, 0)
|
||||
field("probe").set(null, null)
|
||||
}
|
||||
|
||||
private fun brightness(value: Int) = VitureProtocol.Frame(
|
||||
VitureProtocol.MAGIC_REPORT, VitureProtocol.EVENT_BRIGHTNESS, byteArrayOf(value.toByte()), true,
|
||||
)
|
||||
|
||||
@Test fun statusIncludesCalibrationAndTransportChecksumDiagnostics() {
|
||||
val previousLink = field("link").get(null)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val calibration = field("_calibration").get(null) as MutableStateFlow<GlassesCalibrationState>
|
||||
val previousCalibration = calibration.value
|
||||
try {
|
||||
field("link").set(null, object : GlassesLink {
|
||||
override val hasImu = true
|
||||
override val badFrames = 7L
|
||||
override fun send(msgId: Int, payload: ByteArray) = false
|
||||
override fun close() {}
|
||||
})
|
||||
calibration.value = GlassesCalibrationState(supported = true, saved = true,
|
||||
phase = CalibrationPhase.SAVED, progress = 100)
|
||||
val status = Glasses.status()
|
||||
assertEquals(7L, status.getLong("badFrames"))
|
||||
val reportedCalibration = status.getJSONObject("calibration")
|
||||
assertTrue(reportedCalibration.getBoolean("supported"))
|
||||
assertTrue(reportedCalibration.getBoolean("saved"))
|
||||
assertEquals("SAVED", reportedCalibration.getString("phase"))
|
||||
assertEquals(100, reportedCalibration.getInt("progress"))
|
||||
field("link").set(null, null)
|
||||
assertEquals(0L, Glasses.status().getLong("badFrames"))
|
||||
} finally {
|
||||
field("link").set(null, previousLink)
|
||||
calibration.value = previousCalibration
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun queuedDisconnectCannotCloseAReplacementLink() {
|
||||
val old = listener(1)
|
||||
old.onLost()
|
||||
old.onLost()
|
||||
generation(2)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertTrue(Glasses.state.value.connected)
|
||||
}
|
||||
|
||||
@Test fun queuedReportCannotOverwriteReplacementState() {
|
||||
listener(1).onFrame(brightness(7))
|
||||
generation(2)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(2, Glasses.state.value.brightness)
|
||||
listener(2).onFrame(brightness(4))
|
||||
listener(1).onFrame(brightness(8))
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(4, Glasses.state.value.brightness)
|
||||
}
|
||||
|
||||
@Test fun oldImuPacketsCannotReachTheNewProbe() {
|
||||
var samples = 0
|
||||
val probe: (ByteArray?, FloatArray?) -> Unit = { _, _ -> samples++ }
|
||||
field("probe").set(null, probe)
|
||||
generation(2)
|
||||
listener(1).onImu(ByteArray(12), 0)
|
||||
assertEquals(0, samples)
|
||||
assertEquals(0L, field("imuPackets").getLong(null))
|
||||
listener(2).onImu(ByteArray(12), 0)
|
||||
assertEquals(1, samples)
|
||||
assertEquals(1L, field("imuPackets").getLong(null))
|
||||
}
|
||||
|
||||
@Test fun rayNeoPoseUsesTheExistingSinkAndRejectsAnOldConnection() {
|
||||
fun rayneo(gen: Long) = Glasses::class.java.getDeclaredMethod("rayNeoListener", Long::class.javaPrimitiveType)
|
||||
.apply { isAccessible = true }.invoke(Glasses, gen) as RayNeoLink.Listener
|
||||
var delivered: FloatArray? = null
|
||||
var timestamp = 0L
|
||||
val sink = Glasses.PoseSink { x, y, z, w, time ->
|
||||
delivered = floatArrayOf(x, y, z, w)
|
||||
timestamp = time
|
||||
}
|
||||
field("sink").set(null, sink)
|
||||
try {
|
||||
val q = floatArrayOf(0f, 0f, 0.6f, 0.8f)
|
||||
rayneo(0).onPose(ByteArray(64), q, 99)
|
||||
assertEquals(null, delivered)
|
||||
rayneo(1).onPose(ByteArray(64), q, 123)
|
||||
org.junit.Assert.assertArrayEquals(q, delivered, 0f)
|
||||
assertEquals(123L, timestamp)
|
||||
assertEquals(1L, field("imuPackets").getLong(null))
|
||||
rayneo(0).onLost()
|
||||
generation(2)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertTrue(Glasses.state.value.connected)
|
||||
} finally {
|
||||
field("sink").set(null, null)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun deviceModeReportsCannotConfirmThePhysicalStereoSignal() {
|
||||
listener(1).onFrame(VitureProtocol.Frame(
|
||||
VitureProtocol.MAGIC_REPORT, VitureProtocol.EVENT_DISPLAY, byteArrayOf(0x32), true,
|
||||
))
|
||||
listener(1).onFrame(VitureProtocol.Frame(
|
||||
VitureProtocol.MAGIC_REPORT, VitureProtocol.MSG_GET_DISPLAY, byteArrayOf(0, 0x32), true,
|
||||
))
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertFalse(Glasses.state.value.sbs)
|
||||
assertTrue(Glasses.state.value.noSignal)
|
||||
assertEquals(640, Glasses.state.value.width)
|
||||
}
|
||||
|
||||
@Test fun xrealCallbacksUseSharedPoseAndIgnoreDetachedDeviceValues() {
|
||||
fun xreal(gen: Long) = Glasses::class.java.getDeclaredMethod("xrealListener", Long::class.javaPrimitiveType)
|
||||
.apply { isAccessible = true }.invoke(Glasses, gen) as XrealLink.Listener
|
||||
var delivered: FloatArray? = null
|
||||
field("sink").set(null, Glasses.PoseSink { x, y, z, w, _ -> delivered = floatArrayOf(x, y, z, w) })
|
||||
try {
|
||||
val q = floatArrayOf(0f, 0f, 0.6f, 0.8f)
|
||||
xreal(0).onPose(ByteArray(128), q, 1)
|
||||
assertEquals(null, delivered)
|
||||
xreal(1).onPose(ByteArray(128), q, 2)
|
||||
org.junit.Assert.assertArrayEquals(q, delivered, 0f)
|
||||
xreal(1).onValue(VitureProtocol.MSG_BRIGHTNESS, 8)
|
||||
xreal(1).onUnavailable(true)
|
||||
generation(2)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(2, Glasses.state.value.brightness)
|
||||
assertTrue(Glasses.state.value.connected)
|
||||
} finally { field("sink").set(null, null) }
|
||||
}
|
||||
|
||||
@Test fun cancelledCalibrationCallbacksCannotResurrectOrOverwriteTheProfile() {
|
||||
val rayneo = Glasses::class.java.getDeclaredMethod("rayNeoListener", Long::class.javaPrimitiveType)
|
||||
.apply { isAccessible = true }.invoke(Glasses, 1L) as RayNeoLink.Listener
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val calibration = field("_calibration").get(null) as MutableStateFlow<GlassesCalibrationState>
|
||||
calibration.value = GlassesCalibrationState(supported = true, saved = true, phase = CalibrationPhase.SETTLING)
|
||||
field("calibrationRevision").setLong(null, 1)
|
||||
rayneo.onCalibrationSaved(1, RayNeoCalibration(Vec3(0f, 0f, 0f), Quaternion.IDENTITY))
|
||||
rayneo.onCalibration(1, CalibrationPhase.SAVED, 100)
|
||||
field("calibrationRevision").setLong(null, 2)
|
||||
calibration.value = calibration.value.copy(phase = CalibrationPhase.IDLE)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(CalibrationPhase.IDLE, calibration.value.phase)
|
||||
assertTrue(calibration.value.saved)
|
||||
}
|
||||
|
||||
@Test fun disconnectedRayNeoBoardCannotChangeTheCurrentCalibrationKey() {
|
||||
val rayneo = Glasses::class.java.getDeclaredMethod("rayNeoListener", Long::class.javaPrimitiveType)
|
||||
.apply { isAccessible = true }.invoke(Glasses, 1L) as RayNeoLink.Listener
|
||||
val previous = field("calibrationKey").get(null)
|
||||
try {
|
||||
field("calibrationKey").set(null, "current-profile")
|
||||
rayneo.onDeviceInfo(0x40, true)
|
||||
generation(2)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals("current-profile", field("calibrationKey").get(null))
|
||||
} finally { field("calibrationKey").set(null, previous) }
|
||||
}
|
||||
|
||||
@Test fun sdkCallbacksCannotCrossConnectionGenerationsOrFakeStereoConfirmation() {
|
||||
fun sdk(gen: Long) = Glasses::class.java.getDeclaredMethod("sdkListener", Long::class.javaPrimitiveType)
|
||||
.apply { isAccessible = true }.invoke(Glasses, gen) as VitureSdkLink.Listener
|
||||
var samples = 0
|
||||
val probe: (ByteArray?, FloatArray?) -> Unit = { raw, pose ->
|
||||
assertEquals(null, raw)
|
||||
assertEquals(4, pose!!.size)
|
||||
samples++
|
||||
}
|
||||
field("probe").set(null, probe)
|
||||
try {
|
||||
sdk(0).onPose(floatArrayOf(0f, 0f, 0f, 1f), 100)
|
||||
sdk(1).onPose(floatArrayOf(0f, 0f, 0f, 1f), 101)
|
||||
assertEquals(1, samples)
|
||||
sdk(0).onValue(VitureProtocol.MSG_GET_BRIGHTNESS, 8)
|
||||
sdk(1).onValue(VitureProtocol.MSG_GET_DISPLAY, 0x42)
|
||||
sdk(0).onLost()
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(2, Glasses.state.value.brightness)
|
||||
assertFalse(Glasses.state.value.sbs)
|
||||
assertTrue(Glasses.state.value.connected)
|
||||
} finally { field("probe").set(null, null) }
|
||||
}
|
||||
|
||||
@Test fun rejectedTrackingStartDoesNotAdvertiseTracking() {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
|
||||
state.value = state.value.copy(tracking = false)
|
||||
val rejected = object : GlassesLink {
|
||||
override val hasImu = true
|
||||
override fun send(msgId: Int, payload: ByteArray) = false
|
||||
override fun close() {}
|
||||
}
|
||||
field("link").set(null, rejected)
|
||||
field("sink").set(null, Glasses.PoseSink { _, _, _, _, _ -> })
|
||||
try {
|
||||
Glasses::class.java.getDeclaredMethod("updateImu").apply { isAccessible = true }.invoke(Glasses)
|
||||
assertFalse(Glasses.state.value.tracking)
|
||||
} finally {
|
||||
field("link").set(null, null)
|
||||
field("sink").set(null, null)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.hardware.display.DeviceProductInfo
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.view.Display
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.ShadowDisplay
|
||||
import org.robolectric.shadows.ShadowDisplayManager
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [31, 34], manifest = Config.NONE, shadows = [GlassesDisplayTest.ProductMetadataDisplay::class])
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class GlassesDisplayTest {
|
||||
@Test fun connectedGlassesUseTheOnlyExternalPanelWhenProductMetadataIsMissing() {
|
||||
val panel = display("External Display")
|
||||
assertNull(panel.deviceProductInfo)
|
||||
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(panel), usbPresent = true)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun missingProductMetadataDoesNotIdentifyGlassesWithoutUsb() {
|
||||
val panel = display("External Display")
|
||||
|
||||
assertNull(Glasses.selectGlassesDisplay(listOf(panel), usbPresent = false))
|
||||
}
|
||||
|
||||
@Test fun connectedGlassesDoNotChooseBetweenMultipleUnidentifiedPanels() {
|
||||
val first = display("External Display 1")
|
||||
val second = display("External Display 2")
|
||||
|
||||
assertNull(Glasses.selectGlassesDisplay(listOf(first, second), usbPresent = true))
|
||||
}
|
||||
|
||||
@Test fun primaryAndNonPresentationDisplaysCannotBecomeTheGlassesOutput() {
|
||||
val primary = displayManager().getDisplay(Display.DEFAULT_DISPLAY)
|
||||
shadowOf(primary).setName("RayNeo Air 4 Pro")
|
||||
shadowOf(primary).setFlags(Display.FLAG_PRESENTATION)
|
||||
val nonPresentation = display("RayNeo Air 4 Pro", flags = 0)
|
||||
|
||||
assertNull(Glasses.selectGlassesDisplay(listOf(primary, nonPresentation), usbPresent = true))
|
||||
}
|
||||
|
||||
@Test fun rayNeoDisplayNameIsRecognizedEvenWhenProductMetadataHasAGenericName() {
|
||||
val panel = display("RayNeo Air 4 Pro", productName = "External Monitor")
|
||||
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(panel), usbPresent = false)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun rayNeoProductMetadataIsRecognizedWhenAndroidUsesAGenericDisplayName() {
|
||||
val panel = display("External Display", productName = "RayNeo Air 3")
|
||||
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(panel), usbPresent = false)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun anUnrelatedNamedMonitorIsNotClaimedByConnectedGlasses() {
|
||||
val monitor = display("External Display", productName = "TCL TV")
|
||||
|
||||
assertNull(Glasses.selectGlassesDisplay(listOf(monitor), usbPresent = true))
|
||||
}
|
||||
|
||||
@Test fun namedRayNeoDisplayWorksWithoutUsbControlPermission() {
|
||||
val panel = display("RayNeo GT")
|
||||
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(panel), usbPresent = false)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun blankProductNameRetainsTheSinglePanelUsbFallback() {
|
||||
val panel = display("External Display", productName = " ")
|
||||
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(panel), usbPresent = true)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun brandedGlassesWinOverAnUnidentifiedSecondDisplay() {
|
||||
val monitor = display("External Display")
|
||||
val glasses = display("RayNeo Air 2s")
|
||||
|
||||
assertEquals(glasses.displayId, Glasses.selectGlassesDisplay(listOf(monitor, glasses), usbPresent = true)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun gtMaxSmartGlassesEdidUsesTheSameOutputSelectionAsViture() {
|
||||
val panel = display("SmartGlasses", productName = "SmartGlasses", productId = "980")
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(panel), usbPresent = true)?.displayId)
|
||||
// The display remains usable while USB control reconnects during a mode switch.
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(panel), usbPresent = false)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun gtMaxMetadataIsRecognizedBehindGenericAndroidNameAndOtherPanels() {
|
||||
val monitor = display("External Display", productName = "TCL TV")
|
||||
val panel = display("External Display", productName = "SmartGlasses", productId = "980")
|
||||
assertEquals(panel.displayId, Glasses.selectGlassesDisplay(listOf(monitor, panel), usbPresent = true)?.displayId)
|
||||
}
|
||||
|
||||
@Test fun genericSmartGlassesNameDoesNotClaimUnverifiedProducts() {
|
||||
val unknown = display("SmartGlasses", productName = "SmartGlasses")
|
||||
assertNull(Glasses.selectGlassesDisplay(listOf(unknown), usbPresent = true))
|
||||
assertFalse(RayNeoModel.isDisplayProduct("SmartGlasses", "OTHER", "980"))
|
||||
assertFalse(RayNeoModel.isDisplayProduct("TCL TV", "TCL", "980"))
|
||||
}
|
||||
|
||||
private fun displayManager(): DisplayManager =
|
||||
RuntimeEnvironment.getApplication().getSystemService(DisplayManager::class.java)
|
||||
|
||||
private fun display(name: String, productName: String? = null, flags: Int = Display.FLAG_PRESENTATION,
|
||||
productId: String = "test-panel"): Display {
|
||||
val external = ReflectionHelpers.getStaticField<Int>(Display::class.java, "TYPE_EXTERNAL")
|
||||
val id = ShadowDisplayManager.addDisplay("w1920dp-h1080dp", external)
|
||||
return displayManager().getDisplay(id).also { panel ->
|
||||
shadowOf(panel).setName(name)
|
||||
shadowOf(panel).setFlags(flags)
|
||||
Shadow.extract<ProductMetadataDisplay>(panel).productMetadata = productName?.let {
|
||||
DeviceProductInfo(it, "TCL", productId, 2024, DeviceProductInfo.CONNECTION_TO_SINK_DIRECT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Robolectric exposes display names and flags, but has no product-metadata setter.
|
||||
@Implements(Display::class)
|
||||
class ProductMetadataDisplay : ShadowDisplay() {
|
||||
var productMetadata: DeviceProductInfo? = null
|
||||
|
||||
@Implementation(minSdk = 31)
|
||||
protected fun getDeviceProductInfo(): DeviceProductInfo? = productMetadata
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class GlassesFovTest {
|
||||
@Test fun native1sAspectUsesThePerEyeResolution() {
|
||||
val native = GlassesState(width = 1920, height = 1200, diagonalFovDeg = 52f)
|
||||
val stereo = native.copy(width = 3840, sbs = true)
|
||||
val widescreen = native.copy(height = 1080)
|
||||
assertEquals(native.verticalFovDeg, stereo.verticalFovDeg, 0.001f)
|
||||
assertTrue(native.verticalFovDeg > widescreen.verticalFovDeg)
|
||||
assertEquals(widescreen.verticalFovDeg, native.copy(width = 0, height = 0).verticalFovDeg, 0.001f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.view.Display
|
||||
import androidx.compose.runtime.MutableState
|
||||
import com.droiddeck.launcher.SessionActivity
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.ShadowDisplay
|
||||
import org.robolectric.shadows.ShadowDisplayManager
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [31, 34], manifest = Config.NONE, shadows = [GlassesMirrorPacingTest.ModeDisplay::class])
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class GlassesMirrorPacingTest {
|
||||
private lateinit var activity: SessionActivity
|
||||
private val connected = GlassesState(present = true, displayId = 2, width = 1920, height = 1080)
|
||||
private val settings = GlassesSettings(gameOnGlasses = false, refreshHz = 60)
|
||||
|
||||
@Before fun setup() {
|
||||
activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
val display = activity.getSystemService(DisplayManager::class.java).getDisplay(Display.DEFAULT_DISPLAY)
|
||||
ReflectionHelpers.setField(activity, "mBase", activity.baseContext.createDisplayContext(display))
|
||||
val panel = Shadow.extract<ModeDisplay>(activity.display)
|
||||
panel.supported = arrayOf(mode(1, 60f), mode(2, 90f), mode(3, 120f))
|
||||
panel.current = panel.supported.last()
|
||||
SessionState.fpsLimit = 0
|
||||
}
|
||||
|
||||
@After fun cleanup() { SessionState.fpsLimit = 0 }
|
||||
|
||||
@Test fun connectingChangingRateAndDisconnectingUpdatePhonePacingWithoutSavingPreferences() {
|
||||
apply(connected, settings)
|
||||
expect(1, 60f, 60)
|
||||
apply(connected, settings.copy(refreshHz = 90))
|
||||
expect(2, 90f, 90)
|
||||
apply(GlassesState(), settings)
|
||||
expect(3, 120f, 0)
|
||||
assertEquals(0, SessionState.fpsLimit)
|
||||
}
|
||||
|
||||
@Test fun lowerSavedLimitSurvivesMirroringAndStalePhoneRefreshReports() {
|
||||
SessionState.fpsLimit = 30
|
||||
apply(connected, settings)
|
||||
expect(1, 60f, 30)
|
||||
apply(connected.copy(hz = 120f), settings)
|
||||
expect(1, 60f, 30)
|
||||
apply(GlassesState(), settings)
|
||||
expect(3, 120f, 30)
|
||||
assertEquals(30, SessionState.fpsLimit)
|
||||
}
|
||||
|
||||
private fun apply(state: GlassesState, config: GlassesSettings) {
|
||||
ReflectionHelpers.getField<MutableState<GlassesState>>(activity, "glasses\$delegate").value = state
|
||||
ReflectionHelpers.getField<MutableState<GlassesSettings>>(activity, "glassesSettings\$delegate").value = config
|
||||
ReflectionHelpers.callInstanceMethod<Void>(activity, "applyOutputPacing")
|
||||
}
|
||||
|
||||
private fun expect(mode: Int, hz: Float, fps: Int) {
|
||||
assertEquals(mode, activity.window.attributes.preferredDisplayModeId)
|
||||
assertEquals(hz, ReflectionHelpers.callInstanceMethod<Float>(activity, "refreshHz"), 0.01f)
|
||||
assertEquals(fps, ReflectionHelpers.callInstanceMethod<Int>(activity, "outputFpsLimit").toInt())
|
||||
}
|
||||
|
||||
private fun mode(id: Int, hz: Float): Display.Mode = ShadowDisplayManager.ModeBuilder.modeBuilder(id)
|
||||
.setWidth(1080).setHeight(2520).setRefreshRate(hz).build()
|
||||
|
||||
@Implements(Display::class)
|
||||
class ModeDisplay : ShadowDisplay() {
|
||||
lateinit var current: Display.Mode
|
||||
var supported = emptyArray<Display.Mode>()
|
||||
@Implementation protected fun getMode(): Display.Mode = current
|
||||
@Implementation protected fun getSupportedModes(): Array<Display.Mode> = supported
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import com.droiddeck.launcher.glasses.GlassesModeState.Confirmation
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class GlassesModeStateTest {
|
||||
private val device = GlassesModeState.Device(0x101d, "pro")
|
||||
private val state = GlassesModeState(20_000).apply { updateDevice(device, 0) }
|
||||
|
||||
@Test fun transientFallbackDoesNotRejectAWorkingStereoTransition() {
|
||||
state.sent(true, 100)
|
||||
assertEquals(Confirmation.WAITING, state.observe(false, 2_000))
|
||||
assertEquals(Confirmation.WAITING, state.observe(false, 19_000))
|
||||
assertFalse(state.restorePending)
|
||||
assertEquals(Confirmation.CONFIRMED, state.observe(true, 19_500))
|
||||
assertFalse(state.pendingStereo)
|
||||
assertEquals(Confirmation.NONE, state.observe(false, 20_100))
|
||||
}
|
||||
|
||||
@Test fun missingSignalAtDeadlineRequestsOneFallback() {
|
||||
state.sent(true, 100)
|
||||
assertEquals(Confirmation.WAITING, state.observe(false, 20_099))
|
||||
assertEquals(Confirmation.FAILED, state.observe(false, 20_100))
|
||||
assertTrue(state.restorePending)
|
||||
assertEquals(Confirmation.NONE, state.observe(false, 40_000))
|
||||
state.sent(false, 40_001)
|
||||
assertFalse(state.restorePending)
|
||||
assertFalse(state.stopVr())
|
||||
}
|
||||
|
||||
@Test fun stoppingVrBeforeStereoAppearsStillRestores2d() {
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
assertTrue(state.stopVr())
|
||||
assertTrue(state.restorePending)
|
||||
state.sent(false, 200)
|
||||
assertFalse(state.pendingStereo)
|
||||
assertFalse(state.restorePending)
|
||||
assertFalse(state.stopVr())
|
||||
assertEquals(Confirmation.NONE, state.observe(true, 400))
|
||||
}
|
||||
|
||||
@Test fun failedRevertRemainsPendingUntilAWriteSucceeds() {
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
state.observe(true, 200)
|
||||
state.stopVr()
|
||||
state.cancelPending()
|
||||
assertTrue(state.restorePending)
|
||||
assertTrue(state.stopVr())
|
||||
state.sent(false, 300)
|
||||
assertFalse(state.restorePending)
|
||||
}
|
||||
|
||||
@Test fun manualModeTakesOwnershipFromVr() {
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
state.manual()
|
||||
state.sent(true, 200)
|
||||
assertFalse(state.stopVr())
|
||||
assertFalse(state.restorePending)
|
||||
assertEquals(Confirmation.CONFIRMED, state.observe(true, 300))
|
||||
}
|
||||
|
||||
@Test fun manualRetryGetsItsOwnConfirmationWindow() {
|
||||
state.sent(true, 100)
|
||||
assertEquals(Confirmation.FAILED, state.observe(false, 20_100))
|
||||
state.sent(false, 20_101)
|
||||
state.manual()
|
||||
state.sent(true, 30_000)
|
||||
assertEquals(Confirmation.WAITING, state.observe(false, 49_999))
|
||||
assertEquals(Confirmation.CONFIRMED, state.observe(true, 50_000))
|
||||
assertFalse(state.restorePending)
|
||||
}
|
||||
|
||||
@Test fun cancelledOrDisconnectedRequestCannotFailLater() {
|
||||
state.sent(true, 100)
|
||||
state.cancelPending()
|
||||
assertFalse(state.pendingStereo)
|
||||
assertEquals(Confirmation.NONE, state.observe(false, 20_100))
|
||||
assertFalse(state.restorePending)
|
||||
}
|
||||
|
||||
@Test fun manual2dCancelsThePendingStereoDeadline() {
|
||||
state.sent(true, 100)
|
||||
state.manual()
|
||||
state.sent(false, 200)
|
||||
assertFalse(state.pendingStereo)
|
||||
assertEquals(Confirmation.NONE, state.observe(false, 20_100))
|
||||
}
|
||||
|
||||
@Test fun modeReenumerationPreservesDeadlineAndVrOwnership() {
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
assertFalse(state.updateDevice(null, 1_000))
|
||||
assertEquals(Confirmation.WAITING, state.observe(false, 1_000))
|
||||
assertFalse(state.updateDevice(device, 2_000))
|
||||
assertEquals(20_100L, state.nextCheckAt)
|
||||
assertEquals(Confirmation.CONFIRMED, state.observe(true, 3_000))
|
||||
assertTrue(state.stopVr())
|
||||
}
|
||||
|
||||
@Test fun absentAtDeadlineCancelsWithoutFailureOrLateRestore() {
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
state.updateDevice(null, 1_000)
|
||||
state.stopVr()
|
||||
assertEquals(Confirmation.NONE, state.observe(false, 20_100))
|
||||
assertFalse(state.restorePending)
|
||||
assertNull(state.nextCheckAt)
|
||||
assertFalse(state.updateDevice(device, 40_000))
|
||||
assertFalse(state.stopVr())
|
||||
}
|
||||
|
||||
@Test fun differentModelOrKnownSerialCannotInheritARequest() {
|
||||
listOf(GlassesModeState.Device(0x1131, "pro"), device.copy(serial = "other")).forEach { other ->
|
||||
state.updateDevice(device, 0)
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
state.updateDevice(null, 1_000)
|
||||
assertTrue(state.updateDevice(other, 2_000))
|
||||
assertFalse(state.pendingStereo)
|
||||
assertFalse(state.stopVr())
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun permissionPendingReconnectKeepsTheKnownIdentity() {
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
state.updateDevice(null, 1_000)
|
||||
assertFalse(state.updateDevice(device.copy(serial = null), 2_000))
|
||||
assertTrue(state.pendingStereo)
|
||||
assertTrue(state.updateDevice(device.copy(serial = "other"), 3_000))
|
||||
assertFalse(state.pendingStereo)
|
||||
assertFalse(state.stopVr())
|
||||
}
|
||||
|
||||
@Test fun sameDeviceReconnectDoesNotResetFailureStatus() {
|
||||
state.sent(true, 100)
|
||||
assertEquals(Confirmation.FAILED, state.observe(false, 20_100))
|
||||
state.sent(false, 20_101)
|
||||
assertFalse(state.updateDevice(null, 21_000))
|
||||
assertFalse(state.updateDevice(device, 22_000))
|
||||
}
|
||||
|
||||
@Test fun confirmedVrCannotRestoreAfterAProlongedUnplug() {
|
||||
state.sent(true, 100)
|
||||
state.startVr()
|
||||
state.observe(true, 200)
|
||||
state.updateDevice(null, 1_000)
|
||||
assertTrue(state.stopVr())
|
||||
assertEquals(21_000L, state.nextCheckAt)
|
||||
state.updateDevice(device, 22_000)
|
||||
assertFalse(state.restorePending)
|
||||
assertFalse(state.stopVr())
|
||||
assertNull(state.nextCheckAt)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.view.Display
|
||||
import android.view.SurfaceHolder
|
||||
import android.view.SurfaceView
|
||||
import android.view.View
|
||||
import androidx.compose.runtime.MutableState
|
||||
import com.droiddeck.launcher.SessionActivity
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.shadows.ShadowDisplayManager
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import org.robolectric.util.ReflectionHelpers.ClassParameter.from
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [31, 34], manifest = Config.NONE)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class GlassesOutputHandoffTest {
|
||||
private lateinit var activity: SessionActivity
|
||||
private lateinit var phone: SurfaceView
|
||||
private lateinit var screen: GlassesScreen
|
||||
|
||||
@Before fun setup() {
|
||||
// Exercise the output lifecycle without starting the native Linux runtime.
|
||||
activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
phone = SurfaceView(activity)
|
||||
ReflectionHelpers.setField(activity, "surfaceView", phone)
|
||||
ReflectionHelpers.setField(activity, "ended", true)
|
||||
val app = RuntimeEnvironment.getApplication()
|
||||
val id = ShadowDisplayManager.addDisplay("w1920dp-h1080dp")
|
||||
val display = app.getSystemService(DisplayManager::class.java).getDisplay(id)
|
||||
shadowOf(display).setFlags(Display.FLAG_PRESENTATION)
|
||||
screen = GlassesScreen(activity, display, 60, activity)
|
||||
ReflectionHelpers.setField(activity, "glassesScreen", screen)
|
||||
}
|
||||
|
||||
@Test fun selectingGlassesDoesNotHidePhoneOrClaimOutputBeforeAttachment() {
|
||||
assertFalse(active())
|
||||
assertEquals(View.VISIBLE, phone.visibility)
|
||||
attach(phone.holder) // A late phone callback must not complete the handoff.
|
||||
assertFalse(active())
|
||||
assertEquals(View.VISIBLE, phone.visibility)
|
||||
}
|
||||
|
||||
@Test fun attachingGlassesRemovesPhoneSurfaceButPreservesItsInputLayout() {
|
||||
phone.layout(0, 0, 1200, 800)
|
||||
attach(screen.holder)
|
||||
|
||||
assertTrue(active())
|
||||
assertEquals(View.INVISIBLE, phone.visibility)
|
||||
assertEquals(1200, phone.width)
|
||||
assertEquals(800, phone.height)
|
||||
}
|
||||
|
||||
@Test fun latePhoneDestructionDoesNotResetTheGlassesOutput() {
|
||||
attach(screen.holder)
|
||||
ReflectionHelpers.setField(activity, "surfaceW", 3840)
|
||||
ReflectionHelpers.setField(activity, "surfaceH", 1080)
|
||||
|
||||
activity.surfaceDestroyed(phone.holder)
|
||||
|
||||
assertTrue(active())
|
||||
assertEquals(3840, ReflectionHelpers.getField<Int>(activity, "surfaceW"))
|
||||
assertEquals(1080, ReflectionHelpers.getField<Int>(activity, "surfaceH"))
|
||||
}
|
||||
|
||||
@Test fun disconnectRestoresPhoneAndRejectsLateGlassesAttachment() {
|
||||
attach(screen.holder)
|
||||
ReflectionHelpers.callInstanceMethod<Void>(activity, "restorePhoneOutput", from(GlassesScreen::class.java, screen))
|
||||
|
||||
assertFalse(active())
|
||||
assertEquals(View.VISIBLE, phone.visibility)
|
||||
attach(screen.holder)
|
||||
assertFalse(active())
|
||||
assertEquals(View.VISIBLE, phone.visibility)
|
||||
}
|
||||
|
||||
@Test fun losingGlassesSurfaceClearsThePlayingHint() {
|
||||
attach(screen.holder)
|
||||
activity.surfaceDestroyed(screen.holder)
|
||||
assertFalse(active())
|
||||
}
|
||||
|
||||
private fun attach(holder: SurfaceHolder) {
|
||||
ReflectionHelpers.callInstanceMethod<Void>(activity, "onOutputAttached", from(SurfaceHolder::class.java, holder))
|
||||
}
|
||||
|
||||
private fun active(): Boolean =
|
||||
ReflectionHelpers.getField<MutableState<Boolean>>(activity, "glassesOutputActive\$delegate").value
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import com.droiddeck.launcher.gpu.Cardboard
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class GlassesRenderingTest {
|
||||
private val ultra = GlassesState(
|
||||
model = "VITURE Luma Ultra", connected = true, controllable = true,
|
||||
displayId = 2, width = 1920, height = 1200,
|
||||
)
|
||||
private val base = Cardboard(ipdMm = 67)
|
||||
|
||||
@Test fun mirroredRefreshUsesTheSelectedGlassesRateEvenBeforeTheDisplaySwitches() {
|
||||
val settings = GlassesSettings(gameOnGlasses = false, refreshHz = 60)
|
||||
assertEquals(60, GlassesRendering.mirrorRefreshLimit(settings, ultra.copy(hz = 120f), false))
|
||||
assertEquals(90, GlassesRendering.mirrorRefreshLimit(settings.copy(refreshHz = 90), ultra, false))
|
||||
assertEquals(60, GlassesRendering.mirrorRefreshLimit(settings, ultra.copy(connected = false), false))
|
||||
}
|
||||
|
||||
@Test fun mirroredStereoUsesTheStereoRefreshAndItsHardwareLimit() {
|
||||
val settings = GlassesSettings(refreshHz = 120, sbsHz = 90)
|
||||
assertEquals(60, GlassesRendering.mirrorRefreshLimit(settings, ultra.copy(sbs = true), false))
|
||||
assertEquals(90, GlassesRendering.mirrorRefreshLimit(settings, ultra.copy(sbs = true, sbs90 = true), false))
|
||||
}
|
||||
|
||||
@Test fun disconnectOrDedicatedOutputRemovesTheMirrorCap() {
|
||||
val settings = GlassesSettings(refreshHz = 60)
|
||||
assertEquals(0, GlassesRendering.mirrorRefreshLimit(settings, GlassesState(), false))
|
||||
assertEquals(0, GlassesRendering.mirrorRefreshLimit(settings, ultra.copy(noSignal = true), false))
|
||||
assertEquals(0, GlassesRendering.mirrorRefreshLimit(settings, ultra, true))
|
||||
}
|
||||
|
||||
@Test fun mirrorCapPreservesLowerLimitsAndRestoresTheRequestedLimitAfterDisconnect() {
|
||||
for (requested in listOf(0, 30, 60, 90, 120)) {
|
||||
assertEquals(if (requested == 30) 30 else 60, GlassesRendering.fpsLimit(requested, 60))
|
||||
assertEquals(requested, GlassesRendering.fpsLimit(requested, 0))
|
||||
}
|
||||
}
|
||||
|
||||
private fun render(settings: GlassesSettings, state: GlassesState = ultra, steamVr: Boolean = false) =
|
||||
GlassesRendering.cardboard(base, settings, state, outputActive = true, steamVrRunning = steamVr)
|
||||
|
||||
@Test fun steamVrDefaultsToOneTrackedEyeWithoutRequestingStereo() {
|
||||
val settings = GlassesSettings()
|
||||
assertFalse(settings.auto3d)
|
||||
val projection = render(settings, steamVr = true)
|
||||
assertTrue(projection.enabled)
|
||||
assertTrue(projection.mono)
|
||||
assertTrue(projection.headTracking)
|
||||
assertEquals(Cardboard.SOURCE_SBS, projection.source)
|
||||
}
|
||||
|
||||
@Test fun automatic2dIsFlatSoVirtualScreenSettingsDoNotApply() {
|
||||
val settings = GlassesSettings(screenWidthDm = 40, screenDistanceDm = 80)
|
||||
assertEquals(GlassesRendering.Picture.FLAT, GlassesRendering.picture(settings, false, false))
|
||||
assertEquals(Cardboard.OFF, render(settings))
|
||||
assertEquals(Cardboard.OFF, render(settings.copy(screenWidthDm = 10, screenDistanceDm = 10)))
|
||||
}
|
||||
|
||||
@Test fun selectingVirtualScreenMakesSizeDistanceAndTrackingReachTheRendererIn2d() {
|
||||
val settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN,
|
||||
screenWidthDm = 40, screenDistanceDm = 80)
|
||||
val projection = render(settings)
|
||||
assertTrue(projection.enabled)
|
||||
assertTrue(projection.mono)
|
||||
assertTrue(projection.headTracking)
|
||||
assertEquals(Cardboard.SOURCE_SCREEN, projection.source)
|
||||
assertEquals(40, projection.screenWidthDm)
|
||||
assertEquals(80, projection.screenDistanceDm)
|
||||
assertEquals(67, projection.ipdMm)
|
||||
val changed = render(settings.copy(screenWidthDm = 15, screenDistanceDm = 20, headTracking = false))
|
||||
assertEquals(15, changed.screenWidthDm)
|
||||
assertEquals(20, changed.screenDistanceDm)
|
||||
assertFalse(changed.headTracking)
|
||||
}
|
||||
|
||||
@Test fun automatic3dGamesUseAVirtualScreenButSteamVrUsesDirectStereo() {
|
||||
val settings = GlassesSettings()
|
||||
val stereo = ultra.copy(sbs = true, width = 3840)
|
||||
assertEquals(GlassesRendering.Picture.VIRTUAL_SCREEN, GlassesRendering.picture(settings, true, false))
|
||||
assertEquals(Cardboard.SOURCE_SCREEN, render(settings, stereo).source)
|
||||
assertFalse(render(settings, stereo).mono)
|
||||
assertEquals(GlassesRendering.Picture.STEREO, GlassesRendering.picture(settings, true, true))
|
||||
assertEquals(Cardboard.SOURCE_SBS, render(settings, stereo, steamVr = true).source)
|
||||
}
|
||||
|
||||
@Test fun explicitPictureChoiceOverridesAutomaticResolution() {
|
||||
val virtual = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN)
|
||||
val direct = GlassesSettings(picture3d = GlassesSettings.PICTURE_SBS)
|
||||
for (stereo in listOf(false, true)) {
|
||||
for (vr in listOf(false, true)) {
|
||||
assertEquals(GlassesRendering.Picture.VIRTUAL_SCREEN, GlassesRendering.picture(virtual, stereo, vr))
|
||||
assertEquals(GlassesRendering.Picture.STEREO, GlassesRendering.picture(direct, stereo, vr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun mirrorModeKeepsThePhoneProjectionInsteadOfApplyingDormantGlassesSettings() {
|
||||
val phone = base.copy(enabled = true, screenWidthDm = 60, screenDistanceDm = 20)
|
||||
val settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN,
|
||||
gameOnGlasses = false, screenWidthDm = 10, screenDistanceDm = 50)
|
||||
assertSame(phone, GlassesRendering.cardboard(phone, settings, ultra, false, false))
|
||||
}
|
||||
|
||||
@Test fun steamVrOnPhoneKeepsHeadsetActiveWithoutEnablingTheSavedCardboardSetting() {
|
||||
val settings = GlassesSettings(gameOnGlasses = false, headTracking = false,
|
||||
picture3d = GlassesSettings.PICTURE_SCREEN)
|
||||
for (state in listOf(ultra.copy(displayId = -1), GlassesState())) {
|
||||
val preview = GlassesRendering.cardboard(base, settings, state, false, true)
|
||||
assertTrue(preview.enabled) // Published as headset proximity, preventing standby.
|
||||
assertTrue(preview.mono)
|
||||
assertEquals(Cardboard.SOURCE_SBS, preview.source)
|
||||
assertEquals(Cardboard.Viewer.NONE.id, preview.viewer)
|
||||
assertTrue(preview.headTracking) // Phone preference, not dormant glasses settings.
|
||||
assertFalse(base.enabled)
|
||||
assertSame(base, GlassesRendering.cardboard(base, settings, state, false, false))
|
||||
}
|
||||
val noTracking = base.copy(headTracking = false)
|
||||
assertFalse(GlassesRendering.cardboard(noTracking, settings, GlassesState(), false, true).headTracking)
|
||||
}
|
||||
|
||||
@Test fun steamVrKeepsAnEnabledPhoneViewerAndSwitchesBackToGlassesRenderingOnHandoff() {
|
||||
val phone = base.copy(enabled = true, source = Cardboard.SOURCE_SBS)
|
||||
val settings = GlassesSettings()
|
||||
assertSame(phone, GlassesRendering.cardboard(phone, settings, GlassesState(), false, true))
|
||||
val glasses = GlassesRendering.cardboard(base, settings, ultra.copy(sbs = true), true, true)
|
||||
assertTrue(glasses.enabled)
|
||||
assertFalse(glasses.mono)
|
||||
assertEquals(Cardboard.Viewer.VITURE.id, glasses.viewer)
|
||||
assertEquals(Cardboard.SOURCE_SBS, glasses.source)
|
||||
}
|
||||
|
||||
@Test fun steamVrMirroringKeepsTheSameGlassesPictureAndSensorSource() {
|
||||
for (stereo in listOf(false, true)) {
|
||||
val state = ultra.copy(sbs = stereo)
|
||||
for (picture in listOf(GlassesSettings.PICTURE_AUTO, GlassesSettings.PICTURE_SCREEN, GlassesSettings.PICTURE_SBS)) {
|
||||
val settings = GlassesSettings(picture3d = picture)
|
||||
val dedicated = GlassesRendering.cardboard(base, settings, state, true, true)
|
||||
val mirrored = GlassesRendering.cardboard(base, settings.copy(gameOnGlasses = false), state, false, true)
|
||||
assertEquals(dedicated, mirrored)
|
||||
assertTrue(mirrored.enabled)
|
||||
assertTrue(mirrored.headTracking)
|
||||
assertTrue(GlassesRendering.usesGlasses(state, false, true))
|
||||
}
|
||||
}
|
||||
assertFalse(GlassesRendering.usesGlasses(ultra, false, false))
|
||||
assertFalse(GlassesRendering.usesGlasses(GlassesState(), false, true))
|
||||
assertTrue(GlassesRendering.usesGlasses(ultra, true, false))
|
||||
}
|
||||
|
||||
@Test fun missingUsbControlOnlyDisablesTrackingNotScreenSize() {
|
||||
val settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN, screenWidthDm = 35)
|
||||
val projection = render(settings, ultra.copy(connected = false))
|
||||
assertTrue(projection.enabled)
|
||||
assertFalse(projection.headTracking)
|
||||
assertEquals(35, projection.screenWidthDm)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.view.Display
|
||||
import android.view.SurfaceHolder
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.ShadowDisplay
|
||||
import org.robolectric.shadows.ShadowDisplayManager
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [31, 34], manifest = Config.NONE, shadows = [GlassesScreenTest.ModeDisplay::class])
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class GlassesScreenTest {
|
||||
@Test fun switchingBetween2dAndStereoRefreshesTheModePreferenceEvenAtTheSameHz() {
|
||||
val flat = mode(1, 1920, 1200, 60f)
|
||||
val stereo = mode(2, 3840, 1200, 60f)
|
||||
val (screen, panel) = screen(flat, arrayOf(flat, stereo), 60)
|
||||
assertEquals(flat.modeId, screen.window!!.attributes.preferredDisplayModeId)
|
||||
|
||||
panel.current = stereo
|
||||
screen.prefer(60)
|
||||
assertEquals(stereo.modeId, screen.window!!.attributes.preferredDisplayModeId)
|
||||
|
||||
panel.current = flat
|
||||
screen.prefer(60)
|
||||
assertEquals(flat.modeId, screen.window!!.attributes.preferredDisplayModeId)
|
||||
}
|
||||
|
||||
@Test fun refreshPreferenceKeepsBothDimensionsOfTheActiveStereoMode() {
|
||||
val stereo1080 = mode(1, 3840, 1080, 90f)
|
||||
val stereo1200 = mode(2, 3840, 1200, 60f)
|
||||
val (screen, _) = screen(stereo1200, arrayOf(stereo1080, stereo1200), 90)
|
||||
|
||||
assertEquals(stereo1200.modeId, screen.window!!.attributes.preferredDisplayModeId)
|
||||
}
|
||||
|
||||
@Test fun refreshChangesStillChooseTheNearestRateAtTheCurrentResolution() {
|
||||
val stereo60 = mode(1, 3840, 1200, 60f)
|
||||
val stereo90 = mode(2, 3840, 1200, 90f)
|
||||
val flat120 = mode(3, 1920, 1200, 120f)
|
||||
val (screen, _) = screen(stereo60, arrayOf(stereo60, stereo90, flat120), 60)
|
||||
|
||||
screen.prefer(120)
|
||||
assertEquals(stereo90.modeId, screen.window!!.attributes.preferredDisplayModeId)
|
||||
}
|
||||
|
||||
private fun mode(id: Int, width: Int, height: Int, hz: Float): Display.Mode =
|
||||
ShadowDisplayManager.ModeBuilder.modeBuilder(id)
|
||||
.setWidth(width).setHeight(height).setRefreshRate(hz).build()
|
||||
|
||||
private fun screen(current: Display.Mode, modes: Array<Display.Mode>, hz: Int): Pair<GlassesScreen, ModeDisplay> {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val id = ShadowDisplayManager.addDisplay("w1920dp-h1200dp")
|
||||
val display = context.getSystemService(DisplayManager::class.java).getDisplay(id)
|
||||
shadowOf(display).setFlags(Display.FLAG_PRESENTATION)
|
||||
val panel = Shadow.extract<ModeDisplay>(display).apply {
|
||||
this.current = current
|
||||
supported = modes
|
||||
}
|
||||
val callback = object : SurfaceHolder.Callback {
|
||||
override fun surfaceCreated(holder: SurfaceHolder) = Unit
|
||||
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) = Unit
|
||||
override fun surfaceDestroyed(holder: SurfaceHolder) = Unit
|
||||
}
|
||||
return GlassesScreen(context, display, hz, callback).apply { create() } to panel
|
||||
}
|
||||
|
||||
@Implements(Display::class)
|
||||
class ModeDisplay : ShadowDisplay() {
|
||||
lateinit var current: Display.Mode
|
||||
var supported = emptyArray<Display.Mode>()
|
||||
|
||||
@Implementation protected fun getMode(): Display.Mode = current
|
||||
@Implementation protected fun getSupportedModes(): Array<Display.Mode> = supported
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import com.droiddeck.launcher.SessionActivity
|
||||
import com.droiddeck.launcher.gpu.SteamVr
|
||||
import com.droiddeck.launcher.session.LoadingState
|
||||
import com.droiddeck.launcher.session.SessionService
|
||||
import com.droiddeck.launcher.session.SessionState
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import org.junit.Assert.*
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.ThreadPoolExecutor
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], application = Application::class)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class GlassesSteamVrTest {
|
||||
private val commands = mutableListOf<Int>()
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private lateinit var restore: () -> Unit
|
||||
private fun field(name: String) = Glasses::class.java.getDeclaredField(name).apply { isAccessible = true }
|
||||
|
||||
@Before fun prepare() {
|
||||
val fields = listOf("handler", "app", "model").associateWith { field(it).get(null) }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val flows = listOf("_state", "_settings").map { field(it).get(null) as MutableStateFlow<Any> }
|
||||
val values = flows.map { it.value }
|
||||
val mode = SessionState.mode
|
||||
restore = {
|
||||
fields.forEach { (name, value) -> field(name).set(null, value) }
|
||||
flows.zip(values).forEach { (flow, value) -> flow.value = value }
|
||||
SessionState.mode = mode
|
||||
}
|
||||
field("handler").set(null, handler)
|
||||
field("app").set(null, RuntimeEnvironment.getApplication())
|
||||
field("model").set(null, VitureProtocol.model(0x1019, null))
|
||||
field("link").set(null, object : GlassesLink {
|
||||
override val hasImu = false
|
||||
override fun send(msgId: Int, payload: ByteArray): Boolean {
|
||||
if (msgId == VitureProtocol.MSG_DISPLAY) commands.add(payload[0].toInt() and 255)
|
||||
return true
|
||||
}
|
||||
override fun close() {}
|
||||
})
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
|
||||
state.value = GlassesState(present = true, connected = true, controllable = true,
|
||||
displayId = 2, width = 1920, height = 1080)
|
||||
}
|
||||
|
||||
@After fun cleanUp() {
|
||||
handler.removeCallbacksAndMessages(null)
|
||||
field("link").set(null, null)
|
||||
(field("modeState").get(null) as GlassesModeState).apply { manual(); sent(false, 0) }
|
||||
SessionState.steamVr = false
|
||||
restore()
|
||||
}
|
||||
|
||||
@Test fun defaultLaunchAndRunningStatusKeepTheDisplayIn2d() = withLaunch(auto3d = false) { activity ->
|
||||
status(activity, SteamVr.Status.READY)
|
||||
status(activity, SteamVr.Status.RUNNING)
|
||||
status(activity, SteamVr.Status.READY)
|
||||
assertEquals(emptyList<Int>(), commands)
|
||||
assertFalse(Glasses.state.value.switching3d)
|
||||
}
|
||||
|
||||
@Test fun optedInLaunchWaitsForRunningAndRestoresOnlyAfterStopping() = withLaunch(auto3d = true) { activity ->
|
||||
status(activity, SteamVr.Status.READY)
|
||||
status(activity, SteamVr.Status.READY)
|
||||
assertEquals("Startup must not change the display mode", emptyList<Int>(), commands)
|
||||
status(activity, SteamVr.Status.RUNNING)
|
||||
status(activity, SteamVr.Status.RUNNING)
|
||||
assertEquals(listOf(0x32), commands)
|
||||
assertTrue(Glasses.state.value.switching3d)
|
||||
status(activity, SteamVr.Status.UNAVAILABLE)
|
||||
assertEquals("A failed status poll is not a stop", listOf(0x32), commands)
|
||||
status(activity, SteamVr.Status.READY)
|
||||
assertEquals(listOf(0x32, 0x34), commands)
|
||||
assertFalse(Glasses.state.value.switching3d)
|
||||
}
|
||||
|
||||
@Test fun ignoredManualSwitchKeepsThePending2dRestore() {
|
||||
val mode = field("modeState").get(null) as GlassesModeState
|
||||
mode.startVr()
|
||||
mode.stopVr()
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
|
||||
state.value = state.value.copy(width = 640, height = 480, noSignal = true)
|
||||
Glasses.set3d(true)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(emptyList<Int>(), commands)
|
||||
assertTrue(mode.restorePending)
|
||||
state.value = state.value.copy(width = 1920, height = 1080, noSignal = false)
|
||||
Glasses.set3d(false)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
assertEquals(listOf(0x34), commands)
|
||||
assertFalse(mode.restorePending)
|
||||
}
|
||||
|
||||
@Test fun absentPreferenceDefaultsTo2dAndAnExplicitOptInSurvivesReload() {
|
||||
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("glasses", Context.MODE_PRIVATE)
|
||||
prefs.edit().clear().commit()
|
||||
fun load() = Glasses::class.java.getDeclaredMethod("load").apply { isAccessible = true }
|
||||
.invoke(Glasses) as GlassesSettings
|
||||
assertFalse(load().auto3d)
|
||||
prefs.edit().putBoolean("auto3d", true).commit()
|
||||
assertTrue(load().auto3d)
|
||||
prefs.edit().putBoolean("auto3d", false).commit()
|
||||
assertFalse(load().auto3d)
|
||||
}
|
||||
|
||||
@Test fun manualStartOnDisplayOnlyGlassesRequestsPollingWithoutDuplicatingLaunch() {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val state = field("_state").get(null) as MutableStateFlow<GlassesState>
|
||||
state.value = GlassesState(present = true, displayId = 2, width = 1920, height = 1080)
|
||||
withLaunch(auto3d = false) { activity ->
|
||||
SessionState.steamVr = false
|
||||
fun action(name: String) = SessionActivity::class.java.getDeclaredMethod("steamVrAction", String::class.java)
|
||||
.apply { isAccessible = true }.invoke(activity, name)
|
||||
action("start")
|
||||
assertTrue("A manual start must keep status polling alive", SessionState.steamVr)
|
||||
val worker = SessionActivity::class.java.getDeclaredField("steamVrWorker")
|
||||
.apply { isAccessible = true }.get(null) as ThreadPoolExecutor
|
||||
assertEquals(1, worker.queue.size)
|
||||
status(activity, SteamVr.Status.READY)
|
||||
assertEquals("Polling must not launch a duplicate instance", 1, worker.queue.size)
|
||||
status(activity, SteamVr.Status.RUNNING)
|
||||
status(activity, SteamVr.Status.UNAVAILABLE)
|
||||
assertTrue(SessionState.steamVr)
|
||||
action("stop")
|
||||
assertFalse(SessionState.steamVr)
|
||||
}
|
||||
}
|
||||
|
||||
private fun status(activity: SessionActivity, status: SteamVr.Status) {
|
||||
SessionActivity::class.java.getDeclaredMethod("onSteamVrStatus", SteamVr.Status::class.java)
|
||||
.apply { isAccessible = true }.invoke(activity, status)
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
}
|
||||
|
||||
private fun withLaunch(auto3d: Boolean, check: (SessionActivity) -> Unit) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val settings = field("_settings").get(null) as MutableStateFlow<GlassesSettings>
|
||||
settings.value = GlassesSettings(auto3d = auto3d)
|
||||
val activity = Robolectric.buildActivity(SessionActivity::class.java).get()
|
||||
SessionActivity::class.java.getDeclaredField("loading").apply { isAccessible = true }
|
||||
.set(activity, LoadingState(activity).apply { visible = false })
|
||||
SessionState.steamVr = true
|
||||
SessionState.mode = SessionService.MODE_STEAM
|
||||
// Hold the external worker so status transitions can be exercised
|
||||
// without launching a Linux runtime from the host test process.
|
||||
val worker = SessionActivity::class.java.getDeclaredField("steamVrWorker")
|
||||
.apply { isAccessible = true }.get(null) as ThreadPoolExecutor
|
||||
val blocked = CountDownLatch(1)
|
||||
val release = CountDownLatch(1)
|
||||
worker.execute { blocked.countDown(); release.await(30, TimeUnit.SECONDS) }
|
||||
try {
|
||||
assertTrue(blocked.await(5, TimeUnit.SECONDS))
|
||||
check(activity)
|
||||
} finally {
|
||||
worker.queue.clear()
|
||||
release.countDown()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.hardware.usb.UsbManager
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.os.SystemClock
|
||||
import android.view.Display
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.ShadowDisplayManager
|
||||
import org.robolectric.shadows.ShadowSystemClock
|
||||
import org.robolectric.shadows.ShadowUsbManager
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import org.robolectric.util.ReflectionHelpers.ClassParameter.from
|
||||
import java.time.Duration
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE, shadows = [GlassesUsbTest.RecordingUsbManager::class])
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class GlassesUsbTest {
|
||||
private lateinit var manager: RecordingUsbManager
|
||||
private lateinit var state: MutableStateFlow<GlassesState>
|
||||
private fun field(name: String) = Glasses::class.java.getDeclaredField(name).apply { isAccessible = true }
|
||||
|
||||
@Before fun prepare() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
val usb = context.getSystemService(Context.USB_SERVICE) as UsbManager
|
||||
manager = Shadow.extract(usb)
|
||||
field("app").set(null, context)
|
||||
field("usb").set(null, usb)
|
||||
field("displays").set(null, context.getSystemService(Context.DISPLAY_SERVICE))
|
||||
field("handler").set(null, Handler(Looper.getMainLooper()))
|
||||
field("asked").setBoolean(null, false)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
state = field("_state").get(null) as MutableStateFlow<GlassesState>
|
||||
state.value = GlassesState()
|
||||
}
|
||||
|
||||
@Test fun controllableGlassesTakePriorityOverDockAndUnsupportedGlasses() {
|
||||
val dock = device(0x3011)
|
||||
val beast = device(0x1201)
|
||||
val pro = device(0x101d)
|
||||
manager.devices.putAll(listOf(dock, beast, pro).associateBy { it.deviceName })
|
||||
refresh()
|
||||
assertEquals(listOf(pro), manager.requests)
|
||||
assertTrue(state.value.controllable)
|
||||
assertEquals(Glasses.Permission.ASKING, state.value.permission)
|
||||
}
|
||||
|
||||
@Test fun unsupportedDevicesNeverRequestOrAdvertiseUsbPermission() {
|
||||
for (pid in listOf(0x3011, 0xffff, 0x1201)) {
|
||||
val device = device(pid)
|
||||
manager.devices.clear()
|
||||
manager.devices[device.deviceName] = device
|
||||
state.value = GlassesState()
|
||||
refresh(ask = true)
|
||||
assertTrue(manager.requests.isEmpty())
|
||||
assertEquals(Glasses.Permission.NONE, state.value.permission)
|
||||
manager.granted.add(device.deviceName)
|
||||
state.value = GlassesState(permission = Glasses.Permission.ASKING)
|
||||
refresh(ask = true)
|
||||
assertTrue(manager.requests.isEmpty())
|
||||
assertEquals(Glasses.Permission.NONE, state.value.permission)
|
||||
assertEquals(pid == 0x1201, state.value.present)
|
||||
assertFalse(state.value.controllable)
|
||||
assertFalse(state.value.connected)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun dockPresenceStillIdentifiesAnUnnamedPresentationDisplay() {
|
||||
val hdmi = ReflectionHelpers.getStaticField<Int>(Display::class.java, "TYPE_HDMI")
|
||||
val displayId = ShadowDisplayManager.addDisplay("w1920dp-h1080dp", hdmi)
|
||||
val displays = RuntimeEnvironment.getApplication().getSystemService(DisplayManager::class.java)
|
||||
val display = displays.getDisplay(displayId)
|
||||
shadowOf(display).setFlags(Display.FLAG_PRESENTATION)
|
||||
assertEquals(displayId, displays.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION).single().displayId)
|
||||
assertFalse(display.name.contains("VITURE", ignoreCase = true))
|
||||
val dock = device(0x3011)
|
||||
manager.devices[dock.deviceName] = dock
|
||||
refresh()
|
||||
assertEquals(displayId, state.value.displayId)
|
||||
assertTrue(manager.requests.isEmpty())
|
||||
assertEquals(Glasses.Permission.NONE, state.value.permission)
|
||||
}
|
||||
|
||||
@Test fun noSignalStereoFailureIsRememberedPerModel() {
|
||||
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("glasses", Context.MODE_PRIVATE)
|
||||
prefs.edit().clear().commit()
|
||||
val hdmi = ReflectionHelpers.getStaticField<Int>(Display::class.java, "TYPE_HDMI")
|
||||
val displayId = ShadowDisplayManager.addDisplay("w640dp-h480dp", hdmi)
|
||||
val displays = RuntimeEnvironment.getApplication().getSystemService(DisplayManager::class.java)
|
||||
shadowOf(displays.getDisplay(displayId)).setFlags(Display.FLAG_PRESENTATION)
|
||||
val pro = device(0x1019)
|
||||
manager.devices[pro.deviceName] = pro
|
||||
refresh()
|
||||
assertTrue(state.value.noSignal)
|
||||
(field("modeState").get(null) as GlassesModeState).sent(true, SystemClock.uptimeMillis())
|
||||
ShadowSystemClock.advanceBy(Duration.ofSeconds(21))
|
||||
refresh()
|
||||
assertEquals(GlassesState.SBS_FAILS, state.value.sbsSupport)
|
||||
assertEquals(setOf("1019"), prefs.getStringSet("sbsFailed", null))
|
||||
ShadowDisplayManager.removeDisplay(displayId)
|
||||
|
||||
val xr = device(0x101d)
|
||||
manager.devices.clear()
|
||||
manager.devices[xr.deviceName] = xr
|
||||
refresh()
|
||||
assertEquals(GlassesState.SBS_UNKNOWN, state.value.sbsSupport)
|
||||
manager.devices.clear()
|
||||
manager.devices[pro.deviceName] = pro
|
||||
refresh()
|
||||
assertEquals(GlassesState.SBS_FAILS, state.value.sbsSupport)
|
||||
assertFalse(state.value.can3d)
|
||||
}
|
||||
|
||||
@Test fun rayNeoRequestsPermissionWithoutClaimingUnrelatedTclDevices() {
|
||||
val air = device(0xaf50, 0x1bbb, "RayNeo Air 4 Pro")
|
||||
manager.devices[air.deviceName] = air
|
||||
refresh(ask = true)
|
||||
assertEquals("RayNeo Air 4 Pro", state.value.model)
|
||||
assertTrue(state.value.controllable)
|
||||
assertFalse(state.value.hardwareControls)
|
||||
assertEquals(listOf(air), manager.requests)
|
||||
|
||||
manager.devices.clear()
|
||||
manager.devices["phone"] = device(0x1234, 0x1bbb, "TCL Phone")
|
||||
manager.requests.clear()
|
||||
refresh(ask = true)
|
||||
assertFalse(state.value.present)
|
||||
assertTrue(manager.requests.isEmpty())
|
||||
}
|
||||
|
||||
@Test fun alternateBridgeRequiresRayNeoModelIdentity() {
|
||||
val gt = device(0x1234, 0x22d9, "RayNeo GT")
|
||||
manager.devices[gt.deviceName] = gt
|
||||
refresh(ask = true)
|
||||
assertEquals("RayNeo GT", state.value.model)
|
||||
assertEquals(listOf(gt), manager.requests)
|
||||
}
|
||||
|
||||
@Test fun gtVendorRequestsPermissionAndRetainsMaxProductName() {
|
||||
val gt = device(0xaf50, 0x3941, "RayNeo GT MAX")
|
||||
manager.devices[gt.deviceName] = gt
|
||||
refresh(ask = true)
|
||||
assertEquals("RayNeo GT MAX", state.value.model)
|
||||
assertTrue(state.value.controllable)
|
||||
assertFalse(state.value.hardwareControls)
|
||||
assertEquals(listOf(gt), manager.requests)
|
||||
}
|
||||
|
||||
private fun refresh(ask: Boolean = false) {
|
||||
Glasses::class.java.getDeclaredMethod("refresh", Boolean::class.javaPrimitiveType)
|
||||
.apply { isAccessible = true }.invoke(Glasses, ask)
|
||||
}
|
||||
|
||||
@Test fun xreal1SUsesExactIdentityAndExcludesAirOneProAndRecoveryProducts() {
|
||||
assertEquals(XrealModel, Glasses.deviceModel(device(0x043e, 0x3318, "XREAL 1S")))
|
||||
assertTrue(Glasses.isGlassesDisplayName("XREAL 1S"))
|
||||
for (pid in listOf(0x043d, 0x0435, 0x0436, 0x0424, 0xffff)) {
|
||||
assertEquals(null, Glasses.deviceModel(device(pid, 0x3318, "XREAL 1S")))
|
||||
}
|
||||
assertEquals(null, Glasses.deviceModel(device(0x043e, 0x1234, "XREAL 1S")))
|
||||
}
|
||||
|
||||
private fun device(pid: Int, vid: Int = VitureProtocol.VENDOR_ID, name: String = "VITURE device"): UsbDevice = ReflectionHelpers.callConstructor(
|
||||
UsbDevice::class.java,
|
||||
from(String::class.java, "/dev/bus/usb/$pid"),
|
||||
from(Int::class.javaPrimitiveType, vid),
|
||||
from(Int::class.javaPrimitiveType, pid),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(String::class.java, "VITURE"),
|
||||
from(String::class.java, name),
|
||||
from(String::class.java, "1.0"),
|
||||
from(String::class.java, "test-$pid"),
|
||||
)
|
||||
|
||||
@Implements(UsbManager::class)
|
||||
class RecordingUsbManager : ShadowUsbManager() {
|
||||
val devices = linkedMapOf<String, UsbDevice>()
|
||||
val granted = mutableSetOf<String>()
|
||||
val requests = mutableListOf<UsbDevice>()
|
||||
|
||||
@Implementation override fun getDeviceList(): HashMap<String, UsbDevice> = LinkedHashMap(devices)
|
||||
@Implementation override fun hasPermission(device: UsbDevice): Boolean = device.deviceName in granted
|
||||
@Implementation fun requestPermission(device: UsbDevice, intent: PendingIntent) { requests += device }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.content.Context
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
class RayNeoCalibrationTest {
|
||||
private val axes = RayNeoSensorAxes(0f, false)
|
||||
private fun sample(a: FloatArray = floatArrayOf(0f, -9.81f, 0f), g: FloatArray = floatArrayOf(0.5f, -0.3f, 0.2f)) =
|
||||
RayNeoSensorSample(a, g, 0f, FloatArray(3), 0f, 0f, 0)
|
||||
|
||||
private fun finish(c: RayNeoCalibrationCollector, start: Long = 0, sample: RayNeoSensorSample = sample()): RayNeoCalibration? {
|
||||
var result: RayNeoCalibration? = null
|
||||
for (ms in start..(start + 8100) step 10) c.update(sample, ms)?.let { result = it }
|
||||
return result
|
||||
}
|
||||
|
||||
@Test fun upsideDownStillPoseMeasuresBiasAfterSettlingAndFiveSeconds() {
|
||||
val c = RayNeoCalibrationCollector(axes, 0)
|
||||
for (ms in 0L..7990 step 10) assertNull(c.update(sample(), ms))
|
||||
val result = c.update(sample(), 8000)!!
|
||||
assertEquals(CalibrationPhase.SAVED, c.phase)
|
||||
assertEquals(100, c.progress)
|
||||
assertEquals(Vec3(0.5f, -0.3f, 0.2f), result.gyroBiasDps)
|
||||
assertEquals(Quaternion.IDENTITY, result.level)
|
||||
}
|
||||
|
||||
@Test fun uprightOrSidewaysCannotSave() {
|
||||
for (a in listOf(floatArrayOf(0f, 9.81f, 0f), floatArrayOf(9.81f, 0f, 0f))) {
|
||||
val c = RayNeoCalibrationCollector(axes, 0)
|
||||
assertNull(finish(c, sample = sample(a)))
|
||||
assertEquals(CalibrationPhase.TURN_OVER, c.phase)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun movementRestartsTheWindowAndAllowsRecovery() {
|
||||
val c = RayNeoCalibrationCollector(axes, 0)
|
||||
for (ms in 0L..6000 step 10) c.update(sample(), ms)
|
||||
c.update(sample(g = floatArrayOf(20f, 0f, 0f)), 6010)
|
||||
assertEquals(CalibrationPhase.MOVING, c.phase)
|
||||
assertEquals(0, c.progress)
|
||||
assertNotNull(finish(c, 6020))
|
||||
}
|
||||
|
||||
@Test fun VibrationIsRejectedEvenBelowTheInstantSpeedLimit() {
|
||||
val c = RayNeoCalibrationCollector(axes, 0)
|
||||
var result: RayNeoCalibration? = null
|
||||
for (ms in 0L..12000 step 10) {
|
||||
val gyro = if (ms % 20 == 0L) 4f else -4f
|
||||
c.update(sample(g = floatArrayOf(gyro, 0f, 0f)), ms)?.let { result = it }
|
||||
}
|
||||
assertNull(result)
|
||||
}
|
||||
|
||||
@Test fun missingSamplesCannotCompleteAStationaryWindow() {
|
||||
val c = RayNeoCalibrationCollector(axes, 0)
|
||||
for (ms in 0L..6000 step 10) c.update(sample(), ms)
|
||||
assertNull(c.update(sample(), 9000))
|
||||
assertEquals(CalibrationPhase.MOVING, c.phase)
|
||||
assertNull(c.update(sample(), 30_000))
|
||||
assertEquals(CalibrationPhase.FAILED, c.phase)
|
||||
}
|
||||
|
||||
@Test fun nonFiniteOrInvalidGravityCannotSave() {
|
||||
for (a in listOf(floatArrayOf(Float.NaN, -9.81f, 0f), FloatArray(3), floatArrayOf(0f, -20f, 0f))) {
|
||||
assertNull(finish(RayNeoCalibrationCollector(axes, 0), sample = sample(a)))
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun levelCorrectionAlignsBothInvertedAndUprightGravity() {
|
||||
val c = RayNeoCalibrationCollector(axes, 0)
|
||||
val result = finish(c, sample = sample(floatArrayOf(0.85f, -9.735f, 0.85f)))!!
|
||||
val up = result.level.rotate(Vec3(-0.85f, 9.735f, -0.85f).normalized())
|
||||
assertEquals(0f, up.x, 0.001f); assertEquals(1f, up.y, 0.001f); assertEquals(0f, up.z, 0.001f)
|
||||
val filter = RayNeoOrientationFilter(0f, calibration = result)
|
||||
var q = FloatArray(4)
|
||||
repeat(1000) { q = filter.update(sample(floatArrayOf(-0.85f, 9.735f, -0.85f)).copy(deviceTick100us = it * 100L)) }
|
||||
assertArrayEquals(floatArrayOf(1f, 0f, 0f, 0f), q, 0.001f)
|
||||
}
|
||||
|
||||
@Test fun profilePersistenceIsIsolatedByVendorProductAndSerialAndCanReset() {
|
||||
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("cal-test", Context.MODE_PRIVATE)
|
||||
val store = RayNeoCalibrationStore(prefs)
|
||||
val value = RayNeoCalibration(Vec3(0.1f, 0.2f, 0.3f), Quaternion.IDENTITY)
|
||||
val key = store.key(0x3941, 0xaf50, "first")
|
||||
store.save(key, value)
|
||||
assertEquals(value, store.load(key))
|
||||
for (other in listOf(store.key(0x3941, 0xaf50, "second"), store.key(0x1bbb, 0xaf50, "first"), store.key(0x3941, 1, "first"))) {
|
||||
assertNull(store.load(other))
|
||||
}
|
||||
store.save(key, null)
|
||||
assertNull(store.load(key))
|
||||
}
|
||||
|
||||
@Test fun corruptOrExcessiveProfilesFallBackToDefaults() {
|
||||
assertNull(RayNeoCalibration.decode("bad"))
|
||||
assertNull(RayNeoCalibration.decode("{\"version\":2}"))
|
||||
assertNull(RayNeoCalibration.decode(RayNeoCalibration(Vec3(10f, 0f, 0f), Quaternion.IDENTITY).encode()))
|
||||
assertNull(RayNeoCalibration.decode(RayNeoCalibration(Vec3(0f, 0f, 0f), Quaternion(0f, 1f, 0f, 0f)).encode()))
|
||||
}
|
||||
|
||||
@Test fun recordedNonMaxRestingPoseCalibratesWithItsOwnProfile() {
|
||||
val profile = RayNeoSensorProfile.forDevice(true, 0x40)
|
||||
val collector = RayNeoCalibrationCollector(RayNeoSensorAxes(profile.rotationXDeg, profile.gtMaxAxes), 0)
|
||||
val reading = sample(floatArrayOf(0.02f, -8.819f, -4.106f), floatArrayOf(-0.1f, 0.35f, -0.038f))
|
||||
val saved = finish(collector, sample = reading)!!
|
||||
assertEquals(CalibrationPhase.SAVED, collector.phase)
|
||||
assertEquals(-0.1f, saved.gyroBiasDps.x, 0.0001f)
|
||||
val upright = RayNeoOrientationFilter(profile.rotationXDeg, profile.gtMaxAxes, saved)
|
||||
.update(sample(floatArrayOf(-0.02f, 8.819f, 4.106f), reading.gyroDps))
|
||||
assertArrayEquals(floatArrayOf(1f, 0f, 0f, 0f), upright, 0.001f)
|
||||
// This was the reported failure: the same physical pose cannot calibrate
|
||||
// through the Max's permutation and 35-degree mounting correction.
|
||||
assertNull(finish(RayNeoCalibrationCollector(RayNeoSensorAxes(35f, true), 0), sample = reading))
|
||||
}
|
||||
|
||||
@Test fun nonMaxProfileCannotOverwriteLegacyMaxCalibrationWithTheSameUsbIdentity() {
|
||||
val prefs = RuntimeEnvironment.getApplication().getSharedPreferences("cal-profile-test", Context.MODE_PRIVATE)
|
||||
val store = RayNeoCalibrationStore(prefs)
|
||||
val base = store.key(0x3941, 0xaf50, "00000001")
|
||||
val standard = RayNeoCalibrationStore.profileKey(base, true, 0x40)
|
||||
val legacy = RayNeoCalibration(Vec3(1f, 0f, 0f), Quaternion.IDENTITY)
|
||||
val current = RayNeoCalibration(Vec3(0f, 1f, 0f), Quaternion.IDENTITY)
|
||||
store.save(base, legacy); store.save(standard, current)
|
||||
assertEquals(legacy, store.load(base))
|
||||
assertEquals(current, store.load(standard))
|
||||
assertEquals(base, RayNeoCalibrationStore.profileKey(base, false, 0x40))
|
||||
assertEquals(base, RayNeoCalibrationStore.profileKey(base, true, 0x7f))
|
||||
assertEquals(base, RayNeoCalibrationStore.profileKey(base, true, 0x41))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.hardware.usb.UsbEndpoint
|
||||
import android.hardware.usb.UsbInterface
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import org.robolectric.util.ReflectionHelpers.ClassParameter.from
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
class RayNeoEndpointsTest {
|
||||
@Test fun gtPrefersInterfaceNumberFiveOverEarlierPairs() {
|
||||
val other = iface(1)
|
||||
val control = iface(5)
|
||||
val selected = RayNeoProtocol.selectEndpoints(device(0x3941, other, control))!!
|
||||
assertSame(control, selected.usbInterface)
|
||||
assertEquals(0x81, selected.inEndpoint.address)
|
||||
assertEquals(0x01, selected.outEndpoint.address)
|
||||
}
|
||||
|
||||
@Test fun gtAcceptsUniqueRenumberedPairAndIgnoresSmallButtonReports() {
|
||||
val control = iface(0)
|
||||
assertSame(control, RayNeoProtocol.selectEndpoints(
|
||||
device(0x3941, iface(5, packetSize = 8), control))?.usbInterface)
|
||||
}
|
||||
|
||||
@Test fun gtDoesNotGuessBetweenAmbiguousPairsOrUseInputOnlyInterface() {
|
||||
assertNull(RayNeoProtocol.selectEndpoints(device(0x3941, iface(0), iface(1))))
|
||||
assertNull(RayNeoProtocol.selectEndpoints(device(0x3941, iface(5, output = false))))
|
||||
assertNull(RayNeoProtocol.selectEndpoints(device(0x3941, iface(5, packetSize = 8))))
|
||||
}
|
||||
|
||||
@Test fun airRetainsItsExistingFirstInterruptPairSelection() {
|
||||
val first = iface(1)
|
||||
assertSame(first, RayNeoProtocol.selectEndpoints(device(0x1bbb, first, iface(5)))?.usbInterface)
|
||||
}
|
||||
|
||||
private fun device(vid: Int, vararg interfaces: UsbInterface): UsbDevice =
|
||||
Shadow.newInstanceOf(UsbDevice::class.java).also {
|
||||
ReflectionHelpers.setField(it, "mVendorId", vid)
|
||||
ReflectionHelpers.setField(it, "mProductId", 0xaf50)
|
||||
ReflectionHelpers.setField(it, "mInterfaces", interfaces)
|
||||
}
|
||||
|
||||
private fun iface(id: Int, packetSize: Int = 64, output: Boolean = true): UsbInterface =
|
||||
Shadow.newInstanceOf(UsbInterface::class.java).also {
|
||||
ReflectionHelpers.setField(it, "mId", id)
|
||||
val endpoints = if (output) arrayOf(endpoint(0x81, packetSize), endpoint(1, packetSize))
|
||||
else arrayOf(endpoint(0x81, packetSize))
|
||||
ReflectionHelpers.setField(it, "mEndpoints", endpoints)
|
||||
}
|
||||
|
||||
private fun endpoint(address: Int, size: Int) = ReflectionHelpers.callConstructor(UsbEndpoint::class.java,
|
||||
from(Int::class.javaPrimitiveType, address), from(Int::class.javaPrimitiveType, 3),
|
||||
from(Int::class.javaPrimitiveType, size), from(Int::class.javaPrimitiveType, 1))
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.hardware.usb.UsbDeviceConnection
|
||||
import android.hardware.usb.UsbEndpoint
|
||||
import android.hardware.usb.UsbInterface
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.time.Duration
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.ShadowSystemClock
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import org.robolectric.util.ReflectionHelpers.ClassParameter.from
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE, shadows = [RayNeoLinkTest.Connection::class])
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class RayNeoLinkTest {
|
||||
@Test fun handshakeWarmupPoseAndShutdownUseOnlyRayNeoWireCommands() = exerciseLink(false)
|
||||
|
||||
@Test fun gtControlTrafficDoesNotBreakHandshakeOrTracking() = exerciseLink(true)
|
||||
@Test fun nonMaxBoardSelectsItsProfileBeforeTrackingStarts() = exerciseLink(true, true)
|
||||
|
||||
private fun exerciseLink(gtTraffic: Boolean, nonMax: Boolean = false) {
|
||||
fun endpoint(address: Int) = ReflectionHelpers.callConstructor(UsbEndpoint::class.java,
|
||||
from(Int::class.javaPrimitiveType, address), from(Int::class.javaPrimitiveType, 3),
|
||||
from(Int::class.javaPrimitiveType, 64), from(Int::class.javaPrimitiveType, 1))
|
||||
val iface = Shadow.newInstanceOf(UsbInterface::class.java)
|
||||
val conn = Shadow.newInstanceOf(UsbDeviceConnection::class.java)
|
||||
val io = Shadow.extract<Connection>(conn)
|
||||
io.gtTraffic = gtTraffic
|
||||
io.boardId = if (nonMax) 0x40 else if (gtTraffic) 0x41 else 0
|
||||
lateinit var link: RayNeoLink
|
||||
var ready = false
|
||||
var pose: FloatArray? = null
|
||||
var lost = false
|
||||
var loadedBoard = -1
|
||||
var reportedBoard = -1
|
||||
val listener = object : RayNeoLink.Listener {
|
||||
override fun onDeviceInfo(boardId: Int, calibrationSaved: Boolean) {
|
||||
reportedBoard = boardId
|
||||
assertFalse(calibrationSaved)
|
||||
}
|
||||
override fun onReady() {
|
||||
assertEquals(io.boardId, loadedBoard)
|
||||
assertEquals(io.boardId, reportedBoard)
|
||||
ready = true
|
||||
if (gtTraffic) {
|
||||
assertTrue(link.send(VitureProtocol.MSG_DISPLAY, byteArrayOf(6)))
|
||||
assertTrue(link.send(VitureProtocol.MSG_DISPLAY, byteArrayOf(7)))
|
||||
}
|
||||
}
|
||||
override fun onPose(raw: ByteArray, xyzw: FloatArray, timeNs: Long) {
|
||||
pose = xyzw
|
||||
link.close()
|
||||
}
|
||||
override fun onLost() { lost = true }
|
||||
}
|
||||
val ctor = RayNeoLink::class.java.declaredConstructors.first { it.parameterCount == 5 }.apply { isAccessible = true }
|
||||
val loadCalibration: (Int) -> RayNeoCalibration? = { board -> loadedBoard = board; null }
|
||||
link = ctor.newInstance(conn, RayNeoEndpointSelection(iface, endpoint(0x81), endpoint(0x01)), listener, gtTraffic, loadCalibration) as RayNeoLink
|
||||
ReflectionHelpers.setField(link, "worker", Thread.currentThread())
|
||||
assertFalse(link.send(VitureProtocol.MSG_BRIGHTNESS, byteArrayOf(8)))
|
||||
assertFalse(link.send(VitureProtocol.MSG_FILM, byteArrayOf(1)))
|
||||
assertFalse(link.send(VitureProtocol.MSG_IMU_RATE, byteArrayOf(3)))
|
||||
assertTrue(io.commands.isEmpty())
|
||||
assertTrue(link.send(VitureProtocol.MSG_IMU, byteArrayOf(1)))
|
||||
RayNeoLink::class.java.getDeclaredMethod("run").apply { isAccessible = true }.invoke(link)
|
||||
assertTrue(ready)
|
||||
assertFalse(lost)
|
||||
assertEquals(if (gtTraffic) listOf(0, 6, 7, 1, 2) else listOf(0, 1, 2), io.commands)
|
||||
assertTrue(io.samples >= 200)
|
||||
assertTrue(io.closed)
|
||||
assertTrue(io.released)
|
||||
assertNotNull(pose)
|
||||
assertEquals(0f, pose!![0], 1e-5f)
|
||||
assertEquals(0f, pose!![1], 1e-5f)
|
||||
assertTrue(pose!![2] > 0)
|
||||
}
|
||||
|
||||
@Implements(UsbDeviceConnection::class)
|
||||
class Connection {
|
||||
val commands = mutableListOf<Int>()
|
||||
private val responses = ArrayDeque<ByteArray>()
|
||||
var samples = 0
|
||||
var closed = false
|
||||
var released = false
|
||||
var gtTraffic = false
|
||||
var boardId = 0
|
||||
private var streaming = false
|
||||
|
||||
@Implementation fun bulkTransfer(ep: UsbEndpoint, bytes: ByteArray, count: Int, timeout: Int): Int {
|
||||
if (ep.direction == 0) {
|
||||
check(bytes[0] == 0x66.toByte() && count == 64)
|
||||
val cmd = bytes[1].toInt() and 0xff
|
||||
commands += cmd
|
||||
if (gtTraffic && cmd in listOf(0, 1)) {
|
||||
// Notifications and unrelated ACKs can precede the requested response.
|
||||
for (other in listOf(0x08, 6, 7, 0x50)) {
|
||||
responses += ByteArray(64).apply {
|
||||
this[0] = 0x99.toByte(); this[1] = 0xc8.toByte(); this[2] = 64
|
||||
this[8] = other.toByte()
|
||||
}
|
||||
}
|
||||
}
|
||||
responses += ByteArray(64).apply {
|
||||
this[0] = 0x99.toByte(); this[1] = 0xc8.toByte(); this[2] = 64
|
||||
this[8] = cmd.toByte()
|
||||
if (cmd == 0) this[21] = boardId.toByte()
|
||||
}
|
||||
if (cmd == 1) streaming = true
|
||||
if (cmd == 2) streaming = false
|
||||
return count
|
||||
}
|
||||
ShadowSystemClock.advanceBy(Duration.ofMillis(10))
|
||||
val packet = if (responses.isNotEmpty()) responses.removeFirst() else {
|
||||
check(streaming)
|
||||
check(++samples < 500) { "Warmup never delivered a pose" }
|
||||
ByteBuffer.allocate(64).order(ByteOrder.LITTLE_ENDIAN).apply {
|
||||
put(0, 0x99.toByte()); put(1, 0x65); put(2, 64)
|
||||
if (gtTraffic && boardId == 0x40) {
|
||||
val s = kotlin.math.sin(Math.toRadians(25.0)).toFloat()
|
||||
val c = kotlin.math.cos(Math.toRadians(25.0)).toFloat()
|
||||
putFloat(8, 9.81f * c); putFloat(12, 9.81f * s)
|
||||
putFloat(20, 90f * c); putFloat(24, 90f * s)
|
||||
} else if (gtTraffic) {
|
||||
val s = kotlin.math.sin(Math.toRadians(35.0)).toFloat()
|
||||
val c = kotlin.math.cos(Math.toRadians(35.0)).toFloat()
|
||||
putFloat(4, 9.81f * s); putFloat(12, 9.81f * c)
|
||||
putFloat(16, 90f * s); putFloat(24, 90f * c)
|
||||
} else {
|
||||
putFloat(8, 9.81f); putFloat(20, 90f)
|
||||
}
|
||||
putInt(40, samples * 100)
|
||||
}.array()
|
||||
}
|
||||
packet.copyInto(bytes)
|
||||
return packet.size
|
||||
}
|
||||
@Implementation fun releaseInterface(iface: UsbInterface): Boolean { released = true; return true }
|
||||
@Implementation fun close() { closed = true }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import com.droiddeck.launcher.gpu.HeadPose
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class RayNeoOrientationFilterTest {
|
||||
private fun sample(tick: Long, accel: FloatArray, gyro: FloatArray = FloatArray(3)) =
|
||||
RayNeoSensorSample(accel, gyro, 0f, FloatArray(3), 0f, 0f, tick)
|
||||
|
||||
private fun screen(q: FloatArray): FloatArray {
|
||||
val p = RayNeoOrientationFilter.glassesPose(q)
|
||||
return HeadPose.fromGlasses(p[0], p[1], p[2], p[3])
|
||||
}
|
||||
|
||||
@Test fun gravityCorrectionSettlesTowardTiltInsteadOfAwayFromIt() {
|
||||
val filter = RayNeoOrientationFilter(0f)
|
||||
filter.update(sample(0, floatArrayOf(0f, 9.81f, 0f)))
|
||||
val angle = Math.toRadians(20.0)
|
||||
val accel = floatArrayOf(0f, 9.81f * cos(angle).toFloat(), -9.81f * sin(angle).toFloat())
|
||||
var q = FloatArray(4)
|
||||
repeat(1000) { q = filter.update(sample((it + 1) * 100L, accel)) }
|
||||
assertArrayEquals(floatArrayOf(cos(angle / 2).toFloat(), sin(angle / 2).toFloat(), 0f, 0f), q, 0.002f)
|
||||
}
|
||||
|
||||
@Test fun initialGravityDoesNotRotateAfterTheFirstRecenter() {
|
||||
val filter = RayNeoOrientationFilter(0f)
|
||||
val accel = floatArrayOf(4.905f, 8.4957f, 0f)
|
||||
val first = filter.update(sample(0, accel))
|
||||
var last = first
|
||||
repeat(1000) { last = filter.update(sample((it + 1) * 100L, accel)) }
|
||||
assertArrayEquals(first, last, 0.0001f)
|
||||
assertEquals(sin(Math.toRadians(15.0)).toFloat(), first[3], 0.001f)
|
||||
}
|
||||
|
||||
@Test fun recordedGtLevelGravityStartsWithAnUprightHorizon() {
|
||||
val q = RayNeoOrientationFilter(0f, gt = true)
|
||||
.update(sample(0, floatArrayOf(5.697f, 0.001f, 7.947f)))
|
||||
val m = screen(q)
|
||||
val relative = HeadPose.relative(HeadPose.recenter(m), m)
|
||||
assertArrayEquals(HeadPose.IDENTITY, relative, 0.02f)
|
||||
}
|
||||
|
||||
@Test fun gtLeftTurnLooksLeftWithoutPitchOrRoll() = checkGtMotion(0)
|
||||
@Test fun gtLookingUpLooksUpWithoutYawOrRoll() = checkGtMotion(1)
|
||||
@Test fun gtRollTiltsTheHorizonWithoutTurningTheView() = checkGtMotion(2)
|
||||
|
||||
@Test fun nonMaxMountingKeepsYawAndPitchSeparateThroughTheSharedPosePath() {
|
||||
val profile = RayNeoSensorProfile.forDevice(true, 0x40)
|
||||
assertFalse(profile.gtMaxAxes)
|
||||
val mount = Math.toRadians(25.0)
|
||||
val s = sin(mount).toFloat(); val c = cos(mount).toFloat()
|
||||
for (pitch in listOf(false, true)) {
|
||||
val filter = RayNeoOrientationFilter(profile.rotationXDeg, profile.gtMaxAxes)
|
||||
val level = floatArrayOf(0f, 9.81f * c, 9.81f * s)
|
||||
val ref = HeadPose.recenter(screen(filter.update(sample(0, level))))
|
||||
var q = FloatArray(4)
|
||||
repeat(100) { i ->
|
||||
val angle = Math.toRadians((i + 1) * 0.3)
|
||||
val accel = if (pitch) floatArrayOf(0f, 9.81f * cos(mount - angle).toFloat(), 9.81f * sin(mount - angle).toFloat()) else level
|
||||
val gyro = if (pitch) floatArrayOf(30f, 0f, 0f) else floatArrayOf(0f, 30f * c, 30f * s)
|
||||
q = filter.update(sample((i + 1) * 100L, accel, gyro))
|
||||
}
|
||||
val forward = HeadPose.apply(HeadPose.relative(ref, screen(q)), floatArrayOf(0f, 0f, -1f))
|
||||
assertArrayEquals(if (pitch) floatArrayOf(0f, 0.5f, -0.8660254f) else floatArrayOf(-0.5f, 0f, -0.8660254f), forward, 0.01f)
|
||||
}
|
||||
assertTrue(RayNeoSensorProfile.forDevice(true, 0x7f).gtMaxAxes)
|
||||
assertTrue(RayNeoSensorProfile.forDevice(true, 0x41).gtMaxAxes)
|
||||
assertEquals(-20f, RayNeoSensorProfile.forDevice(false, 0x3a).rotationXDeg, 0f)
|
||||
}
|
||||
|
||||
private fun checkGtMotion(axis: Int) {
|
||||
val mount = Math.toRadians(35.0)
|
||||
val s = sin(mount).toFloat(); val c = cos(mount).toFloat()
|
||||
val filter = RayNeoOrientationFilter(0f, gt = true)
|
||||
val level = floatArrayOf(9.81f * s, 0f, 9.81f * c)
|
||||
val first = screen(filter.update(sample(0, level)))
|
||||
val ref = HeadPose.recenter(first)
|
||||
var q = FloatArray(4)
|
||||
// Physical 30-degree turn over one second, through the complete shared
|
||||
// VITURE conversion and recenter pipeline. GT signs match device captures.
|
||||
repeat(100) { i ->
|
||||
val a = Math.toRadians((i + 1) * 0.3)
|
||||
val accel = when (axis) {
|
||||
1 -> floatArrayOf(9.81f * sin(mount + a).toFloat(), 0f, 9.81f * cos(mount + a).toFloat())
|
||||
2 -> floatArrayOf(9.81f * s * cos(a).toFloat(), -9.81f * sin(a).toFloat(), 9.81f * c * cos(a).toFloat())
|
||||
else -> level
|
||||
}
|
||||
val gyro = when (axis) {
|
||||
0 -> floatArrayOf(30f * s, 0f, 30f * c)
|
||||
1 -> floatArrayOf(0f, -30f, 0f)
|
||||
else -> floatArrayOf(-30f * c, 0f, 30f * s)
|
||||
}
|
||||
q = filter.update(sample((i + 1) * 100L, accel, gyro))
|
||||
}
|
||||
val view = HeadPose.relative(ref, screen(q))
|
||||
val forward = HeadPose.apply(view, floatArrayOf(0f, 0f, -1f))
|
||||
val up = HeadPose.apply(view, floatArrayOf(0f, 1f, 0f))
|
||||
val c30 = cos(Math.toRadians(30.0)).toFloat()
|
||||
val expectedForward = when (axis) {
|
||||
0 -> floatArrayOf(-0.5f, 0f, -c30)
|
||||
1 -> floatArrayOf(0f, 0.5f, -c30)
|
||||
else -> floatArrayOf(0f, 0f, -1f)
|
||||
}
|
||||
val expectedUp = when (axis) {
|
||||
0 -> floatArrayOf(0f, 1f, 0f)
|
||||
1 -> floatArrayOf(0f, c30, 0.5f)
|
||||
else -> floatArrayOf(-0.5f, c30, 0f)
|
||||
}
|
||||
assertArrayEquals(expectedForward, forward, 0.01f)
|
||||
assertArrayEquals(expectedUp, up, 0.01f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import kotlin.math.sqrt
|
||||
|
||||
class RayNeoProtocolTest {
|
||||
private fun sensor(tick: Long = 1234L): ByteArray = ByteBuffer.allocate(64).order(ByteOrder.LITTLE_ENDIAN).apply {
|
||||
put(0, 0x99.toByte()); put(1, 0x65); put(2, 64)
|
||||
putFloat(4, 1f); putFloat(8, 9.81f); putFloat(12, -2f)
|
||||
putFloat(16, 30f); putFloat(20, -60f); putFloat(24, 90f)
|
||||
putFloat(28, 25f); putFloat(32, 2f); putFloat(36, 3f)
|
||||
putInt(40, tick.toInt()); putFloat(44, 1f); putFloat(48, 42f); putFloat(52, 4f)
|
||||
}.array()
|
||||
|
||||
@Test fun decodesScaffoldLayoutAndUnsignedTick() {
|
||||
val p = RayNeoProtocol.classifyPacket(sensor(0xfffffff0L)) as RayNeoPacket.Sensor
|
||||
assertArrayEquals(floatArrayOf(1f, 9.81f, -2f), p.sample.accelMps2, 0f)
|
||||
assertArrayEquals(floatArrayOf(30f, -60f, 90f), p.sample.gyroDps, 0f)
|
||||
assertArrayEquals(floatArrayOf(2f, 3f, 4f), p.sample.magnet, 0f)
|
||||
assertEquals(0xfffffff0L, p.sample.deviceTick100us)
|
||||
assertEquals(25f, p.sample.temperatureC, 0f)
|
||||
assertEquals(42f, p.sample.light, 0f)
|
||||
}
|
||||
|
||||
@Test fun rejectsTruncationBadHeaderAndNonfiniteMotion() {
|
||||
assertSame(RayNeoPacket.Unknown, RayNeoProtocol.classifyPacket(sensor().copyOf(40)))
|
||||
assertSame(RayNeoPacket.Unknown, RayNeoProtocol.classifyPacket(sensor().apply { this[0] = 0 }))
|
||||
assertSame(RayNeoPacket.Unknown, RayNeoProtocol.classifyPacket(sensor().apply { this[2] = 56 }))
|
||||
val nan = sensor().apply { ByteBuffer.wrap(this).order(ByteOrder.LITTLE_ENDIAN).putFloat(16, Float.NaN) }
|
||||
assertSame(RayNeoPacket.Unknown, RayNeoProtocol.classifyPacket(nan))
|
||||
}
|
||||
|
||||
@Test fun assemblesFragmentedAndCoalescedFramesAfterNoise() {
|
||||
val a = RayNeoPacketAssembler()
|
||||
val packet = sensor()
|
||||
val bytes = byteArrayOf(1, 2, 0x99.toByte(), 0, 0) + packet + packet
|
||||
a.append(bytes.copyOf(20), 20)
|
||||
assertNull(a.nextPacket())
|
||||
val rest = bytes.copyOfRange(20, bytes.size)
|
||||
a.append(rest, rest.size)
|
||||
assertArrayEquals(packet, a.nextPacket())
|
||||
assertArrayEquals(packet, a.nextPacket())
|
||||
assertNull(a.nextPacket())
|
||||
}
|
||||
|
||||
@Test fun commandAndDeviceInfoMatchScaffold() {
|
||||
assertArrayEquals(byteArrayOf(0x66, 1, 0) + ByteArray(61), RayNeoProtocol.buildCommandPacket(1, 0, 64))
|
||||
val info = ByteArray(64).apply {
|
||||
this[0] = 0x99.toByte(); this[1] = 0xc8.toByte(); this[2] = 64
|
||||
this[8] = 0; this[21] = 0x3a; this[43] = 1
|
||||
}
|
||||
assertEquals(RayNeoDeviceInfo(0x3a, true), RayNeoProtocol.parseDeviceInfo(info))
|
||||
assertNull(RayNeoProtocol.parseDeviceInfo(info.copyOf(21)))
|
||||
info[8] = 1
|
||||
assertNull(RayNeoProtocol.parseDeviceInfo(info))
|
||||
}
|
||||
|
||||
@Test fun discoveryDoesNotClaimEveryTclOrBridgeDevice() {
|
||||
assertNotNull(RayNeoModel.model(0x1bbb, 0xaf50, null))
|
||||
for (name in listOf("RayNeo Air 2", "RayNeo Air 2s", "RayNeo Air 3", "RayNeo Air 4 Pro", "RayNeo GT")) {
|
||||
assertEquals(name, RayNeoModel.model(0x22d9, 1, name)?.name)
|
||||
assertTrue(Glasses.isGlassesDisplayName(name))
|
||||
}
|
||||
assertNull(RayNeoModel.model(0x1bbb, 1, "TCL Phone"))
|
||||
assertNull(RayNeoModel.model(0x22d9, 1, "USB bridge"))
|
||||
assertNull(RayNeoModel.model(0x1234, 0xaf50, "RayNeo Air 3"))
|
||||
assertFalse(Glasses.isGlassesDisplayName("TCL TV"))
|
||||
assertTrue(Glasses.isGlassesDisplayName("VITURE Pro"))
|
||||
}
|
||||
|
||||
@Test fun gtIdentityWorksWithoutProductNameAndPreservesMaxName() {
|
||||
assertEquals("RayNeo GT series", RayNeoModel.model(0x3941, 0xaf50, null)?.name)
|
||||
assertEquals("RayNeo GT series", RayNeoModel.model(0x3941, 0xaf50, "USB device")?.name)
|
||||
for (name in listOf("RayNeo GT", "RayNeo GT MAX")) {
|
||||
val model = RayNeoModel.model(0x3941, 0xaf50, name)!!
|
||||
assertEquals(name, model.name)
|
||||
assertEquals(6, model.mode(60, true))
|
||||
assertEquals(7, model.mode(120, false))
|
||||
assertTrue(Glasses.isGlassesDisplayName(name))
|
||||
}
|
||||
assertNull(RayNeoModel.model(0x3941, 0x1234, "RayNeo GT"))
|
||||
assertNull(RayNeoModel.model(0x1234, 0xaf50, "RayNeo GT MAX"))
|
||||
}
|
||||
|
||||
@Test fun gtNotificationsAndControlAcksAreNotDeviceInfo() {
|
||||
for (cmd in listOf(0x08, 6, 7, 0x50)) {
|
||||
val raw = ByteArray(64).apply {
|
||||
this[0] = 0x99.toByte(); this[1] = 0xc8.toByte(); this[2] = 64
|
||||
this[8] = cmd.toByte()
|
||||
}
|
||||
assertEquals(cmd, (RayNeoProtocol.classifyPacket(raw) as RayNeoPacket.Response).cmd)
|
||||
assertNull(RayNeoProtocol.parseDeviceInfo(raw))
|
||||
}
|
||||
}
|
||||
|
||||
private fun sample(tick: Long, yawDps: Float = 0f) = RayNeoSensorSample(
|
||||
floatArrayOf(0f, 9.81f, 0f), floatArrayOf(0f, yawDps, 0f), 0f,
|
||||
FloatArray(3), 0f, 0f, tick,
|
||||
)
|
||||
|
||||
@Test fun stationaryPoseIsFiniteAndYawUsesVitureUpAxis() {
|
||||
val stationary = RayNeoOrientationFilter(0f)
|
||||
repeat(100) { assertArrayEquals(floatArrayOf(1f, 0f, 0f, 0f), stationary.update(sample(it * 100L)), 1e-6f) }
|
||||
val filter = RayNeoOrientationFilter(0f)
|
||||
var q = FloatArray(4)
|
||||
repeat(100) { q = filter.update(sample(it * 100L, 90f)) }
|
||||
assertEquals(1f, sqrt(q.sumOf { (it * it).toDouble() }).toFloat(), 1e-5f)
|
||||
val pose = RayNeoOrientationFilter.glassesPose(q)
|
||||
assertEquals(0f, pose[0], 1e-5f); assertEquals(0f, pose[1], 1e-5f)
|
||||
assertEquals(sqrt(0.5f), pose[2], 0.01f); assertEquals(sqrt(0.5f), pose[3], 0.01f)
|
||||
}
|
||||
|
||||
@Test fun tickWrapDoesNotIntroduceOrientationJump() {
|
||||
val filter = RayNeoOrientationFilter(0f)
|
||||
filter.update(sample(0xfffffff0L, 90f))
|
||||
val q = filter.update(sample(84, 90f)) // 100 ticks = 10ms across uint32 rollover.
|
||||
assertEquals(kotlin.math.sin(Math.PI * 0.005).toFloat(), q[2], 1e-4f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.usb.UsbConfiguration
|
||||
import android.hardware.usb.UsbConstants
|
||||
import android.hardware.usb.UsbDevice
|
||||
import android.hardware.usb.UsbDeviceConnection
|
||||
import android.hardware.usb.UsbEndpoint
|
||||
import android.hardware.usb.UsbInterface
|
||||
import android.hardware.usb.UsbManager
|
||||
import android.os.Parcelable
|
||||
import android.util.Log
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assert.fail
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.shadow.api.Shadow
|
||||
import org.robolectric.shadows.ShadowLog
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import org.robolectric.util.ReflectionHelpers.ClassParameter.from
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.LinkedBlockingQueue
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE, shadows = [VitureLinkTest.QueuedConnection::class])
|
||||
class VitureLinkTest {
|
||||
private val frames = LinkedBlockingQueue<VitureProtocol.Frame>()
|
||||
private val imu = LinkedBlockingQueue<ByteArray>()
|
||||
private var link: VitureLink? = null
|
||||
|
||||
private val listener = object : VitureLink.Listener {
|
||||
override fun onFrame(frame: VitureProtocol.Frame) { frames += frame }
|
||||
override fun onImu(payload: ByteArray, timeNs: Long) { imu += payload }
|
||||
override fun onLost() {}
|
||||
}
|
||||
|
||||
@Before fun prepare() {
|
||||
incoming.clear()
|
||||
ShadowLog.clear()
|
||||
}
|
||||
|
||||
@After fun close() { link?.close() }
|
||||
|
||||
@Test fun framesWithABadChecksumAreDroppedAndCounted() {
|
||||
val usb = RuntimeEnvironment.getApplication().getSystemService(Context.USB_SERVICE) as UsbManager
|
||||
val l = VitureLink.open(usb, glasses(), listener)
|
||||
assertNotNull(l)
|
||||
link = l
|
||||
val report = packet(VitureProtocol.MAGIC_REPORT, VitureProtocol.EVENT_BRIGHTNESS, byteArrayOf(6))
|
||||
val pose = ByteArray(36) { it.toByte() }
|
||||
val imuPacket = packet(VitureProtocol.MAGIC_IMU, 0, pose)
|
||||
feed(MCU_IN, corrupt(report), report)
|
||||
feed(IMU_IN, corrupt(imuPacket), imuPacket)
|
||||
|
||||
val frame = frames.poll(2, TimeUnit.SECONDS)
|
||||
assertNotNull(frame)
|
||||
assertEquals(VitureProtocol.EVENT_BRIGHTNESS, frame!!.msgId)
|
||||
assertArrayEquals(byteArrayOf(6), frame.payload)
|
||||
assertArrayEquals(pose, imu.poll(2, TimeUnit.SECONDS))
|
||||
assertNull(frames.poll(200, TimeUnit.MILLISECONDS))
|
||||
assertNull(imu.poll(0, TimeUnit.MILLISECONDS))
|
||||
val transport: GlassesLink = l!!
|
||||
assertEquals(2L, transport.badFrames)
|
||||
assertEquals(1, ShadowLog.getLogsForTag("VitureLink").count { it.type == Log.WARN })
|
||||
}
|
||||
|
||||
@Test fun bothReadersDiscardCorruptReportsAndAcceptTheNextValidPacket() {
|
||||
for (imuEndpoint in listOf(false, true)) {
|
||||
checkPackets(VitureProtocol.MAGIC_REPORT, imuEndpoint, expectImu = false)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun imuReaderDiscardsCorruptPoseAndAcceptsTheNextValidPacket() {
|
||||
checkPackets(VitureProtocol.MAGIC_IMU, imuEndpoint = true, expectImu = true)
|
||||
}
|
||||
|
||||
private fun checkPackets(magic: Int, imuEndpoint: Boolean, expectImu: Boolean) {
|
||||
val frames = mutableListOf<VitureProtocol.Frame>()
|
||||
val poses = mutableListOf<ByteArray>()
|
||||
val listener = object : VitureLink.Listener {
|
||||
override fun onFrame(frame: VitureProtocol.Frame) { frames += frame }
|
||||
override fun onImu(payload: ByteArray, timeNs: Long) { poses += payload }
|
||||
override fun onLost() { fail("A corrupt packet must not disconnect the link") }
|
||||
}
|
||||
val ctor = VitureLink::class.java.getDeclaredConstructor(UsbDeviceConnection::class.java,
|
||||
UsbInterface::class.java, UsbEndpoint::class.java, UsbEndpoint::class.java,
|
||||
UsbInterface::class.java, UsbEndpoint::class.java, VitureLink.Listener::class.java)
|
||||
.apply { isAccessible = true }
|
||||
val link = ctor.newInstance(Shadow.newInstanceOf(UsbDeviceConnection::class.java),
|
||||
Shadow.newInstanceOf(UsbInterface::class.java), Shadow.newInstanceOf(UsbEndpoint::class.java),
|
||||
null, null, null, listener)
|
||||
val receive = VitureLink::class.java.getDeclaredMethod("onPacket", ByteArray::class.java,
|
||||
Int::class.javaPrimitiveType, Boolean::class.javaPrimitiveType).apply { isAccessible = true }
|
||||
val payload = byteArrayOf(0, 0x32)
|
||||
val valid = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, payload).also { it[1] = magic.toByte() }
|
||||
val corrupt = valid.copyOf().also { it[18] = 1 }
|
||||
assertFalse(VitureProtocol.parse(corrupt, corrupt.size)!!.crcOk)
|
||||
receive.invoke(link, corrupt, corrupt.size, imuEndpoint)
|
||||
assertTrue(frames.isEmpty())
|
||||
assertTrue(poses.isEmpty())
|
||||
receive.invoke(link, valid, valid.size, imuEndpoint)
|
||||
assertEquals(if (expectImu) 0 else 1, frames.size)
|
||||
assertEquals(if (expectImu) 1 else 0, poses.size)
|
||||
assertArrayEquals(payload, if (expectImu) poses.single() else frames.single().payload)
|
||||
}
|
||||
|
||||
private fun packet(magic: Int, msgId: Int, payload: ByteArray) =
|
||||
VitureProtocol.packet(msgId, payload).also { it[1] = magic.toByte() }
|
||||
|
||||
private fun corrupt(packet: ByteArray) = packet.copyOf().also { it[18] = (it[18].toInt() xor 0x01).toByte() }
|
||||
|
||||
private fun feed(address: Int, vararg packets: ByteArray) {
|
||||
val queue = incoming.getOrPut(address) { LinkedBlockingQueue() }
|
||||
packets.forEach(queue::put)
|
||||
}
|
||||
|
||||
private fun glasses(): UsbDevice {
|
||||
fun endpoint(address: Int): UsbEndpoint = ReflectionHelpers.callConstructor(
|
||||
UsbEndpoint::class.java,
|
||||
from(Int::class.javaPrimitiveType, address),
|
||||
from(Int::class.javaPrimitiveType, UsbConstants.USB_ENDPOINT_XFER_INT),
|
||||
from(Int::class.javaPrimitiveType, VitureProtocol.PACKET_SIZE),
|
||||
from(Int::class.javaPrimitiveType, 1),
|
||||
)
|
||||
fun hid(id: Int, vararg endpoints: UsbEndpoint): UsbInterface = ReflectionHelpers.callConstructor(
|
||||
UsbInterface::class.java,
|
||||
from(Int::class.javaPrimitiveType, id),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(String::class.java, "hid$id"),
|
||||
from(Int::class.javaPrimitiveType, UsbConstants.USB_CLASS_HID),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
).also { hidden(it, "setEndpoints", *endpoints) }
|
||||
val config: UsbConfiguration = ReflectionHelpers.callConstructor(
|
||||
UsbConfiguration::class.java,
|
||||
from(Int::class.javaPrimitiveType, 1),
|
||||
from(String::class.java, "default"),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(Int::class.javaPrimitiveType, 250),
|
||||
)
|
||||
hidden(config, "setInterfaces", hid(0, endpoint(IMU_IN)), hid(1, endpoint(MCU_IN), endpoint(MCU_OUT)))
|
||||
return ReflectionHelpers.callConstructor(
|
||||
UsbDevice::class.java,
|
||||
from(String::class.java, "/dev/bus/usb/001/002"),
|
||||
from(Int::class.javaPrimitiveType, VitureProtocol.VENDOR_ID),
|
||||
from(Int::class.javaPrimitiveType, 0x101d),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(Int::class.javaPrimitiveType, 0),
|
||||
from(String::class.java, "VITURE"),
|
||||
from(String::class.java, "VITURE Pro XR"),
|
||||
from(String::class.java, "1.0"),
|
||||
from(String::class.java, "test"),
|
||||
).also { hidden(it, "setConfigurations", config) }
|
||||
}
|
||||
|
||||
private fun hidden(target: Any, method: String, vararg items: Parcelable) {
|
||||
ReflectionHelpers.callInstanceMethod<Unit>(target, method, from(Array<Parcelable>::class.java, arrayOf(*items)))
|
||||
}
|
||||
|
||||
@Implements(UsbDeviceConnection::class)
|
||||
class QueuedConnection {
|
||||
@Implementation fun claimInterface(intf: UsbInterface, force: Boolean) = true
|
||||
@Implementation fun releaseInterface(intf: UsbInterface) = true
|
||||
@Implementation fun bulkTransfer(endpoint: UsbEndpoint, buffer: ByteArray, length: Int, timeout: Int): Int {
|
||||
if (endpoint.direction == UsbConstants.USB_DIR_OUT) return length
|
||||
val packet = incoming[endpoint.address]?.poll(timeout.toLong(), TimeUnit.MILLISECONDS) ?: return 0
|
||||
packet.copyInto(buffer, endIndex = minOf(packet.size, length))
|
||||
return minOf(packet.size, length)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val IMU_IN = 0x81
|
||||
private const val MCU_IN = 0x82
|
||||
private const val MCU_OUT = 0x02
|
||||
private val incoming = ConcurrentHashMap<Int, LinkedBlockingQueue<ByteArray>>()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.nio.ByteBuffer
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
|
||||
class VitureProtocolTest {
|
||||
private fun u8(b: Byte) = b.toInt() and 0xFF
|
||||
private fun u16(b: ByteArray, at: Int) = u8(b[at]) or (u8(b[at + 1]) shl 8)
|
||||
|
||||
@Test fun crcIsXmodem() {
|
||||
val check = "123456789".toByteArray()
|
||||
assertEquals(0x31C3, VitureProtocol.crc16(check, 0, check.size))
|
||||
}
|
||||
|
||||
@Test fun packetLayout() {
|
||||
val p = VitureProtocol.packet(VitureProtocol.MSG_BRIGHTNESS, byteArrayOf(5, 0))
|
||||
assertEquals(VitureProtocol.PACKET_SIZE, p.size)
|
||||
assertEquals(0xFF, u8(p[0]))
|
||||
assertEquals(VitureProtocol.MAGIC_COMMAND, u8(p[1]))
|
||||
assertEquals(2 + 0x0C, u16(p, 4))
|
||||
assertEquals(VitureProtocol.MSG_BRIGHTNESS, u16(p, 14))
|
||||
assertEquals(5, u8(p[18]))
|
||||
assertEquals(VitureProtocol.crc16(p, 4, u16(p, 4) + 2), u16(p, 2))
|
||||
for (i in 6 until 14) assertEquals(0, u8(p[i]))
|
||||
}
|
||||
|
||||
@Test fun parseRoundTripsAndChecksCrc() {
|
||||
val p = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, byteArrayOf(0x34))
|
||||
val f = VitureProtocol.parse(p, p.size)
|
||||
assertNotNull(f)
|
||||
assertEquals(VitureProtocol.MAGIC_COMMAND, f!!.magic)
|
||||
assertEquals(VitureProtocol.MSG_DISPLAY, f.msgId)
|
||||
assertArrayEquals(byteArrayOf(0x34), f.payload)
|
||||
assertTrue(f.crcOk)
|
||||
p[18] = 0x31
|
||||
val badCrc = VitureProtocol.parse(p, p.size)!!
|
||||
assertFalse(badCrc.crcOk)
|
||||
assertArrayEquals(byteArrayOf(0x31), badCrc.payload)
|
||||
}
|
||||
|
||||
@Test fun parseAcceptsPaddingOutsideDeclaredFrame() {
|
||||
val payload = byteArrayOf(0, 0x32)
|
||||
val p = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, payload)
|
||||
val end = 18 + payload.size
|
||||
p.fill(0x5A, end)
|
||||
for (size in listOf(end, p.size)) {
|
||||
val frame = VitureProtocol.parse(p, size)!!
|
||||
assertArrayEquals(payload, frame.payload)
|
||||
assertTrue(frame.crcOk)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun parseRejectsTruncatedDeclaredFrame() {
|
||||
val p = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, byteArrayOf(0, 0x32))
|
||||
val received = 19
|
||||
assertNull(VitureProtocol.parse(p, received))
|
||||
assertNull(VitureProtocol.parse(p.copyOf(received), received))
|
||||
p[4] = 0xFF.toByte()
|
||||
p[5] = 0xFF.toByte()
|
||||
assertNull(VitureProtocol.parse(p, p.size))
|
||||
}
|
||||
|
||||
@Test fun parseRejectsInvalidReceiveSize() {
|
||||
val p = VitureProtocol.packet(VitureProtocol.MSG_DISPLAY, byteArrayOf(0, 0x32))
|
||||
assertNull(VitureProtocol.parse(p, p.size + 1))
|
||||
assertNull(VitureProtocol.parse(p, 17))
|
||||
assertNull(VitureProtocol.parse(ByteArray(0), 18))
|
||||
assertNull(VitureProtocol.parse(ByteArray(0), 0))
|
||||
assertNull(VitureProtocol.parse(p, -1))
|
||||
}
|
||||
|
||||
@Test fun parseReadsRepliesAndRejectsJunk() {
|
||||
val reply = VitureProtocol.packet(VitureProtocol.MSG_GET_VOLUME, byteArrayOf(0, 5))
|
||||
reply[1] = VitureProtocol.MAGIC_REPORT.toByte()
|
||||
val f = VitureProtocol.parse(reply, reply.size)!!
|
||||
assertEquals(VitureProtocol.MAGIC_REPORT, f.magic)
|
||||
assertArrayEquals(byteArrayOf(0, 5), f.payload)
|
||||
assertNull(VitureProtocol.parse(ByteArray(64), 64))
|
||||
assertNull(VitureProtocol.parse(reply, 8))
|
||||
val wrongMagic = reply.copyOf().also { it[1] = 0x10 }
|
||||
assertNull(VitureProtocol.parse(wrongMagic, wrongMagic.size))
|
||||
}
|
||||
|
||||
@Test fun modelsAndModes() {
|
||||
val pro = VitureProtocol.model(0x101d, "VITURE Pro XR Glasses")!!
|
||||
assertEquals("VITURE Pro XR", pro.name)
|
||||
assertEquals(VitureProtocol.Family.PRO, pro.family)
|
||||
assertTrue(pro.controllable)
|
||||
assertFalse(pro.sbs90)
|
||||
assertEquals(0x34, pro.mode(120, false))
|
||||
assertEquals(0x33, pro.mode(90, false))
|
||||
assertEquals(0x31, pro.mode(60, false))
|
||||
assertEquals(0x32, pro.mode(90, true))
|
||||
assertEquals(0x32, pro.mode(60, true))
|
||||
|
||||
val luma = VitureProtocol.model(0x1131, null)!!
|
||||
assertEquals("VITURE Luma", luma.name)
|
||||
assertFalse(luma.sbs90)
|
||||
assertEquals(0x44, luma.mode(120, false))
|
||||
assertEquals(0x42, luma.mode(90, true))
|
||||
assertEquals(0x42, luma.mode(60, true))
|
||||
|
||||
val ultra = VitureProtocol.model(0x1101, null)!!
|
||||
assertTrue(ultra.sbs90)
|
||||
assertEquals(0x45, ultra.mode(90, true))
|
||||
assertEquals(0x42, ultra.mode(60, true))
|
||||
|
||||
assertTrue(VitureProtocol.model(0x1201, null)!!.controllable)
|
||||
assertTrue(VitureProtocol.model(0x1201, null)!!.needsSdk)
|
||||
assertNull(VitureProtocol.model(0x9999, "VITURE"))
|
||||
assertEquals("VITURE One", VitureProtocol.model(0x1011, "Something else")!!.name)
|
||||
}
|
||||
|
||||
@Test fun imuPrefersTheQuaternion() {
|
||||
val half = Math.toRadians(30.0) / 2
|
||||
val payload = ByteBuffer.allocate(40)
|
||||
.putFloat(1f).putFloat(2f).putFloat(3f)
|
||||
.putFloat(0f).putFloat(0f)
|
||||
.putFloat(cos(half).toFloat()).putFloat(0f).putFloat(0f).putFloat(sin(half).toFloat())
|
||||
.array()
|
||||
val q = VitureProtocol.imuQuaternion(payload)!!
|
||||
assertArrayEquals(floatArrayOf(0f, 0f, sin(half).toFloat(), cos(half).toFloat()), q, 1e-5f)
|
||||
}
|
||||
|
||||
@Test fun imuFallsBackToEuler() {
|
||||
val payload = ByteBuffer.allocate(12).putFloat(0f).putFloat(0f).putFloat(30f).array()
|
||||
val q = VitureProtocol.imuQuaternion(payload)!!
|
||||
val half = Math.toRadians(30.0) / 2
|
||||
assertArrayEquals(floatArrayOf(0f, 0f, sin(half).toFloat(), cos(half).toFloat()), q, 1e-5f)
|
||||
assertNull(VitureProtocol.imuQuaternion(ByteArray(8)))
|
||||
val nan = ByteBuffer.allocate(12).putFloat(Float.NaN).putFloat(0f).putFloat(0f).array()
|
||||
assertNull(VitureProtocol.imuQuaternion(nan))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import kotlin.math.sqrt
|
||||
import com.droiddeck.launcher.gpu.HeadPose
|
||||
|
||||
class VitureSdkTest {
|
||||
@Test fun pro2UsesItsOwnSdkTransportAndDisplayLimits() {
|
||||
val m = VitureProtocol.sdkModel("Pro 2")!!
|
||||
assertTrue(m.needsSdk)
|
||||
assertEquals(VitureProtocol.Family.PRO2, m.family)
|
||||
assertEquals(0x34, m.mode(120, false))
|
||||
assertEquals(0x32, m.mode(90, true))
|
||||
assertFalse(m.sbs90)
|
||||
assertFalse(m.hasSunblock)
|
||||
assertEquals(8, m.volumeMax)
|
||||
assertEquals(50f, m.diagonalFovDeg, 0f)
|
||||
}
|
||||
|
||||
@Test fun ultraUsesSdkWith1200pAnd90HzStereo() {
|
||||
val m = VitureProtocol.sdkModel("Luma Ultra")!!
|
||||
assertTrue(m.needsSdk)
|
||||
assertEquals(0x44, m.mode(120, false))
|
||||
assertEquals(0x42, m.mode(60, true))
|
||||
assertEquals(0x45, m.mode(90, true))
|
||||
assertTrue(m.hasSunblock)
|
||||
}
|
||||
|
||||
@Test fun gen2DoesNotImplyBeastCapabilities() {
|
||||
val beast = VitureProtocol.sdkModel("Beast")!!
|
||||
assertTrue(beast.needsSdk)
|
||||
assertEquals(15, beast.volumeMax)
|
||||
assertTrue(beast.hasSunblock)
|
||||
assertFalse(VitureProtocol.sdkModel("Pro")!!.needsSdk)
|
||||
assertNull(VitureProtocol.sdkModel("Unknown"))
|
||||
assertNull(VitureProtocol.sdkModel("Mobile Dock"))
|
||||
}
|
||||
|
||||
@Test fun otherGen2DevicesPreserveSdkNwuAxesAndChangeComponentOrder() {
|
||||
val a = sqrt(0.5f)
|
||||
assertArrayEquals(floatArrayOf(0f, 0f, a, a),
|
||||
VitureSdkPose.toGlasses(floatArrayOf(a, 0f, 0f, a), false), 0.00001f)
|
||||
}
|
||||
|
||||
@Test fun pro2AndCarinaSelectOpenGlButOtherGen2ModelsKeepTheirExistingBasis() {
|
||||
assertTrue(VitureSdkPose.usesOpenGlAxes(false, VitureProtocol.Family.PRO2))
|
||||
assertTrue(VitureSdkPose.usesOpenGlAxes(true, VitureProtocol.Family.ULTRA))
|
||||
assertFalse(VitureSdkPose.usesOpenGlAxes(false, VitureProtocol.Family.BEAST))
|
||||
assertFalse(VitureSdkPose.usesOpenGlAxes(false, VitureProtocol.Family.PRO))
|
||||
assertFalse(VitureSdkPose.usesOpenGlAxes(false, null))
|
||||
}
|
||||
|
||||
@Test fun pro2YawPitchAndRollReachTheCorrectRendererAxes() {
|
||||
val openGl = VitureSdkPose.usesOpenGlAxes(false, VitureProtocol.sdkModel("Pro 2")!!.family)
|
||||
fun screen(wxyz: FloatArray): FloatArray {
|
||||
val q = VitureSdkPose.toGlasses(wxyz, openGl)!!
|
||||
return HeadPose.fromGlasses(q[0], q[1], q[2], q[3])
|
||||
}
|
||||
val ref = HeadPose.recenter(screen(floatArrayOf(1f, 0f, 0f, 0f)))
|
||||
val s = kotlin.math.sin(Math.PI / 12).toFloat()
|
||||
val c = kotlin.math.cos(Math.PI / 12).toFloat()
|
||||
for ((axis, expected) in listOf(
|
||||
2 to floatArrayOf(-0.5f, 0f, -0.8660254f), // Observed left turn: positive Y.
|
||||
1 to floatArrayOf(0f, 0.5f, -0.8660254f), // Observed upward look: positive X.
|
||||
3 to floatArrayOf(-0.5f, 0.8660254f, 0f),
|
||||
)) {
|
||||
val q = floatArrayOf(c, 0f, 0f, 0f).apply { this[axis] = s }
|
||||
val view = HeadPose.relative(ref, screen(q))
|
||||
val direction = if (axis == 3) floatArrayOf(0f, 1f, 0f) else floatArrayOf(0f, 0f, -1f)
|
||||
assertArrayEquals(expected, HeadPose.apply(view, direction), 0.001f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun ultraOpenGlAxesMatchExistingGlassesHeadTracker() {
|
||||
val a = sqrt(0.5f)
|
||||
// OpenGL Y yaw maps to NWU Z; OpenGL X pitch maps to negative NWU Y.
|
||||
assertArrayEquals(floatArrayOf(0f, 0f, a, a),
|
||||
VitureSdkPose.toGlasses(floatArrayOf(a, 0f, a, 0f), true), 0.00001f)
|
||||
assertArrayEquals(floatArrayOf(0f, -a, 0f, a),
|
||||
VitureSdkPose.toGlasses(floatArrayOf(a, a, 0f, 0f), true), 0.00001f)
|
||||
assertArrayEquals(floatArrayOf(-a, 0f, 0f, a),
|
||||
VitureSdkPose.toGlasses(floatArrayOf(a, 0f, 0f, a), true), 0.00001f)
|
||||
}
|
||||
|
||||
@Test fun invalidOrUninitializedSdkPosesNeverReachTheRenderer() {
|
||||
for (q in listOf(FloatArray(4), floatArrayOf(Float.NaN, 0f, 0f, 0f),
|
||||
floatArrayOf(Float.POSITIVE_INFINITY, 0f, 0f, 0f), floatArrayOf(3f, 0f, 0f, 0f), FloatArray(7))) {
|
||||
assertNull(VitureSdkPose.toGlasses(q, false))
|
||||
assertNull(VitureSdkPose.toGlasses(q, true))
|
||||
}
|
||||
assertArrayEquals(floatArrayOf(0f, 0f, 0f, 1f),
|
||||
VitureSdkPose.toGlasses(floatArrayOf(1.1f, 0f, 0f, 0f), true), 0.00001f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.LinkedBlockingQueue
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
class XrealLinkTest {
|
||||
private class Input : InputStream() {
|
||||
private val bytes = LinkedBlockingQueue<Int>()
|
||||
@Volatile private var closed = false
|
||||
fun add(value: ByteArray) { value.forEach { bytes.add(it.toInt() and 255) } }
|
||||
override fun read(): Int = if (closed && bytes.isEmpty()) -1 else bytes.take()
|
||||
override fun read(b: ByteArray, off: Int, len: Int): Int {
|
||||
if (len == 0) return 0
|
||||
val first = read()
|
||||
if (first < 0) return -1
|
||||
b[off] = first.toByte()
|
||||
var count = 1
|
||||
while (count < len) {
|
||||
val value = bytes.poll() ?: break
|
||||
if (value < 0) break
|
||||
b[off + count++] = value.toByte()
|
||||
}
|
||||
return count
|
||||
}
|
||||
override fun close() { closed = true; bytes.clear(); bytes.offer(-1) }
|
||||
}
|
||||
|
||||
private class Channels : XrealChannels {
|
||||
override val controlInput = Input()
|
||||
override val reports = Input()
|
||||
val wrote = LinkedBlockingQueue<Int>()
|
||||
val closed = CountDownLatch(1)
|
||||
@Volatile var acknowledge = true
|
||||
override val controlOutput = object : OutputStream() {
|
||||
private val buffer = ByteArrayOutputStream()
|
||||
override fun write(b: Int) { buffer.write(b) }
|
||||
override fun flush() {
|
||||
val data = buffer.toByteArray(); buffer.reset()
|
||||
val request = XrealProtocol.read(data.inputStream())
|
||||
val id = ByteBuffer.wrap(request.body).int and Int.MAX_VALUE
|
||||
wrote.offer(request.command)
|
||||
if (acknowledge) controlInput.add(XrealProtocolTest.response(request.command, id,
|
||||
if (request.command == XrealProtocol.GET_DISPLAY) byteArrayOf(0x22, 2, 0x10, 2) else byteArrayOf(0x22, 0)))
|
||||
}
|
||||
}
|
||||
override fun close() { controlInput.close(); reports.close(); closed.countDown() }
|
||||
}
|
||||
|
||||
private class Listener : XrealLink.Listener {
|
||||
val ready = CountDownLatch(1)
|
||||
val poses = LinkedBlockingQueue<FloatArray>()
|
||||
val values = LinkedBlockingQueue<Pair<Int, Int>>()
|
||||
val errors = LinkedBlockingQueue<Boolean>()
|
||||
override fun onReady() { ready.countDown() }
|
||||
override fun onValue(message: Int, value: Int) { values.offer(message to value) }
|
||||
override fun onPose(raw: ByteArray, pose: FloatArray, timeNs: Long) { poses.offer(pose) }
|
||||
override fun onUnavailable(networkMissing: Boolean) { errors.offer(networkMissing) }
|
||||
}
|
||||
|
||||
@Test fun trackingUsesSharedPoseAndCommandsAreAcknowledgedBeforePublishing() {
|
||||
val channels = Channels()
|
||||
val listener = Listener()
|
||||
val connector = object : XrealConnector {
|
||||
override fun connect() = channels
|
||||
override fun close() = channels.close()
|
||||
}
|
||||
val link = XrealLink(connector, listener)
|
||||
try {
|
||||
assertTrue(listener.ready.await(2, TimeUnit.SECONDS))
|
||||
assertEquals(VitureProtocol.MSG_GET_DISPLAY to 2, listener.values.poll(2, TimeUnit.SECONDS))
|
||||
assertTrue(link.send(VitureProtocol.MSG_IMU, byteArrayOf(1)))
|
||||
channels.reports.add(XrealProtocolTest.frame(XrealProtocol.REPORT, XrealProtocolTest.sensor()))
|
||||
assertNotNull(listener.poses.poll(2, TimeUnit.SECONDS))
|
||||
assertFalse(link.send(VitureProtocol.MSG_VOLUME, byteArrayOf(5)))
|
||||
assertFalse(link.send(VitureProtocol.MSG_DISPLAY, byteArrayOf(0x32)))
|
||||
assertTrue(link.send(VitureProtocol.MSG_BRIGHTNESS, byteArrayOf(6)))
|
||||
assertEquals(VitureProtocol.MSG_BRIGHTNESS to 6, listener.values.poll(2, TimeUnit.SECONDS))
|
||||
} finally { link.close() }
|
||||
assertFalse(link.ready)
|
||||
assertTrue(channels.closed.await(1, TimeUnit.SECONDS))
|
||||
assertFalse(link.send(VitureProtocol.MSG_IMU, byteArrayOf(1)))
|
||||
}
|
||||
|
||||
@Test fun failedControlReadDisconnectsWithoutPublishingRequestedBrightness() {
|
||||
val channels = Channels()
|
||||
val listener = Listener()
|
||||
val link = XrealLink(object : XrealConnector {
|
||||
override fun connect() = channels
|
||||
override fun close() = channels.close()
|
||||
}, listener)
|
||||
try {
|
||||
assertTrue(listener.ready.await(2, TimeUnit.SECONDS))
|
||||
listener.values.clear()
|
||||
channels.acknowledge = false
|
||||
assertTrue(link.send(VitureProtocol.MSG_BRIGHTNESS, byteArrayOf(8)))
|
||||
channels.controlInput.close()
|
||||
assertEquals(false, listener.errors.poll(2, TimeUnit.SECONDS))
|
||||
assertFalse(link.ready)
|
||||
assertNull(listener.values.poll())
|
||||
} finally { link.close() }
|
||||
}
|
||||
|
||||
@Test fun missingNetworkIsReportedAndCloseCancelsRetry() {
|
||||
val listener = Listener()
|
||||
val closed = AtomicBoolean()
|
||||
val link = XrealLink(object : XrealConnector {
|
||||
override fun connect(): XrealChannels = throw XrealNetworkMissing()
|
||||
override fun close() { closed.set(true) }
|
||||
}, listener)
|
||||
try {
|
||||
assertEquals(true, listener.errors.poll(2, TimeUnit.SECONDS))
|
||||
assertFalse(link.ready)
|
||||
} finally { link.close() }
|
||||
assertTrue(closed.get())
|
||||
}
|
||||
|
||||
@Test fun closeDuringConnectCannotPublishReadyOrLeakSockets() {
|
||||
val entered = CountDownLatch(1)
|
||||
val release = CountDownLatch(1)
|
||||
val channels = Channels()
|
||||
val listener = Listener()
|
||||
val link = XrealLink(object : XrealConnector {
|
||||
override fun connect(): XrealChannels {
|
||||
entered.countDown()
|
||||
release.await()
|
||||
return channels
|
||||
}
|
||||
override fun close() { release.countDown(); channels.close() }
|
||||
}, listener)
|
||||
assertTrue(entered.await(2, TimeUnit.SECONDS))
|
||||
link.close()
|
||||
assertTrue(channels.closed.await(1, TimeUnit.SECONDS))
|
||||
assertFalse(listener.ready.await(100, TimeUnit.MILLISECONDS))
|
||||
assertTrue(listener.values.isEmpty())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import android.net.IpPrefix
|
||||
import android.net.LinkProperties
|
||||
import android.net.RouteInfo
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.util.ReflectionHelpers
|
||||
import org.robolectric.util.ReflectionHelpers.ClassParameter.from
|
||||
import java.net.InetAddress
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
class XrealNetworkTest {
|
||||
private val target = InetAddress.getByName(XrealProtocol.HOST)
|
||||
private fun properties(address: String, prefix: Int, gateway: String? = null) = LinkProperties().apply {
|
||||
interfaceName = "eth0"
|
||||
val route = ReflectionHelpers.callConstructor<RouteInfo>(RouteInfo::class.java,
|
||||
from(IpPrefix::class.java, IpPrefix(InetAddress.getByName(address), prefix)),
|
||||
from(InetAddress::class.java, InetAddress.getByName(gateway ?: "0.0.0.0")),
|
||||
from(String::class.java, "eth0"))
|
||||
addRoute(route)
|
||||
}
|
||||
@Test fun onlyAnOnLinkGlassesRouteQualifies() {
|
||||
val direct = properties("169.254.2.0", 24)
|
||||
val default = properties("0.0.0.0", 0)
|
||||
val gateway = properties("169.254.2.0", 24, "192.168.1.1")
|
||||
val unrelated = properties("192.168.1.0", 24)
|
||||
assertTrue(AndroidXrealConnector.hasDirectRoute(direct, target))
|
||||
assertFalse(AndroidXrealConnector.hasDirectRoute(default, target))
|
||||
assertFalse(AndroidXrealConnector.hasDirectRoute(gateway, target))
|
||||
assertFalse(AndroidXrealConnector.hasDirectRoute(unrelated, target))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import com.droiddeck.launcher.gpu.HeadPose
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
|
||||
class XrealOrientationFilterTest {
|
||||
private fun sample(time: Long, gyro: Vec3 = Vec3(0f, 0f, 0f), accel: Vec3 = Vec3(0f, -9.81f, 0f)) =
|
||||
XrealProtocol.Sample(time, gyro, accel)
|
||||
private fun matrix(q: FloatArray) = HeadPose.fromGlasses(q[0], q[1], q[2], q[3])
|
||||
|
||||
@Test fun firstPoseAlignsGravityAndDuplicateOrReversedTimeIsDiscarded() {
|
||||
val filter = XrealOrientationFilter()
|
||||
val tilted = Vec3(4.905f, -8.4957f, 0f)
|
||||
val first = filter.update(sample(1000000, accel = tilted))!!
|
||||
assertNull(filter.update(sample(1000000)))
|
||||
assertNull(filter.update(sample(999999)))
|
||||
assertArrayEquals(first, filter.update(sample(2000000, accel = tilted)), 0.0001f)
|
||||
}
|
||||
|
||||
@Test fun reconnectGapDoesNotIntegrateSecondsOfGyro() {
|
||||
val filter = XrealOrientationFilter()
|
||||
val first = filter.update(sample(1000000))!!
|
||||
assertArrayEquals(first, filter.update(sample(5000000000, Vec3(2f, 2f, 2f))), 0.0001f)
|
||||
}
|
||||
|
||||
@Test fun invalidStartupGravityWaitsForAStationarySample() {
|
||||
val filter = XrealOrientationFilter()
|
||||
assertNull(filter.update(sample(1000000, accel = Vec3(0f, 0f, 0f))))
|
||||
assertNotNull(filter.update(sample(2000000)))
|
||||
}
|
||||
|
||||
@Test fun observedNegativeYawTurnsLeftThirtyDegreesAtOneKhzThroughSharedPosePath() {
|
||||
val filter = XrealOrientationFilter()
|
||||
val first = matrix(filter.update(sample(1000000))!!)
|
||||
var last = FloatArray(4)
|
||||
repeat(1000) { last = filter.update(sample((it + 2) * 1000000L, Vec3(0f, -(Math.PI / 6).toFloat(), 0f)))!! }
|
||||
val view = HeadPose.relative(HeadPose.recenter(first), matrix(last))
|
||||
assertArrayEquals(floatArrayOf(-0.5f, 0f, -0.8660254f), HeadPose.apply(view, floatArrayOf(0f, 0f, -1f)), 0.001f)
|
||||
}
|
||||
|
||||
@Test fun observedPositivePitchAndRollLookUpAndTiltRightWithoutCrossingAxes() {
|
||||
for (roll in listOf(false, true)) {
|
||||
val filter = XrealOrientationFilter()
|
||||
val first = matrix(filter.update(sample(1000000))!!)
|
||||
var last = FloatArray(4)
|
||||
repeat(1000) {
|
||||
val angle = (it + 1) * Math.PI / 6000
|
||||
val accel = if (roll) Vec3(-9.81f * sin(angle).toFloat(), -9.81f * cos(angle).toFloat(), 0f)
|
||||
else Vec3(0f, -9.81f * cos(angle).toFloat(), 9.81f * sin(angle).toFloat())
|
||||
val rate = (Math.PI / 6).toFloat()
|
||||
last = filter.update(sample((it + 2) * 1000000L,
|
||||
if (roll) Vec3(0f, 0f, rate) else Vec3(rate, 0f, 0f), accel))!!
|
||||
}
|
||||
val view = HeadPose.relative(HeadPose.recenter(first), matrix(last))
|
||||
val actual = HeadPose.apply(view, if (roll) floatArrayOf(0f, 1f, 0f) else floatArrayOf(0f, 0f, -1f))
|
||||
assertArrayEquals(if (roll) floatArrayOf(0.5f, 0.8660254f, 0f) else floatArrayOf(0f, 0.5f, -0.8660254f), actual, 0.002f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun capturedNeutralGravityStartsUprightBeforeRecenter() {
|
||||
val pose = XrealOrientationFilter().update(sample(1000000,
|
||||
accel = Vec3(-0.0909f, -9.6529f, -1.0731f)))!!
|
||||
// An upright start must not contain the previous approximately 180-degree
|
||||
// pitch correction. Check before recenter can hide a mounting error.
|
||||
assertTrue(kotlin.math.abs(pose[3]) > 0.99f)
|
||||
val screen = matrix(pose)
|
||||
assertTrue(screen[7] > 0.99f) // Screen up points toward shared world +Z.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.droiddeck.launcher.glasses
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
|
||||
class XrealProtocolTest {
|
||||
@Test fun fragmentedAndCoalescedFramesRetainBoundariesAndDeviceUnits() {
|
||||
val bytes = frame(XrealProtocol.REPORT, sensor()) + frame(0x272e, byteArrayOf(1, 2))
|
||||
val input = object : ByteArrayInputStream(bytes) {
|
||||
override fun read(b: ByteArray, off: Int, len: Int) = super.read(b, off, minOf(3, len))
|
||||
}
|
||||
val sample = XrealProtocol.sample(XrealProtocol.read(input))!!
|
||||
assertEquals(123456789L, sample.timeNs)
|
||||
assertEquals(0.5f, sample.gyro.y, 0f)
|
||||
assertEquals(9.81f, sample.accel.y, 0f)
|
||||
assertArrayEquals(byteArrayOf(1, 2), XrealProtocol.read(input).body)
|
||||
}
|
||||
|
||||
@Test fun invalidLengthsAndTruncationFailWithoutUnboundedAllocation() {
|
||||
for (size in listOf(-1, Int.MAX_VALUE, XrealProtocol.MAX_BODY + 1)) {
|
||||
val bytes = ByteBuffer.allocate(6).putShort(0x2836).putInt(size).array()
|
||||
assertThrows(IOException::class.java) { XrealProtocol.read(bytes.inputStream()) }
|
||||
}
|
||||
assertThrows(IOException::class.java) { XrealProtocol.read(byteArrayOf(0x28).inputStream()) }
|
||||
assertThrows(IOException::class.java) { XrealProtocol.read(frame(0x2836, sensor()).dropLast(1).toByteArray().inputStream()) }
|
||||
}
|
||||
|
||||
@Test fun onlyCompleteFiniteImuReportsAreAccepted() {
|
||||
assertNull(XrealProtocol.sample(XrealProtocol.Frame(0x2836, ByteArray(84))))
|
||||
assertNull(XrealProtocol.sample(XrealProtocol.Frame(0x272e, sensor())))
|
||||
for ((offset, value) in listOf(24 to 4, 28 to Float.NaN.toRawBits(), 40 to Float.POSITIVE_INFINITY.toRawBits())) {
|
||||
val bytes = sensor()
|
||||
ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).putInt(offset, value)
|
||||
assertNull(XrealProtocol.sample(XrealProtocol.Frame(0x2836, bytes)))
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun commandAndTransactionMustBothMatchBeforeParsingAcknowledgement() {
|
||||
val body = ByteBuffer.allocate(6).putInt(7).put(byteArrayOf(0x22, 0)).array()
|
||||
val reply = XrealProtocol.Frame(0x271c, body)
|
||||
assertNull(XrealProtocol.response(reply, 0x271c, 8))
|
||||
assertNull(XrealProtocol.response(reply, 0x275f, 7))
|
||||
assertTrue(XrealProtocol.acknowledged(XrealProtocol.response(reply, 0x271c, 7)!!))
|
||||
assertFalse(XrealProtocol.acknowledged(byteArrayOf(0x22, 1, 0)))
|
||||
assertEquals(5, XrealProtocol.numeric(byteArrayOf(0x22, 2, 0x10, 5)))
|
||||
assertNull(XrealProtocol.numeric(byteArrayOf(0x22, 2, 8, 5)))
|
||||
}
|
||||
|
||||
@Test fun getAndSetCommandsUseBigEndianHeaderAndProtobufBody() {
|
||||
assertArrayEquals(byteArrayOf(0x27, 0x5e, 0, 0, 0, 6, 0x80.toByte(), 0, 0, 1, 0x18, 0),
|
||||
XrealProtocol.request(XrealProtocol.GET_DISPLAY, 1))
|
||||
assertArrayEquals(byteArrayOf(0x27, 0x1c, 0, 0, 0, 8, 0x80.toByte(), 0, 0, 2, 0x1a, 2, 8, 9),
|
||||
XrealProtocol.request(XrealProtocol.SET_BRIGHTNESS, 2, 9))
|
||||
}
|
||||
|
||||
@Test fun controlIgnoresUnsolicitedAndStaleRepliesAndRejectsFailure() {
|
||||
val output = ByteArrayOutputStream()
|
||||
val replies = frame(0x272e, ByteArray(8)) + response(0x275e, 99, byteArrayOf(0x22, 2, 0x10, 4)) +
|
||||
response(0x275e, 1, byteArrayOf(0x22, 2, 0x10, 2)) + response(0x271c, 2, byteArrayOf(0x22, 1, 1))
|
||||
val control = XrealControl(channels(replies.inputStream(), output))
|
||||
assertEquals(2, control.display())
|
||||
assertThrows(IOException::class.java) { control.set(0x271c, 5) }
|
||||
assertEquals(26, output.size())
|
||||
}
|
||||
|
||||
@Test fun unrelatedResponsesCannotKeepControlWaitingForever() {
|
||||
val replies = ByteArrayOutputStream().apply { repeat(32) { write(frame(0x272e, ByteArray(8))) } }
|
||||
val control = XrealControl(channels(replies.toByteArray().inputStream(), ByteArrayOutputStream()))
|
||||
assertThrows(IOException::class.java) { control.display() }
|
||||
}
|
||||
|
||||
@Test fun captured1sNativeDisplayResponsePreserves1200LineMode() {
|
||||
val reply = byteArrayOf(0x22, 2, 0x10, 0x1d)
|
||||
val control = XrealControl(channels(response(0x275e, 1, reply).inputStream(), ByteArrayOutputStream()))
|
||||
assertEquals(29, control.display())
|
||||
assertEquals(29, XrealModel.mode(60, false))
|
||||
}
|
||||
|
||||
@Test fun stereoWaitsForEdidChangeWithoutSendingASecondModeCommandWhileBusy() {
|
||||
val output = ByteArrayOutputStream()
|
||||
val replies = response(0x2822, 1, byteArrayOf(0x22, 0)) +
|
||||
response(0x275e, 2, byteArrayOf(0x22, 2, 0x10, 29)) +
|
||||
response(0x275e, 3, byteArrayOf(0x22, 0)) +
|
||||
response(0x275e, 4, byteArrayOf(0x22, 2, 0x10, 5))
|
||||
assertEquals(5, XrealControl(channels(replies.inputStream(), output), 0).changeDisplay(5))
|
||||
val requests = output.toByteArray().inputStream()
|
||||
assertEquals(0x2822, XrealProtocol.read(requests).command)
|
||||
repeat(3) { assertEquals(0x275e, XrealProtocol.read(requests).command) }
|
||||
assertEquals(0, requests.available())
|
||||
// Actual 1S busy response remains an error, never a successful ack.
|
||||
assertFalse(XrealProtocol.acknowledged(byteArrayOf(0x22, 3, 8, 0x89.toByte(), 0x27)))
|
||||
}
|
||||
|
||||
@Test fun stereoCanReturnToNative1200LinesAndAnUnfinishedTransitionIsBounded() {
|
||||
val replies = response(0x2822, 1, byteArrayOf(0x22, 0)) +
|
||||
response(0x275e, 2, byteArrayOf(0x22, 0)) +
|
||||
response(0x275e, 3, byteArrayOf(0x22, 2, 0x10, 29))
|
||||
assertEquals(29, XrealControl(channels(replies.inputStream(), ByteArrayOutputStream()), 0).changeDisplay(29))
|
||||
val stalled = ByteArrayOutputStream().apply {
|
||||
write(response(0x2822, 1, byteArrayOf(0x22, 0)))
|
||||
repeat(40) { write(response(0x275e, it + 2, byteArrayOf(0x22, 0))) }
|
||||
}
|
||||
val control = XrealControl(channels(stalled.toByteArray().inputStream(), ByteArrayOutputStream()), 0)
|
||||
assertThrows(IOException::class.java) { control.changeDisplay(5) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun sensor(time: Long = 123456789L): ByteArray = ByteBuffer.allocate(128).order(ByteOrder.LITTLE_ENDIAN)
|
||||
.putLong(8, time).putInt(24, 11).putFloat(32, 0.5f).putFloat(44, 9.81f).array()
|
||||
fun frame(command: Int, body: ByteArray): ByteArray = ByteBuffer.allocate(6 + body.size)
|
||||
.putShort(command.toShort()).putInt(body.size).put(body).array()
|
||||
fun response(command: Int, transaction: Int, body: ByteArray) = frame(command,
|
||||
ByteBuffer.allocate(4 + body.size).putInt(transaction).put(body).array())
|
||||
internal fun channels(input: InputStream, output: ByteArrayOutputStream) = object : XrealChannels {
|
||||
override val controlInput = input
|
||||
override val controlOutput = output
|
||||
override val reports = ByteArrayInputStream(ByteArray(0))
|
||||
override fun close() = Unit
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,4 +153,37 @@ class CardboardTest {
|
||||
assertVec(HeadPose.apply(r, floatArrayOf(0f, 1f, 0f)), HeadPose.apply(s270, floatArrayOf(1f, 0f, 0f)))
|
||||
assertVec(r, HeadPose.toScreen(HeadPose.toScreen(HeadPose.toScreen(HeadPose.toScreen(r, 1), 1), 1), 1))
|
||||
}
|
||||
|
||||
@Test fun glassesLevelAndAheadIsStraightAhead() {
|
||||
val s2w = HeadPose.fromGlasses(0f, 0f, 0f, 1f)
|
||||
assertVec(floatArrayOf(1f, 0f, 0f), HeadPose.apply(s2w, floatArrayOf(0f, 0f, -1f)))
|
||||
assertVec(floatArrayOf(0f, 0f, 1f), HeadPose.apply(s2w, floatArrayOf(0f, 1f, 0f)))
|
||||
assertVec(HeadPose.IDENTITY, HeadPose.relative(HeadPose.recenter(s2w), s2w))
|
||||
}
|
||||
|
||||
@Test fun glassesTurnedLeftLookLeft() {
|
||||
val ref = HeadPose.recenter(HeadPose.fromGlasses(0f, 0f, 0f, 1f))
|
||||
val q = quat(0f, 0f, 1f, 30.0)
|
||||
val view = HeadPose.relative(ref, HeadPose.fromGlasses(q[0], q[1], q[2], q[3]))
|
||||
val s = sin(PI / 6).toFloat(); val c = cos(PI / 6).toFloat()
|
||||
assertVec(floatArrayOf(-s, 0f, -c), HeadPose.apply(view, floatArrayOf(0f, 0f, -1f)))
|
||||
assertVec(floatArrayOf(0f, 1f, 0f), HeadPose.apply(view, floatArrayOf(0f, 1f, 0f)))
|
||||
}
|
||||
|
||||
@Test fun glassesRecenterKeepsTheHorizon() {
|
||||
val q = quat(0f, 0f, 1f, 75.0)
|
||||
val ref = HeadPose.recenter(HeadPose.fromGlasses(q[0], q[1], q[2], q[3]))
|
||||
assertVec(HeadPose.IDENTITY, HeadPose.relative(ref, HeadPose.fromGlasses(q[0], q[1], q[2], q[3])))
|
||||
val tilt = quat(0f, 1f, 0f, 20.0)
|
||||
val both = HeadPose.multiply(HeadPose.fromQuaternion(q[0], q[1], q[2], q[3]), HeadPose.fromQuaternion(tilt[0], tilt[1], tilt[2], tilt[3]))
|
||||
val view = HeadPose.multiply(HeadPose.transpose(ref.basis), HeadPose.multiply(both, floatArrayOf(0f, 0f, -1f, -1f, 0f, 0f, 0f, 1f, 0f)))
|
||||
val ahead = HeadPose.apply(view, floatArrayOf(0f, 0f, -1f))
|
||||
assertEquals(0f, ahead[0], eps)
|
||||
assertEquals(-sin(PI / 9).toFloat(), ahead[1], eps)
|
||||
}
|
||||
|
||||
@Test fun monoIsNotPartOfTheSavedSettings() {
|
||||
val c = Cardboard(enabled = true, mono = true)
|
||||
assertEquals(false, Cardboard.decode(c.encode()).mono)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.droiddeck.launcher.gpu
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import java.io.File
|
||||
|
||||
class SteamVrDeviceLossLogTest {
|
||||
@get:Rule val temp = TemporaryFolder()
|
||||
|
||||
@Test fun oldErrorsAreIgnoredButFreshSplitLinesAreDetected() {
|
||||
val file = temp.newFile().apply { writeText("old vkerror=-4\n") }
|
||||
val log = SteamVrDeviceLossLog(file)
|
||||
assertFalse(log.poll())
|
||||
file.appendText("WaitForAcquire timed out\nFailed GetDeltas(Compositor): vkerror=")
|
||||
assertFalse(log.poll())
|
||||
file.appendText("-4\n")
|
||||
assertTrue(log.poll())
|
||||
assertTrue(log.poll()) // Keep pending until the activity is foreground.
|
||||
}
|
||||
|
||||
@Test fun onlyExplicitDeviceLossTriggersRecovery() {
|
||||
assertTrue(SteamVrDeviceLossLog.isDeviceLoss("Failed GetDeltas: vkerror=-4\n"))
|
||||
assertTrue(SteamVrDeviceLossLog.isDeviceLoss("Submit returned VK_ERROR_DEVICE_LOST\n"))
|
||||
for (text in listOf("vkerror=-40", "vkerror=-1", "headset taken off", "WaitForAcquire timed out",
|
||||
"Unexpected failure when checking for timeline signal", "Failed to submit to queue")) {
|
||||
assertFalse(text, SteamVrDeviceLossLog.isDeviceLoss(text))
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun handlesMissingLogCreationAndTruncation() {
|
||||
val file = File(temp.root, "vrcompositor.txt")
|
||||
val log = SteamVrDeviceLossLog(file)
|
||||
assertFalse(log.poll())
|
||||
file.writeText("healthy line\n".repeat(100))
|
||||
assertFalse(log.poll())
|
||||
file.writeText("vkerror=-4\n")
|
||||
assertTrue(log.poll())
|
||||
}
|
||||
|
||||
@Test fun detectsReplacementEvenWhenNewFileIsLargerThanTheOldOffset() {
|
||||
val file = temp.newFile().apply { writeText("old\n".repeat(50)) }
|
||||
val log = SteamVrDeviceLossLog(file)
|
||||
assertTrue(file.renameTo(File(temp.root, "previous.txt")))
|
||||
file.writeText("VK_ERROR_DEVICE_LOST\n" + "new\n".repeat(100))
|
||||
assertTrue(log.poll())
|
||||
}
|
||||
|
||||
@Test fun scansBoundedTailOfRapidlyGrowingFaultLogs() {
|
||||
val file = temp.newFile()
|
||||
val log = SteamVrDeviceLossLog(file)
|
||||
file.writeText("ordinary line\n".repeat(100_000) + "vkerror=-4\n")
|
||||
assertTrue(log.poll())
|
||||
}
|
||||
|
||||
@Test fun catchesTruncateAndRegrowBetweenPollsWithoutAnInodeChange() {
|
||||
val file = temp.newFile().apply { writeText("previous run\n".repeat(100)) }
|
||||
val log = SteamVrDeviceLossLog(file)
|
||||
file.writeText("new run vkerror=-4\n" + "new output\n".repeat(200))
|
||||
assertTrue(log.poll())
|
||||
}
|
||||
|
||||
@Test fun cooldownSurvivesRepeatedFailuresAndClockRollback() {
|
||||
val now = 1_000_000L
|
||||
assertTrue(SteamVrRecoveryPolicy.canRestartAutomatically(0, now))
|
||||
assertFalse(SteamVrRecoveryPolicy.canRestartAutomatically(now, now))
|
||||
assertFalse(SteamVrRecoveryPolicy.canRestartAutomatically(now, now - 1))
|
||||
assertFalse(SteamVrRecoveryPolicy.canRestartAutomatically(now, now + 599_999))
|
||||
assertTrue(SteamVrRecoveryPolicy.canRestartAutomatically(now, now + 600_000))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.droiddeck.launcher.gpu
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class SteamVrLaunchTest {
|
||||
@Test fun manualRequestKeepsPollingWithoutGlassesControlOrCardboard() {
|
||||
for (status in listOf(null, SteamVr.Status.READY, SteamVr.Status.UNAVAILABLE, SteamVr.Status.RUNNING)) {
|
||||
assertTrue(SteamVr.shouldPoll(status, false, false, false, true))
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun observedActivityIsPolledUntilItStopsEvenWithoutARequest() {
|
||||
assertTrue(SteamVr.shouldPoll(SteamVr.Status.RUNNING, false, false, false, false))
|
||||
assertTrue(SteamVr.shouldPoll(SteamVr.Status.INSTALLING, false, false, false, false))
|
||||
assertFalse(SteamVr.shouldPoll(SteamVr.Status.READY, false, false, false, false))
|
||||
assertFalse(SteamVr.shouldPoll(null, false, false, false, false))
|
||||
}
|
||||
|
||||
@Test fun explicitPlayRequestStartsWhenRuntimeIsReadyWithoutWaitingForGlassesControlOrOutput() {
|
||||
assertEquals("start", action(SteamVr.Status.READY))
|
||||
}
|
||||
|
||||
@Test fun pendingRequestWaitsForGuestAndLoadingThenStarts() {
|
||||
assertNull(action(SteamVr.Status.UNAVAILABLE))
|
||||
assertNull(action(SteamVr.Status.READY, loading = true))
|
||||
assertEquals("start", action(SteamVr.Status.READY))
|
||||
}
|
||||
|
||||
@Test fun missingRuntimeIsInstalledOnceAndThenStarted() {
|
||||
assertEquals("install", action(SteamVr.Status.NOT_INSTALLED))
|
||||
assertNull(action(SteamVr.Status.NOT_INSTALLED, installTried = true))
|
||||
assertNull(action(SteamVr.Status.INSTALLING, installTried = true))
|
||||
assertEquals("start", action(SteamVr.Status.READY, installTried = true))
|
||||
}
|
||||
|
||||
@Test fun runningRuntimeOrAttemptedStartDoesNotLaunchAnotherInstance() {
|
||||
assertNull(action(SteamVr.Status.RUNNING))
|
||||
assertNull(action(SteamVr.Status.READY, startTried = true))
|
||||
}
|
||||
|
||||
@Test fun ordinarySessionsAndCancelledRequestsDoNotAutostart() {
|
||||
assertNull(SteamVr.autoAction(SteamVr.Status.READY, false, false, true, false, false))
|
||||
assertNull(SteamVr.autoAction(SteamVr.Status.READY, true, false, false, false, false))
|
||||
}
|
||||
|
||||
private fun action(status: SteamVr.Status, loading: Boolean = false,
|
||||
startTried: Boolean = false, installTried: Boolean = false) =
|
||||
SteamVr.autoAction(status, true, loading, true, startTried, installTried)
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.droiddeck.launcher.gpu
|
||||
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.File
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
class SteamVrRecoverySettingsTest {
|
||||
@get:Rule val temp = TemporaryFolder()
|
||||
|
||||
@Test fun recoveryUnblocksOnlyOurHeadsetAndKeepsUserSettings() {
|
||||
val file = temp.newFile().apply { writeText("""{
|
||||
"driver_droiddeck":{"blocked_by_safe_mode":true,"other":42},
|
||||
"driver_other":{"blocked_by_safe_mode":true},
|
||||
"steamvr":{"enableSafeMode":true,"environmentMode":0},
|
||||
"GpuSpeed":{"gpuSpeed0":450}
|
||||
}""") }
|
||||
assertTrue(SteamVrRecoverySettings.unblockHeadset(file))
|
||||
val after = JSONObject(file.readText())
|
||||
assertFalse(after.getJSONObject("driver_droiddeck").getBoolean("blocked_by_safe_mode"))
|
||||
assertEquals(42, after.getJSONObject("driver_droiddeck").getInt("other"))
|
||||
assertTrue(after.getJSONObject("driver_other").getBoolean("blocked_by_safe_mode"))
|
||||
assertTrue(after.getJSONObject("steamvr").getBoolean("enableSafeMode"))
|
||||
assertEquals(0, after.getJSONObject("steamvr").getInt("environmentMode"))
|
||||
assertEquals(450, after.getJSONObject("GpuSpeed").getInt("gpuSpeed0"))
|
||||
val unchanged = file.readText()
|
||||
assertFalse(SteamVrRecoverySettings.unblockHeadset(file))
|
||||
assertEquals(unchanged, file.readText())
|
||||
}
|
||||
|
||||
@Test fun missingOrUnblockedConfigIsNotRewritten() {
|
||||
assertFalse(SteamVrRecoverySettings.unblockHeadset(File(temp.root, "missing")))
|
||||
for (text in listOf("{}", "{\"driver_droiddeck\":{\"blocked_by_safe_mode\":false}}")) {
|
||||
val file = temp.newFile().apply { writeText(text) }
|
||||
assertFalse(SteamVrRecoverySettings.unblockHeadset(file))
|
||||
assertEquals(text, file.readText())
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun malformedConfigIsPreservedAndRecoveryFailsInsteadOfReplacingIt() {
|
||||
val file = temp.newFile().apply { writeText("broken JSON") }
|
||||
assertTrue(runCatching { SteamVrRecoverySettings.unblockHeadset(file) }.isFailure)
|
||||
assertEquals("broken JSON", file.readText())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.droiddeck.launcher.gpu
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.os.Looper
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.Robolectric
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
import org.robolectric.shadows.ShadowAlertDialog
|
||||
import java.time.Duration
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class SteamVrRecoveryTest {
|
||||
private lateinit var activity: Activity
|
||||
|
||||
@Before fun setup() {
|
||||
activity = Robolectric.buildActivity(Activity::class.java).setup().get()
|
||||
activity.getSharedPreferences(SteamVrRecovery.PREFS, Context.MODE_PRIVATE).edit().clear().commit()
|
||||
}
|
||||
|
||||
@Test fun countdownLaunchesPrivateRestartActivityAndPersistsLoopGuard() {
|
||||
val recovery = SteamVrRecovery(activity) { true }
|
||||
recovery.detected()
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(7))
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(1))
|
||||
assertEquals(SteamVrRestartActivity::class.java.name, shadowOf(activity).nextStartedActivity.component?.className)
|
||||
assertTrue(activity.getSharedPreferences(SteamVrRecovery.PREFS, Context.MODE_PRIVATE)
|
||||
.getLong(SteamVrRecovery.LAST_ATTEMPT, 0) > 0)
|
||||
recovery.close()
|
||||
}
|
||||
|
||||
@Test fun cancellationPreventsRestartAndRepeatedPrompts() {
|
||||
val recovery = SteamVrRecovery(activity) { true }
|
||||
recovery.detected()
|
||||
ShadowAlertDialog.getLatestAlertDialog().getButton(android.app.AlertDialog.BUTTON_NEGATIVE).performClick()
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(20))
|
||||
recovery.detected()
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
assertFalse(ShadowAlertDialog.getLatestAlertDialog().isShowing)
|
||||
}
|
||||
|
||||
@Test fun recentRecoveryDoesNotAutomaticallyRestartAgain() {
|
||||
activity.getSharedPreferences(SteamVrRecovery.PREFS, Context.MODE_PRIVATE).edit()
|
||||
.putLong(SteamVrRecovery.LAST_ATTEMPT, System.currentTimeMillis()).commit()
|
||||
val recovery = SteamVrRecovery(activity) { true }
|
||||
recovery.detected()
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(20))
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
assertTrue(ShadowAlertDialog.getLatestAlertDialog().isShowing)
|
||||
val outside = android.view.MotionEvent.obtain(0, 0, android.view.MotionEvent.ACTION_OUTSIDE, -10f, -10f, 0)
|
||||
try {
|
||||
assertFalse(ShadowAlertDialog.getLatestAlertDialog().onTouchEvent(outside))
|
||||
assertTrue(ShadowAlertDialog.getLatestAlertDialog().isShowing)
|
||||
} finally { outside.recycle() }
|
||||
recovery.close()
|
||||
}
|
||||
|
||||
@Test fun backgroundingCancelsTheCountdownUntilTheSessionReturns() {
|
||||
var foreground = true
|
||||
val recovery = SteamVrRecovery(activity) { foreground }
|
||||
recovery.detected()
|
||||
foreground = false
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(20))
|
||||
assertNull(shadowOf(activity).nextStartedActivity)
|
||||
assertFalse(ShadowAlertDialog.getLatestAlertDialog().isShowing)
|
||||
recovery.detected()
|
||||
assertFalse(ShadowAlertDialog.getLatestAlertDialog().isShowing)
|
||||
foreground = true
|
||||
recovery.detected()
|
||||
assertTrue(ShadowAlertDialog.getLatestAlertDialog().isShowing)
|
||||
shadowOf(Looper.getMainLooper()).idleFor(Duration.ofSeconds(8))
|
||||
assertEquals(SteamVrRestartActivity::class.java.name, shadowOf(activity).nextStartedActivity.component?.className)
|
||||
recovery.close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.droiddeck.launcher.gpu
|
||||
|
||||
import com.droiddeck.launcher.gpu.SteamVrRecoveryPolicy.Shutdown
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE)
|
||||
class SteamVrShutdownTest {
|
||||
private var clock = 0L
|
||||
|
||||
private fun session(phase: String, logs: Boolean = true, collected: Boolean = false) =
|
||||
JSONObject().put("phase", phase).put("artifactsAvailable", logs).put("artifactsComplete", collected)
|
||||
|
||||
private fun await(state: (Long) -> JSONObject?) =
|
||||
SteamVrRecoveryPolicy.awaitShutdown({ state(clock) }, { clock }, { clock += it })
|
||||
|
||||
@Test fun waitsForTheLogsOnceTheSessionHasStopped() {
|
||||
val result = await { t ->
|
||||
when {
|
||||
t < 4_000 -> session("STOPPING")
|
||||
t < 24_000 -> session("IDLE")
|
||||
else -> session("IDLE", collected = true)
|
||||
}
|
||||
}
|
||||
assertEquals(Shutdown.COMPLETE, result)
|
||||
assertEquals(24_000L, clock)
|
||||
}
|
||||
|
||||
@Test fun aStuckTeardownGivesUpAfterTheStopDeadline() {
|
||||
assertEquals(Shutdown.STUCK, await { session("STOPPING") })
|
||||
assertEquals(SteamVrRecoveryPolicy.STOP_MS, clock)
|
||||
}
|
||||
|
||||
@Test fun slowLogCollectionIsBounded() {
|
||||
assertEquals(Shutdown.LOGS_PENDING, await { if (it < 2_000) null else session("FAILED") })
|
||||
assertEquals(2_000L + SteamVrRecoveryPolicy.LOGS_MS, clock)
|
||||
}
|
||||
|
||||
@Test fun aSessionWithoutLogsNeedsNoWait() {
|
||||
assertEquals(Shutdown.COMPLETE, await { session("IDLE", logs = false) })
|
||||
assertEquals(0L, clock)
|
||||
}
|
||||
|
||||
@Test fun unreadableStateIsRetriedUntilTheDeadline() {
|
||||
var reads = 0
|
||||
assertEquals(Shutdown.STUCK, await { reads++; null })
|
||||
assertTrue(reads > 1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package com.droiddeck.launcher.handtracking
|
||||
|
||||
import com.droiddeck.launcher.glasses.Vec3
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class BodyPoseTest {
|
||||
private val k = Intrinsics(800f, 800f, 480f, 360f, 960, 720)
|
||||
private val body = mapOf(
|
||||
BodyPose.NOSE to Vec3(0f, 0.62f, 0.10f),
|
||||
BodyPose.LEFT_EYE to Vec3(0.035f, 0.66f, 0.08f), BodyPose.RIGHT_EYE to Vec3(-0.035f, 0.66f, 0.08f),
|
||||
BodyPose.LEFT_EAR to Vec3(0.075f, 0.64f, 0f), BodyPose.RIGHT_EAR to Vec3(-0.075f, 0.64f, 0f),
|
||||
BodyPose.LEFT_SHOULDER to Vec3(0.19f, 0.48f, 0f), BodyPose.RIGHT_SHOULDER to Vec3(-0.19f, 0.48f, 0f),
|
||||
BodyPose.LEFT_ELBOW to Vec3(0.3f, 0.3f, 0.1f), BodyPose.RIGHT_ELBOW to Vec3(-0.28f, 0.25f, 0.05f),
|
||||
BodyPose.LEFT_WRIST to Vec3(0.35f, 0.45f, 0.35f), BodyPose.RIGHT_WRIST to Vec3(-0.25f, 0.6f, 0.3f),
|
||||
BodyPose.LEFT_PINKY to Vec3(0.37f, 0.47f, 0.42f), BodyPose.RIGHT_PINKY to Vec3(-0.27f, 0.63f, 0.37f),
|
||||
BodyPose.LEFT_INDEX to Vec3(0.34f, 0.5f, 0.43f), BodyPose.RIGHT_INDEX to Vec3(-0.24f, 0.66f, 0.37f),
|
||||
BodyPose.LEFT_HIP to Vec3(0.1f, 0f, 0f), BodyPose.RIGHT_HIP to Vec3(-0.1f, 0f, 0f),
|
||||
)
|
||||
private val hips = Vec3(0.1f, -0.3f, -2.2f)
|
||||
|
||||
@Test fun handsComeOutRelativeToTheEyesInALevelFrame() {
|
||||
val frame = solve(turn = 0.0, pitch = 0.0)!!
|
||||
assertTrue(frame.seen)
|
||||
assertEquals(2.2f, frame.distance, 1e-3f)
|
||||
for (side in 0..1) assertClose(expectedHand(side, 0.0), frame.hands[side]!!)
|
||||
assertEquals(PI, abs(frame.faceYaw!!.toDouble()), 1e-4)
|
||||
}
|
||||
|
||||
@Test fun aCameraPointingDownStillGivesLevelHands() {
|
||||
for (pitch in listOf(-0.25, 0.1, 0.15)) {
|
||||
val frame = solve(turn = 0.0, pitch = pitch)!!
|
||||
for (side in 0..1) assertClose(expectedHand(side, 0.0), frame.hands[side]!!)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun aTurnedPersonGivesTheirFaceHeading() {
|
||||
for (turn in listOf(-0.6, 0.4)) {
|
||||
val frame = solve(turn = turn, pitch = 0.1)!!
|
||||
assertEquals(wrap(PI + turn), wrap(frame.faceYaw!!.toDouble()), 1e-3)
|
||||
for (side in 0..1) assertClose(expectedHand(side, turn), frame.hands[side]!!)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun shoulderSpanIsAShareOfTheImageWidth() {
|
||||
val frame = solve(turn = 0.0, pitch = 0.0)!!
|
||||
assertEquals(800f * 0.38f / 2.2f / 960f, frame.shoulderSpan, 1e-3f)
|
||||
}
|
||||
|
||||
@Test fun anUnseenWristLeavesThatHandOut() {
|
||||
val frame = solve(turn = 0.0, pitch = 0.0, hidden = setOf(BodyPose.RIGHT_WRIST))!!
|
||||
assertNotNull(frame.hands[0])
|
||||
assertNull(frame.hands[1])
|
||||
}
|
||||
|
||||
@Test fun tooLittleOfThePersonOrAnUnlevelableCameraGivesNothing() {
|
||||
assertNull(solve(turn = 0.0, pitch = 0.0, hidden = body.keys - setOf(BodyPose.NOSE, BodyPose.LEFT_EYE, BodyPose.RIGHT_EYE)))
|
||||
assertNull(solve(turn = 0.0, pitch = PI / 2))
|
||||
}
|
||||
|
||||
@Test fun aHandModelFixIsPlacedByTheHandsOwnSize() {
|
||||
val palmAt = Vec3(0.15f, 0.1f, 0.6f)
|
||||
val shape = List(BodyPose.HAND_POINTS) { i -> WorldPoint(((i * 37) % 11 - 5) * 0.01f, ((i * 53) % 13 - 6) * 0.01f, ((i * 29) % 7 - 3) * 0.01f) }
|
||||
val points = FloatArray(BodyPose.HAND_POINTS * 2) { j ->
|
||||
val w = shape[j / 2]
|
||||
val x = palmAt.x + w.x
|
||||
val y = palmAt.y + w.y
|
||||
val z = palmAt.z + w.z
|
||||
if (j % 2 == 0) k.fx * x / z + k.cx else k.fy * y / z + k.cy
|
||||
}
|
||||
val fix = BodyPose.handFix(points, shape, k)!!
|
||||
val palm = intArrayOf(0, 5, 9, 13, 17)
|
||||
val expectedPalm = Vec3(palmAt.x + palm.map { shape[it].x }.average().toFloat(), palmAt.y + palm.map { shape[it].y }.average().toFloat(),
|
||||
palmAt.z + palm.map { shape[it].z }.average().toFloat())
|
||||
assertClose(expectedPalm, fix.palm)
|
||||
val frame = solve(turn = 0.0, pitch = 0.0, fixes = arrayOf(fix, null))!!
|
||||
val eyes = toCamera(hips + (body[BodyPose.LEFT_EYE]!! + body[BodyPose.RIGHT_EYE]!!) * 0.5f)
|
||||
val d = fix.palm - eyes
|
||||
assertClose(Vec3(d.x, -d.y, -d.z), frame.hands[0]!!)
|
||||
assertNull("no fix and no held depth leaves the hand out", frame.hands[1])
|
||||
assertTrue(!frame.estimated[0])
|
||||
val joints = frame.joints[0]!!
|
||||
val mean = Vec3(palm.map { shape[it].x }.average().toFloat(), palm.map { shape[it].y }.average().toFloat(),
|
||||
palm.map { shape[it].z }.average().toFloat())
|
||||
for (i in 0 until BodyPose.HAND_POINTS) {
|
||||
assertClose(Vec3(shape[i].x - mean.x, -(shape[i].y - mean.y), -(shape[i].z - mean.z)),
|
||||
Vec3(joints[i * 3], joints[i * 3 + 1], joints[i * 3 + 2]))
|
||||
}
|
||||
assertNull(frame.joints[1])
|
||||
}
|
||||
|
||||
@Test fun aHandTheModelMissedKeepsItsLastDepth() {
|
||||
val frame = solve(turn = 0.0, pitch = 0.0, fixes = arrayOfNulls(2), held = floatArrayOf(Float.NaN, 1.8f))!!
|
||||
assertNull(frame.hands[0])
|
||||
assertTrue(frame.estimated[1])
|
||||
assertNull("a guessed hand has no finger joints", frame.joints[1])
|
||||
val eyes = toCamera(hips + (body[BodyPose.LEFT_EYE]!! + body[BodyPose.RIGHT_EYE]!!) * 0.5f)
|
||||
assertEquals(-(1.8f - eyes.z), frame.hands[1]!!.z, 1e-3f)
|
||||
}
|
||||
|
||||
@Test fun jointsNearTheEdgeOfThePictureShowAsLittleRoom() {
|
||||
val frame = solve(turn = 0.0, pitch = 0.0)!!
|
||||
assertTrue(frame.edgeRoom > 0.05f && frame.edgeRoom < 0.5f)
|
||||
val near = solve(turn = 0.0, pitch = 0.0, shift = 0.75f)!!
|
||||
assertTrue("${near.edgeRoom}", near.edgeRoom < frame.edgeRoom)
|
||||
}
|
||||
|
||||
private fun solve(turn: Double, pitch: Double, hidden: Set<Int> = emptySet(), fixes: Array<HandFix?>? = null,
|
||||
held: FloatArray? = null, shift: Float = 0f): BodyFrame? {
|
||||
val image = ArrayList<Landmark>()
|
||||
val world = ArrayList<WorldPoint>()
|
||||
for (i in 0 until 33) {
|
||||
val rel = body[i]?.let { rotateY(it, turn) }
|
||||
if (rel == null) {
|
||||
image += Landmark(0.5f, 0.5f, 0f)
|
||||
world += WorldPoint(0f, 0f, 0f)
|
||||
continue
|
||||
}
|
||||
val camera = tilt(toCamera(hips + rel), pitch)
|
||||
image += Landmark((k.fx * (camera.x + shift) / camera.z + k.cx) / k.width, (k.fy * camera.y / camera.z + k.cy) / k.height,
|
||||
if (i in hidden) 0.1f else 0.95f)
|
||||
val w = tilt(toCamera(rel), pitch)
|
||||
world += WorldPoint(w.x, w.y, w.z)
|
||||
}
|
||||
val up = tilt(toCamera(Vec3(0f, 1f, 0f)), pitch)
|
||||
return BodyPose.solve(image, world, k, CameraLevel(up), fixes, held)
|
||||
}
|
||||
|
||||
private fun expectedHand(side: Int, turn: Double): Vec3 {
|
||||
val (wrist, pinky, index) = if (side == 0) Triple(BodyPose.LEFT_WRIST, BodyPose.LEFT_PINKY, BodyPose.LEFT_INDEX)
|
||||
else Triple(BodyPose.RIGHT_WRIST, BodyPose.RIGHT_PINKY, BodyPose.RIGHT_INDEX)
|
||||
val grip = (rotateY(body[wrist]!!, turn) + rotateY(body[pinky]!!, turn) + rotateY(body[index]!!, turn)) * (1f / 3f)
|
||||
val head = (rotateY(body[BodyPose.LEFT_EYE]!!, turn) + rotateY(body[BodyPose.RIGHT_EYE]!!, turn)) * 0.5f
|
||||
return grip - head
|
||||
}
|
||||
|
||||
private fun toCamera(v: Vec3) = Vec3(v.x, -v.y, -v.z)
|
||||
|
||||
private fun tilt(v: Vec3, pitch: Double): Vec3 {
|
||||
val c = cos(pitch).toFloat()
|
||||
val s = sin(pitch).toFloat()
|
||||
return Vec3(v.x, v.y * c - v.z * s, v.y * s + v.z * c)
|
||||
}
|
||||
|
||||
private fun rotateY(v: Vec3, angle: Double): Vec3 {
|
||||
val c = cos(angle).toFloat()
|
||||
val s = sin(angle).toFloat()
|
||||
return Vec3(c * v.x + s * v.z, v.y, -s * v.x + c * v.z)
|
||||
}
|
||||
|
||||
private fun wrap(a: Double): Double = Math.IEEEremainder(a, 2 * PI)
|
||||
|
||||
private fun assertClose(want: Vec3, got: Vec3) {
|
||||
assertEquals("x", want.x, got.x, 2e-4f)
|
||||
assertEquals("y", want.y, got.y, 2e-4f)
|
||||
assertEquals("z", want.z, got.z, 2e-4f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.droiddeck.launcher.handtracking
|
||||
|
||||
import com.droiddeck.launcher.glasses.Vec3
|
||||
import java.io.File
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.nio.file.Files
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class HandCameraShmTest {
|
||||
private lateinit var dir: File
|
||||
private lateinit var file: File
|
||||
private var fences = 0
|
||||
|
||||
@Before fun setUp() {
|
||||
dir = Files.createTempDirectory("handcam-shm").toFile()
|
||||
file = File(dir, "run/${HandCameraShm.FILE}")
|
||||
}
|
||||
|
||||
@After fun tearDown() { dir.deleteRecursively() }
|
||||
|
||||
@Test fun anOpenedFileCarriesTheHeaderAndNothingSeen() {
|
||||
assertNotNull(HandCameraShm.open(file) { fences++ })
|
||||
val bytes = read()
|
||||
assertEquals(HandCameraShm.SIZE, bytes.capacity())
|
||||
assertEquals(568, HandCameraShm.SIZE)
|
||||
assertEquals(0x4D434844, bytes.getInt(0))
|
||||
assertEquals(2, bytes.getInt(4))
|
||||
assertEquals(2, bytes.getInt(8))
|
||||
for (at in 12 until HandCameraShm.SIZE step 4) assertEquals("byte $at", 0, bytes.getInt(at))
|
||||
assertEquals(2, fences)
|
||||
}
|
||||
|
||||
@Test fun aFrameLandsWhereTheDriverReadsIt() {
|
||||
val shm = HandCameraShm.open(file) { fences++ }!!
|
||||
shm.write(123_456_789_012L, BodyFrame(arrayOf(Vec3(0.2f, -0.45f, -0.3f), null), 2.5f, 1.8f, 0.2f, true))
|
||||
val bytes = read()
|
||||
assertEquals(4, bytes.getInt(8))
|
||||
assertEquals(3, bytes.getInt(12))
|
||||
assertEquals(123_456_789_012L, bytes.getLong(16))
|
||||
assertEquals(1, bytes.getInt(24))
|
||||
assertEquals(2.5f, bytes.getFloat(28), 0f)
|
||||
assertEquals(1, bytes.getInt(32))
|
||||
assertEquals(0.2f, bytes.getFloat(36), 0f)
|
||||
assertEquals(-0.45f, bytes.getFloat(40), 0f)
|
||||
assertEquals(-0.3f, bytes.getFloat(44), 0f)
|
||||
assertEquals(0f, bytes.getFloat(48), 0f)
|
||||
assertEquals(0, bytes.getInt(300))
|
||||
assertEquals(0f, bytes.getFloat(304), 0f)
|
||||
}
|
||||
|
||||
@Test fun fingerJointsAndTheBareHandsFlagLandWhereTheDriverReadsThem() {
|
||||
val shm = HandCameraShm.open(file) { fences++ }!!
|
||||
val joints = FloatArray(BodyPose.HAND_POINTS * 3) { it * 0.001f - 0.03f }
|
||||
shm.write(7L, BodyFrame(arrayOf(null, Vec3(0.1f, -0.4f, -0.35f)), 0f, 2f, 0.2f, true, joints = arrayOf(null, joints)), bareHands = true)
|
||||
val bytes = read()
|
||||
assertEquals(1 or 2 or 4, bytes.getInt(12))
|
||||
assertEquals(0, bytes.getInt(32))
|
||||
assertEquals(1 or 4, bytes.getInt(300))
|
||||
assertEquals(0.1f, bytes.getFloat(304), 0f)
|
||||
for (i in joints.indices) assertEquals("joint float $i", joints[i], bytes.getFloat(316 + i * 4), 0f)
|
||||
assertEquals(HandCameraShm.SIZE, 316 + joints.size * 4)
|
||||
}
|
||||
|
||||
@Test fun aFrameWithoutAPersonOnlySaysTheCameraRuns() {
|
||||
val shm = HandCameraShm.open(file) { fences++ }!!
|
||||
shm.write(5L, BodyFrame(arrayOf(Vec3(1f, 1f, 1f), Vec3(1f, 1f, 1f)), null, 2f, 0.2f, true))
|
||||
shm.write(6L, null)
|
||||
val bytes = read()
|
||||
assertEquals(1, bytes.getInt(12))
|
||||
assertEquals(6L, bytes.getLong(16))
|
||||
assertEquals(2, bytes.getInt(24))
|
||||
assertEquals(0, bytes.getInt(32))
|
||||
assertEquals(0, bytes.getInt(300))
|
||||
}
|
||||
|
||||
@Test fun closingClearsTheFrame() {
|
||||
val shm = HandCameraShm.open(file) { fences++ }!!
|
||||
shm.write(5L, BodyFrame(arrayOf(Vec3(1f, 1f, 1f), null), 0f, 2f, 0.2f, true))
|
||||
shm.close()
|
||||
val bytes = read()
|
||||
assertEquals(0x4D434844, bytes.getInt(0))
|
||||
assertEquals(0, bytes.getInt(8) and 1)
|
||||
for (at in 12 until HandCameraShm.SIZE step 4) assertEquals("byte $at", 0, bytes.getInt(at))
|
||||
}
|
||||
|
||||
private fun read(): ByteBuffer = ByteBuffer.wrap(file.readBytes()).order(ByteOrder.LITTLE_ENDIAN)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.droiddeck.launcher.handtracking
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Application
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33], application = Application::class)
|
||||
class HandTrackingTest {
|
||||
private val both = HandTracking.Settings(sides = setOf(TrackingCameras.REAR, TrackingCameras.FRONT))
|
||||
|
||||
@Test fun theQuestionComesOnceWhenAHeadsetConnects() {
|
||||
assertTrue(HandTracking.shouldAsk(both, headset = true))
|
||||
assertFalse(HandTracking.shouldAsk(both, headset = false))
|
||||
assertFalse(HandTracking.shouldAsk(both.copy(asked = true), headset = true))
|
||||
assertFalse(HandTracking.shouldAsk(HandTracking.Settings(), headset = true))
|
||||
}
|
||||
|
||||
@Test fun answersAreRemembered() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
HandTracking.allow(context, TrackingCameras.FRONT)
|
||||
var s = HandTracking.load(context)
|
||||
assertTrue(s.asked)
|
||||
assertTrue(s.enabled)
|
||||
assertEquals(TrackingCameras.FRONT, s.side)
|
||||
assertFalse(s.permitted)
|
||||
HandTracking.allow(context, null)
|
||||
assertEquals(TrackingCameras.FRONT, HandTracking.load(context).side)
|
||||
HandTracking.decline(context)
|
||||
s = HandTracking.load(context)
|
||||
assertTrue(s.asked)
|
||||
assertFalse(s.enabled)
|
||||
HandTracking.setEnabled(context, true)
|
||||
HandTracking.setSide(context, TrackingCameras.REAR)
|
||||
s = HandTracking.load(context)
|
||||
assertTrue(s.enabled)
|
||||
assertEquals(TrackingCameras.REAR, s.side)
|
||||
}
|
||||
|
||||
@Test fun trackingNeedsThePermissionAndACamera() {
|
||||
val context = RuntimeEnvironment.getApplication()
|
||||
assertFalse(both.copy(enabled = true).ready)
|
||||
assertTrue(both.copy(enabled = true, permitted = true).ready)
|
||||
assertFalse(HandTracking.Settings(enabled = true, permitted = true).ready)
|
||||
shadowOf(context).grantPermissions(Manifest.permission.CAMERA)
|
||||
HandTracking.refreshPermission(context)
|
||||
assertTrue(HandTracking.settings.value.permitted)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.droiddeck.launcher.handtracking
|
||||
|
||||
import com.droiddeck.launcher.glasses.Vec3
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class PlacementAdviceTest {
|
||||
private val hand = Vec3(0.2f, -0.4f, -0.3f)
|
||||
private fun body(span: Float = 0.2f, left: Vec3? = hand, right: Vec3? = hand, seen: Boolean = true, room: Float = 0.2f) =
|
||||
BodyFrame(arrayOf(left, right), 3f, 2f, span, seen, edgeRoom = room)
|
||||
|
||||
@Test fun aFlatOrTiltedDeviceIsFixedFirst() {
|
||||
assertEquals(Advice.STAND_UP, PlacementAdvice.of(Placement.FACE_UP, body()))
|
||||
assertEquals(Advice.STAND_UP, PlacementAdvice.of(Placement.FACE_DOWN, body()))
|
||||
assertEquals(Advice.UPRIGHT, PlacementAdvice.of(Placement.POINTS_UP, null))
|
||||
assertEquals(Advice.UPRIGHT, PlacementAdvice.of(Placement.POINTS_DOWN, body()))
|
||||
}
|
||||
|
||||
@Test fun thenThePersonDistanceAndHands() {
|
||||
assertEquals(Advice.NOT_SEEN, PlacementAdvice.of(Placement.STANDING, null))
|
||||
assertEquals(Advice.NOT_SEEN, PlacementAdvice.of(Placement.STANDING, body(seen = false)))
|
||||
assertEquals(Advice.MOVE_BACK, PlacementAdvice.of(Placement.STANDING, body(span = 0.5f)))
|
||||
assertEquals("a person 0.65 m from a wide camera is too close", Advice.MOVE_BACK, PlacementAdvice.of(Placement.STANDING, body(span = 0.3f)))
|
||||
assertEquals("hands at the edge of the picture", Advice.MOVE_BACK, PlacementAdvice.of(Placement.STANDING, body(room = 0.02f)))
|
||||
assertEquals(Advice.MOVE_CLOSER, PlacementAdvice.of(Placement.STANDING, body(span = 0.05f)))
|
||||
assertEquals(Advice.MOVE_CLOSER, PlacementAdvice.of(Placement.STANDING, body(span = 0.1f)))
|
||||
assertEquals(Advice.SHOW_HANDS, PlacementAdvice.of(Placement.STANDING, body(right = null)))
|
||||
assertEquals(Advice.GOOD, PlacementAdvice.of(Placement.STANDING, body()))
|
||||
assertEquals(Advice.GOOD, PlacementAdvice.of(Placement.UNKNOWN, body()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.droiddeck.launcher.handtracking
|
||||
|
||||
import com.droiddeck.launcher.glasses.Vec3
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Test
|
||||
|
||||
class TrackingCamerasTest {
|
||||
private val wide = TrackingCamera("0", front = false, orientation = 90, focalPixels = 3000f, activeWidth = 4000, activeHeight = 3000, minZoom = 1f, realtime = true)
|
||||
private val ultra = TrackingCamera("2", front = false, orientation = 90, focalPixels = 1000f, activeWidth = 4000, activeHeight = 3000, minZoom = 1f, realtime = true)
|
||||
private val selfie = TrackingCamera("1", front = true, orientation = 270, focalPixels = 2000f, activeWidth = 4000, activeHeight = 3000, minZoom = 1f, realtime = true)
|
||||
|
||||
@Test fun theBackCameraIsPreferredAndTheWidestBackLensWins() {
|
||||
assertSame(ultra, TrackingCameras.pick(listOf(selfie, wide, ultra), null))
|
||||
assertSame(ultra, TrackingCameras.pick(listOf(selfie, wide, ultra), TrackingCameras.REAR))
|
||||
assertSame(selfie, TrackingCameras.pick(listOf(selfie, wide, ultra), TrackingCameras.FRONT))
|
||||
}
|
||||
|
||||
@Test fun aMissingSideFallsBackToWhatTheDeviceHas() {
|
||||
assertSame(wide, TrackingCameras.pick(listOf(wide), TrackingCameras.FRONT))
|
||||
assertSame(selfie, TrackingCameras.pick(listOf(selfie), null))
|
||||
assertNull(TrackingCameras.pick(emptyList(), null))
|
||||
assertEquals(setOf(TrackingCameras.REAR), TrackingCameras.sides(listOf(wide, ultra)))
|
||||
assertEquals(setOf(TrackingCameras.FRONT, TrackingCameras.REAR), TrackingCameras.sides(listOf(selfie, wide)))
|
||||
}
|
||||
|
||||
@Test fun theCameraIsHeldAtItsFastestSteadyFrameRate() {
|
||||
val front = listOf(10 to 10, 15 to 15, 10 to 24, 24 to 24, 10 to 30, 30 to 30)
|
||||
assertEquals(30 to 30, TrackingCameras.steadyFps(front))
|
||||
assertEquals(24 to 24, TrackingCameras.steadyFps(listOf(15 to 15, 10 to 30, 24 to 24, 60 to 60)))
|
||||
assertEquals(15 to 30, TrackingCameras.steadyFps(listOf(7 to 30, 15 to 30)))
|
||||
assertNull(TrackingCameras.steadyFps(emptyList()))
|
||||
}
|
||||
|
||||
@Test fun zoomingOutOnlyHappensWhenTheLensAllowsIt() {
|
||||
assertEquals(1f, wide.zoom(), 0f)
|
||||
val logical = TrackingCamera("0", false, 90, 3000f, 4000, 3000, minZoom = 0.6f, realtime = true)
|
||||
assertEquals(0.6f, logical.zoom(), 1e-6f)
|
||||
val roomy = TrackingCamera("0", false, 90, 3000f, 4000, 3000, minZoom = 0.3f, realtime = true)
|
||||
assertEquals((Math.tan(Math.toRadians(logical.fovDegrees / 2.0)) / Math.tan(Math.toRadians(50.0))).toFloat(), roomy.zoom(), 1e-5f)
|
||||
assertEquals(100f, Math.toDegrees(2 * Math.atan(Math.tan(Math.toRadians(roomy.fovDegrees / 2.0)) / roomy.zoom())).toFloat(), 1e-3f)
|
||||
}
|
||||
|
||||
@Test fun intrinsicsFollowTheBufferZoomAndTurns() {
|
||||
val k = wide.intrinsics(960, 720, 1f, 0)
|
||||
assertEquals(720f, k.fx, 1e-3f)
|
||||
assertEquals(480f, k.cx, 0f)
|
||||
assertEquals(360f, k.cy, 0f)
|
||||
val turned = wide.intrinsics(960, 720, 1f, 1)
|
||||
assertEquals(720, turned.width)
|
||||
assertEquals(960, turned.height)
|
||||
assertEquals(360f, turned.cx, 0f)
|
||||
assertEquals(360f, wide.intrinsics(960, 720, 0.5f, 0).fx, 1e-3f)
|
||||
}
|
||||
|
||||
@Test fun framesAreTurnedUprightFromGravity() {
|
||||
val portrait = Vec3(0f, 9.8f, 0f)
|
||||
assertEquals(1, CameraGeometry.uprightTurns(false, 90, portrait))
|
||||
assertEquals(3, CameraGeometry.uprightTurns(true, 270, portrait))
|
||||
assertEquals(0, CameraGeometry.uprightTurns(false, 0, portrait))
|
||||
val landscape = Vec3(9.8f, 0f, 0f)
|
||||
assertEquals(0, CameraGeometry.uprightTurns(false, 90, landscape))
|
||||
assertEquals(0, CameraGeometry.uprightTurns(true, 270, landscape))
|
||||
assertEquals(2, CameraGeometry.uprightTurns(false, 90, Vec3(-9.8f, 0f, 0f)))
|
||||
for ((front, orientation) in listOf(false to 90, true to 270, false to 270, true to 90)) {
|
||||
for (up in listOf(portrait, landscape, Vec3(0f, -9.8f, 0f), Vec3(-9.8f, 0f, 0f))) {
|
||||
val turns = CameraGeometry.uprightTurns(front, orientation, up)
|
||||
val seen = CameraGeometry.upInCamera(front, orientation, turns, up)
|
||||
assertEquals("front=$front $orientation° up=$up", -1f, seen.y, 1e-5f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun theCameraLooksOutOfItsOwnSide() {
|
||||
val back = CameraGeometry.upInCamera(false, 90, 1, Vec3(0f, 0f, -9.8f))
|
||||
assertEquals(1f, back.z, 1e-5f)
|
||||
val front = CameraGeometry.upInCamera(true, 270, 3, Vec3(0f, 0f, 9.8f))
|
||||
assertEquals(1f, front.z, 1e-5f)
|
||||
}
|
||||
|
||||
@Test fun placementTellsFlatFromStandingAndTilted() {
|
||||
assertEquals(Placement.FACE_UP, CameraGeometry.placement(false, Vec3(0f, 0.5f, 9.7f)))
|
||||
assertEquals(Placement.FACE_DOWN, CameraGeometry.placement(true, Vec3(0f, 0.5f, -9.7f)))
|
||||
assertEquals(Placement.STANDING, CameraGeometry.placement(false, Vec3(0f, 9.8f, 0f)))
|
||||
assertEquals(Placement.STANDING, CameraGeometry.placement(true, Vec3(9.2f, 0f, 3f)))
|
||||
val leanBack = Vec3(0f, Math.cos(Math.toRadians(60.0)).toFloat(), -Math.sin(Math.toRadians(60.0)).toFloat())
|
||||
assertEquals(Placement.POINTS_UP, CameraGeometry.placement(false, leanBack))
|
||||
assertEquals(Placement.POINTS_DOWN, CameraGeometry.placement(true, leanBack))
|
||||
assertEquals(Placement.UNKNOWN, CameraGeometry.placement(false, Vec3(0f, 0f, 0f)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import com.droiddeck.launcher.glasses.Quaternion
|
||||
import com.droiddeck.launcher.glasses.Vec3
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.roundToInt
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class JoyConMotionTest {
|
||||
private val bias = Vec3(0.3f, -0.25f, 0.2f)
|
||||
|
||||
@Test fun restingInSeveralPositionsLearnsTheAccelerometerOffset() {
|
||||
val calibration = JoyConAccelCalibration()
|
||||
assertFalse(calibration.ready)
|
||||
var learned = false
|
||||
for (up in directions) repeat(200) { learned = calibration.addResting(up * G + bias) || learned }
|
||||
assertTrue(learned)
|
||||
assertTrue(calibration.ready)
|
||||
assertVec(bias, calibration.bias, 0.01f)
|
||||
assertEquals(1f, calibration.scale, 1e-3f)
|
||||
assertVec(Vec3(0f, G, 0f), calibration.correct(Vec3(0f, G, 0f) + bias), 0.01f)
|
||||
}
|
||||
|
||||
@Test fun restingInOnePositionTeachesNothing() {
|
||||
val calibration = JoyConAccelCalibration()
|
||||
repeat(2000) { assertFalse(calibration.addResting(Vec3(0f, G, 0f) + bias)) }
|
||||
assertFalse(calibration.ready)
|
||||
assertVec(Vec3(1f, 2f, 3f), calibration.correct(Vec3(1f, 2f, 3f)))
|
||||
}
|
||||
|
||||
@Test fun aSavedOffsetComesBackAndAnImplausibleOneIsIgnored() {
|
||||
val first = JoyConAccelCalibration()
|
||||
for (up in directions) repeat(200) { first.addResting(up * G + bias) }
|
||||
val restored = JoyConAccelCalibration().apply { restore(first.saved()) }
|
||||
assertTrue(restored.ready)
|
||||
assertVec(first.bias, restored.bias, 0f)
|
||||
assertEquals(first.scale, restored.scale, 0f)
|
||||
for (bad in listOf(floatArrayOf(3f, 0f, 0f, 1f), floatArrayOf(0f, 0f, 0f, 1.5f), floatArrayOf(Float.NaN, 0f, 0f, 1f), floatArrayOf(0f, 0f, 1f))) {
|
||||
val c = JoyConAccelCalibration().apply { restore(bad) }
|
||||
assertFalse(bad.contentToString(), c.ready)
|
||||
assertNull(c.saved())
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun aLiftIsMeasuredAndTheHandStopsWhenItDoes() {
|
||||
val motion = JoyConMotion()
|
||||
still(motion, 1f)
|
||||
val lift = 0.25f
|
||||
val seconds = 0.5f
|
||||
val steps = (seconds / DT).roundToInt()
|
||||
for (i in 1..steps) {
|
||||
val s = (i - 0.5f) / steps
|
||||
val a = lift / (seconds * seconds) * (60 * s - 180 * s * s + 120 * s * s * s)
|
||||
motion.update(Quaternion.IDENTITY, Vec3(0f, G + a, 0f), Vec3(0f, 0f, 0f), true, DT)
|
||||
}
|
||||
assertEquals(lift, motion.displacement.y, 0.01f)
|
||||
assertTrue("moving up at ${motion.velocity}", motion.velocity.length() < 0.05f)
|
||||
still(motion, 2f)
|
||||
assertEquals(lift, motion.displacement.y, 0.015f)
|
||||
assertEquals(0f, motion.velocity.length(), 1e-3f)
|
||||
assertEquals(0f, motion.displacement.x, 1e-3f)
|
||||
assertEquals(0f, motion.displacement.z, 1e-3f)
|
||||
}
|
||||
|
||||
@Test fun theInertialVelocityKeepsAccelerationThatStillnessWouldDrop() {
|
||||
val motion = JoyConMotion()
|
||||
still(motion, 1f)
|
||||
repeat((2f / DT).roundToInt()) { motion.update(Quaternion.IDENTITY, Vec3(0f, G + 0.05f, 0f), Vec3(0f, 0f, 0f), true, DT) }
|
||||
assertEquals(0f, motion.velocity.y, 1e-3f)
|
||||
assertEquals(0.1f, motion.inertial.y, 0.005f)
|
||||
val before = motion.inertial.y
|
||||
val seconds = 0.25f
|
||||
repeat((seconds / DT).roundToInt()) { motion.update(Quaternion.IDENTITY, Vec3(2f, G, 0f), Vec3(0f, 0f, 0f), true, DT) }
|
||||
assertEquals(2f * seconds, motion.inertial.x, 0.01f)
|
||||
assertEquals(before, motion.inertial.y, 0.005f)
|
||||
}
|
||||
|
||||
@Test fun aSidewaysPushIsMeasuredInTheWorldFrame() {
|
||||
val motion = JoyConMotion()
|
||||
val turned = Quaternion.fromAxisAngle(Vec3(0f, 0f, 1f), (PI / 2).toFloat())
|
||||
val down = turned.conjugate().rotate(Vec3(0f, G, 0f))
|
||||
repeat((1f / DT).toInt()) { motion.update(turned, down, Vec3(0f, 0f, 0f), true, DT) }
|
||||
val steps = (0.4f / DT).roundToInt()
|
||||
for (i in 1..steps) {
|
||||
val s = (i - 0.5f) / steps
|
||||
val a = 0.3f / (0.4f * 0.4f) * (60 * s - 180 * s * s + 120 * s * s * s)
|
||||
motion.update(turned, down + turned.conjugate().rotate(Vec3(a, 0f, 0f)), Vec3(0f, 0f, 0f), true, DT)
|
||||
}
|
||||
assertEquals(0.3f, motion.displacement.x, 0.015f)
|
||||
assertEquals(0f, motion.displacement.y, 0.01f)
|
||||
}
|
||||
|
||||
@Test fun turningInPlaceDoesNotMoveTheHand() {
|
||||
val motion = JoyConMotion()
|
||||
still(motion, 1f)
|
||||
val rate = 2f
|
||||
var q = Quaternion.IDENTITY
|
||||
repeat((1f / DT).toInt()) {
|
||||
q = q.integrateBodyRate(Vec3(0f, 0f, rate), DT).normalized()
|
||||
motion.update(q, q.conjugate().rotate(Vec3(0f, G, 0f)), Vec3(0f, 0f, rate), true, DT)
|
||||
}
|
||||
repeat((1f / DT).toInt()) { motion.update(q, q.conjugate().rotate(Vec3(0f, G, 0f)), Vec3(0f, 0f, 0f), true, DT) }
|
||||
assertEquals(0f, motion.displacement.length(), 0.005f)
|
||||
}
|
||||
|
||||
@Test fun anUncalibratedJoyConAtRestDoesNotDrift() {
|
||||
val motion = JoyConMotion()
|
||||
val filter = JoyConOrientation()
|
||||
val tilted = Vec3(0.3f, G + 0.35f, -0.2f)
|
||||
repeat((30f / DT).toInt()) {
|
||||
filter.update(tilted, Vec3(0f, 0f, 0f), DT)
|
||||
if (filter.ready) motion.update(filter.rotation, tilted, filter.rate, false, DT)
|
||||
}
|
||||
assertTrue("drifted ${motion.displacement}", motion.displacement.length() < 0.05f)
|
||||
assertEquals(0f, motion.velocity.length(), 1e-3f)
|
||||
}
|
||||
|
||||
@Test fun longHandlingWithATiltErrorCannotRunAway() {
|
||||
val motion = JoyConMotion()
|
||||
still(motion, 1f)
|
||||
val leaking = Vec3(0.5f, G, 0f)
|
||||
repeat((10f / DT).toInt()) { motion.update(Quaternion.IDENTITY, leaking, Vec3(0f, 0f, 1f), true, DT) }
|
||||
assertTrue("velocity ${motion.velocity}", motion.velocity.length() < 0.15f)
|
||||
assertTrue("moved ${motion.displacement}", motion.displacement.length() < 1.5f)
|
||||
}
|
||||
|
||||
@Test fun onlyAStillJoyConCountsAsResting() {
|
||||
val motion = JoyConMotion()
|
||||
still(motion, 0.2f)
|
||||
assertFalse(motion.resting)
|
||||
still(motion, 0.2f)
|
||||
assertTrue(motion.resting)
|
||||
motion.update(Quaternion.IDENTITY, Vec3(0f, G, 0f), Vec3(0.3f, 0f, 0f), true, DT)
|
||||
assertFalse(motion.resting)
|
||||
}
|
||||
|
||||
private fun still(motion: JoyConMotion, seconds: Float) {
|
||||
repeat((seconds / DT).toInt()) { motion.update(Quaternion.IDENTITY, Vec3(0f, G, 0f), Vec3(0f, 0f, 0f), true, DT) }
|
||||
}
|
||||
|
||||
private fun assertVec(want: Vec3, got: Vec3, tolerance: Float = 1e-4f) {
|
||||
assertEquals("x of $got", want.x, got.x, tolerance)
|
||||
assertEquals("y of $got", want.y, got.y, tolerance)
|
||||
assertEquals("z of $got", want.z, got.z, tolerance)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val G = 9.80665f
|
||||
const val DT = 0.005f
|
||||
val directions = listOf(Vec3(0f, 1f, 0f), Vec3(0f, -1f, 0f), Vec3(1f, 0f, 0f), Vec3(-1f, 0f, 0f), Vec3(0f, 0f, 1f), Vec3(0f, 0f, -1f))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import com.droiddeck.launcher.glasses.Vec3
|
||||
import kotlin.math.atan2
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.sin
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
class JoyConOrientationTest {
|
||||
private val faceUp = floatArrayOf(0f, 0f, G)
|
||||
private val still = floatArrayOf(0f, 0f, 0f)
|
||||
|
||||
@Test fun aJoyConLyingFaceUpPointsStraightAhead() {
|
||||
val filter = JoyConOrientation()
|
||||
filter.update(faceUp, still, 0f)
|
||||
assertTrue(filter.ready)
|
||||
assertVec(Vec3(0f, 0f, -1f), forward(filter))
|
||||
assertVec(Vec3(0f, 1f, 0f), filter.rotation.rotate(Vec3(0f, 1f, 0f)))
|
||||
}
|
||||
|
||||
@Test fun standingOnItsBaseAimsUp() {
|
||||
val filter = JoyConOrientation()
|
||||
filter.update(floatArrayOf(G, 0f, 0f), still, 0f)
|
||||
assertVec(Vec3(0f, 1f, 0f), forward(filter))
|
||||
}
|
||||
|
||||
@Test fun nothingIsReportedUntilGravityIsPlausible() {
|
||||
val filter = JoyConOrientation()
|
||||
filter.update(floatArrayOf(0f, 0f, 3f), still, 0f)
|
||||
assertFalse(filter.ready)
|
||||
filter.update(floatArrayOf(0f, 0f, 30f), still, 0f)
|
||||
assertFalse(filter.ready)
|
||||
filter.update(faceUp, still, 0f)
|
||||
assertTrue(filter.ready)
|
||||
}
|
||||
|
||||
@Test fun turningAboutTheFaceTurnsTheAimLeft() {
|
||||
val filter = JoyConOrientation()
|
||||
filter.update(faceUp, still, 0f)
|
||||
repeat(100) { filter.update(faceUp, floatArrayOf(0f, 0f, 1f), 0.01f) }
|
||||
assertVec(Vec3(-sin(1f), 0f, -cos(1f)), forward(filter), 2e-3f)
|
||||
assertVec(Vec3(0f, 1f, 0f), filter.rate)
|
||||
}
|
||||
|
||||
@Test fun gravityPullsATiltedEstimateBackLevel() {
|
||||
val filter = JoyConOrientation()
|
||||
filter.update(faceUp, still, 0f)
|
||||
repeat(50) { filter.update(faceUp, floatArrayOf(1f, 0f, 0f), 0.01f) }
|
||||
assertTrue(filter.rotation.rotate(Vec3(0f, 1f, 0f)).y < 0.97f)
|
||||
repeat(600) { filter.update(faceUp, still, 0.01f) }
|
||||
assertEquals(1f, filter.rotation.rotate(Vec3(0f, 1f, 0f)).y, 1e-3f)
|
||||
}
|
||||
|
||||
@Test fun aSteadyGyroOffsetIsLearnedWhileTheJoyConRests() {
|
||||
val filter = JoyConOrientation()
|
||||
filter.update(faceUp, still, 0f)
|
||||
val drifting = floatArrayOf(0f, 0f, 0.02f)
|
||||
repeat(1000) { filter.update(faceUp, drifting, 0.01f) }
|
||||
assertEquals(0f, filter.rate.length(), 1e-3f)
|
||||
val heading = heading(filter)
|
||||
repeat(1000) { filter.update(faceUp, drifting, 0.01f) }
|
||||
assertEquals(heading, heading(filter), 2e-3f)
|
||||
}
|
||||
|
||||
@Test fun movingTheJoyConDoesNotTeachTheBias() {
|
||||
val filter = JoyConOrientation()
|
||||
filter.update(faceUp, still, 0f)
|
||||
repeat(300) { filter.update(faceUp, floatArrayOf(0f, 0f, 0.5f), 0.01f) }
|
||||
assertEquals(0.5f, filter.rate.length(), 1e-4f)
|
||||
}
|
||||
|
||||
@Test fun reportsArriveNewestFirstAndTheRepeatedSampleIsDropped() {
|
||||
val reports = JoyConReports()
|
||||
fun sample(n: Int) = floatArrayOf(n.toFloat(), 0f, G) to floatArrayOf(n * 0.1f, 0f, 0f)
|
||||
fun send(at: Long, vararg newestFirst: Int): List<JoyConReports.Sample> {
|
||||
for (n in newestFirst) sample(n).let { reports.add(it.first, it.second, at) }
|
||||
return reports.flush()
|
||||
}
|
||||
assertEquals(listOf(1f, 2f, 3f), send(1_000_000_000L, 3, 2, 1).map { it.accel[0] })
|
||||
val second = send(1_010_000_000L, 5, 4, 3)
|
||||
assertEquals(listOf(4f, 5f), second.map { it.accel[0] })
|
||||
assertEquals("the report interval is shared by its new samples", 0.005f, second[0].dt, 1e-6f)
|
||||
assertEquals(listOf(6f, 7f, 8f), send(1_025_000_000L, 8, 7, 6).map { it.accel[0] })
|
||||
val still = send(1_035_000_000L, 8, 8, 8)
|
||||
assertEquals("a still report still moves time on", 1, still.size)
|
||||
assertEquals(0.01f, still[0].dt, 1e-6f)
|
||||
assertEquals("a long gap is capped", 0.05f, send(2_000_000_000L, 11, 10, 9)[0].dt, 1e-6f)
|
||||
}
|
||||
|
||||
@Test fun reportsInOldestFirstOrderAreRecognised() {
|
||||
val reports = JoyConReports()
|
||||
var at = 1_000_000_000L
|
||||
var next = 1
|
||||
val seen = ArrayList<Float>()
|
||||
repeat(12) {
|
||||
val samples = listOf(next - 1, next, next + 1).filter { it > 0 }
|
||||
for (n in samples) reports.add(floatArrayOf(n.toFloat(), 0f, G), floatArrayOf(0f, 0f, 0f), at)
|
||||
seen += reports.flush().map { it.accel[0] }
|
||||
next += 2
|
||||
at += 10_000_000L
|
||||
}
|
||||
assertEquals((10..24).map { it.toFloat() }, seen.takeLast(15))
|
||||
}
|
||||
|
||||
private fun forward(filter: JoyConOrientation) = filter.rotation.rotate(Vec3(0f, 0f, -1f))
|
||||
|
||||
private fun heading(filter: JoyConOrientation): Float = forward(filter).let { atan2(-it.x, -it.z) }
|
||||
|
||||
private fun assertVec(want: Vec3, got: Vec3, tolerance: Float = 1e-4f) {
|
||||
assertEquals("x of $got", want.x, got.x, tolerance)
|
||||
assertEquals("y of $got", want.y, got.y, tolerance)
|
||||
assertEquals("z of $got", want.z, got.z, tolerance)
|
||||
}
|
||||
|
||||
private companion object { const val G = 9.80665f }
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.hardware.input.InputManager
|
||||
import android.view.InputDevice
|
||||
import android.view.KeyCharacterMap
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import org.junit.After
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.Implementation
|
||||
import org.robolectric.annotation.Implements
|
||||
import org.robolectric.shadows.InputDeviceBuilder
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE, instrumentedPackages = ["com.droiddeck.launcher.input.FakeInputWriter"],
|
||||
shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class, PadBridgeRoutingTest.RegisteredMotionEvent::class,
|
||||
JoyConPadTest.RecordingWriter::class])
|
||||
class JoyConPadTest {
|
||||
private lateinit var dir: File
|
||||
private lateinit var bridge: PadBridge
|
||||
private lateinit var left: InputDevice
|
||||
private lateinit var right: InputDevice
|
||||
|
||||
@Before fun setUp() {
|
||||
RecordingWriter.pads.clear()
|
||||
RecordingWriter.grips = 0
|
||||
dir = Files.createTempDirectory("joycon-pad").toFile()
|
||||
bridge = PadBridge(dir)
|
||||
left = joyCon(LEFT_PRODUCT, MotionEvent.AXIS_X, MotionEvent.AXIS_Y)
|
||||
right = joyCon(RIGHT_PRODUCT, MotionEvent.AXIS_RX, MotionEvent.AXIS_RY)
|
||||
}
|
||||
|
||||
@After fun tearDown() {
|
||||
bridge.stop()
|
||||
dir.deleteRecursively()
|
||||
}
|
||||
|
||||
@Test fun twoJoyConsBecomeOnePlayerWithBothSticks() {
|
||||
pair()
|
||||
assertEquals(left.id, PadBridge.joyConForSlot(0, JoyCon.LEFT))
|
||||
assertEquals(right.id, PadBridge.joyConForSlot(0, JoyCon.RIGHT))
|
||||
assertTrue(press(left, KeyEvent.KEYCODE_UNKNOWN, 0x220))
|
||||
assertTrue(press(right, KeyEvent.KEYCODE_BUTTON_X, BTN_NORTH))
|
||||
assertTrue(pad(0).up)
|
||||
assertTrue(pad(0).isDown(PadState.Y))
|
||||
assertTrue(bridge.onMotionEvent(motion(left, MotionEvent.AXIS_X to 0.75f, MotionEvent.AXIS_Y to -0.5f)))
|
||||
assertTrue(bridge.onMotionEvent(motion(right, MotionEvent.AXIS_RX to -0.6f, MotionEvent.AXIS_X to 0f)))
|
||||
assertEquals(0.75f, pad(0).leftX)
|
||||
assertEquals(-0.5f, pad(0).leftY)
|
||||
assertEquals(-0.6f, pad(0).rightX)
|
||||
assertNull("a second player appeared", RecordingWriter.pads[1])
|
||||
assertEquals(PadBridge.NO_CONTROLLER, PadBridge.joyConForSlot(1, JoyCon.LEFT))
|
||||
}
|
||||
|
||||
@Test fun railButtonsAreShouldersOnlyForALoneJoyCon() {
|
||||
assertTrue(press(left, KeyEvent.KEYCODE_BUTTON_R1, BTN_TR))
|
||||
assertTrue(pad(0).isDown(PadState.RB))
|
||||
tap(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH)
|
||||
assertFalse("a rail button held while pairing stayed down", pad(0).isDown(PadState.RB))
|
||||
assertTrue(bridge.onKeyEvent(JoyCon.normalize(key(left, KeyEvent.KEYCODE_BUTTON_R1, BTN_TR, KeyEvent.ACTION_UP))))
|
||||
assertTrue(press(right, KeyEvent.KEYCODE_BUTTON_R1, BTN_TR))
|
||||
assertTrue(press(left, KeyEvent.KEYCODE_BUTTON_R1, BTN_TR))
|
||||
assertTrue(bridge.onKeyEvent(JoyCon.normalize(key(left, KeyEvent.KEYCODE_BUTTON_R1, BTN_TR, KeyEvent.ACTION_UP))))
|
||||
assertTrue("the left rail released the right shoulder", pad(0).isDown(PadState.RB))
|
||||
assertTrue(press(right, KeyEvent.KEYCODE_BUTTON_L1, BTN_TL))
|
||||
assertFalse(pad(0).isDown(PadState.LB))
|
||||
}
|
||||
|
||||
@Test fun captureOnTheFirstPlayerIsTheLeftGripUntilPairingReleasesIt() {
|
||||
assertTrue(press(left, KeyEvent.KEYCODE_BUTTON_Z, BTN_Z))
|
||||
assertEquals(DeckControls.L4, RecordingWriter.grips and DeckControls.L4)
|
||||
tap(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH)
|
||||
assertEquals(0, RecordingWriter.grips)
|
||||
}
|
||||
|
||||
@Test fun whenOneJoyConLeavesTheOtherKeepsThePlayer() {
|
||||
pair()
|
||||
assertTrue(press(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH))
|
||||
bridge.onDeviceRemoved(left.id)
|
||||
assertEquals(PadBridge.NO_CONTROLLER, PadBridge.joyConForSlot(0, JoyCon.LEFT))
|
||||
assertEquals(PadBridge.NO_CONTROLLER, PadBridge.joyConForSlot(0, JoyCon.RIGHT))
|
||||
assertFalse("a held button survived the split", pad(0).isDown(PadState.A))
|
||||
assertEquals(right.id, PadBridge.controllerForSlot(0))
|
||||
tap(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH)
|
||||
assertNull(RecordingWriter.pads[1])
|
||||
val another = joyCon(LEFT_PRODUCT, MotionEvent.AXIS_X, MotionEvent.AXIS_Y)
|
||||
tap(another, KeyEvent.KEYCODE_UNKNOWN, 0x221)
|
||||
assertEquals(another.id, PadBridge.joyConForSlot(0, JoyCon.LEFT))
|
||||
assertEquals(right.id, PadBridge.joyConForSlot(0, JoyCon.RIGHT))
|
||||
bridge.onDeviceRemoved(right.id)
|
||||
assertEquals(another.id, PadBridge.controllerForSlot(0))
|
||||
tap(another, KeyEvent.KEYCODE_UNKNOWN, 0x221)
|
||||
assertNull(RecordingWriter.pads[1])
|
||||
}
|
||||
|
||||
@Test fun aSecondPlayerCanBeAJoyConPair() {
|
||||
val xbox = InputDeviceBuilder.newBuilder().setId(90).setName("pad").setDescriptor("pad-90").setExternal(true)
|
||||
.setVendorId(0x045e).setProductId(0x0b13).setSources(InputDevice.SOURCE_GAMEPAD or InputDevice.SOURCE_JOYSTICK)
|
||||
.setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)
|
||||
.setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)).build()
|
||||
register(xbox)
|
||||
tap(xbox, KeyEvent.KEYCODE_BUTTON_A, 0x130)
|
||||
pair()
|
||||
assertEquals(left.id, PadBridge.joyConForSlot(1, JoyCon.LEFT))
|
||||
assertEquals(right.id, PadBridge.joyConForSlot(1, JoyCon.RIGHT))
|
||||
assertEquals(PadBridge.NO_CONTROLLER, PadBridge.joyConForSlot(0, JoyCon.LEFT))
|
||||
assertTrue(press(right, KeyEvent.KEYCODE_BUTTON_B, 0x131))
|
||||
assertTrue(pad(1).isDown(PadState.B))
|
||||
assertNull(RecordingWriter.pads[2])
|
||||
}
|
||||
|
||||
@Test fun joyConsHeldForVrAreLeftAlone() {
|
||||
bridge.setExcluded { it == left.id }
|
||||
assertFalse(bridge.onKeyEvent(key(left, KeyEvent.KEYCODE_BUTTON_L1, BTN_TL)))
|
||||
assertFalse(bridge.onMotionEvent(motion(left, MotionEvent.AXIS_X to 1f)))
|
||||
bridge.syncConnectedControllers()
|
||||
assertEquals(PadBridge.NO_CONTROLLER, PadBridge.joyConForSlot(0, JoyCon.LEFT))
|
||||
tap(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH)
|
||||
assertEquals(right.id, PadBridge.controllerForSlot(0))
|
||||
assertEquals(PadBridge.NO_CONTROLLER, PadBridge.joyConForSlot(0, JoyCon.RIGHT))
|
||||
bridge.setExcluded { false }
|
||||
bridge.syncConnectedControllers()
|
||||
assertEquals(left.id, PadBridge.joyConForSlot(0, JoyCon.LEFT))
|
||||
assertEquals(right.id, PadBridge.joyConForSlot(0, JoyCon.RIGHT))
|
||||
}
|
||||
|
||||
private fun pair() {
|
||||
tap(left, KeyEvent.KEYCODE_BUTTON_L1, BTN_TL)
|
||||
tap(right, KeyEvent.KEYCODE_BUTTON_R1, BTN_TR)
|
||||
}
|
||||
|
||||
private fun pad(slot: Int): PadState = RecordingWriter.pads[slot] ?: PadState()
|
||||
|
||||
private fun press(device: InputDevice, code: Int, scanCode: Int): Boolean =
|
||||
bridge.onKeyEvent(JoyCon.normalize(key(device, code, scanCode)))
|
||||
|
||||
private fun tap(device: InputDevice, code: Int, scanCode: Int) {
|
||||
assertTrue(press(device, code, scanCode))
|
||||
assertTrue(bridge.onKeyEvent(JoyCon.normalize(key(device, code, scanCode, KeyEvent.ACTION_UP))))
|
||||
}
|
||||
|
||||
private fun key(device: InputDevice, code: Int, scanCode: Int, action: Int = KeyEvent.ACTION_DOWN): KeyEvent {
|
||||
val event = KeyEvent(0L, 0L, action, code, 0, 0, device.id, scanCode, 0, InputDevice.SOURCE_GAMEPAD)
|
||||
assertSame(device, event.device)
|
||||
return event
|
||||
}
|
||||
|
||||
private fun motion(device: InputDevice, vararg values: Pair<Int, Float>): MotionEvent {
|
||||
val coords = MotionEvent.PointerCoords().apply { for ((axis, value) in values) setAxisValue(axis, value) }
|
||||
return MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_MOVE, 1, arrayOf(MotionEvent.PointerProperties().apply { id = 0 }),
|
||||
arrayOf(coords), 0, 0, 1f, 1f, device.id, 0, InputDevice.SOURCE_JOYSTICK, 0)
|
||||
}
|
||||
|
||||
private var nextId = 60
|
||||
|
||||
private fun joyCon(product: Int, vararg axes: Int): InputDevice {
|
||||
val id = nextId++
|
||||
val builder = InputDeviceBuilder.newBuilder().setId(id).setName("Joy-Con $id").setDescriptor("joycon-$id")
|
||||
.setExternal(true).setVendorId(NINTENDO).setProductId(product)
|
||||
.setSources(InputDevice.SOURCE_GAMEPAD or InputDevice.SOURCE_JOYSTICK or InputDevice.SOURCE_KEYBOARD)
|
||||
.setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)
|
||||
.setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD))
|
||||
for (axis in axes) builder.addMotionRange(axis, InputDevice.SOURCE_JOYSTICK, -1f, 1f, 0f, 0f, 0f)
|
||||
return builder.build().also(::register)
|
||||
}
|
||||
|
||||
private fun register(device: InputDevice) {
|
||||
val manager = RuntimeEnvironment.getApplication().getSystemService(InputManager::class.java)
|
||||
shadowOf(manager).addInputDevice(device)
|
||||
assertSame(device, InputDevice.getDevice(device.id))
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val NINTENDO = 0x057e
|
||||
const val LEFT_PRODUCT = 0x2006
|
||||
const val RIGHT_PRODUCT = 0x2007
|
||||
const val BTN_SOUTH = 0x130
|
||||
const val BTN_NORTH = 0x133
|
||||
const val BTN_Z = 0x135
|
||||
const val BTN_TL = 0x136
|
||||
const val BTN_TR = 0x137
|
||||
}
|
||||
|
||||
@Implements(FakeInputWriter::class)
|
||||
class RecordingWriter {
|
||||
private var slot = 0
|
||||
|
||||
@Implementation fun __constructor__(fakeInputPath: String, slot: Int) { this.slot = slot }
|
||||
@Implementation fun open(): Boolean = true
|
||||
@Implementation fun close() = Unit
|
||||
@Implementation fun destroy() = Unit
|
||||
@Implementation fun writePad(state: PadState) { pads[slot] = PadState().apply { copyFrom(state) } }
|
||||
|
||||
companion object {
|
||||
val pads = HashMap<Int, PadState>()
|
||||
var grips = 0
|
||||
|
||||
@JvmStatic @Implementation fun __staticInitializer__() = Unit
|
||||
|
||||
@JvmStatic @Implementation
|
||||
fun writeDeckControls(slot: Int, controls: Int, pads: ShortArray, pressure: ShortArray) { grips = controls }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import com.droiddeck.launcher.glasses.Quaternion
|
||||
import com.droiddeck.launcher.glasses.Vec3
|
||||
import java.io.File
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.nio.file.Files
|
||||
import org.junit.After
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class JoyConShmTest {
|
||||
private lateinit var dir: File
|
||||
private lateinit var file: File
|
||||
private var fences = 0
|
||||
|
||||
@Before fun setUp() {
|
||||
dir = Files.createTempDirectory("joycon-shm").toFile()
|
||||
file = File(dir, "run/${JoyConShm.FILE}")
|
||||
}
|
||||
|
||||
@After fun tearDown() { dir.deleteRecursively() }
|
||||
|
||||
@Test fun anOpenedFileCarriesTheHeaderAndNoHands() {
|
||||
assertNotNull(JoyConShm.open(file) { fences++ })
|
||||
val bytes = read()
|
||||
assertEquals(JoyConShm.SIZE, bytes.capacity())
|
||||
assertEquals(0x4E434A44, bytes.getInt(0))
|
||||
assertEquals(3, bytes.getInt(4))
|
||||
assertEquals(2, bytes.getInt(8))
|
||||
for (at in 16 until JoyConShm.SIZE step 4) assertEquals("byte $at", 0, bytes.getInt(at))
|
||||
assertEquals(2, fences)
|
||||
}
|
||||
|
||||
@Test fun aHandLandsWhereTheDriverReadsIt() {
|
||||
val shm = JoyConShm.open(file) { fences++ }!!
|
||||
shm.write(JoyCon.RIGHT, JoyConShm.Hand(connected = true, charging = true,
|
||||
buttons = JoyCon.VR_TRIGGER or JoyCon.VR_A, rotation = Quaternion(0.5f, -0.5f, 0.25f, 0.75f),
|
||||
rate = Vec3(1f, -2f, 3f), displacement = Vec3(0.1f, 0.2f, -0.3f), velocity = Vec3(-1.5f, 0.5f, 2f), inertial = Vec3(4f, -5f, 6f), stickX = 0.25f, stickY = -0.5f, battery = 0.75f, recenter = 3, timeNs = 1_234_567_890_123L))
|
||||
val bytes = read()
|
||||
assertEquals(4, bytes.getInt(8))
|
||||
val at = 16 + 104
|
||||
assertEquals(3, bytes.getInt(at))
|
||||
assertEquals(JoyCon.VR_TRIGGER or JoyCon.VR_A, bytes.getInt(at + 4))
|
||||
assertArrayEquals(floatArrayOf(0.5f, -0.5f, 0.25f, 0.75f, 1f, -2f, 3f, 0.25f, -0.5f, 0.75f),
|
||||
FloatArray(10) { bytes.getFloat(at + 8 + it * 4) }, 0f)
|
||||
assertEquals(3, bytes.getInt(at + 48))
|
||||
assertEquals(1_234_567_890_123L, bytes.getLong(at + 56))
|
||||
assertArrayEquals(floatArrayOf(0.1f, 0.2f, -0.3f, -1.5f, 0.5f, 2f, 4f, -5f, 6f), FloatArray(9) { bytes.getFloat(at + 64 + it * 4) }, 0f)
|
||||
assertEquals(224, JoyConShm.SIZE)
|
||||
assertEquals("the left hand was touched", 0, bytes.getInt(16))
|
||||
assertEquals(4, fences)
|
||||
}
|
||||
|
||||
@Test fun closingDisconnectsBothHands() {
|
||||
val shm = JoyConShm.open(file) {}!!
|
||||
shm.write(JoyCon.LEFT, JoyConShm.Hand(connected = true))
|
||||
shm.write(JoyCon.RIGHT, JoyConShm.Hand(connected = true))
|
||||
shm.close()
|
||||
val bytes = read()
|
||||
assertEquals(0, bytes.getInt(16))
|
||||
assertEquals(0, bytes.getInt(16 + 104))
|
||||
assertEquals(0, bytes.getInt(8) and 1)
|
||||
}
|
||||
|
||||
@Test fun reopeningKeepsTheSequenceMovingForward() {
|
||||
JoyConShm.open(file) {}!!.write(JoyCon.LEFT, JoyConShm.Hand(connected = true))
|
||||
assertEquals(4, read().getInt(8))
|
||||
file.outputStream().use { }
|
||||
assertEquals(0L, file.length())
|
||||
assertNotNull(JoyConShm.open(file) {})
|
||||
assertEquals(2, read().getInt(8))
|
||||
java.io.RandomAccessFile(file, "rw").use { it.seek(8); it.write(byteArrayOf(7, 0, 0, 0)) }
|
||||
JoyConShm.open(file) {}!!
|
||||
val after = read().getInt(8)
|
||||
assertEquals(0, after and 1)
|
||||
assertTrue("$after after a torn write", after > 7)
|
||||
}
|
||||
|
||||
@Test fun anUnwritableLocationIsReportedAsMissing() {
|
||||
val blocker = File(dir, "blocker").apply { writeText("") }
|
||||
assertNull(JoyConShm.open(File(blocker, JoyConShm.FILE)) {})
|
||||
}
|
||||
|
||||
private fun read(): ByteBuffer = ByteBuffer.wrap(file.readBytes()).order(ByteOrder.LITTLE_ENDIAN)
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.hardware.input.InputManager
|
||||
import android.view.InputDevice
|
||||
import android.view.KeyCharacterMap
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import org.junit.After
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.InputDeviceBuilder
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE, shadows = [PadBridgeRoutingTest.RegisteredKeyEvent::class])
|
||||
class JoyConTest {
|
||||
@After fun clearVrHands() { JoyCon.clearVrDevices(this) }
|
||||
|
||||
@Test fun onlyNintendoJoyConsHaveASide() {
|
||||
assertEquals(JoyCon.LEFT, JoyCon.side(device(LEFT_PRODUCT)))
|
||||
assertEquals(JoyCon.RIGHT, JoyCon.side(device(RIGHT_PRODUCT)))
|
||||
assertEquals(-1, JoyCon.side(device(PRO_CONTROLLER)))
|
||||
assertEquals(-1, JoyCon.side(device(LEFT_PRODUCT, vendor = 0x045e)))
|
||||
assertEquals(-1, JoyCon.side(null))
|
||||
}
|
||||
|
||||
@Test fun theRightJoyConFaceButtonsKeepTheirPositions() {
|
||||
val right = device(RIGHT_PRODUCT)
|
||||
assertEquals(KeyEvent.KEYCODE_BUTTON_Y, JoyCon.normalize(key(right, KeyEvent.KEYCODE_BUTTON_X, BTN_NORTH)).keyCode)
|
||||
assertEquals(KeyEvent.KEYCODE_BUTTON_X, JoyCon.normalize(key(right, KeyEvent.KEYCODE_BUTTON_Y, BTN_WEST)).keyCode)
|
||||
val south = key(right, KeyEvent.KEYCODE_BUTTON_A, BTN_SOUTH)
|
||||
assertSame(south, JoyCon.normalize(south))
|
||||
val remapped = JoyCon.normalize(key(right, KeyEvent.KEYCODE_BUTTON_X, BTN_NORTH, KeyEvent.ACTION_UP))
|
||||
assertEquals(KeyEvent.ACTION_UP, remapped.action)
|
||||
assertEquals(right.id, remapped.deviceId)
|
||||
assertEquals(BTN_NORTH, remapped.scanCode)
|
||||
assertEquals(InputDevice.SOURCE_GAMEPAD, remapped.source)
|
||||
}
|
||||
|
||||
@Test fun theLeftJoyConArrowsBecomeADpad() {
|
||||
val left = device(LEFT_PRODUCT)
|
||||
val arrows = mapOf(0x220 to KeyEvent.KEYCODE_DPAD_UP, 0x221 to KeyEvent.KEYCODE_DPAD_DOWN,
|
||||
0x222 to KeyEvent.KEYCODE_DPAD_LEFT, 0x223 to KeyEvent.KEYCODE_DPAD_RIGHT)
|
||||
for ((scanCode, code) in arrows) {
|
||||
assertEquals(code, JoyCon.normalize(key(left, KeyEvent.KEYCODE_UNKNOWN, scanCode)).keyCode)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun railButtonsAndOtherPadsPassThroughUntouched() {
|
||||
val left = device(LEFT_PRODUCT)
|
||||
val right = device(RIGHT_PRODUCT)
|
||||
for (event in listOf(key(left, KeyEvent.KEYCODE_BUTTON_R1, BTN_TR), key(left, KeyEvent.KEYCODE_BUTTON_R2, BTN_TR2),
|
||||
key(right, KeyEvent.KEYCODE_BUTTON_L1, BTN_TL), key(right, KeyEvent.KEYCODE_BUTTON_L2, BTN_TL2),
|
||||
key(device(PRO_CONTROLLER), KeyEvent.KEYCODE_BUTTON_X, BTN_NORTH))) {
|
||||
assertSame(event, JoyCon.normalize(event))
|
||||
}
|
||||
assertTrue(JoyCon.isRail(JoyCon.LEFT, BTN_TR))
|
||||
assertTrue(JoyCon.isRail(JoyCon.RIGHT, BTN_TL2))
|
||||
assertFalse(JoyCon.isRail(JoyCon.LEFT, BTN_TL))
|
||||
assertFalse(JoyCon.isRail(JoyCon.RIGHT, BTN_TR))
|
||||
assertFalse(JoyCon.isRail(-1, BTN_TR))
|
||||
}
|
||||
|
||||
@Test fun eachJoyConReadsItsOwnStick() {
|
||||
val left = device(LEFT_PRODUCT)
|
||||
val right = device(RIGHT_PRODUCT, axes = intArrayOf(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY))
|
||||
val oldRight = device(RIGHT_PRODUCT, axes = intArrayOf(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ))
|
||||
val source = InputDevice.SOURCE_JOYSTICK
|
||||
assertArrayEquals(intArrayOf(MotionEvent.AXIS_X, MotionEvent.AXIS_Y), JoyCon.stickAxes(JoyCon.LEFT, left, source))
|
||||
assertArrayEquals(intArrayOf(MotionEvent.AXIS_RX, MotionEvent.AXIS_RY), JoyCon.stickAxes(JoyCon.RIGHT, right, source))
|
||||
assertArrayEquals(intArrayOf(MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ), JoyCon.stickAxes(JoyCon.RIGHT, oldRight, source))
|
||||
}
|
||||
|
||||
@Test fun vrButtonsFollowTheIndexLayoutOnEitherHand() {
|
||||
val left = mapOf(BTN_TL2 to JoyCon.VR_TRIGGER, BTN_TL to JoyCon.VR_GRIP, BTN_TR to JoyCon.VR_GRIP,
|
||||
BTN_TR2 to JoyCon.VR_GRIP, 0x221 to JoyCon.VR_A, 0x223 to JoyCon.VR_A, 0x220 to JoyCon.VR_B,
|
||||
0x222 to JoyCon.VR_B, BTN_SELECT to JoyCon.VR_B, BTN_THUMBL to JoyCon.VR_STICK, BTN_Z to JoyCon.VR_SYSTEM)
|
||||
val right = mapOf(BTN_TR2 to JoyCon.VR_TRIGGER, BTN_TR to JoyCon.VR_GRIP, BTN_TL to JoyCon.VR_GRIP,
|
||||
BTN_TL2 to JoyCon.VR_GRIP, BTN_SOUTH to JoyCon.VR_A, BTN_EAST to JoyCon.VR_A, BTN_NORTH to JoyCon.VR_B,
|
||||
BTN_WEST to JoyCon.VR_B, BTN_START to JoyCon.VR_B, BTN_THUMBR to JoyCon.VR_STICK, BTN_MODE to JoyCon.VR_SYSTEM)
|
||||
for ((scanCode, bit) in left) assertEquals("left 0x${scanCode.toString(16)}", bit, JoyCon.vrButton(JoyCon.LEFT, scanCode))
|
||||
for ((scanCode, bit) in right) assertEquals("right 0x${scanCode.toString(16)}", bit, JoyCon.vrButton(JoyCon.RIGHT, scanCode))
|
||||
assertEquals(0, JoyCon.vrButton(JoyCon.LEFT, BTN_SOUTH))
|
||||
assertEquals(0, JoyCon.vrButton(JoyCon.RIGHT, 0x220))
|
||||
assertEquals(0, JoyCon.vrButton(-1, BTN_TR2))
|
||||
}
|
||||
|
||||
@Test fun vrRumbleSlotsNameTheHandsOfTheCurrentOwnerOnly() {
|
||||
for (slot in listOf(0, 1, PadBridge.SLOTS, JoyCon.VR_RUMBLE_SLOT + 2)) assertNull(JoyCon.vrRumbleDevice(slot))
|
||||
assertEquals(PadBridge.NO_CONTROLLER, JoyCon.vrRumbleDevice(JoyCon.VR_RUMBLE_SLOT))
|
||||
JoyCon.setVrDevices(this, intArrayOf(31, 32))
|
||||
assertEquals(31, JoyCon.vrRumbleDevice(JoyCon.VR_RUMBLE_SLOT + JoyCon.LEFT))
|
||||
assertEquals(32, JoyCon.vrRumbleDevice(JoyCon.VR_RUMBLE_SLOT + JoyCon.RIGHT))
|
||||
JoyCon.clearVrDevices(Any())
|
||||
assertEquals(31, JoyCon.vrRumbleDevice(JoyCon.VR_RUMBLE_SLOT))
|
||||
JoyCon.clearVrDevices(this)
|
||||
assertEquals(PadBridge.NO_CONTROLLER, JoyCon.vrRumbleDevice(JoyCon.VR_RUMBLE_SLOT))
|
||||
assertEquals(PadBridge.NO_CONTROLLER, JoyCon.vrRumbleDevice(JoyCon.VR_RUMBLE_SLOT + 1))
|
||||
}
|
||||
|
||||
private var nextId = 40
|
||||
|
||||
private fun device(product: Int, vendor: Int = NINTENDO, axes: IntArray = intArrayOf(MotionEvent.AXIS_X, MotionEvent.AXIS_Y)): InputDevice {
|
||||
val id = nextId++
|
||||
val builder = InputDeviceBuilder.newBuilder().setId(id).setName("Joy-Con fixture $id").setDescriptor("joycon-$id")
|
||||
.setExternal(true).setVendorId(vendor).setProductId(product)
|
||||
.setSources(InputDevice.SOURCE_GAMEPAD or InputDevice.SOURCE_JOYSTICK or InputDevice.SOURCE_KEYBOARD)
|
||||
.setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC)
|
||||
.setKeyCharacterMap(KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD))
|
||||
for (axis in axes) builder.addMotionRange(axis, InputDevice.SOURCE_JOYSTICK, -1f, 1f, 0f, 0f, 0f)
|
||||
val device = builder.build()
|
||||
val manager = RuntimeEnvironment.getApplication().getSystemService(InputManager::class.java)
|
||||
shadowOf(manager).addInputDevice(device)
|
||||
return device
|
||||
}
|
||||
|
||||
private fun key(device: InputDevice, code: Int, scanCode: Int, action: Int = KeyEvent.ACTION_DOWN): KeyEvent {
|
||||
val event = KeyEvent(0L, 0L, action, code, 0, 0, device.id, scanCode, 0, InputDevice.SOURCE_GAMEPAD)
|
||||
assertSame(device, event.device)
|
||||
return event
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val NINTENDO = 0x057e
|
||||
const val LEFT_PRODUCT = 0x2006
|
||||
const val RIGHT_PRODUCT = 0x2007
|
||||
const val PRO_CONTROLLER = 0x2009
|
||||
const val BTN_SOUTH = 0x130
|
||||
const val BTN_EAST = 0x131
|
||||
const val BTN_NORTH = 0x133
|
||||
const val BTN_WEST = 0x134
|
||||
const val BTN_Z = 0x135
|
||||
const val BTN_TL = 0x136
|
||||
const val BTN_TR = 0x137
|
||||
const val BTN_TL2 = 0x138
|
||||
const val BTN_TR2 = 0x139
|
||||
const val BTN_SELECT = 0x13a
|
||||
const val BTN_START = 0x13b
|
||||
const val BTN_MODE = 0x13c
|
||||
const val BTN_THUMBL = 0x13d
|
||||
const val BTN_THUMBR = 0x13e
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import android.view.Display
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.shadows.ShadowDisplayManager
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [31, 34], manifest = Config.NONE)
|
||||
class PerfModeTest {
|
||||
private fun mode(id: Int, hz: Float, width: Int = 1080): Display.Mode =
|
||||
ShadowDisplayManager.ModeBuilder.modeBuilder(id)
|
||||
.setWidth(width).setHeight(2520).setRefreshRate(hz).build()
|
||||
|
||||
@Test fun sixtyHzMirrorChoosesSixtyEvenThoughOneTwentyHasMatchingCadence() {
|
||||
val low = mode(1, 60f)
|
||||
val high = mode(2, 120f)
|
||||
assertEquals(low, PerfMode.selectMode(arrayOf(low, high), high, 60, 60f))
|
||||
assertEquals(high, PerfMode.selectMode(arrayOf(low, high), low, 0, Float.POSITIVE_INFINITY))
|
||||
}
|
||||
|
||||
@Test fun aLowerGameLimitDoesNotOverrideTheGlassesRefreshCeiling() {
|
||||
val modes = arrayOf(mode(1, 60f), mode(2, 90f), mode(3, 120f))
|
||||
assertEquals(modes[0], PerfMode.selectMode(modes, modes[2], 30, 60f))
|
||||
assertEquals(modes[1], PerfMode.selectMode(modes, modes[2], 45, 90f))
|
||||
}
|
||||
|
||||
@Test fun selectionPreservesResolutionAndUsesTheLowestModeIfTheCapIsUnsupported() {
|
||||
val low = mode(1, 90f)
|
||||
val high = mode(2, 120f)
|
||||
val otherResolution = mode(3, 60f, 720)
|
||||
assertEquals(low, PerfMode.selectMode(arrayOf(low, high, otherResolution), high, 60, 60f))
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import android.os.Looper
|
||||
import android.os.Vibrator
|
||||
import android.os.VibratorManager
|
||||
import com.droiddeck.launcher.input.ControllerPrefs
|
||||
import com.droiddeck.launcher.input.JoyCon
|
||||
import com.droiddeck.launcher.input.PadBridge
|
||||
import java.io.IOException
|
||||
import org.junit.After
|
||||
@@ -202,8 +203,53 @@ class RumbleComponentTest {
|
||||
assertFalse(shadowOf(vibrator).isVibrating)
|
||||
}
|
||||
|
||||
@Test fun eachVrHandRumblesItsOwnJoyConAndNeverThePhone() {
|
||||
val left = FakeMotors()
|
||||
val right = FakeMotors()
|
||||
rumble.controllerMotors = { id -> if (id == PAD_ID) left else if (id == SECOND_PAD_ID) right else null }
|
||||
JoyCon.setVrDevices(this, intArrayOf(PAD_ID, SECOND_PAD_ID))
|
||||
effect(strong = 30000, weak = 30000, slot = JoyCon.VR_RUMBLE_SLOT + JoyCon.LEFT)
|
||||
effect(strong = 20000, weak = 20000, slot = JoyCon.VR_RUMBLE_SLOT + JoyCon.RIGHT)
|
||||
assertEquals(listOf(Triple(30000, 30000, 5000L)), left.played)
|
||||
assertEquals(listOf(Triple(20000, 20000, 5000L)), right.played)
|
||||
JoyCon.clearVrDevices(this)
|
||||
effect(strong = 10000, weak = 10000, slot = JoyCon.VR_RUMBLE_SLOT + JoyCon.LEFT)
|
||||
assertFalse("the hand kept rumbling after SteamVR let it go", left.vibrating)
|
||||
assertFalse(shadowOf(vibrator).isVibrating)
|
||||
}
|
||||
|
||||
@Test fun aJoyConPairSplitsTheMotorsLikeOneController() {
|
||||
val left = FakeMotors()
|
||||
val right = FakeMotors()
|
||||
rumble.controllerMotors = { id -> if (id == PAD_ID) left else if (id == SECOND_PAD_ID) right else null }
|
||||
activeController(SECOND_PAD_ID)
|
||||
joyCons(0, PAD_ID, SECOND_PAD_ID)
|
||||
effect(strong = 40000, weak = 1000)
|
||||
assertEquals(listOf(Triple(40000, 0, 5000L)), left.played)
|
||||
assertEquals(listOf(Triple(0, 1000, 5000L)), right.played)
|
||||
shadowOf(Looper.getMainLooper()).idleFor(java.time.Duration.ofMillis(RumbleComponent.MIN_GAP_MS))
|
||||
effect(strong = 20000, weak = 0)
|
||||
assertTrue(left.vibrating)
|
||||
assertFalse(right.vibrating)
|
||||
joyCons(0, PadBridge.NO_CONTROLLER, PadBridge.NO_CONTROLLER)
|
||||
effect(strong = 10000, weak = 10000)
|
||||
assertEquals("a lone Joy-Con missed the effect", Triple(10000, 10000, 5000L), right.played.last())
|
||||
assertFalse("the departed Joy-Con kept rumbling", left.vibrating)
|
||||
assertFalse(shadowOf(vibrator).isVibrating)
|
||||
}
|
||||
|
||||
@After fun clearActiveController() {
|
||||
for (slot in 0 until PadBridge.SLOTS) activeController(PadBridge.NO_CONTROLLER, slot)
|
||||
for (slot in 0 until PadBridge.SLOTS) {
|
||||
activeController(PadBridge.NO_CONTROLLER, slot)
|
||||
joyCons(slot, PadBridge.NO_CONTROLLER, PadBridge.NO_CONTROLLER)
|
||||
}
|
||||
JoyCon.clearVrDevices(this)
|
||||
}
|
||||
|
||||
private fun joyCons(slot: Int, left: Int, right: Int) {
|
||||
val pairs = PadBridge::class.java.getDeclaredField("slotJoyCons").apply { isAccessible = true }.get(null)
|
||||
(pairs as java.util.concurrent.atomic.AtomicIntegerArray).set(slot * 2 + JoyCon.LEFT, left)
|
||||
pairs.set(slot * 2 + JoyCon.RIGHT, right)
|
||||
}
|
||||
|
||||
private fun activeController(id: Int, slot: Int = 0) {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.test.performClick
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.glasses.CalibrationPhase
|
||||
import com.droiddeck.launcher.glasses.GlassesCalibrationState
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28])
|
||||
class GlassesCalibrationControlsTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
private fun text(id: Int) = RuntimeEnvironment.getApplication().getString(id)
|
||||
|
||||
@Test fun startRequiresInstructionsAndDoneRecentersAfterSuccessfulCapture() {
|
||||
val state = mutableStateOf(GlassesCalibrationState(supported = true))
|
||||
var starts = 0; var cancels = 0; var recentered = 0
|
||||
compose.setContent {
|
||||
Column {
|
||||
GlassesCalibrationControls(state.value,
|
||||
onStart = { starts++; state.value = state.value.copy(phase = CalibrationPhase.SETTLING) },
|
||||
onCancel = { cancels++ }, onReset = {}, onDone = { recentered++ })
|
||||
}
|
||||
}
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_open)).performClick()
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_instructions)).assertExists()
|
||||
assertEquals(0, starts)
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_start)).performClick()
|
||||
assertEquals(1, starts)
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_settling)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_start)).assertDoesNotExist()
|
||||
compose.runOnIdle { state.value = state.value.copy(saved = true, phase = CalibrationPhase.SAVED, progress = 100) }
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_complete)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_done)).performClick()
|
||||
assertEquals(1, recentered)
|
||||
}
|
||||
|
||||
@Test fun cancelAndResetDoNotSilentlyStartOrOverwriteCalibration() {
|
||||
var starts = 0; var cancels = 0; var resets = 0
|
||||
compose.setContent {
|
||||
Column {
|
||||
GlassesCalibrationControls(GlassesCalibrationState(supported = true, saved = true),
|
||||
onStart = { starts++ }, onCancel = { cancels++ }, onReset = { resets++ }, onDone = {})
|
||||
}
|
||||
}
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_open)).performClick()
|
||||
compose.onNodeWithText(text(R.string.common_cancel)).performClick()
|
||||
assertEquals(0, starts)
|
||||
assertTrue(cancels > 0)
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_reset_button)).performClick()
|
||||
compose.onNodeWithText(text(R.string.glasses_calibration_reset_hint)).assertExists()
|
||||
assertEquals(0, resets)
|
||||
compose.onNodeWithText(text(R.string.common_cancel)).performClick()
|
||||
assertEquals(0, resets)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.view.Display
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.testTag
|
||||
import androidx.compose.ui.test.assertIsEnabled
|
||||
import androidx.compose.ui.test.onNodeWithTag
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.glasses.GlassesSettings
|
||||
import com.droiddeck.launcher.glasses.GlassesState
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28])
|
||||
class GlassesPanelTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
|
||||
@Test fun automaticFlatGameOffersVirtualScreenInsteadOfIneffectiveSliders() {
|
||||
show()
|
||||
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertDoesNotExist()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_use)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_flat_hint)).assertExists()
|
||||
compose.onNodeWithTag("setting-glasses-hz").assertExists()
|
||||
compose.onNodeWithTag("setting-glasses-3d-hz").assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun virtualScreenEnablesDimensionsInTwoDimensionalOutput() {
|
||||
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN))
|
||||
compose.onNodeWithTag("glasses-width").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertDoesNotExist()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_flat_hint)).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun automaticSteamVrShowsFieldOfViewAndStereoRefresh() {
|
||||
show(stereo = true, steamVrRunning = true)
|
||||
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
|
||||
compose.onNodeWithTag("setting-glasses-hz").assertDoesNotExist()
|
||||
compose.onNodeWithTag("setting-glasses-3d-hz").assertExists()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_direct_hint)).assertExists()
|
||||
}
|
||||
|
||||
@Test fun mirroredSteamVrIn2dKeepsFieldOfViewAndRecenter() {
|
||||
show(steamVrRunning = true, outputActive = false, tab = null)
|
||||
assertMirroredVrControls()
|
||||
}
|
||||
|
||||
@Test fun mirroredSteamVrIn3dKeepsFieldOfViewAndRecenter() {
|
||||
show(stereo = true, steamVrRunning = true, outputActive = false, tab = null)
|
||||
assertMirroredVrControls()
|
||||
}
|
||||
|
||||
@Test fun mirroredSteamVrVirtualScreenKeepsGeometryAndRecenter() {
|
||||
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN),
|
||||
steamVrRunning = true, outputActive = false, tab = null)
|
||||
compose.onNodeWithTag("glasses-width").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
|
||||
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_no_output)).assertDoesNotExist()
|
||||
}
|
||||
|
||||
private fun assertMirroredVrControls() {
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
|
||||
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_no_output)).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun automaticStereoOutputWithoutSteamVrUsesVirtualScreenDimensions() {
|
||||
show(stereo = true)
|
||||
compose.onNodeWithTag("glasses-width").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertDoesNotExist()
|
||||
compose.onNodeWithTag("setting-glasses-hz").assertDoesNotExist()
|
||||
compose.onNodeWithTag("setting-glasses-3d-hz").assertExists()
|
||||
}
|
||||
|
||||
@Test fun gameWithoutGlassesOutputExplainsWhyGeometryIsUnavailable() {
|
||||
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN), outputActive = false)
|
||||
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertDoesNotExist()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_no_output)).assertExists()
|
||||
}
|
||||
|
||||
@Test fun homeCanConfigureTheNextSessionWithAnExplanation() {
|
||||
show(inSession = false, outputActive = false)
|
||||
compose.onNodeWithTag("glasses-width").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_next_session)).assertExists()
|
||||
}
|
||||
|
||||
@Test fun flatTrackingExplainsTheModeAndHidesInactiveRecenter() {
|
||||
show(tab = GlassesTab.TRACKING)
|
||||
compose.onNodeWithText(text(R.string.glasses_tracking_flat_hint)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun virtualScreenGeometryRemainsAvailableWhenUsbControlDisconnects() {
|
||||
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SCREEN), controlAvailable = false)
|
||||
compose.onNodeWithTag("setting-glasses-picture").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-width").assertIsEnabled()
|
||||
compose.onNodeWithTag("glasses-distance").assertIsEnabled()
|
||||
compose.onNodeWithTag("setting-glasses-hz").assertDoesNotExist()
|
||||
compose.onNodeWithTag("setting-glasses-3d").assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun runningSteamVrFieldOfViewRemainsAvailableWhenUsbControlDisconnects() {
|
||||
show(stereo = true, steamVrRunning = true, controlAvailable = false)
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertIsEnabled()
|
||||
compose.onNodeWithTag("setting-glasses-picture").assertIsEnabled()
|
||||
compose.onNodeWithTag("setting-glasses-3d-hz").assertDoesNotExist()
|
||||
compose.onNodeWithTag("setting-glasses-3d").assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun sideBySideGameWithoutSteamVrDoesNotOfferIneffectiveFieldOfView() {
|
||||
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SBS), stereo = true)
|
||||
compose.onNodeWithTag("glasses-width").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-distance").assertDoesNotExist()
|
||||
compose.onNodeWithTag("glasses-vr-fov").assertDoesNotExist()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_sbs_hint)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.glasses_screen_direct_hint)).assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test fun sideBySideTrackingWithoutSteamVrExplainsTheScopeAndHidesRecenter() {
|
||||
show(settings = GlassesSettings(picture3d = GlassesSettings.PICTURE_SBS), stereo = true,
|
||||
tab = GlassesTab.TRACKING)
|
||||
compose.onNodeWithText(text(R.string.glasses_tracking_sbs_hint)).assertExists()
|
||||
compose.onNodeWithText(text(R.string.cardboard_recenter_button)).assertDoesNotExist()
|
||||
}
|
||||
|
||||
private fun show(
|
||||
settings: GlassesSettings = GlassesSettings(),
|
||||
stereo: Boolean = false,
|
||||
steamVrRunning: Boolean = false,
|
||||
inSession: Boolean = true,
|
||||
outputActive: Boolean = inSession,
|
||||
controlAvailable: Boolean = true,
|
||||
tab: String? = GlassesTab.DISPLAY,
|
||||
) {
|
||||
val state = GlassesState(present = true, connected = controlAvailable, controllable = true,
|
||||
displayId = Display.DEFAULT_DISPLAY + 1, width = if (stereo) 3840 else 1920, height = 1200,
|
||||
model = "VITURE Luma Ultra", sbs = stereo, sbs90 = true)
|
||||
compose.setContent {
|
||||
DroidDeckTheme {
|
||||
Column(Modifier.verticalScroll(rememberScrollState())) {
|
||||
GlassesGroups(rememberMenuHost(), state, settings,
|
||||
track = { key ->
|
||||
when (key) {
|
||||
"glasses-width", "glasses-distance", "glasses-vr-fov" -> Modifier.testTag(key)
|
||||
else -> Modifier
|
||||
}
|
||||
}, onRecenter = {},
|
||||
inSession = inSession, outputActive = outputActive,
|
||||
steamVrRunning = steamVrRunning, tab = tab)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun text(id: Int) = RuntimeEnvironment.getApplication().getString(id)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.ui.test.assertCountEquals
|
||||
import androidx.compose.ui.test.onAllNodesWithTag
|
||||
import androidx.compose.ui.test.onNodeWithTag
|
||||
import androidx.compose.ui.test.performClick
|
||||
import com.droiddeck.launcher.handtracking.TrackingCameras
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33], qualifiers = "w960dp-h540dp-land", application = android.app.Application::class)
|
||||
class HandCameraPromptTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
|
||||
@Test fun bothCamerasOfferAChoiceWithTheBackOnePicked() {
|
||||
var allowed: String? = "unset"
|
||||
compose.setContent {
|
||||
DroidDeckTheme { HandCameraPrompt(setOf(TrackingCameras.REAR, TrackingCameras.FRONT), onAllow = { allowed = it }, onNotNow = {}) }
|
||||
}
|
||||
compose.onNodeWithTag("hand-camera-rear").assertExists()
|
||||
compose.onNodeWithTag("hand-camera-allow").performClick()
|
||||
assertEquals(TrackingCameras.REAR, allowed)
|
||||
}
|
||||
|
||||
@Test fun theFrontCameraCanBeChosen() {
|
||||
var allowed: String? = "unset"
|
||||
compose.setContent {
|
||||
DroidDeckTheme { HandCameraPrompt(setOf(TrackingCameras.REAR, TrackingCameras.FRONT), onAllow = { allowed = it }, onNotNow = {}) }
|
||||
}
|
||||
compose.onNodeWithTag("hand-camera-front").performClick()
|
||||
compose.onNodeWithTag("hand-camera-allow").performClick()
|
||||
assertEquals(TrackingCameras.FRONT, allowed)
|
||||
}
|
||||
|
||||
@Test fun aSingleCameraIsUsedWithoutAsking() {
|
||||
var allowed: String? = "unset"
|
||||
compose.setContent {
|
||||
DroidDeckTheme { HandCameraPrompt(setOf(TrackingCameras.REAR), onAllow = { allowed = it }, onNotNow = {}) }
|
||||
}
|
||||
compose.onAllNodesWithTag("hand-camera-rear").assertCountEquals(0)
|
||||
compose.onAllNodesWithTag("hand-camera-front").assertCountEquals(0)
|
||||
compose.onNodeWithTag("hand-camera-allow").performClick()
|
||||
assertEquals(null, allowed)
|
||||
}
|
||||
|
||||
@Test fun notNowDeclines() {
|
||||
var declined = false
|
||||
compose.setContent {
|
||||
DroidDeckTheme { HandCameraPrompt(setOf(TrackingCameras.FRONT), onAllow = {}, onNotNow = { declined = true }) }
|
||||
}
|
||||
compose.onNodeWithTag("hand-camera-not-now").performClick()
|
||||
assertEquals(true, declined)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.test.assertCountEquals
|
||||
import androidx.compose.ui.test.assertIsEnabled
|
||||
import androidx.compose.ui.test.assertIsNotEnabled
|
||||
import androidx.compose.ui.test.onAllNodesWithTag
|
||||
import androidx.compose.ui.test.onAllNodesWithText
|
||||
import androidx.compose.ui.test.onNodeWithTag
|
||||
import androidx.compose.ui.test.performClick
|
||||
import com.droiddeck.launcher.glasses.Glasses
|
||||
import com.droiddeck.launcher.glasses.GlassesState
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], qualifiers = "w1200dp-h752dp-land", application = android.app.Application::class)
|
||||
class SteamHomeHeadsetTest {
|
||||
@get:Rule val compose = createRobolectricComposeRule()
|
||||
private var vrStarts = 0
|
||||
private val connected = GlassesState(present = true, model = "VITURE Pro XR", controllable = true, connected = true,
|
||||
displayId = 2, width = 1920, height = 1080, hz = 60f)
|
||||
|
||||
@After fun unplug() { headset(GlassesState()) }
|
||||
|
||||
@Test fun nothingShowsUntilAHeadsetIsConnected() {
|
||||
show()
|
||||
for (state in listOf(GlassesState(), GlassesState(present = true, displayId = 2, width = 1920, height = 1080),
|
||||
connected.copy(connected = false, permission = Glasses.Permission.ASKING))) {
|
||||
headset(state)
|
||||
settle()
|
||||
compose.onAllNodesWithTag("play-steamvr").assertCountEquals(0)
|
||||
compose.onAllNodesWithTag("drop-open").assertCountEquals(0)
|
||||
}
|
||||
compose.onAllNodesWithTag("glasses-open").assertCountEquals(0)
|
||||
}
|
||||
|
||||
@Test fun aConnectedHeadsetGetsPlaySteamVrWithItsSettingsBadge() {
|
||||
headset(connected)
|
||||
show()
|
||||
compose.onAllNodesWithTag("glasses-open").assertCountEquals(0)
|
||||
compose.onNodeWithTag("play-steamvr").assertIsEnabled().performClick()
|
||||
assertEquals(1, vrStarts)
|
||||
compose.onAllNodesWithText("VITURE PRO XR").assertCountEquals(0)
|
||||
compose.onNodeWithTag("drop-open").performClick()
|
||||
settle()
|
||||
compose.onNodeWithTag("drop-close").assertExists()
|
||||
compose.onAllNodesWithText("VITURE PRO XR").assertCountEquals(1)
|
||||
compose.onNodeWithTag("drop-close").performClick()
|
||||
settle()
|
||||
compose.onAllNodesWithTag("drop-close").assertCountEquals(0)
|
||||
}
|
||||
|
||||
@Test fun unpluggingClosesTheSheetAndHidesTheButton() {
|
||||
headset(connected)
|
||||
show()
|
||||
compose.onNodeWithTag("drop-open").performClick()
|
||||
settle()
|
||||
headset(GlassesState())
|
||||
settle()
|
||||
compose.onAllNodesWithTag("drop-close").assertCountEquals(0)
|
||||
compose.onAllNodesWithTag("play-steamvr").assertCountEquals(0)
|
||||
}
|
||||
|
||||
@Test fun aMomentWithoutAPictureOnlyPausesPlaySteamVr() {
|
||||
headset(connected.copy(displayId = android.view.Display.INVALID_DISPLAY))
|
||||
show()
|
||||
compose.onNodeWithTag("play-steamvr").assertIsNotEnabled()
|
||||
compose.onNodeWithTag("drop-open").assertExists()
|
||||
}
|
||||
|
||||
private fun headset(state: GlassesState) {
|
||||
val field = Glasses::class.java.getDeclaredField("_state").apply { isAccessible = true }
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
(field.get(Glasses) as MutableStateFlow<GlassesState>).value = state
|
||||
}
|
||||
|
||||
private fun settle() = compose.mainClock.advanceTimeBy(2_000)
|
||||
|
||||
private fun show() {
|
||||
compose.mainClock.autoAdvance = false
|
||||
compose.setContent {
|
||||
DroidDeckTheme {
|
||||
CompositionLocalProvider(LocalFrontFocus provides FrontFocus()) {
|
||||
FocusGlideHost {
|
||||
SteamHome(FrontEndState(
|
||||
installed = "r9", ready = true, available = null, busy = false, stage = "", percent = -1,
|
||||
desktopInstalled = true, offlineAccount = null, offline = false, frameGenLabel = "",
|
||||
romsDir = null, logsEnabled = false, steamGames = emptyList(), emulators = emptyList(), running = null,
|
||||
), FrontEndActions(
|
||||
onPlay = {}, onPlaySteamVr = { vrStarts++ }, onPlayDesktopUi = {}, onSteamGame = {}, onDesktop = {}, onEmulator = {},
|
||||
onRom = {}, onResume = {}, onSteamSettings = {}, onDesktopSettings = {},
|
||||
onInstallPackage = {}, onRemovePackage = {}, onRuntime = {}, onFrameGenPick = {},
|
||||
onImportLossless = {}, onProtons = {}, onComponents = {}, onPerformance = {},
|
||||
onRoms = {}, onFiles = {}, onLogs = {}, onOffline = {},
|
||||
onWirelessAdbPair = { _, _, _, _ -> }, onWirelessAdbApply = { _, _, _, _ -> },
|
||||
onSetPhantomProcessLimit = { _, _ -> },
|
||||
), Modifier.fillMaxSize())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
settle()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user