# -*- 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

[((18, 8, 18, 25), 'splinter.Browser', 'Browser', ({(18, 16, 18, 24): '"""chrome"""'}, {}), "('chrome')", False, 'from splinter import Browser\n'), ((28, 22, 28, 39), 'splinter.Browser', 'Browser', ({(28, 30, 28, 38): '"""chrome"""'}, {}), "('chrome')", False, 'from splinter import Browser\n'), ((59, 22, 59, 56), 'splinter.Browser', 'Browser', (), '', False, 'from splinter import Browser\n'), ((51, 13, 51, 30), 'splinter.Browser', 'Browser', ({(51, 21, 51, 29): '"""chrome"""'}, {}), "('chrome')", False, 'from splinter import Browser\n'), ((69, 13, 69, 47), 'splinter.Browser', 'Browser', (), '', False, 'from splinter import Browser\n'), ((40, 28, 40, 53), 'os.path.dirname', 'os.path.dirname', ({(40, 44, 40, 52): '__file__'}, {}), '(__file__)', False, 'import os\n')]