<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import sys
import threading
import unittest

import mozdevice
from marionette import Marionette

from webapi_tests import certapp
from webapi_tests.semiauto import environment
from webapi_tests.semiauto.environment import InProcessTestEnvironment


"""The default time to wait for a user to respond to a prompt or
instruction in a test."""
prompt_timeout = 600  # 10 minutes


class TestCase(unittest.TestCase):
    stored = threading.local()

    def __init__(self, *args, **kwargs):
        super(TestCase, self).__init__(*args, **kwargs)
        self.stored.handler = None
        self.stored.marionette = None

        self.marionette, self.server, self.handler, self.app = None, None, None, None

        device = mozdevice.DeviceManagerADB()
        device.forward("tcp:2828", "tcp:2828")

        # Cleanups are run irrespective of whether setUp fails
        self.addCleanup(self.cleanup)

    def cleanup(self):
        if self.marionette is not None:
            certapp.kill(self.marionette, app=self.app)

    def setUp(self):
        """Sets up the environment for a test case.

        Retreive already running test environment, or create a new one if
        one doesn't exist.  A test environment consists of a web
        server with HTTP and WebSocket handlers which tests can access
        through ``self.handler``.

        Then set up a Marionette session to the connected device if
        one does not already exist.  This is assigned to
        ``self.marionette``.  Marionette can be used to remote control
        the device.

        """

        super(TestCase, self).setUp()

        env = environment.get(InProcessTestEnvironment)
        self.server = env.server
        self.handler = env.handler

        self.assert_browser_connected()
        self.marionette = TestCase.create_marionette()

        turn_screen_on(self.marionette)
        unlock_screen(self.marionette)

        print certapp
        print dir(certapp)
        if not certapp.is_installed():
            certapp.install(self.marionette)

        # Make sure we don't reuse the certapp context from a previous
        # testrun that was interrupted and left the certapp open.
        try:
            certapp.kill(self.marionette)
        except certapp.CloseError:
            self.close_app_manually()

        try:
            self.app = certapp.launch(self.marionette)
        except certapp.LaunchError:
            self.launch_app_manually()

    def assert_browser_connected(self):
        """Asserts (and consequently fails the test if not true) that the
        browser is connected to the semiauto test harness.

        """

        self.assertTrue(self.handler.connected, "Browser disconnected")<commit_msg>fixup! Bug 1020457 - Merge webapi_tests package into the fxos-certsuite package<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import sys
import threading
import unittest

import mozdevice
from marionette import Marionette

from webapi_tests import certapp
from webapi_tests.semiauto import environment
from webapi_tests.semiauto.environment import InProcessTestEnvironment


"""The default time to wait for a user to respond to a prompt or
instruction in a test."""
prompt_timeout = 600  # 10 minutes


class TestCase(unittest.TestCase):
    stored = threading.local()

    def __init__(self, *args, **kwargs):
        super(TestCase, self).__init__(*args, **kwargs)
        self.stored.handler = None
        self.stored.marionette = None

        self.marionette, self.server, self.handler, self.app = None, None, None, None

        device = mozdevice.DeviceManagerADB()
        device.forward("tcp:2828", "tcp:2828")

        # Cleanups are run irrespective of whether setUp fails
        self.addCleanup(self.cleanup)

    def cleanup(self):
        if self.marionette is not None:
            certapp.kill(self.marionette, app=self.app)

    def setUp(self):
        """Sets up the environment for a test case.

        Retreive already running test environment, or create a new one if
        one doesn't exist.  A test environment consists of a web
        server with HTTP and WebSocket handlers which tests can access
        through ``self.handler``.

        Then set up a Marionette session to the connected device if
        one does not already exist.  This is assigned to
        ``self.marionette``.  Marionette can be used to remote control
        the device.

        """

        super(TestCase, self).setUp()

        env = environment.get(InProcessTestEnvironment)
        self.server = env.server
        self.handler = env.handler

        self.assert_browser_connected()
        self.marionette = TestCase.create_marionette()

        turn_screen_on(self.marionette)
        unlock_screen(self.marionette)

        if not certapp.is_installed():
            certapp.install(self.marionette)

        # Make sure we don't reuse the certapp context from a previous
        # testrun that was interrupted and left the certapp open.
        try:
            certapp.kill(self.marionette)
        except certapp.CloseError:
            self.close_app_manually()

        try:
            self.app = certapp.launch(self.marionette)
        except certapp.LaunchError:
            self.launch_app_manually()

    def assert_browser_connected(self):
        """Asserts (and consequently fails the test if not true) that the
        browser is connected to the semiauto test harness.

        """

        self.assertTrue(self.handler.connected, "Browser disconnected")
{'fraction_non_alphanumeric': 0.07429345066847182, 'fraction_numerical': 0.006938568285665934, 'mean_word_length': 2.6776602364654636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3932313', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1590, 'n_words': 558}