Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/tiny4linux.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ dialoguer = { version = "0.12.0", features = ["fuzzy-select"], optional = true }
[build-dependencies]
tiny4linux_assets = { git = "https://github.com/OpenFoxes/Tiny4Linux-Icons", rev = "57c10f204b75f1c61087fd9568814ee52d0ec036", optional = true }

[dev-dependencies]
test-case = "3.3.1"
assertables = "9.8.2"

[[bin]]
name = "tiny4linux-gui"
path = "src/gui/main.rs"
Expand Down
85 changes: 85 additions & 0 deletions src/libs/camera/command02.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,88 @@ pub fn command02(
.try_into()
.unwrap()
}

#[cfg(test)]
mod tests {
mod unit {
use crate::libs::camera::command02::command02;

#[test]
fn full_builder() {
let command = command02()
.function_group([0x01, 0x02, 0x03, 0x04, 0x05, 0x06])
.sequence_nr([0x10, 0x11])
.checksum([0x20, 0x21])
.command([0x30, 0x31, 0x32, 0x33, 0x34, 0x35])
.appendix([
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
0x4d, 0x4e, 0x4f,
])
.build();

assert_eq!(
command[0..2],
[0xaa, 0x25],
"Command should start with frame id"
);
assert_eq!(command[2..4], [0x10, 0x11], "Sequence number should be set");
assert_eq!(command[4..6], [0x0c, 0x00], "Segment size should be fix");
assert_eq!(command[6..8], [0x20, 0x21], "Checksum should be set");
assert_eq!(
command[8..14],
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06],
"Function group should be set"
);
assert_eq!(
command[14..20],
[0x30, 0x31, 0x32, 0x33, 0x34, 0x35],
"Command should be set"
);
assert_eq!(
command[20..36],
[
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
0x4d, 0x4e, 0x4f
],
"Appendix should be set"
);
}

#[test]
fn minimal_builder() {
let command = command02()
.function_group([0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5])
.sequence_nr([0xb0, 0xb1])
.checksum([0xc0, 0xc1])
.command([0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5])
.build();

assert_eq!(
command[0..2],
[0xaa, 0x25],
"Command should start with frame id"
);
assert_eq!(command[2..4], [0xb0, 0xb1], "Sequence number should be set");
assert_eq!(command[4..6], [0x0c, 0x00], "Segment size should be fix");
assert_eq!(command[6..8], [0xc0, 0xc1], "Checksum should be set");
assert_eq!(
command[8..14],
[0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5],
"Function group should be set"
);
assert_eq!(
command[14..20],
[0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5],
"Command should be set"
);
assert_eq!(
command[20..36],
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
],
"Appendix should be empty"
);
}
}
}
21 changes: 21 additions & 0 deletions src/libs/camera/commands/ai_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,24 @@ impl AIModeCommand {
})
}
}

#[cfg(test)]
mod tests {
use crate::{AIMode, AIModeCommand};
use test_case::test_case;

#[test_case(AIMode::NoTracking, [0x16, 0x02, 0x00, 0x00]; "no tracking")]
#[test_case(AIMode::NormalTracking, [0x16, 0x02, 0x02, 0x00]; "normal tracking")]
#[test_case(AIMode::UpperBody, [0x16, 0x02, 0x02, 0x01]; "upper body")]
#[test_case(AIMode::DeskMode, [0x16, 0x02, 0x05, 0x00]; "desk mode")]
#[test_case(AIMode::Whiteboard, [0x16, 0x02, 0x04, 0x00]; "whiteboard")]
#[test_case(AIMode::Group, [0x16, 0x02, 0x01, 0x00]; "group")]
#[test_case(AIMode::Hand, [0x16, 0x02, 0x03, 0x00]; "hand")]
#[test_case(AIMode::CloseUp, [0x16, 0x02, 0x02, 0x02]; "close up")]
#[test_case(AIMode::Headless, [0x16, 0x02, 0x02, 0x03]; "headless")]
#[test_case(AIMode::LowerBody, [0x16, 0x02, 0x02, 0x04]; "lower body")]
#[test_case(AIMode::Unknown, [0x16, 0x02, 0x00, 0x00]; "unknown")]
fn ai_mode(mode: AIMode, expected: [u8; 4]) {
assert_eq!(AIModeCommand::build(mode).unwrap(), expected)
}
}
35 changes: 35 additions & 0 deletions src/libs/camera/commands/exposure_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,38 @@ impl ExposureModeCommand {
}
}
}

#[cfg(test)]
mod tests {
mod unit {
use crate::{ExposureMode, ExposureModeCommand};
use assertables::{assert_none, assert_some};
use test_case::test_case;

#[test]
fn manual_exposure_mode() {
let exposure_command = ExposureModeCommand::build(ExposureMode::Manual);

assert_none!(
exposure_command,
"manual exposure mode should not set a exposure mode command"
)
}

#[test_case(ExposureMode::Global, [0x03, 0x01, 0x00]; "global automatic exposure should be set")]
#[test_case(ExposureMode::Face, [0x03, 0x01, 0x01]; "face automatic exposure should be set")]
fn automatic_exposure_modes(mode: ExposureMode, expected: [u8; 3]) {
let exposure_command = ExposureModeCommand::build(mode);

assert_some!(
exposure_command,
"automatic exposure mode should set a exposure mode command"
);
assert_eq!(
exposure_command.unwrap(),
expected,
"correct command is expected"
)
}
}
}
59 changes: 59 additions & 0 deletions src/libs/camera/commands/exposure_mode_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,62 @@ impl ExposureModeTypeCommand {
.build()
}
}

#[cfg(test)]
mod tests {
use crate::{ExposureMode, ExposureModeTypeCommand};
use test_case::test_case;

#[test]
fn manual_exposure_mode() {
let exposure_command = ExposureModeTypeCommand::build(ExposureMode::Manual);

assert_eq!(
exposure_command[8..14],
[0x0a, 0x02, 0x82, 0x29, 0x05, 0x00],
"Function group should be set for exposure mode type"
);
assert_eq!(
exposure_command[2..4],
[0x16, 0x00],
"Sequence number should be set"
);
assert_eq!(
exposure_command[6..8],
[0x58, 0x91],
"Checksum should be set"
);
assert_eq!(
exposure_command[14..20],
[0xb2, 0xaf, 0x02, 0x04, 0x00, 0x00],
"Command should be set"
);
}

#[test_case(ExposureMode::Global; "global automatic exposure should be set")]
#[test_case(ExposureMode::Face; "face automatic exposure should be set")]
fn automatic_exposure_modes(mode: ExposureMode) {
let exposure_command = ExposureModeTypeCommand::build(mode);

assert_eq!(
exposure_command[8..14],
[0x0a, 0x02, 0x82, 0x29, 0x05, 0x00],
"Function group should be set for exposure mode type"
);
assert_eq!(
exposure_command[2..4],
[0x15, 0x00],
"Sequence number should be set"
);
assert_eq!(
exposure_command[6..8],
[0xa8, 0x9e],
"Checksum should be set"
);
assert_eq!(
exposure_command[14..20],
[0xf9, 0x27, 0x01, 0x32, 0x00, 0x00],
"Command should be set"
);
}
}
51 changes: 51 additions & 0 deletions src/libs/camera/commands/goto_preset_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,54 @@ impl GotoPresetPositionCommand {
.build())
}
}

#[cfg(test)]
mod tests {
use crate::GotoPresetPositionCommand;
use assertables::{assert_err, assert_ok};
use test_case::test_case;

#[test_case(0, [0x20, 0x00], [0x6b, 0xdc], [0xd6, 0xfb, 0x00, 0x00, 0x00, 0x00]; "preset position 1 (id 0)")]
#[test_case(1, [0x1a, 0x00], [0x4b, 0x03], [0xeb, 0x2a, 0x01, 0x00, 0x00, 0x00]; "preset position 2 (id 1)")]
#[test_case(2, [0x26, 0x00], [0x8b, 0xc3], [0xaf, 0x19, 0x02, 0x00, 0x00, 0x00]; "preset position 3 (id 2)")]
fn valid_preset_position(
position: i8,
sequence_nr: [u8; 2],
checksum: [u8; 2],
command: [u8; 6],
) {
let position_command_option = GotoPresetPositionCommand::build(position);

assert_ok!(
&position_command_option,
"preset position command should be built"
);

let position_command = position_command_option.unwrap();
assert_eq!(
position_command[8..14],
[0x0a, 0x04, 0xc4, 0x39, 0x14, 0x00],
"Function group should be set for preset position"
);
assert_eq!(
position_command[2..4],
sequence_nr,
"Sequence number should be set"
);
assert_eq!(position_command[6..8], checksum, "Checksum should be set");
assert_eq!(position_command[14..20], command, "Command should be set");
}

#[test_case(-1; "invalid preset position (lower than 0)")]
#[test_case(3; "invalid preset position (greater than 2)")]
#[test_case(i8::MIN; "invalid preset position (minimal number)")]
#[test_case(i8::MAX - 1; "invalid preset position (maximal number - 1)")]
fn invalid_preset_position(invalid_position: i8) {
let position_command_option = GotoPresetPositionCommand::build(invalid_position);

assert_err!(
position_command_option,
"invalid position should return an error"
);
}
}
12 changes: 12 additions & 0 deletions src/libs/camera/commands/hdr_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ impl HdrModeCommand {
}
}
}

#[cfg(test)]
mod tests {
use crate::HdrModeCommand;
use test_case::test_case;

#[test_case(true, [0x01, 0x01, 0x01]; "HDR on")]
#[test_case(false, [0x01, 0x01, 0x00]; "HDR off")]
fn hdr_mode(mode: bool, expected: [u8; 3]) {
assert_eq!(HdrModeCommand::build(mode), expected)
}
}
Loading