<commit_before># Run with ./run-tests.sh

test_wait_for_match() {
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", consecutive_matches=24)
	EOF
    stbt-run -v test.py
}

test_wait_for_match_no_match() {
    cat > test.py <<-EOF
	wait_for_match("$testdir/videotestsrc-bw-flipped.png", timeout_secs=1)
	EOF
    ! stbt-run -v test.py &&
    [ -f screenshot.png ]
}

test_wait_for_match_changing_template() {
    # Tests that we can change the image given to templatematch.
    # Also tests the remote-control infrastructure by using the null control.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", consecutive_matches=24)
	press("MENU")
	wait_for_match("$testdir/videotestsrc-bw.png", consecutive_matches=24)
	press("OK")
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", consecutive_matches=24)
	EOF
    stbt-run -v --control=none test.py
}

test_wait_for_match_nonexistent_template() {
    cat > test.py <<-EOF
	wait_for_match("idontexist.png")
	EOF
    ! stbt-run -v test.py
}

test_wait_for_match_noise_threshold_raises_warning() {
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", noise_threshold=0.2)
	EOF
    stbt-run -v test.py 2>&1 | grep 'DeprecationWarning'
}

test_wait_for_match_match_method_param_affects_first_pass() {
    # This works on the fact that match_method="ccorr-normed" registers a
    # first_pass_result greater than 0.80 which is then falsely confirmed as
    # a match, whereas match_method="sqdiff-normed" does not produce a
    # first_pass_result above 0.80 and so the match fails.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-bw-flipped.png",
	    match_parameters=MatchParameters(
	        match_method="ccorr-normed", confirm_method="none"),
	    timeout_secs=1)
	EOF
    stbt-run -v test.py || return

    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-bw-flipped.png",
	    match_parameters=MatchParameters(
	        match_method="sqdiff-normed", confirm_method="none"),
	    timeout_secs=1)
	EOF
    ! stbt-run -v test.py
}

test_wait_for_match_match_threshold_param_affects_match() {
    # Confirm_method="none" means that if anything passes the first pass of
    # templatematching, it is considered a positive result. Using this, by
    # using 2 detect_matches with match_thresholds either side of the
    # first_pass_result of this match, we can get one to pass and the other
    # to fail.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-checkers-8.png", timeout_secs=1,
	    match_parameters=MatchParameters(
	        match_threshold=0.8, confirm_method="none"))
	EOF
    ! stbt-run -v test.py || return

    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-checkers-8.png", timeout_secs=1,
	    match_parameters=MatchParameters(
	        match_threshold=0.2, confirm_method="none"))
	EOF
    stbt-run -v test.py
}

test_wait_for_match_confirm_method_none_matches_anything_with_match_threshold_zero() {
    # With match_threshold=0, the first pass is meaningless, and with
    # confirm_method="none", any image with match any source.
    # (In use, this scenario is completely useless).
    cat > test.py <<-EOF
	for img in ['circle-big.png', 'videotestsrc-bw-flipped.png',
	            'videotestsrc-checkers-8.png', 'videotestsrc-gamut.png']:
	    wait_for_match("$testdir/" + img, match_parameters=MatchParameters(
	        match_threshold=0, confirm_method="none"))
	EOF
    stbt-run -v test.py
}

test_wait_for_match_confirm_methods_produce_different_results() {
    local source_pipeline="filesrc location=$testdir/known-fail-source.png ! \
        decodebin2 ! imagefreeze ! ffmpegcolorspace"

    # Expect correct nomatch.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/known-fail-template.png",
	    match_parameters=MatchParameters(confirm_method="normed-absdiff"))
	EOF
    ! stbt-run -v --source-pipeline="$source_pipeline" --control=None test.py \
        || return

    # Expect false match.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/known-fail-template.png",
	    match_parameters=MatchParameters(confirm_method="absdiff"))
	EOF
    stbt-run -v --source-pipeline="$source_pipeline" --control=None test.py
}

test_wait_for_match_erode_passes_affects_match() {
    # This test demonstrates that changing the number of erodePasses
    # can cause incongruent images to match falsely.
    local source_pipeline="filesrc location=$testdir/circle-big.png ! \
        decodebin2 ! imagefreeze ! ffmpegcolorspace"

    cat > test.py <<-EOF
	wait_for_match("$testdir/circle-small.png",
	               match_parameters=MatchParameters(erode_passes=2))
	EOF
    stbt-run -v --source-pipeline="$source_pipeline" --control=none test.py \
        || return

    cat > "$scratchdir/test.py" <<-EOF
	wait_for_match("$testdir/circle-small.png",
	               match_parameters=MatchParameters(erode_passes=1))
	EOF<commit_msg>test_wait_for_match_erode_passes_affects_match: Cleanup: Remove unneeded "$scratchdir"<commit_after># Run with ./run-tests.sh

test_wait_for_match() {
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", consecutive_matches=24)
	EOF
    stbt-run -v test.py
}

test_wait_for_match_no_match() {
    cat > test.py <<-EOF
	wait_for_match("$testdir/videotestsrc-bw-flipped.png", timeout_secs=1)
	EOF
    ! stbt-run -v test.py &&
    [ -f screenshot.png ]
}

test_wait_for_match_changing_template() {
    # Tests that we can change the image given to templatematch.
    # Also tests the remote-control infrastructure by using the null control.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", consecutive_matches=24)
	press("MENU")
	wait_for_match("$testdir/videotestsrc-bw.png", consecutive_matches=24)
	press("OK")
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", consecutive_matches=24)
	EOF
    stbt-run -v --control=none test.py
}

test_wait_for_match_nonexistent_template() {
    cat > test.py <<-EOF
	wait_for_match("idontexist.png")
	EOF
    ! stbt-run -v test.py
}

test_wait_for_match_noise_threshold_raises_warning() {
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-redblue.png", noise_threshold=0.2)
	EOF
    stbt-run -v test.py 2>&1 | grep 'DeprecationWarning'
}

test_wait_for_match_match_method_param_affects_first_pass() {
    # This works on the fact that match_method="ccorr-normed" registers a
    # first_pass_result greater than 0.80 which is then falsely confirmed as
    # a match, whereas match_method="sqdiff-normed" does not produce a
    # first_pass_result above 0.80 and so the match fails.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-bw-flipped.png",
	    match_parameters=MatchParameters(
	        match_method="ccorr-normed", confirm_method="none"),
	    timeout_secs=1)
	EOF
    stbt-run -v test.py || return

    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-bw-flipped.png",
	    match_parameters=MatchParameters(
	        match_method="sqdiff-normed", confirm_method="none"),
	    timeout_secs=1)
	EOF
    ! stbt-run -v test.py
}

test_wait_for_match_match_threshold_param_affects_match() {
    # Confirm_method="none" means that if anything passes the first pass of
    # templatematching, it is considered a positive result. Using this, by
    # using 2 detect_matches with match_thresholds either side of the
    # first_pass_result of this match, we can get one to pass and the other
    # to fail.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-checkers-8.png", timeout_secs=1,
	    match_parameters=MatchParameters(
	        match_threshold=0.8, confirm_method="none"))
	EOF
    ! stbt-run -v test.py || return

    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/videotestsrc-checkers-8.png", timeout_secs=1,
	    match_parameters=MatchParameters(
	        match_threshold=0.2, confirm_method="none"))
	EOF
    stbt-run -v test.py
}

test_wait_for_match_confirm_method_none_matches_anything_with_match_threshold_zero() {
    # With match_threshold=0, the first pass is meaningless, and with
    # confirm_method="none", any image with match any source.
    # (In use, this scenario is completely useless).
    cat > test.py <<-EOF
	for img in ['circle-big.png', 'videotestsrc-bw-flipped.png',
	            'videotestsrc-checkers-8.png', 'videotestsrc-gamut.png']:
	    wait_for_match("$testdir/" + img, match_parameters=MatchParameters(
	        match_threshold=0, confirm_method="none"))
	EOF
    stbt-run -v test.py
}

test_wait_for_match_confirm_methods_produce_different_results() {
    local source_pipeline="filesrc location=$testdir/known-fail-source.png ! \
        decodebin2 ! imagefreeze ! ffmpegcolorspace"

    # Expect correct nomatch.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/known-fail-template.png",
	    match_parameters=MatchParameters(confirm_method="normed-absdiff"))
	EOF
    ! stbt-run -v --source-pipeline="$source_pipeline" --control=None test.py \
        || return

    # Expect false match.
    cat > test.py <<-EOF
	wait_for_match(
	    "$testdir/known-fail-template.png",
	    match_parameters=MatchParameters(confirm_method="absdiff"))
	EOF
    stbt-run -v --source-pipeline="$source_pipeline" --control=None test.py
}

test_wait_for_match_erode_passes_affects_match() {
    # This test demonstrates that changing the number of erodePasses
    # can cause incongruent images to match falsely.
    local source_pipeline="filesrc location=$testdir/circle-big.png ! \
        decodebin2 ! imagefreeze ! ffmpegcolorspace"

    cat > test.py <<-EOF
	wait_for_match("$testdir/circle-small.png",
	               match_parameters=MatchParameters(erode_passes=2))
	EOF
    stbt-run -v --source-pipeline="$source_pipeline" --control=none test.py \
        || return

    cat > test.py <<-EOF
	wait_for_match("$testdir/circle-small.png",
	               match_parameters=MatchParameters(erode_passes=1))
	EOF
{'fraction_non_alphanumeric': 0.11195143795402528, 'fraction_numerical': 0.007363916807642552, 'mean_word_length': 3.7271872060206963, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4470281', 'n_tokens_mistral': 3548, 'n_tokens_neox': 3486, 'n_words': 695}