def test_press_and_wait_stable_timeout(): transition = stbt.press_and_wait("ball", timeout_secs=0.2, stable_secs=0.1, _dut=FakeDeviceUnderTest()) print transition assert not transition assert transition.status == stbt.TransitionStatus.STABLE_TIMEOUT transition = stbt.press_and_wait("ball", stable_secs=0, _dut=FakeDeviceUnderTest()) print transition assert transition assert transition.status == stbt.TransitionStatus.COMPLETE
def test_press_and_wait(): _stbt = FakeDeviceUnderTest() transition = stbt.press_and_wait("white", stable_secs=0.1, _dut=_stbt) print transition assert transition assert transition.status == stbt.TransitionStatus.COMPLETE assert transition.press_time < transition.animation_start_time assert transition.animation_start_time == transition.end_time assert transition.duration < 0.01 # excludes stable period assert transition.frame.min() == 255 transition = stbt.press_and_wait("fade-to-black", stable_secs=0.1, _dut=_stbt) print transition assert transition assert transition.status == stbt.TransitionStatus.COMPLETE assert transition.animation_start_time < transition.end_time assert transition.frame.max() == 0
def test_press_and_wait_timestamps(): _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, rtol=0, atol=0.01) assert isclose(transition.duration, 0.48, rtol=0, atol=0.01) assert isclose(transition.end_time, transition.animation_start_time + 0.08) assert isclose(transition.animation_duration, 0.08)
def select(self, menu, *submenus): """Select the specified menu item (and sub-menus, if specified). Example:: Menu().select("Settings", "Network", "About") """ assert self.is_visible original_value = self.selection f = self # FrameObject of current video-frame being processed target = menu for _ in range(20): assert f.is_visible current_value = f.selection if current_value == target: stbt.debug("Menu.select: Found %s" % target) if submenus: transition = stbt.press_and_wait("KEY_OK") assert transition f = Menu(frame=transition.frame) assert f.selection != current_value return f.select(submenus[0], *submenus[1:]) else: return f stbt.debug("Menu.select: target=%r, current=%r, " "going to press KEY_DOWN" % (target, current_value)) transition = stbt.press_and_wait("KEY_DOWN") assert transition f = Menu(frame=transition.frame) assert f.is_visible assert f.selection != current_value assert f.selection != original_value, ( "Menu.select wrapped around to %r without finding %r" % (original_value, target))
def navigate_to(self, target, page, verify_every_keypress=False): """Move the selection to the specified character. Note that this won't press KEY_OK on the target, it only moves the selection there. :param str target: The key or button to navigate to, for example "A", " ", or "CLEAR". :param stbt.FrameObject page: See ``enter_text``. :param bool verify_every_keypress: See ``enter_text``. :returns: A new FrameObject instance of the same type as ``page``, reflecting the device-under-test's new state after the navigation completed. """ if target not in self.G: raise ValueError("'%s' isn't in the keyboard" % (target, )) assert page, "%s page isn't visible" % type(page).__name__ deadline = time.time() + self.navigate_timeout current = _selection_to_text(page.selection) while current != target: assert time.time() < deadline, ( "Keyboard.navigate_to: Didn't reach %r after %s seconds" % (target, self.navigate_timeout)) keys = list(_keys_to_press(self.G, current, target)) log.info("Keyboard: navigating from %s to %s by pressing %r", current, target, keys) if not verify_every_keypress: for k, _ in keys[:-1]: stbt.press(k) keys = keys[-1:] # only verify the last one for key, possible_targets in keys: assert stbt.press_and_wait(key, mask=self.mask, stable_secs=0.5) page = page.refresh() assert page, "%s page isn't visible" % type(page).__name__ current = _selection_to_text(page.selection) assert current in possible_targets, \ "Expected to see %s after pressing %s, but saw %r" % ( _join_with_commas( [repr(x) for x in sorted(possible_targets)], last_one=" or "), key, current) return page
def navigate_to(self, target, page): """Move the selection to the specified character. Note that this won't press KEY_OK on the target, it only moves the selection there. :param str target: The key or button to navigate to, for example "A", " ", or "CLEAR". :param stbt.FrameObject page: See ``enter_text``. :returns: A new FrameObject instance of the same type as ``page``, reflecting the device-under-test's new state after the navigation completed. """ if target not in self.G: raise ValueError("'%s' isn't in the keyboard" % (target, )) deadline = time.time() + self.navigate_timeout current = _selection_to_text(page.selection) while current != target: assert page, "%s page isn't visible" % type(page).__name__ assert time.time() < deadline, ( "Keyboard.navigate_to: Didn't reach %r after %s seconds" % (target, self.navigate_timeout)) keys = list(_keys_to_press(self.G, current, target)) log.info("Keyboard: navigating from %s to %s by pressing %r", current, target, keys) for k in keys[:-1]: stbt.press(k) assert stbt.press_and_wait(keys[-1], mask=self.mask, stable_secs=0.5) page = page.refresh() current = _selection_to_text(page.selection) return page
def soak_remote_control(key_next="KEY_RIGHT", key_prev="KEY_LEFT", region=stbt.Region.ALL, mask=None, count=100): """ Soaks a remote control by pressing KEY_LEFT and KEY_RIGHT keys and making sure they have an effect each time. We check that every time we press KEY_LEFT and KEY_RIGHT we get back to where we started. This should be sufficient to detect missed keypresses and intermittent double presses. Use ``region`` and/or ``mask`` to exclude parts of the page that might change from press to press, such as picture-in-picture video or clocks. """ if mask is None: m = stbt.crop( numpy.ones(stbt.get_frame().shape[:2], dtype=numpy.uint8) * 255, region) else: m = stbt.load_image(mask, cv2.IMREAD_GRAYSCALE) # Get in a position where we'll be able to press left later. Note: no # assertion - it's ok if we can't move right right now stbt.press(key_next) print(region, m.shape) stbt.press_and_wait(key_next, region=region, mask=m) # pylint:disable=stbt-unused-return-value # Grab reference images of the left and right position. We need these to # check that we've actually moved, and haven't moved too far. We add an # alpha channel (transparency) using the user-supplied mask. right_template = numpy.append(stbt.crop(stbt.get_frame(), region), m[:, :, numpy.newaxis], axis=2) cv2.imwrite("right_template.png", right_template) if stbt.press_and_wait(key_prev, region=region, mask=m).status == \ stbt.TransitionStatus.START_TIMEOUT: raise RuntimeError("No movement after pressing %r during setup" % (key_prev, )) if stbt.match(right_template, region=region): raise RuntimeError( "Setup error: No detectable differences after pressing %r" % (key_prev, )) left_template = numpy.append(stbt.crop(stbt.get_frame(), region), m[:, :, numpy.newaxis], axis=2) cv2.imwrite("left_template.png", left_template) # Error messages: missed_press = "Missed keypress: No change after pressing %s" double_press = \ "Didn't find expected screen after pressing %s (double keypress?)" # Now we perform the actual test: for _ in range(count // 2): assert stbt.press_and_wait(key_next, region=region, mask=m), \ missed_press % (key_next,) assert stbt.match(right_template, region=region), \ double_press % (key_next,) assert stbt.press_and_wait(key_prev, region=region, mask=m), \ missed_press % (key_prev,) assert stbt.match(left_template, region=region), \ double_press % (key_prev,)
def clear(self): page = self page = kb.navigate_to("CLEAR", page) stbt.press_and_wait("KEY_OK") # pylint:disable=stbt-unused-return-value return page.refresh()
def test_press_and_wait_start_timeout(): transition = stbt.press_and_wait("black", timeout_secs=0.2, stable_secs=0.1, _dut=FakeDeviceUnderTest()) print transition assert not transition assert transition.status == stbt.TransitionStatus.START_TIMEOUT
def test_press_and_wait_with_mask_or_region(mask, region, expected): transition = stbt.press_and_wait( "ball", mask=mask, region=region, timeout_secs=0.2, stable_secs=0.1, _dut=FakeDeviceUnderTest()) print transition assert transition.status == expected