# Path: viceroy/api.py
# class ViceroyTestCase(unittest.TestCase):
#     viceroy_url = '/'
#     viceroy_timeout = 30
#     viceroy_driver_class = None
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_server(cls):
#         raise NotImplementedError()
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_driver(cls):
#         if isinstance(cls.viceroy_driver_class, webdriver.Remote):
#             driver_class = cls.viceroy_driver_class
#         elif isinstance(cls.viceroy_driver_class, str):
#             driver_class = getattr(webdriver, cls.viceroy_driver_class)
#         elif 'SELENIUM_WEBDRIVER' in os.environ:
#             driver_class = getattr(webdriver, os.environ['SELENIUM_WEBDRIVER'])
#         else:
#             driver_class = webdriver.Firefox
#         driver = driver_class()
#         try:
# 
#             yield driver
#         finally:
#             driver.quit()
# 
#     @classmethod
#     def viceroy_get_results(cls):
#         with cls.viceroy_server() as port:
#             with cls.viceroy_driver() as driver:
#                 driver.get(
#                     'http://localhost:{}{}'.format(port, cls.viceroy_url)
#                 )
#                 WebDriverWait(
#                     driver,
#                     cls.viceroy_timeout
#                 ).until(
#                     lambda _: driver.execute_script(
#                         'return window.VICEROY && window.VICEROY.DONE;'
#                     )
#                 )
#                 return driver.execute_script('return window.VICEROY.RESULTS;')
# 
#     @classmethod
#     def setUpClass(cls):
#         super(ViceroyTestCase, cls).setUpClass()
#         cls.viceroy_cache = cls.viceroy_get_results()
# 
#     @classmethod
#     def tearDownClass(cls):
#         if hasattr(cls, 'viceroy_cache'):
#             del cls.viceroy_cache
#         super(ViceroyTestCase, cls).tearDownClass()


import contextlib
import logging
import multiprocessing
import socket
import time
from flask.ext.testing import TestCase
from viceroy.api import ViceroyTestCase
from __future__ import absolute_import



try:
    ConnectionRefusedError
except NameError:
    ConnectionRefusedError = socket.error


def _server_started(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.connect(('localhost', port))
    except ConnectionRefusedError:
        return False
    else:
        return True
    finally:
        sock.close()


def run_app(app, port, silent=True):
    if silent:
        log = logging.getLogger('werkzeug')
        log.setLevel(logging.ERROR)
    app.run(port=port)



# Path: viceroy/api.py
# class ViceroyTestCase(unittest.TestCase):
#     viceroy_url = '/'
#     viceroy_timeout = 30
#     viceroy_driver_class = None
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_server(cls):
#         raise NotImplementedError()
# 
#     @classmethod
#     @contextlib.contextmanager
#     def viceroy_driver(cls):
#         if isinstance(cls.viceroy_driver_class, webdriver.Remote):
#             driver_class = cls.viceroy_driver_class
#         elif isinstance(cls.viceroy_driver_class, str):
#             driver_class = getattr(webdriver, cls.viceroy_driver_class)
#         elif 'SELENIUM_WEBDRIVER' in os.environ:
#             driver_class = getattr(webdriver, os.environ['SELENIUM_WEBDRIVER'])
#         else:
#             driver_class = webdriver.Firefox
#         driver = driver_class()
#         try:
# 
#             yield driver
#         finally:
#             driver.quit()
# 
#     @classmethod
#     def viceroy_get_results(cls):
#         with cls.viceroy_server() as port:
#             with cls.viceroy_driver() as driver:
#                 driver.get(
#                     'http://localhost:{}{}'.format(port, cls.viceroy_url)
#                 )
#                 WebDriverWait(
#                     driver,
#                     cls.viceroy_timeout
#                 ).until(
#                     lambda _: driver.execute_script(
#                         'return window.VICEROY && window.VICEROY.DONE;'
#                     )
#                 )
#                 return driver.execute_script('return window.VICEROY.RESULTS;')
# 
#     @classmethod
#     def setUpClass(cls):
#         super(ViceroyTestCase, cls).setUpClass()
#         cls.viceroy_cache = cls.viceroy_get_results()
# 
#     @classmethod
#     def tearDownClass(cls):
#         if hasattr(cls, 'viceroy_cache'):
#             del cls.viceroy_cache
#         super(ViceroyTestCase, cls).tearDownClass()


# Path: viceroy/contrib/flask.py
import contextlib
import logging
import multiprocessing
import socket
import time
from flask.ext.testing import TestCase
from viceroy.api import ViceroyTestCase

from __future__ import absolute_import



try:
    ConnectionRefusedError
except NameError:
    ConnectionRefusedError = socket.error


def _server_started(port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        sock.connect(('localhost', port))
    except ConnectionRefusedError:
        return False
    else:
        return True
    finally:
        sock.close()


def run_app(app, port, silent=True):
    if silent:
        log = logging.getLogger('werkzeug')
        log.setLevel(logging.ERROR)
    app.run(port=port)



class ViceroyFlaskTestCase(ViceroyTestCase, TestCase):