<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 time
import unittest

from webapi_tests.semiauto import TestCase
from webapi_tests.telephony import TelephonyTestCommon


class TestTelephonyIncomingHoldResume(TestCase, TelephonyTestCommon):
    """
    This is a test for the `WebTelephony API`_ which will:

    - Disable the default gaia dialer, so that the test app can handle calls
    - Setup a mozTelephony event listener for incoming calls
    - Ask the test user to phone the Firefox OS device from a second phone
    - Verify that the mozTelephony incoming call event is triggered
    - Answer the incoming call via the API, keep the call active for 5 seconds
    - Hold the call using the API and ask the test user to verify
    - Hangup the hold call via the API,
    - Verify that the corresponding mozTelephonyCall events were triggered
    - Re-enable the default gaia dialer

    .. _`WebTelephony API`: https://developer.mozilla.org/en-US/docs/Web/Guide/API/Telephony
    """

    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)
        TelephonyTestCommon.__init__(self)

    def setUp(self):
        self.addCleanup(self.clean_up)
        super(TestTelephonyIncomingHoldResume, self).setUp()
        self.wait_for_obj("window.navigator.mozTelephony")
        # disable the default dialer manager so it doesn't grab our calls
        self.disable_dialer()

    def test_telephony_incoming_hold_hangup(self):
        # ask user to call the device; answer and verify via webapi
        self.user_guided_incoming_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['0'], self.incoming_call)

        self.answer_call()
        self.assertEqual(self.active_call_list[0]['state'], "connected", "Call state should be 'connected'")
        self.assertEqual(self.active_call_list[0]['id']['number'], self.incoming_call['id']['number'])
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 1, "There should be 1 active call")

        # keep call active for a while
        time.sleep(5)

        self.hold_active_call()
        self.confirm("Is the call now on hold?")

        # hangup the hold call
        self.hangup_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 0, "There should be 0 calls")

    @unittest.skip("Skipping this test as hangup the call through API fails")
    def test_telephony_incoming_hold_resume(self):
        # ask user to call the device; answer and verify via webapi
        self.user_guided_incoming_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['0'], self.incoming_call)

        self.answer_call()
        self.assertEqual(self.active_call_list[0]['state'], "connected", "Call state should be 'connected'")
        self.assertEqual(self.active_call_list[0]['number'], self.incoming_call['number'])
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 1, "There should be 1 active call")

        # keep call active for a while
        time.sleep(5)

        self.hold_active_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual((self.calls['0'])['state'], "held", "Call state should be 'held'")

        time.sleep(2)
        self.resume_held_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual((self.calls['0'])['state'], "resuming", "Call state should be 'resuming'")
        self.assertEqual(self.active_call_list[0]['state'], "connected", "Call state should be 'connected'")

        # hangup the call
        self.hangup_call(remote_hangup=True)

        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 0, "There should be 0 calls")

    def clean_up(self):
        # re-enable the default dialer manager
        self.enable_dialer()
        self.active_call_list = []<commit_msg>Update per 2.0 telephony API<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 time
import unittest

from webapi_tests.semiauto import TestCase
from webapi_tests.telephony import TelephonyTestCommon


class TestTelephonyIncomingHoldResume(TestCase, TelephonyTestCommon):
    """
    This is a test for the `WebTelephony API`_ which will:

    - Disable the default gaia dialer, so that the test app can handle calls
    - Setup a mozTelephony event listener for incoming calls
    - Ask the test user to phone the Firefox OS device from a second phone
    - Verify that the mozTelephony incoming call event is triggered
    - Answer the incoming call via the API, keep the call active for 5 seconds
    - Hold the call using the API and ask the test user to verify
    - Hangup the hold call via the API,
    - Verify that the corresponding mozTelephonyCall events were triggered
    - Re-enable the default gaia dialer

    .. _`WebTelephony API`: https://developer.mozilla.org/en-US/docs/Web/Guide/API/Telephony
    """

    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)
        TelephonyTestCommon.__init__(self)

    def setUp(self):
        self.addCleanup(self.clean_up)
        super(TestTelephonyIncomingHoldResume, self).setUp()
        self.wait_for_obj("window.navigator.mozTelephony")
        # disable the default dialer manager so it doesn't grab our calls
        self.disable_dialer()

    def test_telephony_incoming_hold_hangup(self):
        # ask user to call the device; answer and verify via webapi
        self.user_guided_incoming_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['0'], self.incoming_call)

        self.answer_call()
        self.assertEqual(self.active_call_list[0]['state'], "connected", "Call state should be 'connected'")
        self.assertEqual(self.active_call_list[0]['id']['number'], self.incoming_call['id']['number'])
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 1, "There should be 1 active call")

        # keep call active for a while
        time.sleep(5)

        self.hold_active_call()
        self.confirm("Is the call now on hold?")

        # hangup the hold call
        self.hangup_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 0, "There should be 0 calls")

    @unittest.skip("Skipping this test as hangup the call through API fails")
    def test_telephony_incoming_hold_resume(self):
        # ask user to call the device; answer and verify via webapi
        self.user_guided_incoming_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['0'], self.incoming_call)

        self.answer_call()
        self.assertEqual(self.active_call_list[0]['state'], "connected", "Call state should be 'connected'")
        self.assertEqual(self.active_call_list[0]['id']['number'], self.incoming_call['id']['number'])
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 1, "There should be 1 active call")

        # keep call active for a while
        time.sleep(5)

        self.hold_active_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual((self.calls['0'])['state'], "held", "Call state should be 'held'")

        time.sleep(2)
        self.resume_held_call()
        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual((self.calls['0'])['state'], "resuming", "Call state should be 'resuming'")
        self.assertEqual(self.active_call_list[0]['state'], "connected", "Call state should be 'connected'")

        # hangup the call
        self.hangup_call(remote_hangup=True)

        self.calls = self.marionette.execute_script("return window.wrappedJSObject.calls")
        self.assertEqual(self.calls['length'], 0, "There should be 0 calls")

    def clean_up(self):
        # re-enable the default dialer manager
        self.enable_dialer()
        self.active_call_list = []
{'fraction_non_alphanumeric': 0.10517566219660866, 'fraction_numerical': 0.0057630499833758175, 'mean_word_length': 3.4148727984344425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1216951', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2609, 'n_words': 722}