tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

generate-page-name-two-page-test.py (3563B)


      1 #!/usr/bin/env python
      2 #
      3 # Any copyright is dedicated to the Public Domain.
      4 # http://creativecommons.org/publicdomain/zero/1.0/
      5 #
      6 # Generates HTML test files with permutations for frame tree hierarchies to
      7 # test page-name breaks.
      8 # These should all have page-name-two-pages-ref.html as their ref case.
      9 #
     10 # The generated tests have the structure of two <p> elements that will have
     11 # different page name values through various means:
     12 # * Both <p> elements have different values for the `page` property.
     13 # * One <p> element has a specified non-default `page` property and the other
     14 #   does not.
     15 # * One <p> element has a specified non-default `page` property and the other
     16 #   has `page: auto`.
     17 #
     18 # Additionally, the <p> elements may be contained in a <div> element, which may
     19 # also have the `page` property set on it.
     20 
     21 import os
     22 import sys
     23 
     24 # Test count, used for file numbers
     25 i = 1
     26 
     27 # Generate tests that enumerate putting each paragraph element into a div or
     28 # not, and applying the page-name to the div or the paragraph element.
     29 
     30 # Data that is used to generate the structure and element attributes for a page.
     31 ALL_DATA_COMBOS = (
     32    {"p_page": True, "use_div": False, "div_page": False},
     33    {"p_page": True, "use_div": True, "div_page": False},
     34    {"p_page": False, "use_div": True, "div_page": True},
     35 )
     36 
     37 # Process ALL_DATA_COMBOS to generate data combos for a page with a given name.
     38 
     39 
     40 def gen_data_combos(name):
     41    combos = [{"p_page": False, "use_div": False, "div_page": False}]
     42    for data in ALL_DATA_COMBOS:
     43        data_copy = data.copy()
     44        data_copy["p_page"] = data["p_page"] and name
     45        data_copy["div_page"] = data["div_page"] and name
     46        combos.append(data_copy)
     47        # Make page: auto versions for parts with empty page values.
     48        for k in ("p_page", "div_page"):
     49            # Only care about div page when there is a div
     50            if k == "div_page" and not data["use_div"]:
     51                continue
     52            if not data[k]:
     53                data_copy_auto = data_copy.copy()
     54                data_copy_auto[k] = "auto"
     55                combos.append(data_copy_auto)
     56    return combos
     57 
     58 
     59 A_DATA_COMBOS = gen_data_combos("a")
     60 B_DATA_COMBOS = gen_data_combos("b")
     61 
     62 
     63 def tag(name, page, inner=""):
     64    # Build the opening
     65    open_tag = "<" + name
     66    if page:
     67        open_tag += ' style="page:' + page + '"'
     68    open_tag += ">"
     69 
     70    close_tag = "</" + name + ">"
     71    return open_tag + inner + close_tag
     72 
     73 
     74 def generate_page_html(txt, p_page, use_div, div_page):
     75    p = tag("p", p_page, txt)
     76    if use_div:
     77        return tag("div", div_page, p)
     78    return p
     79 
     80 
     81 # Preamble to all test cases
     82 BEGIN = """\
     83 <!-- AUTOGENERATED BY generate-page-name-two-page-test.py, DO NOT MODIFY -->
     84 <!DOCTYPE html>
     85 <html class="reftest-paged">
     86 <body>
     87 """
     88 
     89 # Closing tags for all test cases.
     90 END = """
     91 </body>
     92 </html>
     93 """
     94 
     95 directory = os.path.dirname(sys.argv[0])
     96 
     97 
     98 def data_has_no_page(d):
     99    return (not d["p_page"] or d["p_page"] == "auto") and (
    100        not d["div_page"] or d["div_page"] == "auto"
    101    )
    102 
    103 
    104 for a in A_DATA_COMBOS:
    105    for b in B_DATA_COMBOS:
    106        # Test for the case of empty or auto page-names in both data
    107        if data_has_no_page(a) and data_has_no_page(b):
    108            continue
    109        file_name = "page-name-two-page-" + str(i).rjust(3, "0") + ".html"
    110        i += 1
    111        f = open(os.path.join(directory, file_name), "w")
    112        f.write(BEGIN)
    113        f.write(generate_page_html("a", **a))
    114        f.write("\n")
    115        f.write(generate_page_html("b", **b))
    116        f.write(END)
    117        f.close()