# Path: _stbt/transition.py
# class StrictDiff(FrameDiffer):
#     """The original `press_and_wait` algorithm."""
# 
#     def __init__(self, initial_frame, region=Region.ALL, mask=None,
#                  min_size=None):
#         super(StrictDiff, self).__init__(initial_frame, region, mask, min_size)
#         if self.mask is not None:
#             # We need 3 channels to match `frame`.
#             self.mask = cv2.cvtColor(self.mask, cv2.COLOR_GRAY2BGR)
# 
#     def diff(self, frame):
#         absdiff = cv2.absdiff(crop(self.prev_frame, self.region),
#                               crop(frame, self.region))
#         if self.mask is not None:
#             absdiff = cv2.bitwise_and(absdiff, self.mask, absdiff)
# 
#         diffs_found = False
#         out_region = None
#         maxdiff = numpy.max(absdiff)
#         if maxdiff > 20:
#             diffs_found = True
#             big_diffs = absdiff > 20
#             out_region = pixel_bounding_box(big_diffs)
#             _ddebug("found %s diffs above 20 (max %s) in %r", frame,
#                     numpy.count_nonzero(big_diffs), maxdiff, out_region)
#         elif maxdiff > 0:
#             small_diffs = absdiff > 5
#             small_diffs_count = numpy.count_nonzero(small_diffs)
#             if small_diffs_count > 50:
#                 diffs_found = True
#                 out_region = pixel_bounding_box(small_diffs)
#                 _ddebug("found %s diffs <= %s in %r", frame, small_diffs_count,
#                         maxdiff, out_region)
#             else:
#                 _ddebug("only found %s diffs <= %s", frame, small_diffs_count,
#                         maxdiff)
# 
#         if diffs_found:
#             # Undo crop:
#             out_region = out_region.translate(self.region)
# 
#         motion = diffs_found and (
#             self.min_size is None or
#             (out_region.width >= self.min_size[0] and
#              out_region.height >= self.min_size[1]))
# 
#         if motion:
#             # Only update the reference frame if we found differences. This
#             # makes the algorithm more sensitive to slow motion.
#             self.prev_frame = frame
# 
#         result = MotionResult(getattr(frame, "time", None), motion,
#                               out_region, frame)
#         return result


from collections import namedtuple
from numpy import isclose
from _stbt.transition import StrictDiff
import cv2
import numpy
import pytest
import stbt_core as stbt

def test_wait_for_transition_to_end(diff_algorithm):
    _stbt = FakeDeviceUnderTest()

    transition = stbt.wait_for_transition_to_end(
        timeout_secs=0.2, stable_secs=0.1, _dut=_stbt)

    _stbt.press("ball")
    transition = stbt.wait_for_transition_to_end(
        timeout_secs=0.2, stable_secs=0.1, _dut=_stbt)
    print(transition)
    assert not transition
    assert transition.status == stbt.TransitionStatus.STABLE_TIMEOUT


def test_press_and_wait_timestamps(diff_algorithm):
    _stbt = FakeDeviceUnderTest(
        ["black"] * 10 + ["fade-to-white"] * 2 + ["white"] * 100)

    transition = stbt.press_and_wait("fade-to-white", _dut=_stbt)
    print(transition)
    assert transition
    assert isclose(transition.animation_start_time,
                   transition.press_time + 0.40)
    assert isclose(transition.duration, 0.48)
    assert isclose(transition.end_time, transition.animation_start_time + 0.08)
    assert isclose(transition.animation_duration, 0.08)


def test_that_strictdiff_ignores_a_few_scattered_small_differences():

# Path: _stbt/transition.py
# class StrictDiff(FrameDiffer):
#     """The original `press_and_wait` algorithm."""
# 
#     def __init__(self, initial_frame, region=Region.ALL, mask=None,
#                  min_size=None):
#         super(StrictDiff, self).__init__(initial_frame, region, mask, min_size)
#         if self.mask is not None:
#             # We need 3 channels to match `frame`.
#             self.mask = cv2.cvtColor(self.mask, cv2.COLOR_GRAY2BGR)
# 
#     def diff(self, frame):
#         absdiff = cv2.absdiff(crop(self.prev_frame, self.region),
#                               crop(frame, self.region))
#         if self.mask is not None:
#             absdiff = cv2.bitwise_and(absdiff, self.mask, absdiff)
# 
#         diffs_found = False
#         out_region = None
#         maxdiff = numpy.max(absdiff)
#         if maxdiff > 20:
#             diffs_found = True
#             big_diffs = absdiff > 20
#             out_region = pixel_bounding_box(big_diffs)
#             _ddebug("found %s diffs above 20 (max %s) in %r", frame,
#                     numpy.count_nonzero(big_diffs), maxdiff, out_region)
#         elif maxdiff > 0:
#             small_diffs = absdiff > 5
#             small_diffs_count = numpy.count_nonzero(small_diffs)
#             if small_diffs_count > 50:
#                 diffs_found = True
#                 out_region = pixel_bounding_box(small_diffs)
#                 _ddebug("found %s diffs <= %s in %r", frame, small_diffs_count,
#                         maxdiff, out_region)
#             else:
#                 _ddebug("only found %s diffs <= %s", frame, small_diffs_count,
#                         maxdiff)
# 
#         if diffs_found:
#             # Undo crop:
#             out_region = out_region.translate(self.region)
# 
#         motion = diffs_found and (
#             self.min_size is None or
#             (out_region.width >= self.min_size[0] and
#              out_region.height >= self.min_size[1]))
# 
#         if motion:
#             # Only update the reference frame if we found differences. This
#             # makes the algorithm more sensitive to slow motion.
#             self.prev_frame = frame
# 
#         result = MotionResult(getattr(frame, "time", None), motion,
#                               out_region, frame)
#         return result


# Path: tests/test_transition.py
from collections import namedtuple
from numpy import isclose
from _stbt.transition import StrictDiff
import cv2
import numpy
import pytest
import stbt_core as stbt


def test_wait_for_transition_to_end(diff_algorithm):
    _stbt = FakeDeviceUnderTest()

    transition = stbt.wait_for_transition_to_end(
        timeout_secs=0.2, stable_secs=0.1, _dut=_stbt)

    _stbt.press("ball")
    transition = stbt.wait_for_transition_to_end(
        timeout_secs=0.2, stable_secs=0.1, _dut=_stbt)
    print(transition)
    assert not transition
    assert transition.status == stbt.TransitionStatus.STABLE_TIMEOUT


def test_press_and_wait_timestamps(diff_algorithm):
    _stbt = FakeDeviceUnderTest(
        ["black"] * 10 + ["fade-to-white"] * 2 + ["white"] * 100)

    transition = stbt.press_and_wait("fade-to-white", _dut=_stbt)
    print(transition)
    assert transition
    assert isclose(transition.animation_start_time,
                   transition.press_time + 0.40)
    assert isclose(transition.duration, 0.48)
    assert isclose(transition.end_time, transition.animation_start_time + 0.08)
    assert isclose(transition.animation_duration, 0.08)


def test_that_strictdiff_ignores_a_few_scattered_small_differences():

    differ = StrictDiff(initial_frame=stbt.load_image("2px-different-1.png"),