def test_addSubtitles_should_wait_for_playback_to_start_before_adding_subtitle(self):
        player = YouTubeSubtitleControl()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.return_value = True
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        sys.modules["__main__"].xbmc.Player().isPlaying.assert_called_with()
        sys.modules["__main__"].xbmc.Player().setSubtitles.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
    def test_addSubtitles_should_wait_for_playback_to_start_before_adding_subtitle(self):
        player = YouTubeSubtitleControl()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.return_value = True
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        sys.modules["__main__"].xbmc.Player().isPlaying.assert_called_with()
        sys.modules["__main__"].xbmc.Player().setSubtitles.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
    def test_addSubtitles_should_call_xbmcs_setSubtitles(self):
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.return_value = True
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        player = YouTubeSubtitleControl()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        player.downloadSubtitle = Mock()
        player.downloadSubtitle.return_value = True

        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        sys.modules["__main__"].xbmcvfs.exists.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
        sys.modules["__main__"].xbmc.Player().setSubtitles.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
    def test_addSubtitles_should_call_xbmcs_setSubtitles(self):
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.return_value = True
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        player = YouTubeSubtitleControl()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        player.downloadSubtitle = Mock()
        player.downloadSubtitle.return_value = True

        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        sys.modules["__main__"].xbmcvfs.exists.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
        sys.modules["__main__"].xbmc.Player().setSubtitles.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
    def test_addSubtitles_should_check_if_subtitle_exists_locally_before_calling_downloadSubtitle(self):
        player = YouTubeSubtitleControl()

        settings = [False, True]
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.side_effect = lambda x: settings.pop()
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        player.downloadSubtitle = Mock()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        player.downloadSubtitle.return_value = False

        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        sys.modules["__main__"].xbmcvfs.exists.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
        assert(player.downloadSubtitle.call_count == 0)
    def test_addSubtitles_should_check_if_subtitle_exists_locally_before_calling_downloadSubtitle(self):
        player = YouTubeSubtitleControl()

        settings = [False, True]
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.side_effect = lambda x: settings.pop()
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        player.downloadSubtitle = Mock()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        player.downloadSubtitle.return_value = False

        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        sys.modules["__main__"].xbmcvfs.exists.assert_called_with('testDownloadPath/testTitle-[testid].ssa')
        assert(player.downloadSubtitle.call_count == 0)
    def test_addSubtitles_should_sleep_for_1_second_if_player_isnt_ready(self):
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.return_value = True
        sys.modules["__main__"].xbmc.Player().isPlaying.side_effect = [False, True] 
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        patcher = patch("time.sleep")
        patcher.start()
        sleep = Mock()
        import time
        time.sleep = sleep
        player = YouTubeSubtitleControl()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        player.downloadSubtitle = Mock()
        player.downloadSubtitle.return_value = True

        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        patcher.stop()
        sleep.assert_any_call(1)
    def test_addSubtitles_should_sleep_for_1_second_if_player_isnt_ready(self):
        sys.modules["__main__"].settings.getSetting.return_value = "testDownloadPath"
        sys.modules["__main__"].xbmcvfs.exists.return_value = True
        sys.modules["__main__"].xbmc.Player().isPlaying.side_effect = [False, True] 
        sys.modules["__main__"].common.makeUTF8.return_value = "testTitle"
        patcher = patch("time.sleep")
        patcher.start()
        sleep = Mock()
        import time
        time.sleep = sleep
        player = YouTubeSubtitleControl()
        player.getSubtitleFileName = Mock(return_value="testTitle-[testid].ssa")
        player.downloadSubtitle = Mock()
        player.downloadSubtitle.return_value = True

        player.addSubtitles({"videoid": "testid", "Title": "testTitle"})

        patcher.stop()
        sleep.assert_any_call(1)