<s> from __future__ import unicode_literals <EOL> import unittest <EOL> import spotify <EOL> import tests <EOL> from tests import mock <EOL> @ mock . patch ( '<STR_LIT>' , spec = spotify . lib ) <EOL> class ImageTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . session = tests . create_session_mock ( ) <EOL> spotify . _session_instance = self . session <EOL> def tearDown ( self ) : <EOL> spotify . _session_instance = None <EOL> def test_create_without_uri_or_sp_image_fails ( self , lib_mock ) : <EOL> with self . assertRaises ( AssertionError ) : <EOL> spotify . Image ( self . session ) <EOL> @ mock . patch ( '<STR_LIT>' , spec = spotify . Link ) <EOL> def test_create_from_uri ( self , link_mock , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> link_instance_mock = link_mock . return_value <EOL> link_instance_mock . as_image . return_value = spotify . Image ( <EOL> self . session , sp_image = sp_image ) <EOL> lib_mock . sp_image_create_from_link . return_value = sp_image <EOL> uri = '<STR_LIT>' <EOL> result = spotify . Image ( self . session , uri = uri ) <EOL> link_mock . assert_called_with ( self . session , uri = uri ) <EOL> link_instance_mock . as_image . assert_called_with ( ) <EOL> lib_mock . sp_image_add_ref . assert_called_with ( sp_image ) <EOL> self . assertEqual ( result . _sp_image , sp_image ) <EOL> @ mock . patch ( '<STR_LIT>' , spec = spotify . Link ) <EOL> def test_create_from_uri_fail_raises_error ( self , link_mock , lib_mock ) : <EOL> link_instance_mock = link_mock . return_value <EOL> link_instance_mock . as_image . return_value = None <EOL> uri = '<STR_LIT>' <EOL> with self . assertRaises ( ValueError ) : <EOL> spotify . Image ( self . session , uri = uri ) <EOL> def test_adds_ref_to_sp_image_when_created ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> spotify . Image ( self . session , sp_image = sp_image ) <EOL> lib_mock . sp_image_add_ref . assert_called_with ( sp_image ) <EOL> def test_releases_sp_image_when_image_dies ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> image = None <EOL> tests . gc_collect ( ) <EOL> lib_mock . sp_image_release . assert_called_with ( sp_image ) <EOL> @ mock . patch ( '<STR_LIT>' , spec = spotify . Link ) <EOL> def test_repr ( self , link_mock , lib_mock ) : <EOL> link_instance_mock = link_mock . return_value <EOL> link_instance_mock . uri = '<STR_LIT:foo>' <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = repr ( image ) <EOL> self . assertEqual ( result , '<STR_LIT>' % '<STR_LIT:foo>' ) <EOL> def test_eq ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image1 = spotify . Image ( self . session , sp_image = sp_image ) <EOL> image2 = spotify . Image ( self . session , sp_image = sp_image ) <EOL> self . assertTrue ( image1 == image2 ) <EOL> self . assertFalse ( image1 == '<STR_LIT:foo>' ) <EOL> def test_ne ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image1 = spotify . Image ( self . session , sp_image = sp_image ) <EOL> image2 = spotify . Image ( self . session , sp_image = sp_image ) <EOL> self . assertFalse ( image1 != image2 ) <EOL> def test_hash ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image1 = spotify . Image ( self . session , sp_image = sp_image ) <EOL> image2 = spotify . Image ( self . session , sp_image = sp_image ) <EOL> self . assertEqual ( hash ( image1 ) , hash ( image2 ) ) <EOL> def test_loaded_event_is_unset_by_default ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> self . assertFalse ( image . loaded_event . is_set ( ) ) <EOL> def test_create_with_callback ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> lib_mock . sp_image_remove_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> lib_mock . sp_image_create . return_value = sp_image <EOL> callback = mock . Mock ( ) <EOL> image = spotify . Image ( <EOL> self . session , sp_image = sp_image , callback = callback ) <EOL> lib_mock . sp_image_add_load_callback . assert_called_with ( <EOL> sp_image , mock . ANY , mock . ANY ) <EOL> image_load_cb = lib_mock . sp_image_add_load_callback . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> callback_handle = lib_mock . sp_image_add_load_callback . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> self . assertEqual ( callback . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( lib_mock . sp_image_remove_load_callback . call_count , <NUM_LIT:0> ) <EOL> self . assertFalse ( image . loaded_event . is_set ( ) ) <EOL> image_load_cb ( sp_image , callback_handle ) <EOL> callback . assert_called_once_with ( image ) <EOL> lib_mock . sp_image_remove_load_callback . assert_called_with ( <EOL> sp_image , image_load_cb , callback_handle ) <EOL> self . assertTrue ( image . loaded_event . is_set ( ) ) <EOL> def test_create_with_callback_and_throw_away_image_and_call_load_callback ( <EOL> self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> lib_mock . sp_image_remove_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> lib_mock . sp_image_create . return_value = sp_image <EOL> callback = mock . Mock ( ) <EOL> image = spotify . Image ( <EOL> self . session , sp_image = sp_image , callback = callback ) <EOL> loaded_event = image . loaded_event <EOL> image = None <EOL> tests . gc_collect ( ) <EOL> image_load_cb = lib_mock . sp_image_add_load_callback . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> callback_handle = lib_mock . sp_image_add_load_callback . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> image_load_cb ( sp_image , callback_handle ) <EOL> loaded_event . wait ( <NUM_LIT:3> ) <EOL> self . assertEqual ( callback . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( callback . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . _sp_image , sp_image ) <EOL> def test_create_with_callback_fails_if_error_adding_callback ( <EOL> self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . BAD_API_VERSION ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> callback = mock . Mock ( ) <EOL> with self . assertRaises ( spotify . Error ) : <EOL> spotify . Image ( self . session , sp_image = sp_image , callback = callback ) <EOL> def test_is_loaded ( self , lib_mock ) : <EOL> lib_mock . sp_image_is_loaded . return_value = <NUM_LIT:1> <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = image . is_loaded <EOL> lib_mock . sp_image_is_loaded . assert_called_once_with ( sp_image ) <EOL> self . assertTrue ( result ) <EOL> def test_error ( self , lib_mock ) : <EOL> lib_mock . sp_image_error . return_value = int ( <EOL> spotify . ErrorType . IS_LOADING ) <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = image . error <EOL> lib_mock . sp_image_error . assert_called_once_with ( sp_image ) <EOL> self . assertIs ( result , spotify . ErrorType . IS_LOADING ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_load ( self , load_mock , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> image . load ( <NUM_LIT:10> ) <EOL> load_mock . assert_called_with ( self . session , image , timeout = <NUM_LIT:10> ) <EOL> def test_format ( self , lib_mock ) : <EOL> lib_mock . sp_image_is_loaded . return_value = <NUM_LIT:1> <EOL> lib_mock . sp_image_format . return_value = int ( spotify . ImageFormat . JPEG ) <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = image . format <EOL> lib_mock . sp_image_format . assert_called_with ( sp_image ) <EOL> self . assertIs ( result , spotify . ImageFormat . JPEG ) <EOL> def test_format_is_none_if_unloaded ( self , lib_mock ) : <EOL> lib_mock . sp_image_is_loaded . return_value = <NUM_LIT:0> <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = image . format <EOL> lib_mock . sp_image_is_loaded . assert_called_with ( sp_image ) <EOL> self . assertIsNone ( result ) <EOL> def test_data ( self , lib_mock ) : <EOL> lib_mock . sp_image_is_loaded . return_value = <NUM_LIT:1> <EOL> size = <NUM_LIT:20> <EOL> data = spotify . ffi . new ( '<STR_LIT>' , size ) <EOL> data [ <NUM_LIT:0> : <NUM_LIT:3> ] = [ b'<STR_LIT:a>' , b'<STR_LIT:b>' , b'<STR_LIT:c>' ] <EOL> def func ( sp_image_ptr , data_size_ptr ) : <EOL> data_size_ptr [ <NUM_LIT:0> ] = size <EOL> return spotify . ffi . cast ( '<STR_LIT>' , data ) <EOL> lib_mock . sp_image_data . side_effect = func <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = image . data <EOL> lib_mock . sp_image_data . assert_called_with ( sp_image , mock . ANY ) <EOL> self . assertEqual ( result [ : <NUM_LIT:5> ] , b'<STR_LIT>' ) <EOL> def test_data_is_none_if_unloaded ( self , lib_mock ) : <EOL> lib_mock . sp_image_is_loaded . return_value = <NUM_LIT:0> <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = image . data <EOL> lib_mock . sp_image_is_loaded . assert_called_with ( sp_image ) <EOL> self . assertIsNone ( result ) <EOL> def test_data_uri ( self , lib_mock ) : <EOL> lib_mock . sp_image_format . return_value = int ( spotify . ImageFormat . JPEG ) <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> prop_mock = mock . PropertyMock ( ) <EOL> with mock . patch . object ( spotify . Image , '<STR_LIT:data>' , prop_mock ) : <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> prop_mock . return_value = b'<STR_LIT>' <EOL> result = image . data_uri <EOL> self . assertEqual ( result , '<STR_LIT>' ) <EOL> def test_data_uri_is_none_if_unloaded ( self , lib_mock ) : <EOL> lib_mock . sp_image_is_loaded . return_value = <NUM_LIT:0> <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> result = image . data_uri <EOL> self . assertIsNone ( result ) <EOL> def test_data_uri_fails_if_unknown_image_format ( self , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> image . __dict__ [ '<STR_LIT>' ] = mock . Mock ( <EOL> return_value = spotify . ImageFormat . UNKNOWN ) <EOL> image . __dict__ [ '<STR_LIT:data>' ] = mock . Mock ( return_value = b'<STR_LIT>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> image . data_uri <EOL> @ mock . patch ( '<STR_LIT>' , spec = spotify . Link ) <EOL> def test_link_creates_link_to_image ( self , link_mock , lib_mock ) : <EOL> lib_mock . sp_image_add_load_callback . return_value = int ( <EOL> spotify . ErrorType . OK ) <EOL> sp_image = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> image = spotify . Image ( self . session , sp_image = sp_image ) <EOL> sp_link = spotify . ffi . cast ( '<STR_LIT>' , <NUM_LIT> ) <EOL> lib_mock . sp_link_create_from_image . return_value = sp_link <EOL> link_mock . return_value = mock . sentinel . link <EOL> result = image . link <EOL> link_mock . assert_called_once_with ( <EOL> self . session , sp_link = sp_link , add_ref = False ) <EOL> self . assertEqual ( result , mock . sentinel . link ) <EOL> class ImageFormatTest ( unittest . TestCase ) : <EOL> def test_has_constants ( self ) : <EOL> self . assertEqual ( spotify . ImageFormat . UNKNOWN , - <NUM_LIT:1> ) <EOL> self . assertEqual ( spotify . ImageFormat . JPEG , <NUM_LIT:0> ) <EOL> class ImageSizeTest ( unittest . TestCase ) : <EOL> def test_has_size_constants ( self ) : <EOL> self . assertEqual ( spotify . ImageSize . NORMAL , <NUM_LIT:0> ) <EOL> self . assertEqual ( spotify . ImageSize . SMALL , <NUM_LIT:1> ) <EOL> self . assertEqual ( spotify . ImageSize . LARGE , <NUM_LIT:2> ) </s>
