Home | History | Annotate | Download | only in cros
      1 # Copyright 2017 The Chromium OS Authors. All rights reserved.
      2 # Use of this source code is governed by a BSD-style license that can be
      3 # found in the LICENSE file.
      4 
      5 import os
      6 import tempfile
      7 
      8 
      9 HTML_TEMPLATE = """<!DOCTYPE html>
     10     <html>
     11         <body id="body">
     12             <h1>{title}</h1>
     13             <p>Status: <span id="status">not-started</span></p>
     14             {scripts}
     15         </body>
     16     </html>
     17     """
     18 
     19 
     20 def generate_test_html(title, *scripts):
     21     """
     22     Generates HTML contents for WebRTC tests.
     23 
     24     @param title: The title of the page.
     25     @param scripts: Paths to the javascript files to include.
     26     @returns HTML contents.
     27     """
     28     script_tag_list = [
     29         '<script src="{}"></script>'.format(script)
     30         for script in scripts
     31     ]
     32     script_tags = '\n'.join(script_tag_list)
     33     return HTML_TEMPLATE.format(title=title, scripts=script_tags)
     34 
     35 
     36 def get_common_script_path(script):
     37     """
     38     Gets the file path to a common script.
     39 
     40     @param script: The file name of the script, e.g. 'foo.js'
     41     @returns The absolute path to the script.
     42     """
     43     return os.path.join(
     44             os.path.dirname(__file__), 'webrtc_scripts', script)
     45 
     46 
     47 def create_temp_html_file(title, tmpdir, *scripts):
     48     """
     49     Creates a temporary file with HTML contents for WebRTC tests.
     50 
     51     @param title: The title of the page.
     52     @param tmpdir: Directory to put the temporary file.
     53     @param scripts: Paths to the javascript files to load.
     54     @returns The file object containing the HTML.
     55     """
     56     html = generate_test_html(
     57             title, *scripts)
     58     html_file = tempfile.NamedTemporaryFile(
     59             suffix = '.html', dir = tmpdir, delete = False)
     60     html_file.write(html)
     61     html_file.close()
     62     return html_file
     63 
     64