diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/dom_tests/dom_tests.coffee | 15 | ||||
| -rw-r--r-- | tests/dom_tests/dom_tests.html | 4 | ||||
| -rw-r--r-- | tests/dom_tests/dom_utils_test.coffee | 92 | ||||
| -rw-r--r-- | tests/dom_tests/test_runner.coffee | 14 | ||||
| -rw-r--r-- | tests/dom_tests/vomnibar_test.coffee | 65 | ||||
| -rw-r--r-- | tests/unit_tests/utils_test.coffee | 9 | 
6 files changed, 184 insertions, 15 deletions
diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index 700951c6..2f1e69b5 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -214,21 +214,6 @@ context "Find prev / next links",      goNext()      assert.equal '#second', window.location.hash -Tests.outputMethod = (args...) -> -  newOutput = args.join "\n" -  # escape html -  newOutput = newOutput.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") -  # highlight the source of the error -  newOutput = newOutput.replace /\/([^:/]+):([0-9]+):([0-9]+)/, "/<span class='errorPosition'>$1:$2</span>:$3" -  document.getElementById("output-div").innerHTML += "<div class='output-section'>" + newOutput + "</div>" -  console.log.apply console, args - -# PhantomJS will call the tests manually -unless navigator.userAgent == 'phantom' -  # ensure the extension has time to load before commencing the tests -  document.addEventListener "DOMContentLoaded", -> -    setTimeout Tests.run, 200 -  createLinks = (n) ->    for i in [0...n] by 1      link = document.createElement("a") diff --git a/tests/dom_tests/dom_tests.html b/tests/dom_tests/dom_tests.html index c658235b..5de9e730 100644 --- a/tests/dom_tests/dom_tests.html +++ b/tests/dom_tests/dom_tests.html @@ -37,8 +37,12 @@      <script type="text/javascript" src="../../content_scripts/link_hints.js"></script>      <script type="text/javascript" src="../../content_scripts/vomnibar.js"></script>      <script type="text/javascript" src="../../content_scripts/vimium_frontend.js"></script> +      <script type="text/javascript" src="../shoulda.js/shoulda.js"></script>      <script type="text/javascript" src="dom_tests.js"></script> +    <script type="text/javascript" src="dom_utils_test.js"></script> +    <script type="text/javascript" src="vomnibar_test.js"></script> +    <script type="text/javascript" src="test_runner.js"></script>    </head>    <body>      <!-- should always be the first element on the page --> diff --git a/tests/dom_tests/dom_utils_test.coffee b/tests/dom_tests/dom_utils_test.coffee new file mode 100644 index 00000000..d0f881ba --- /dev/null +++ b/tests/dom_tests/dom_utils_test.coffee @@ -0,0 +1,92 @@ +context "Check visibility", + +  should "detect visible elements as visible", -> +    document.getElementById("test-div").innerHTML = """ +    <div id='foo'>test</div> +    """ +    assert.isTrue (DomUtils.getVisibleClientRect document.getElementById 'foo') != null + +  should "detect display:none links as hidden", -> +    document.getElementById("test-div").innerHTML = """ +    <a id='foo' style='display:none'>test</a> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' + +  should "detect visibility:hidden links as hidden", -> +    document.getElementById("test-div").innerHTML = """ +    <a id='foo' style='visibility:hidden'>test</a> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' + +  should "detect elements nested in display:none elements as hidden", -> +    document.getElementById("test-div").innerHTML = """ +    <div style='display:none'> +      <a id='foo'>test</a> +    </div> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' + +  should "detect links nested in visibility:hidden elements as hidden", -> +    document.getElementById("test-div").innerHTML = """ +    <div style='visibility:hidden'> +      <a id='foo'>test</a> +    </div> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' + +  should "detect links outside viewport as hidden", -> +    document.getElementById("test-div").innerHTML = """ +    <a id='foo' style='position:absolute;top:-2000px'>test</a> +    <a id='bar' style='position:absolute;left:2000px'>test</a> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'bar' + +  should "detect opacity:0 links as hidden", -> +    document.getElementById("test-div").innerHTML = """ +    <a id='foo' style='opacity:0'>test</a> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' + +  should "detect links that contain only floated / absolutely-positioned divs as visible", -> +    document.getElementById("test-div").innerHTML = """ +    <a id='foo'> +      <div style='float:left'>test</div> +    </a> +    """ +    assert.isTrue (DomUtils.getVisibleClientRect document.getElementById 'foo') != null + +    document.getElementById("test-div").innerHTML = """ +    <a id='foo'> +      <div style='position:absolute;top:0;left:0'>test</div> +    </a> +    """ +    assert.isTrue (DomUtils.getVisibleClientRect document.getElementById 'foo') != null + +  should "detect links that contain only invisible floated divs as invisible", -> +    document.getElementById("test-div").innerHTML = """ +    <a id='foo'> +      <div style='float:left;visibility:hidden'>test</div> +    </a> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' + +  should "detect links inside opacity:0 elements as visible", -> +    # XXX This is an expected failure. See issue #16. +    document.getElementById("test-div").innerHTML = """ +    <div style='opacity:0'> +      <a id='foo'>test</a> +    </div> +    """ +    assert.isTrue (DomUtils.getVisibleClientRect document.getElementById 'foo') != null + +  should "Detect links within SVGs as visible", -> +    # XXX this is an expected failure +    document.getElementById("test-div").innerHTML = """ +    <svg> +      <a id='foo' xlink:href='http://www.example.com/'> +        <text x='0' y='68'>test</text> +      </a> +    </svg> +    """ +    assert.equal null, DomUtils.getVisibleClientRect document.getElementById 'foo' diff --git a/tests/dom_tests/test_runner.coffee b/tests/dom_tests/test_runner.coffee new file mode 100644 index 00000000..9ed3cd3b --- /dev/null +++ b/tests/dom_tests/test_runner.coffee @@ -0,0 +1,14 @@ +Tests.outputMethod = (args...) -> +  newOutput = args.join "\n" +  # escape html +  newOutput = newOutput.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">") +  # highlight the source of the error +  newOutput = newOutput.replace /\/([^:/]+):([0-9]+):([0-9]+)/, "/<span class='errorPosition'>$1:$2</span>:$3" +  document.getElementById("output-div").innerHTML += "<div class='output-section'>" + newOutput + "</div>" +  console.log.apply console, args + +# PhantomJS will call the tests manually +unless navigator.userAgent == 'phantom' +  # ensure the extension has time to load before commencing the tests +  document.addEventListener "DOMContentLoaded", -> +    setTimeout Tests.run, 200 diff --git a/tests/dom_tests/vomnibar_test.coffee b/tests/dom_tests/vomnibar_test.coffee new file mode 100644 index 00000000..dc2a849f --- /dev/null +++ b/tests/dom_tests/vomnibar_test.coffee @@ -0,0 +1,65 @@ +context "Keep selection within bounds", + +  setup -> +    @completions = [] +    oldGetCompleter = Vomnibar.getCompleter.bind Vomnibar +    stub Vomnibar, 'getCompleter', (name) => +      completer = oldGetCompleter name +      stub completer, 'filter', (query, callback) => callback(@completions) +      completer + +  tearDown -> +    Vomnibar.vomnibarUI.hide() + +  should "set selection to position -1 for omni completion by default", -> +    Vomnibar.activate() +    ui = Vomnibar.vomnibarUI + +    @completions = [] +    ui.update(true) +    assert.equal -1, ui.selection + +    @completions = [{html:'foo',type:'tab',url:'http://example.com'}] +    ui.update(true) +    assert.equal -1, ui.selection + +    @completions = [] +    ui.update(true) +    assert.equal -1, ui.selection + +  should "set selection to position 0 for bookmark completion if possible", -> +    Vomnibar.activateBookmarks() +    ui = Vomnibar.vomnibarUI + +    @completions = [] +    ui.update(true) +    assert.equal -1, ui.selection + +    @completions = [{html:'foo',type:'bookmark',url:'http://example.com'}] +    ui.update(true) +    assert.equal 0, ui.selection + +    @completions = [] +    ui.update(true) +    assert.equal -1, ui.selection + +  should "keep selection within bounds", -> +    Vomnibar.activate() +    ui = Vomnibar.vomnibarUI + +    @completions = [] +    ui.update(true) + +    eventMock = +      preventDefault: -> +      stopPropagation: -> + +    @completions = [{html:'foo',type:'tab',url:'http://example.com'}] +    ui.update(true) +    stub ui, "actionFromKeyEvent", -> "down" +    ui.onKeydown eventMock +    assert.equal 0, ui.selection + +    @completions = [] +    ui.update(true) +    assert.equal -1, ui.selection diff --git a/tests/unit_tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee index 3001bb10..366133ac 100644 --- a/tests/unit_tests/utils_test.coffee +++ b/tests/unit_tests/utils_test.coffee @@ -37,7 +37,16 @@ context "convertToUrl",      assert.equal "http://127.0.0.1", Utils.convertToUrl("127.0.0.1")      assert.equal "http://127.0.0.1:8080", Utils.convertToUrl("127.0.0.1:8080")      assert.equal "http://[::]:8080", Utils.convertToUrl("[::]:8080") +    assert.equal "view-source:    0.0.0.0", Utils.convertToUrl("view-source:    0.0.0.0")    should "convert non-URL terms into search queries", ->      assert.equal "http://www.google.com/search?q=google", Utils.convertToUrl("google")      assert.equal "http://www.google.com/search?q=go%20ogle.com", Utils.convertToUrl("go ogle.com") + +context "Function currying", + +  should "Curry correctly", -> +    foo = (a, b) -> "#{a},#{b}" +    assert.equal "1,2", foo.curry()(1,2) +    assert.equal "1,2", foo.curry(1)(2) +    assert.equal "1,2", foo.curry(1,2)()  | 
