Skip to content

Commit

Permalink
develop #comment Improve prev/next video support.
Browse files Browse the repository at this point in the history
 - Simplify tests.
  • Loading branch information
Luc Sinet committed Jan 13, 2019
1 parent 98bd5a4 commit 59aa1cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
11 changes: 7 additions & 4 deletions RaspberryCast/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
class History(object):
def __init__(self, items=[]):
self._index = 0
self._items = items
self._browsing = False
self._items = items

def __repr__(self):
return str(self._items)
Expand All @@ -32,7 +32,8 @@ def push(self, object):
logger.debug("[history] {}".format(self))

def prev(self):
pos = self._index + 1
step = int(self._browsing)
pos = self._index + step
if pos >= len(self._items):
return False

Expand All @@ -46,16 +47,18 @@ def next(self):
return False

self._index -= 1
self._browsing = self._index > 0
return True

def can_prev(self):
return self._index + int(self._browsing) < len(self._items)

def current_item(self):
return self._items[self._index]

def browsing(self):
return self._browsing

def stop_browsing(self):
self._browsing = False
self._index = 0
self._browsing = False
logger.debug("[history] browsing turned off: {}".format(self))
19 changes: 9 additions & 10 deletions RaspberryCast/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, default_volume):
self._volume = default_volume

self._player = None
self._autoplay = True
self._autoplay = False
self._playerMutex = threading.RLock()

self._cv = threading.Condition()
Expand Down Expand Up @@ -71,14 +71,14 @@ def queue(self, video, first=False):
def list_queue(self):
return list(self._queue)

def stop(self):
def stop(self, next_video=True):
with self._cv:
if not self._playing():
logger.debug("[player] is already stopped")
return

logger.info("[player] stopping ...")
self._autoplay = False
self._autoplay = next_video
self._exec_command('stop')

def is_stopped():
Expand All @@ -88,12 +88,12 @@ def is_stopped():

def prev(self):
with self._cv:
if not self._history.prev():
if not self._history.can_prev():
return

if self._playing():
self.stop()
# Call prev() again to compensate the call to next() in at_exit()
self.stop(next_video=False)

self._history.prev()
self.play()

Expand Down Expand Up @@ -219,14 +219,13 @@ def _on_exit(self, player, code):
# NOTE: No need of using 'with self._cv' as
# stop synchronizes the destruction.
logger.info("[player] stopped")
if self._history.browsing():
if self._history.browsing() and self._autoplay:
self._history.next()
self._reset_player()

with self._cv:
if not self._autoplay:
return
self._cv.notify()
if self._autoplay:
self._cv.notify()


def make_player(default_volume):
Expand Down
38 changes: 22 additions & 16 deletions test/history_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,28 @@ def test_push(self):
self.assertFalse(history.browsing())

def test_push_duplicate(self):
history = History([0, 1, 2, 3])
history = History([0, 1])

history.push(2)
history.push(1)
self.assertEqual(history.current_item(), 0)

def test_can_prev_success(self):
history = History([0])

self.assertTrue(history.can_prev())

def test_can_prev_failure(self):
history = History()

self.assertFalse(history.can_prev())

def test_prev_success(self):
history = History([0, 1])

self.assertTrue(history.prev())
self.assertEqual(history.current_item(), 0)
self.assertTrue(history.browsing())

self.assertTrue(history.prev())
self.assertEqual(history.current_item(), 1)
self.assertTrue(history.browsing())
Expand All @@ -28,38 +42,30 @@ def test_prev_failure(self):
history = History()

self.assertFalse(history.prev())
# expect the history to have the currently played video
history = History([0])
self.assertFalse(history.prev())
self.assertEqual(history.current_item(), 0)
self.assertFalse(history.browsing())

def test_next_success(self):
history = History([0, 1, 2, 3])
history = History([0, 1])

self.assertTrue(history.prev()) # 0
self.assertTrue(history.prev()) # 1
self.assertTrue(history.prev()) # 2

self.assertTrue(history.next()) # 1
self.assertEqual(history.current_item(), 1)
self.assertTrue(history.browsing())

self.assertTrue(history.next()) # 0
self.assertEqual(history.current_item(), 0)
self.assertFalse(history.browsing())
self.assertTrue(history.browsing())

def test_next_failure(self):
history = History([0, 1, 2, 3])
history = History([0])

self.assertFalse(history.next())
self.assertEqual(history.current_item(), 0)
self.assertFalse(history.browsing())

def test_stop_browsing(self):
history = History([0, 1, 2, 3])
history = History([0, 1])

history.prev() # 0
history.prev() # 1
history.prev() # 2

history.stop_browsing()
self.assertEqual(history.current_item(), 0)
Expand Down

0 comments on commit 59aa1cf

Please sign in to comment.