# coding: utf-8

import time
import ngta
import threading
from ngta.assertions import assert_that


@ngta.skip('not ready')
def test_init():
    pass


def test_state__setting_and_getting():
    received = []

    class Observer(ngta.TestRunner.BaseObserver):
        def emit(self, data):
            received.append(data)

    runner = ngta.TestRunner()
    runner.observable.attach(Observer())

    runner.state = runner.State.RUNNING
    assert_that(runner.state).is_equal_to(runner.State.RUNNING)

    sending = {'id': runner.id, 'state': runner.state}
    assert_that(received).is_equal_to([sending])


def test_add_testsuite__instance():
    runner = ngta.TestRunner()
    suite = ngta.TestSuite()
    runner.add_testsuite(suite)
    assert_that(runner._testsuites).is_equal_to([suite])
    assert_that(runner.result.ts_records).is_equal_to([suite.record])


def test_add_testsuite__dict():
    runner = ngta.TestRunner()
    runner.add_testsuite({'id': 1111, 'name': 'testsuite object'})
    assert_that(runner._testsuites).is_length(1)

    suite = runner._testsuites[0]
    assert_that(suite.id).is_equal_to(1111)
    assert_that(suite.name).is_equal_to('testsuite object')
    assert_that(runner.result.ts_records).is_equal_to([suite.record])


def test_is_stopped__when_not_started():
    runner = ngta.TestRunner()
    assert_that(runner.is_stopped()).is_false()


def test_is_stopped__when_started_but_not_stopped():
    class Test(ngta.TestCase):
        def test_1(self):
            time.sleep(10)

    runner = ngta.TestRunner()
    runner.run()

    suite = ngta.TestSuite()
    suite.add_test(Test('test_1'))
    runner.add_testsuite(suite)
    runner.run()

    thread = threading.Thread(target=runner.run)
    time.sleep(5)
    assert_that(runner.is_stopped()).is_false()
    thread.join()


def test_is_stopped__when_stopped():
    runner = ngta.TestRunner()
    runner.run()
    assert_that(runner.is_stopped()).is_true()


@ngta.skip('not ready')
def test_run():
    pass


def test_pause():
    runner = ngta.TestRunner()
    runner.pause()
    assert_that(runner.state).is_equal_to(runner.State.PAUSED)


def test_resume():
    runner = ngta.TestRunner()
    runner.resume()
    assert_that(runner.state).is_equal_to(runner.State.RUNNING)


def test_abort():
    runner = ngta.TestRunner()
    runner.abort()
    assert_that(runner.state).is_equal_to(runner.State.ABORTED)


if __name__ == '__main__':
    ngta.main()
