import unittest
from selenium import webdriver
from selenium.webdriver.firefox import firefox_binary
from selenium.common.exceptions import WebDriverException
from multiprocessing import Process
import socket
import shutil
import os
import gnupg
import urllib2
import sys


import config

import source
import journalist
from tests import common
import urllib2

import signal
import traceback
from datetime import datetime
import time
import mock

os.environ['SECUREDROP_ENV'] = 'test'


class FunctionalTest():

    def _unused_port(self):
        s = socket.socket()
        s.bind(("localhost", 0))
        port = s.getsockname()[1]
        s.close()
        return port

    def _create_webdriver(self):
        log_file = open('tests/log/firefox.log', 'a')
        log_file.write('\n\n[%s] Running Functional Tests\n' % str(datetime.now()))
        log_file.flush()
        firefox = firefox_binary.FirefoxBinary(log_file=log_file)
        return webdriver.Firefox(firefox_binary=firefox)

    def setUp(self):
        # Patch the two-factor verification to avoid intermittent errors
        patcher = mock.patch('journalist.Journalist.verify_token')
        self.addCleanup(patcher.stop)
        self.mock_journalist_verify_token = patcher.start()
        self.mock_journalist_verify_token.return_value = True

        signal.signal(signal.SIGUSR1, lambda _, s: traceback.print_stack(s))

        common.create_directories()
        self.gpg = common.init_gpg()
        common.init_db()

        source_port = self._unused_port()
        journalist_port = self._unused_port()

        self.source_location = "http://localhost:%d" % source_port
        self.journalist_location = "http://localhost:%d" % journalist_port

        def start_source_server():
            source.app.run(port=source_port, debug=True, use_reloader=False)

        def start_journalist_server():
            journalist.app.run(port=journalist_port, debug=True, use_reloader=False)

        self.source_process = Process(target=start_source_server)
        self.journalist_process = Process(target=start_journalist_server)

        self.source_process.start()
        self.journalist_process.start()

        self.driver = self._create_webdriver()

        # Poll the DOM briefly to wait for elements. It appears .click() does
        # not always do a good job waiting for the page to load, or perhaps
        # Firefox takes too long to render it (#399)
        self.driver.implicitly_wait(5)

        self.secret_message = 'blah blah blah'

    def tearDown(self):
        common.clean_root()
        self.driver.quit()
        self.source_process.terminate()
        self.journalist_process.terminate()

    def wait_for(self, function_with_assertion, timeout=5):
        """Polling wait for an arbitrary assertion."""
        # Thanks to http://chimera.labs.oreilly.com/books/1234000000754/ch20.html#_a_common_selenium_problem_race_conditions
        start_time = time.time()
        while time.time() - start_time < timeout:
            try:
                return function_with_assertion()
            except (AssertionError, WebDriverException):
                time.sleep(0.1)
        # one more try, which will raise any errors if they are outstanding
        return function_with_assertion()

@@ -2219,24 +2219,247 @@
 ebdriver()%0A%0A
+        # Set window size and position explicitly to avoid potential bugs due%0A        # to discrepancies between environments.%0A        self.driver.set_window_position(0, 0);%0A        self.driver.set_window_size(1024, 768);%0A%0A
         # Po

Set Selenium window size and position explicitly