diff --git a/mopidy_spotify/backend.py b/mopidy_spotify/backend.py index f58b7eeb..62d15c6c 100644 --- a/mopidy_spotify/backend.py +++ b/mopidy_spotify/backend.py @@ -28,7 +28,6 @@ class SpotifyBackend(pykka.ThreadingActor, backend.Backend): _logged_in = threading.Event() _logged_out = threading.Event() _logged_out.set() - _online = threading.Event() def __init__(self, config, audio): super(SpotifyBackend, self).__init__() @@ -80,8 +79,7 @@ def _get_session(self, config): session.on( spotify.SessionEvent.CONNECTION_STATE_UPDATED, on_connection_state_changed, - self._logged_in, self._logged_out, self._online, - backend_actor_proxy) + self._logged_in, self._logged_out, backend_actor_proxy) session.on( spotify.SessionEvent.PLAY_TOKEN_LOST, on_play_token_lost, backend_actor_proxy) @@ -127,28 +125,24 @@ def on_play_token_lost(self): def on_connection_state_changed( - session, logged_in_event, logged_out_event, online_event, backend): + session, logged_in_event, logged_out_event, backend): # Called from the pyspotify event loop, and not in an actor context. if session.connection.state is spotify.ConnectionState.LOGGED_OUT: logger.debug('Logged out of Spotify') logged_in_event.clear() logged_out_event.set() - online_event.clear() elif session.connection.state is spotify.ConnectionState.LOGGED_IN: logger.info('Logged in to Spotify in online mode') logged_in_event.set() logged_out_event.clear() - online_event.set() backend.on_logged_in() elif session.connection.state is spotify.ConnectionState.DISCONNECTED: logger.info('Disconnected from Spotify') - online_event.clear() elif session.connection.state is spotify.ConnectionState.OFFLINE: logger.info('Logged in to Spotify in offline mode') logged_in_event.set() logged_out_event.clear() - online_event.clear() def on_play_token_lost(session, backend): diff --git a/mopidy_spotify/library.py b/mopidy_spotify/library.py index 69d7e45d..238439c6 100644 --- a/mopidy_spotify/library.py +++ b/mopidy_spotify/library.py @@ -33,5 +33,4 @@ def lookup(self, uri): def search(self, query=None, uris=None, exact=False): return search.search( self._config, self._backend._session, - self._backend._online.is_set(), query, uris, exact) diff --git a/mopidy_spotify/search.py b/mopidy_spotify/search.py index 13fb69cc..d4f732eb 100644 --- a/mopidy_spotify/search.py +++ b/mopidy_spotify/search.py @@ -5,13 +5,15 @@ from mopidy import models +import spotify + from mopidy_spotify import lookup, translator logger = logging.getLogger(__name__) -def search(config, session, is_online, query=None, uris=None, exact=False): +def search(config, session, query=None, uris=None, exact=False): # TODO Respect `uris` argument # TODO Support `exact` search @@ -30,7 +32,7 @@ def search(config, session, is_online, query=None, uris=None, exact=False): uri = 'spotify:search:%s' % urllib.quote(sp_query.encode('utf-8')) logger.info('Searching Spotify for: %s', sp_query) - if not is_online: + if session.connection.state is not spotify.ConnectionState.LOGGED_IN: logger.info('Spotify search aborted: Spotify is offline') return models.SearchResult(uri=uri) diff --git a/tests/conftest.py b/tests/conftest.py index 9d00d556..6d319fa9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -206,6 +206,7 @@ def sp_search_mock(sp_album_mock, sp_artist_mock, sp_track_mock): @pytest.fixture def session_mock(): sp_session_mock = mock.Mock(spec=spotify.Session) + sp_session_mock.connection.state = spotify.ConnectionState.LOGGED_IN sp_session_mock.playlist_container = [] return sp_session_mock diff --git a/tests/test_backend.py b/tests/test_backend.py index 038318b9..6b9fb6a5 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -112,7 +112,6 @@ def test_on_start_adds_connection_state_changed_handler_to_session( backend.on_connection_state_changed, backend.SpotifyBackend._logged_in, backend.SpotifyBackend._logged_out, - backend.SpotifyBackend._online, mock.ANY) in session.on.call_args_list) @@ -164,17 +163,14 @@ def test_on_connection_state_changed_when_logged_out(spotify_mock, caplog): session_mock.connection.state = spotify_mock.ConnectionState.LOGGED_OUT logged_in_event = threading.Event() logged_out_event = threading.Event() - online_event = threading.Event() actor_mock = mock.Mock(spec=backend.SpotifyBackend) backend.on_connection_state_changed( - session_mock, logged_in_event, logged_out_event, online_event, - actor_mock) + session_mock, logged_in_event, logged_out_event, actor_mock) assert 'Logged out of Spotify' in caplog.text() assert not logged_in_event.is_set() assert logged_out_event.is_set() - assert not online_event.is_set() def test_on_connection_state_changed_when_logged_in(spotify_mock, caplog): @@ -182,17 +178,14 @@ def test_on_connection_state_changed_when_logged_in(spotify_mock, caplog): session_mock.connection.state = spotify_mock.ConnectionState.LOGGED_IN logged_in_event = threading.Event() logged_out_event = threading.Event() - online_event = threading.Event() actor_mock = mock.Mock(spec=backend.SpotifyBackend) backend.on_connection_state_changed( - session_mock, logged_in_event, logged_out_event, online_event, - actor_mock) + session_mock, logged_in_event, logged_out_event, actor_mock) assert 'Logged in to Spotify in online mode' in caplog.text() assert logged_in_event.is_set() assert not logged_out_event.is_set() - assert online_event.is_set() actor_mock.on_logged_in.assert_called_once_with() @@ -201,15 +194,12 @@ def test_on_connection_state_changed_when_disconnected(spotify_mock, caplog): session_mock.connection.state = spotify_mock.ConnectionState.DISCONNECTED logged_in_event = threading.Event() logged_out_event = threading.Event() - online_event = threading.Event() actor_mock = mock.Mock(spec=backend.SpotifyBackend) backend.on_connection_state_changed( - session_mock, logged_in_event, logged_out_event, online_event, - actor_mock) + session_mock, logged_in_event, logged_out_event, actor_mock) assert 'Disconnected from Spotify' in caplog.text() - assert not online_event.is_set() def test_on_connection_state_changed_when_offline(spotify_mock, caplog): @@ -217,17 +207,14 @@ def test_on_connection_state_changed_when_offline(spotify_mock, caplog): session_mock.connection.state = spotify_mock.ConnectionState.OFFLINE logged_in_event = threading.Event() logged_out_event = threading.Event() - online_event = threading.Event() actor_mock = mock.Mock(spec=backend.SpotifyBackend) backend.on_connection_state_changed( - session_mock, logged_in_event, logged_out_event, online_event, - actor_mock) + session_mock, logged_in_event, logged_out_event, actor_mock) assert 'Logged in to Spotify in offline mode' in caplog.text() assert logged_in_event.is_set() assert not logged_out_event.is_set() - assert not online_event.is_set() def test_on_logged_in_event_activates_private_session( diff --git a/tests/test_search.py b/tests/test_search.py index 47b14de1..e34131e3 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -2,6 +2,8 @@ from mopidy import models +import spotify + def test_search_with_no_query_returns_nothing(provider, caplog): result = provider.search() @@ -51,8 +53,8 @@ def test_search_by_multiple_uris(session_mock, sp_track_mock, provider): assert track.bitrate == 160 -def test_search_when_offline_returns_nothing(provider, caplog): - provider._backend._online.is_set.return_value = False +def test_search_when_offline_returns_nothing(session_mock, provider, caplog): + session_mock.connection.state = spotify.ConnectionState.OFFLINE result = provider.search({'any': ['ABBA']})