aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJez Ng2012-09-03 23:19:35 -0400
committerJez Ng2012-09-08 04:30:26 -0400
commit6df16c591219d87058b4c48682d503382e44693f (patch)
tree3ec4044a51c557cd59df6d6a9ff712e6491a9c18
parent8437dd96144475343562c9a6aa2f14469bc75a56 (diff)
downloadvimium-6df16c591219d87058b4c48682d503382e44693f.tar.bz2
Set up PhantomJS testing.
-rw-r--r--Cakefile20
-rw-r--r--background_scripts/main.coffee2
-rw-r--r--tests/dom_tests/bind.js27
-rw-r--r--tests/dom_tests/chrome.coffee20
-rw-r--r--tests/dom_tests/dom_tests.coffee (renamed from test_harnesses/automated/automated.coffee)9
-rw-r--r--tests/dom_tests/dom_tests.html (renamed from test_harnesses/automated/automated.html)6
-rw-r--r--tests/dom_tests/phantom_runner.coffee32
-rw-r--r--tests/unit_tests/completion_test.coffee (renamed from tests/completion_test.coffee)4
-rw-r--r--tests/unit_tests/test_helper.coffee (renamed from tests/test_helper.coffee)4
-rw-r--r--tests/unit_tests/utils_test.coffee (renamed from tests/utils_test.coffee)2
10 files changed, 110 insertions, 16 deletions
diff --git a/Cakefile b/Cakefile
index 697c03dc..1c324497 100644
--- a/Cakefile
+++ b/Cakefile
@@ -55,10 +55,20 @@ task "package", "build .crx file", ->
crxmake.stdout.on "data", (data) -> console.log data.toString().trim()
crxmake.on "exit", -> fs.writeFileSync "manifest.json", orig_manifest_text
-task "test", "run all unit tests", ->
- test_files = fs.readdirSync("tests/").filter((filename) -> filename.indexOf("_test.js") > 0)
- test_files = test_files.map((filename) -> "tests/" + filename)
+task "test", "run all tests", ->
+ console.log "Running unit tests..."
+ basedir = "tests/unit_tests/"
+ test_files = fs.readdirSync(basedir).filter((filename) -> filename.indexOf("_test.js") > 0)
+ test_files = test_files.map((filename) -> basedir + filename)
test_files.forEach (file) -> require "./" + file
Tests.run()
- if Tests.testsFailed > 0
- process.exit 1
+ returnCode = if Tests.testsFailed > 0 then 1 else 0
+
+ console.log "Running DOM tests..."
+ spawn = (require "child_process").spawn
+ phantom = spawn "phantomjs", ["./tests/dom_tests/phantom_runner.js"]
+ phantom.stdout.on 'data', (data) -> process.stdout.write data
+ phantom.stderr.on 'data', (data) -> process.stderr.write data
+ phantom.on 'exit', (code) ->
+ returnCode += code
+ process.exit returnCode
diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee
index 4a68ced6..bc3fde0b 100644
--- a/background_scripts/main.coffee
+++ b/background_scripts/main.coffee
@@ -553,7 +553,7 @@ sendRequestHandlers =
refreshCompleter: refreshCompleter
# Convenience function for development use.
-window.runTests = -> open(chrome.extension.getURL('test_harnesses/automated/automated.html'))
+window.runTests = -> open(chrome.extension.getURL('tests/dom_tests/dom_tests.html'))
#
# Begin initialization.
diff --git a/tests/dom_tests/bind.js b/tests/dom_tests/bind.js
new file mode 100644
index 00000000..833f8006
--- /dev/null
+++ b/tests/dom_tests/bind.js
@@ -0,0 +1,27 @@
+/*
+ * Polyfill taken from https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind
+ * Necessary because the current version of PhantomJS doesn't yet support bind().
+ */
+if (!Function.prototype.bind) {
+ Function.prototype.bind = function (oThis) {
+ if (typeof this !== "function") {
+ // closest thing possible to the ECMAScript 5 internal IsCallable function
+ throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
+ }
+
+ var aArgs = Array.prototype.slice.call(arguments, 1),
+ fToBind = this,
+ fNOP = function () {},
+ fBound = function () {
+ return fToBind.apply(this instanceof fNOP && oThis
+ ? this
+ : oThis,
+ aArgs.concat(Array.prototype.slice.call(arguments)));
+ };
+
+ fNOP.prototype = this.prototype;
+ fBound.prototype = new fNOP();
+
+ return fBound;
+ };
+}
diff --git a/tests/dom_tests/chrome.coffee b/tests/dom_tests/chrome.coffee
new file mode 100644
index 00000000..ff7a53d0
--- /dev/null
+++ b/tests/dom_tests/chrome.coffee
@@ -0,0 +1,20 @@
+#
+# Mock the Chrome extension API.
+#
+
+root = exports ? window
+
+root.chrome = {
+ extension: {
+ connect: -> {
+ onMessage: {
+ addListener: ->
+ }
+ postMessage: ->
+ }
+ onRequest: {
+ addListener: ->
+ }
+ sendRequest: ->
+ }
+}
diff --git a/test_harnesses/automated/automated.coffee b/tests/dom_tests/dom_tests.coffee
index b5e5af35..f4e63270 100644
--- a/test_harnesses/automated/automated.coffee
+++ b/tests/dom_tests/dom_tests.coffee
@@ -30,6 +30,7 @@ createGeneralHintTests = (isFilteredMode) ->
testContent = "<a>test</a>" + "<a>tress</a>"
document.getElementById("test-div").innerHTML = testContent
stub settings.values, "filterLinkHints", false
+ stub settings.values, "linkHintCharacters", "ab"
tearDown ->
document.getElementById("test-div").innerHTML = ""
@@ -186,9 +187,11 @@ Tests.outputMethod = (args...) ->
document.getElementById("output-div").innerHTML += "<div class='output-section'>" + newOutput + "</div>"
console.log.apply console, args
-# ensure the extension has time to load before commencing the tests
-document.addEventListener "DOMContentLoaded", ->
- setTimeout Tests.run, 200
+# 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
diff --git a/test_harnesses/automated/automated.html b/tests/dom_tests/dom_tests.html
index a0c8c7de..1dc45782 100644
--- a/test_harnesses/automated/automated.html
+++ b/tests/dom_tests/dom_tests.html
@@ -27,6 +27,8 @@
}
</style>
<link rel="stylesheet" type="text/css" href="../../vimium.css" />
+ <script type="text/javascript" src="bind.js"></script>
+ <script type="text/javascript" src="chrome.js"></script>
<script type="text/javascript" src="../../lib/utils.js"></script>
<script type="text/javascript" src="../../lib/keyboard_utils.js"></script>
<script type="text/javascript" src="../../lib/dom_utils.js"></script>
@@ -34,8 +36,8 @@
<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="../../tests/shoulda.js/shoulda.js"></script>
- <script type="text/javascript" src="automated.js"></script>
+ <script type="text/javascript" src="../shoulda.js/shoulda.js"></script>
+ <script type="text/javascript" src="dom_tests.js"></script>
</head>
<body>
<!-- should always be the first element on the page -->
diff --git a/tests/dom_tests/phantom_runner.coffee b/tests/dom_tests/phantom_runner.coffee
new file mode 100644
index 00000000..f0f2b128
--- /dev/null
+++ b/tests/dom_tests/phantom_runner.coffee
@@ -0,0 +1,32 @@
+page = require('webpage').create()
+
+page.settings.userAgent = 'phantom'
+
+# ensure that the elements we test the link hints on are actually visible
+page.viewportSize =
+ width: 900
+ height: 600
+
+page.onConsoleMessage = (msg) ->
+ console.log msg
+
+system = require 'system'
+fs = require 'fs'
+
+pathParts = system.args[0].split(fs.separator)
+pathParts[pathParts.length - 1] = ''
+dirname = pathParts.join(fs.separator)
+
+page.open dirname + 'dom_tests.html', (status) ->
+ if status != 'success'
+ console.log 'Unable to load tests.'
+ phantom.exit 1
+
+ testsFailed = page.evaluate ->
+ Tests.run()
+ return Tests.testsFailed
+
+ if testsFailed > 0
+ phantom.exit 1
+ else
+ phantom.exit 0
diff --git a/tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee
index 7094d720..d3369398 100644
--- a/tests/completion_test.coffee
+++ b/tests/unit_tests/completion_test.coffee
@@ -1,6 +1,6 @@
require "./test_helper.js"
-extend(global, require "../lib/utils.js")
-extend(global, require "../background_scripts/completion.js")
+extend(global, require "../../lib/utils.js")
+extend(global, require "../../background_scripts/completion.js")
global.chrome = {}
diff --git a/tests/test_helper.coffee b/tests/unit_tests/test_helper.coffee
index 237f8e24..bb73bf54 100644
--- a/tests/test_helper.coffee
+++ b/tests/unit_tests/test_helper.coffee
@@ -1,5 +1,5 @@
-require("./shoulda.js/shoulda.js")
+require("../shoulda.js/shoulda.js")
global.extend = (hash1, hash2) ->
for key of hash2
hash1[key] = hash2[key]
- hash1 \ No newline at end of file
+ hash1
diff --git a/tests/utils_test.coffee b/tests/unit_tests/utils_test.coffee
index c5e5d002..6a44b460 100644
--- a/tests/utils_test.coffee
+++ b/tests/unit_tests/utils_test.coffee
@@ -1,5 +1,5 @@
require "./test_helper.js"
-extend(global, require "../lib/utils.js")
+extend(global, require "../../lib/utils.js")
context "convertToUrl",
should "detect and clean up valid URLs", ->