diff options
| author | Jez Ng | 2012-10-29 17:34:51 -0400 | 
|---|---|---|
| committer | Jez Ng | 2012-10-29 17:35:37 -0400 | 
| commit | 0964cf83b338915f9c0e3e0b581e4995ba4fb742 (patch) | |
| tree | 6941da4d87c41b62194ab5a0249f238ac7dc8bb3 | |
| parent | ba3f283e90c95d7b03e585098567606be1064937 (diff) | |
| download | vimium-0964cf83b338915f9c0e3e0b581e4995ba4fb742.tar.bz2 | |
More tests, because I like having coverage.
| -rw-r--r-- | content_scripts/vomnibar.coffee | 5 | ||||
| -rw-r--r-- | lib/dom_utils.coffee | 3 | ||||
| -rw-r--r-- | test_harnesses/visibility_test.html | 2 | ||||
| -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 | 
9 files changed, 190 insertions, 19 deletions
| diff --git a/content_scripts/vomnibar.coffee b/content_scripts/vomnibar.coffee index 31e58f15..6f4527fd 100644 --- a/content_scripts/vomnibar.coffee +++ b/content_scripts/vomnibar.coffee @@ -121,7 +121,7 @@ class VomnibarUI            @completions[@selection].performAction(openInNewTab)            @hide() -    # It seems like we have to manually supress the event here and still return true. +    # It seems like we have to manually suppress the event here and still return true.      event.stopPropagation()      event.preventDefault()      true @@ -180,7 +180,8 @@ class VomnibarUI  # Sends filter and refresh requests to a Vomnibox completer on the background page.  #  class BackgroundCompleter -  # - name: The background page completer that you want to interface with. Either "omni" or "tabs". */ +  # - name: The background page completer that you want to interface with. Either "omni", "tabs", or +  # "bookmarks". */    constructor: (@name) ->      @filterPort = chrome.extension.connect({ name: "filterCompleter" }) diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index a99cb5a6..38b23202 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -59,7 +59,8 @@ DomUtils =        # eliminate invisible elements (see test_harnesses/visibility_test.html)        computedStyle = window.getComputedStyle(element, null)        if (computedStyle.getPropertyValue('visibility') != 'visible' || -          computedStyle.getPropertyValue('display') == 'none') +          computedStyle.getPropertyValue('display') == 'none' || +          computedStyle.getPropertyValue('opacity') == '0')          continue        return clientRect diff --git a/test_harnesses/visibility_test.html b/test_harnesses/visibility_test.html index 7cfab412..c0e4f9fa 100644 --- a/test_harnesses/visibility_test.html +++ b/test_harnesses/visibility_test.html @@ -165,7 +165,7 @@      </div>      <svg>         <a class='testElement' data-expectedresult=1 xlink:href='http://www.example.com/' data-comment="This link is contained within an SVG."> -        <text x='0' y='68'  >test</text> +        <text x='0' y='68'>test</text>        </a>       </svg>    </div> 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)() | 
