import logging

import time

import unittest

import mock

from mopidy.models import Track

from mopidy_gmusic.playback import GMusicPlaybackProvider

from mopidy_gmusic.session import GMusicSession


logger = logging.getLogger(__name__)


class PlaybackTest(unittest.TestCase):

    _track = Track(
        uri='gmusic:track:test_track',
        name='Test Track',
        length=1000  # 1s track length
    )

    def test_init(self):
        audio = mock.Mock()
        backend = mock.Mock()
        p = GMusicPlaybackProvider(audio, backend)
        self.assertIsNotNone(p)

    def test_play_invalid(self):
        audio = mock.Mock()
        audio.prepare_change = mock.Mock()
        audio.set_uri = mock.Mock()
        audio.start_playback = mock.Mock()
        backend = mock.Mock()
        backend.session = GMusicSession()
        p = GMusicPlaybackProvider(audio, backend)
        self.assertFalse(p.play(Track(uri='gmusic:track:invalid_uri')))
        self.assertEqual(p.audio.prepare_change.call_count, 0)
        self.assertEqual(p.audio.set_uri.call_count, 0)
        self.assertEqual(p.audio.start_playback.call_count, 0)

    def test_play_valid(self):
        audio = mock.Mock()
        audio.prepare_change = mock.Mock()
        audio.set_uri = mock.Mock()
        audio.start_playback = mock.Mock()
        backend = mock.Mock()
        backend.session.get_stream_url = mock.Mock(
            return_value='http://stream.example.com/foo.mp3')
        p = GMusicPlaybackProvider(audio, backend)
        self.assertIsNotNone(p.play(Track(uri='gmusic:track:valid_uri')))
        self.assertEqual(p.audio.prepare_change.call_count, 1)
        self.assertEqual(p.audio.set_uri.call_count, 1)
        self.assertEqual(p.audio.start_playback.call_count, 1)

    def _setup_player(self):
        audio = mock.Mock()
        ext = mock.Mock()
        ext.session = mock.Mock()

        playback = GMusicPlaybackProvider(audio, ext)
        return playback

    def test_playback(self):
        playback = self._setup_player()
        playback.play(self._track)

        playback.backend.session.get_stream_url.assert_called_once()
        playback.audio.start_playback.assert_called_once_with()
        self.assertEqual(
            playback.backend.session.increment_song_playcount.call_count,
            0,
            'increment_song_playcount() was called')

    def test_stop_full_track(self):
        playback = self._setup_player()
        playback.play(self._track)
        # sleep for track length
        time.sleep(0.8)
        playback.stop()
        logger.debug(
            'call count: %d',
            playback.backend.session.increment_song_playcount.call_count)
        playback.backend.session.increment_song_playcount\
            .assert_called_once_with(u'test_track')

    def test_stop_skip(self):
        playback = self._setup_player()
        playback.play(self._track)
        # sleep for 1/3 of track length
        time.sleep(0.3)
        playback.stop()
        self.assertEqual(
            playback.backend.session.increment_song_playcount.call_count,
            0,
            'increment_song_playcount() was called')

@@ -585,20 +585,28 @@
 ef test_
-play
+change_track
 _invalid
@@ -645,130 +645,8 @@
 k()%0A
-        audio.prepare_change = mock.Mock()%0A        audio.set_uri = mock.Mock()%0A        audio.start_playback = mock.Mock()%0A
     
@@ -787,28 +787,36 @@
 sertFalse(p.
-play
+change_track
 (Track(uri='
@@ -1040,20 +1040,28 @@
 ef test_
-play
+change_track
 _valid(s
@@ -1098,130 +1098,8 @@
 k()%0A
-        audio.prepare_change = mock.Mock()%0A        audio.set_uri = mock.Mock()%0A        audio.start_playback = mock.Mock()%0A
     
@@ -1312,24 +1312,27 @@
 sert
-IsNotNone(p.play
+True(p.change_track
 (Tra
@@ -1418,33 +1418,33 @@
 nge.call_count, 
-1
+0
 )%0A        self.a
@@ -1545,17 +1545,17 @@
 _count, 
-1
+0
 )%0A%0A    d
@@ -1832,32 +1832,64 @@
 layback.
-play(self._track
+change_track(self._track)%0A        playback.play(
 )%0A%0A     
@@ -2278,32 +2278,64 @@
 layback.
-play(self._track
+change_track(self._track)%0A        playback.play(
 )%0A      
@@ -2738,24 +2738,56 @@
 ack.
-play(self._track
+change_track(self._track)%0A        playback.play(
 )%0A  

Update tests