use laminapdf_cos::{ObjRef, Object, OpenOptions};
use laminapdf_test_fixtures::classic_with_root;
use super::Visibility::{Hidden, Unknown, Visible};
use super::{OptionalContent, Visibility};
use crate::document::Document;
fn doc(config: &str, extra: &[&str]) -> Document {
let catalog =
format!("<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [3 0 R 4 0 R 5 0 R] /D << {config} >> >> >>");
let mut objects = vec![
catalog.as_str(),
"<< /Type /Pages /Kids [] /Count 0 >>",
"<< /Type /OCG /Name (On) >>",
"<< /Type /OCG /Name (Off) >>",
"<< /Type /OCG /Name (Design) /Intent /Design >>",
"<< /Type /OCG /Name (Unlisted) >>",
];
objects.extend_from_slice(extra);
Document::open(classic_with_root(&objects, ""), OpenOptions::default()).unwrap()
}
fn config(doc: &Document) -> OptionalContent {
doc.optional_content().unwrap().unwrap()
}
fn of(doc: &Document, num: u32) -> Visibility {
config(doc).visibility(doc, &Object::Ref(ObjRef::new(num, 0))).unwrap()
}
const OFF: &str = "/OFF [4 0 R 5 0 R 6 0 R]";
#[test]
fn a_group_is_hidden_when_the_default_configuration_turns_it_off() {
let d = doc(OFF, &[]);
assert_eq!((of(&d, 3), of(&d, 4)), (Visible, Hidden));
let base_off = doc("/BaseState /OFF /ON [3 0 R]", &[]);
assert_eq!((of(&base_off, 3), of(&base_off, 4)), (Visible, Hidden));
assert_eq!(config(&d).off_groups().into_iter().collect::<Vec<_>>(), [4, 5]);
}
#[test]
fn a_group_outside_the_intent_or_the_list_is_unknown() {
let d = doc(OFF, &[]);
assert_eq!((of(&d, 5), of(&d, 6)), (Unknown, Unknown));
let every = doc(&format!("{OFF} /Intent /All"), &[]);
assert_eq!(of(&every, 5), Hidden);
}
#[test]
fn each_policy_decides_a_membership_of_one_on_and_one_off_group() {
let d = doc(
OFF,
&[
"<< /Type /OCMD /OCGs [3 0 R 4 0 R] >>",
"<< /Type /OCMD /OCGs [3 0 R 4 0 R] /P /AllOn >>",
"<< /Type /OCMD /OCGs [3 0 R 4 0 R] /P /AnyOff >>",
"<< /Type /OCMD /OCGs [3 0 R 4 0 R] /P /AllOff >>",
"<< /Type /OCMD /OCGs 4 0 R /P 13 0 R >>",
"<< /Type /OCMD /OCGs [3 0 R 4 0 R] /P /SomeOn >>",
"/AllOn",
"<< /Type /OCMD /OCGs [3 0 R] /P /AnyOff >>",
"<< /Type /OCMD /OCGs [4 0 R] /P /AllOff >>",
],
);
let verdicts: Vec<_> = (7..=12).chain(14..=15).map(|n| of(&d, n)).collect();
assert_eq!(
verdicts,
[Visible, Hidden, Visible, Hidden, Hidden, Unknown, Hidden, Visible]
);
}
#[test]
fn a_membership_with_an_unknown_group_is_decided_only_when_the_policy_needs_no_more() {
let d = doc(
OFF,
&[
"<< /Type /OCMD /OCGs [4 0 R 6 0 R] /P /AllOn >>",
"<< /Type /OCMD /OCGs [4 0 R 6 0 R] /P /AnyOn >>",
"<< /Type /OCMD >>",
],
);
assert_eq!([of(&d, 7), of(&d, 8), of(&d, 9)], [Hidden, Unknown, Visible]);
}
#[test]
fn a_visibility_expression_takes_precedence_over_the_policy() {
let d = doc(
OFF,
&[
"<< /Type /OCMD /OCGs [3 0 R] /VE [/And 3 0 R [/Not 4 0 R]] >>",
"<< /Type /OCMD /OCGs [3 0 R] /VE [/Or 4 0 R [/Not 3 0 R]] >>",
"<< /Type /OCMD /VE [/And 6 0 R 4 0 R] >>",
"<< /Type /OCMD /VE [/Xor 3 0 R 4 0 R] >>",
"<< /Type /OCMD /VE [/Not 3 0 R 4 0 R] >>",
"<< /Type /OCMD /VE 13 0 R >>",
"[/Not 4 0 R]",
],
);
let verdicts: Vec<_> = (7..=12).map(|n| of(&d, n)).collect();
assert_eq!(verdicts, [Visible, Hidden, Hidden, Unknown, Unknown, Visible]);
}
#[test]
fn an_expression_too_deep_or_too_long_is_unknown() {
let deep = format!("{}4 0 R{}", "[/Not ".repeat(40), "]".repeat(40));
let long = format!("[/Or {}]", "4 0 R ".repeat(2_000));
let wide = format!("[{}]", "4 0 R ".repeat(2_000));
let d = doc(
OFF,
&[
&format!("<< /Type /OCMD /VE {deep} >>"),
&format!("<< /Type /OCMD /VE {long} >>"),
&format!("<< /Type /OCMD /OCGs {wide} /P /AllOff >>"),
],
);
assert_eq!([of(&d, 7), of(&d, 8), of(&d, 9)], [Unknown, Unknown, Unknown]);
}
#[test]
fn names_are_read_through_references() {
let d = doc(OFF, &["<< /Type 8 0 R /OCGs [4 0 R] >>", "/OCMD"]);
assert_eq!(of(&d, 7), Hidden);
}
#[test]
fn optional_content_off_counts_only_what_is_hidden() {
let d = doc(OFF, &[]);
let off = |n| d.optional_content_off(&Object::Ref(ObjRef::new(n, 0))).unwrap();
assert_eq!((off(3), off(4), off(6)), (false, true, false));
}
#[test]
fn a_group_a_usage_application_names_is_unknown() {
let d = doc(
&format!("{OFF} /AS [<< /Event /View /OCGs [4 0 R 3 0 R] /Category [/View] >>]"),
&[],
);
assert_eq!((of(&d, 3), of(&d, 4)), (Unknown, Unknown));
}
#[test]
fn a_group_both_on_and_off_is_unknown() {
let d = doc("/BaseState /OFF /ON [3 0 R 4 0 R] /OFF [4 0 R]", &[]);
assert_eq!((of(&d, 3), of(&d, 4)), (Visible, Unknown));
assert!(!config(&d).off_groups().contains(&3));
}