blob: 1a3dabafaf5896963d59dac1d4f90a34eb98f565 [file] [log] [blame]
# Copyright 2019 The Cobalt Authors. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests sending deep links before load."""
# This test script works by splitting the work over 3 threads, so that they
# can each make progress even if they come across blocking operations.
# The three threads are:
# 1. Main thread, runs BlackBoxTestCase, sends suspend/resume signals, etc.
# 2. HTTP Server, responsible for slowly responding to a fetch of a javascript
# file.
# 3. Webdriver thread, instructs Cobalt to navigate to a URL
# Steps in ~ chronological order:
# 1. Create a TCP socket and listen on all interfaces.
# 2. Start Cobalt, and point it to the socket created in Step 1.
# 3. Send 3 deep links.
# 4. Load & run the javascript resource.
# 5. Check to see if JSTestsSucceeded().
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import _env # pylint: disable=unused-import,g-bad-import-order
import os
import SimpleHTTPServer
import threading
import traceback
import urlparse
from cobalt.black_box_tests import black_box_tests
from cobalt.black_box_tests.threaded_web_server import MakeRequestHandlerClass
from cobalt.black_box_tests.threaded_web_server import ThreadedWebServer
_FIRE_DEEP_LINK_BEFORE_LOAD_HTML = 'fire_deep_link_before_load.html'
_FIRE_DEEP_LINK_BEFORE_LOAD_JS = 'fire_deep_link_before_load.js'
_links_fired = threading.Event()
# The base path of the requested assets is the parent directory.
_SERVER_ROOT_PATH = os.path.join(os.path.dirname(__file__), os.pardir)
class JavascriptRequestDetector(MakeRequestHandlerClass(_SERVER_ROOT_PATH)):
"""Proxies everything to SimpleHTTPRequestHandler, except some paths."""
def do_GET(self): # pylint: disable=invalid-name
"""Handles HTTP GET requests for resources."""
parsed_path = urlparse.urlparse(self.path)
if parsed_path.path == '/testdata/' + _FIRE_DEEP_LINK_BEFORE_LOAD_JS:
# It is important not to send any response back, so we block.
print('Waiting on links to be fired.')
print('Links have been fired. Getting JS.')
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
class FireDeepLinkBeforeLoad(black_box_tests.BlackBoxTestCase):
"""Tests firing deep links before web module is loaded."""
def _LoadPage(self, webdriver, url):
"""Instructs webdriver to navigate to url."""
# Note: The following is a blocking request, and returns only when the
# page has fully loaded. In this test, the page will not fully load
# so, this does not return until Cobalt exits.
except: # pylint: disable=bare-except
def test_simple(self):
# Step 2. Start Cobalt, and point it to the socket created in Step 1.
with ThreadedWebServer(JavascriptRequestDetector,
self.GetBindingAddress()) as server:
with self.CreateCobaltRunner(url='about:blank') as runner:
target_url = server.GetURL(file_name='../testdata/' +
cobalt_launcher_thread = threading.Thread(
args=(self, runner.webdriver, target_url))
# Step 3. Send 3 deep links
for i in range(1, 4):
link = 'link ' + str(i)
print('Sending link : ' + link)
self.assertTrue(runner.SendDeepLink(link) == 0)
print('Links fired.')
# Step 4. Load & run the javascript resource.
# Step 5. Check to see if JSTestsSucceeded().
# Note that this call will check the DOM multiple times for a period
# of time (current default is 30 seconds).
except: # pylint: disable=bare-except
# Consider an exception being thrown as a test failure.
print('Cleaning up.')