Files
oak-editor/crates/oakaudio/tests/handle_test.rs
T
Mike-Solar 5afa95c80e feat(render,app): footage decode lands (M12 P0) + UI density pass
engine:
- oakrender eval footage hook decodes via oakcodec (JobSpec::Footage
  carries filename/stream); ticket/ffi/manager wiring, real-media
  decode test with programmatically generated MPEG-2
- oakrender bridge/codec.rs + node.rs: direct oakcodec/oaknode calls;
  the crate's dlsym module is gone (project_deep_copy/sync_copy remain
  documented always-fail stubs — never implemented in oaknode)
- oakaudio waveform/decoder path adjustments for the decode hook

app (gpui + gpui_widgets):
- menu bar scrubbing: hovering another top-level title while a menu is
  open switches to it; popup width is content-aware (CJK-aware) instead
  of fixed 160px
- density pass: window rem 16 -> 14px, menu rows 26 -> 22px, dock tabs
  32 -> 26px, viewer transport tightened
- open/import/save-as use the native platform file dialogs
  (prompt_for_paths / prompt_for_new_path; multi-select import);
  MockEngine records imported footage for tests
- project explorer Tree/Icons toggle is localized (explorer.tree /
  explorer.icons widget keys)
2026-08-13 18:16:10 +08:00

127 lines
4.3 KiB
Rust

// Oak Video Editor - Non-Linear Video Editor
// Copyright (C) 2026 Oak Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//! Handle plumbing contract tests (handle.rs).
use oakaudio::error::{OAKAUDIO_E_FAILED, OAKAUDIO_E_INVALID, OAKAUDIO_OK};
use std::sync::{Mutex, MutexGuard};
use oakaudio::handle::{alive_count, get, guard, guard_handle, make_borrowed, make_owned, CHandle};
/// Serializes the tests: the crate's live-object ledger is process-global,
/// so parallel tests in this file would race the count assertions.
fn ledger_lock() -> MutexGuard<'static, ()> {
static LOCK: Mutex<()> = Mutex::new(());
LOCK.lock().unwrap_or_else(|e| e.into_inner())
}
/// make_owned starts at refcount 1; get returns a typed view; dropping the
/// handle decrements to 0.
#[test]
fn owned_lifecycle() {
let _l = ledger_lock();
let before = alive_count();
let mut h = make_owned(42u32);
assert!(!h.is_null());
assert_eq!(alive_count(), before + 1);
// SAFETY: `h` boxes a u32 created above.
let v = unsafe { get::<u32>(&h) }.unwrap();
assert_eq!(*v, 42);
// addref/release round-trip through the function pointers.
let addref = h.addref.unwrap();
let release = h.release.unwrap();
// SAFETY: the ctx was created by make_owned.
unsafe { addref(h.ctx) };
assert_eq!(alive_count(), before + 1); // count unchanged, still 1 box
unsafe { release(h.ctx) };
// Dropping the box (release to zero) decrements the ledger.
let release = h.release.unwrap();
// SAFETY: h.ctx is the box created above; refcount is 1.
unsafe { release(h.ctx) };
h.ctx = std::ptr::null_mut();
assert_eq!(alive_count(), before);
}
/// make_borrowed creates a borrow-only handle whose release frees only the
/// box, never the underlying object.
#[test]
fn borrowed_release() {
let mut value = Box::new(7i32);
// SAFETY: `value` outlives the handle; the ctx points directly at the
// box (not a RefBox), so it is read through the raw pointer.
let mut h = unsafe { make_borrowed(&mut *value) };
assert!(!h.is_null());
// SAFETY: `h.ctx` points at the box created above.
let v = unsafe { &*(h.ctx as *const i32) };
assert_eq!(*v, 7);
// release is a no-op: the box still lives.
let release = h.release.unwrap();
// SAFETY: noop_ref for borrowed handles.
unsafe { release(h.ctx) };
assert_eq!(*value, 7);
h.ctx = std::ptr::null_mut();
}
/// CHandle::null() yields an empty handle; guard over an Ok(()) returns
/// OAKAUDIO_OK (0) and guard_handle over Ok returns a valid handle.
#[test]
fn null_and_guard_ok() {
let null = CHandle::null();
assert!(null.is_null());
// The shared `null()` stamps no ABI version (single-lib unification).
assert_eq!(null.abi_version, 0);
assert_eq!(guard(|| Ok(())), OAKAUDIO_OK);
let h = guard_handle(|| Ok(make_owned(1u32)));
assert!(!h.is_null());
// Release it so the alive ledger returns to baseline (tests share the
// process-wide ledger and run in parallel).
// SAFETY: `h.ctx` is the box created above; refcount is 1.
unsafe { (h.release.unwrap())(h.ctx) };
}
/// guard maps an Err to the negative error code without panicking; a
/// panicking body is caught and returns a failure code rather than
/// unwinding across the FFI boundary.
#[test]
fn guard_error_and_panic() {
assert_eq!(
guard(|| Err(oakaudio::error::Error::Invalid)),
OAKAUDIO_E_INVALID
);
assert_eq!(guard(|| Err(oakaudio::error::Error::State)), -60002);
assert_eq!(
guard(|| Err(oakaudio::error::Error::Failed("x".to_string()))),
-60003
);
assert_eq!(guard(|| Err(oakaudio::error::Error::NotFound)), -60004);
assert_eq!(guard(|| Err(oakaudio::error::Error::NoMem)), -60005);
assert_eq!(guard(|| panic!("boom")), OAKAUDIO_E_FAILED);
let h = guard_handle(|| Err(oakaudio::error::Error::Invalid));
assert!(h.is_null());
let h2 = guard_handle(|| panic!("boom"));
assert!(h2.is_null());
}