# -*- coding: utf-8 -*-

# Copyright 2013 splinter authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import os
import unittest

from splinter import Browser
from .fake_webapp import EXAMPLE_APP
from .base import WebDriverTests
from selenium.common.exceptions import WebDriverException


def chrome_installed():
    try:
        Browser("chrome")
    except WebDriverException:
        return False
    return True


class ChromeBrowserTest(WebDriverTests, unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.browser = Browser("chrome")

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()

    def setUp(self):
        self.browser.visit(EXAMPLE_APP)

    def test_attach_file(self):
        "should provide a way to change file field value"
        file_path = os.path.join(
            os.path.abspath(os.path.dirname(__file__)),
            'mockfile.txt'
        )
        self.browser.attach_file('file', file_path)
        self.browser.find_by_name('upload').click()

        html = self.browser.html
        self.assertIn('text/plain', html)
        self.assertIn(open(file_path).read().encode('utf-8'), html)

    def test_should_support_with_statement(self):
        with Browser('chrome') as internet:
            pass


class ChromeBrowserFullscreenTest(WebDriverTests, unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.browser = Browser("chrome", fullscreen=True)

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()

    def setUp(self):
        self.browser.visit(EXAMPLE_APP)

    def test_should_support_with_statement(self):
        with Browser('chrome', fullscreen=True) as internet:
            pass

[((411, 428), 'splinter.Browser', 'Browser', (['"""chrome"""'], {}), "('chrome')\n", (418, 428), False, 'from splinter import Browser\n'), ((624, 641), 'splinter.Browser', 'Browser', (['"""chrome"""'], {}), "('chrome')\n", (631, 641), False, 'from splinter import Browser\n'), ((1492, 1526), 'splinter.Browser', 'Browser', (['"""chrome"""'], {'fullscreen': '(True)'}), "('chrome', fullscreen=True)\n", (1499, 1526), False, 'from splinter import Browser\n'), ((1307, 1324), 'splinter.Browser', 'Browser', (['"""chrome"""'], {}), "('chrome')\n", (1314, 1324), False, 'from splinter import Browser\n'), ((1726, 1760), 'splinter.Browser', 'Browser', (['"""chrome"""'], {'fullscreen': '(True)'}), "('chrome', fullscreen=True)\n", (1733, 1760), False, 'from splinter import Browser\n'), ((930, 955), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (945, 955), False, 'import os\n')]