import unittest
import os

from SeleniumLibrary import (SeleniumLibrary, _server_startup_command,
                             _command_line_args_for_server,
                             FIREFOX_TEMPLATE_ARG, FIREFOX_PROFILE_DIR,
                             FIREFOX_DEFAULT_PROFILE, SELENIUM_SERVER_PATH)



class TestGetBrowser(unittest.TestCase):

    def setUp(self):
        self.lib = SeleniumLibrary()

    def test_ie_aliases(self):
        for alias in ['ie', 'IE', 'Internet Explorer', 'INTernETexplOrEr']:
            self.assertEquals(self.lib._get_browser(alias), '*iexplore')

    def test_firefox_aliases(self):
        for alias in ['ff', 'FF', 'firefox', 'FireFox']:
            self.assertEquals(self.lib._get_browser(alias), '*firefox')

    def test_non_alias_is_not_modified(self):
        for non_alias in ['FIREFUX', 'i e 8', 'C:\\Program Files\\mybrowser\\brow.exe',
                          '{"username": "user", "access-key": "7A9cea40-84f7-4d3b-8748-0e94fCd4dX4f"}']:
            self.assertEquals(self.lib._get_browser(non_alias), non_alias)

    def test_patched_remote_control(self):
        rc_path = os.path.join(os.path.dirname(__file__), '..', '..', 'src',
                               'SeleniumLibrary', 'selenium.py')
        self.assertTrue('conn.close()' in open(rc_path).read())

    def test_patched_open_browser(self):
        rc_path = os.path.join(os.path.dirname(__file__), '..', '..', 'src',
                               'SeleniumLibrary', 'selenium.py')
        self.assertTrue('self.do_command("open", [url,"true"])' in open(rc_path).read(),
                        "Patch for Firefox 3.6 compatibility required. See issue 114: "+
                        "http://code.google.com/p/robotframework-seleniumlibrary/issues/detail?id=114")


class TestServerArguments(unittest.TestCase):

    def test_default_jar_path_is_correctly_determined(self):
        self.assertEquals(_server_startup_command(None)[:3],
                          ['java', '-jar', SELENIUM_SERVER_PATH])

    def test_given_jar_path_is_used(self):
        self.assertEquals(_server_startup_command('/some/jar.jar')[:3],
                          ['java', '-jar', '/some/jar.jar'])

    def test_selenium_lib_default_profile_is_used_when_no_profile_given(self):
        self.assertEquals(_command_line_args_for_server(),
                          [FIREFOX_TEMPLATE_ARG, FIREFOX_PROFILE_DIR])

    def test_given_profile_is_not_overridden(self):
        self.assertEquals(_command_line_args_for_server(FIREFOX_TEMPLATE_ARG, 'foo'),
                          [FIREFOX_TEMPLATE_ARG, 'foo'])

    def test_real_default_profile_can_be_used(self):
        self.assertEquals(_command_line_args_for_server(FIREFOX_TEMPLATE_ARG,FIREFOX_DEFAULT_PROFILE), [])

    def test_other_options_are_preserved(self):
        self.assertEquals(_command_line_args_for_server('-someOpt', 'value', '-otherOpt'),
                          ['-someOpt', 'value', '-otherOpt', FIREFOX_TEMPLATE_ARG, FIREFOX_PROFILE_DIR])


if __name__ == "__main__":
    unittest.main()

@@ -1319,473 +1319,8 @@
 ))%0A%0A
-    def test_patched_open_browser(self):%0A        rc_path = os.path.join(os.path.dirname(__file__), '..', '..', 'src',%0A                               'SeleniumLibrary', 'selenium.py')%0A        self.assertTrue('self.do_command(%22open%22, %5Burl,%22true%22%5D)' in open(rc_path).read(),%0A                        %22Patch for Firefox 3.6 compatibility required. See issue 114: %22+%0A                        %22http://code.google.com/p/robotframework-seleniumlibrary/issues/detail?id=114%22)%0A
 %0A%0Acl

Fix for this issue is now included in Selenium remote control, no need for this test