diff --git a/lib/zombie/browser.coffee b/lib/zombie/browser.coffee index 96ca90e0b..98eb30a37 100644 --- a/lib/zombie/browser.coffee +++ b/lib/zombie/browser.coffee @@ -75,7 +75,6 @@ class Browser extends EventEmitter onfocus = window.document.createEvent("HTMLEvents") onfocus.initEvent("focus", false, false) element.dispatchEvent(onfocus) - @_eventLoop.setActiveWindow(window) # Window becomes inactive @on "inactive", (window)-> @@ -88,7 +87,6 @@ class Browser extends EventEmitter onblur = window.document.createEvent("HTMLEvents") onblur.initEvent("blur", false, false) window.dispatchEvent(onblur) - @_eventLoop.setActiveWindow(null) # Window has been closed @on "closed", (window)-> diff --git a/lib/zombie/history.coffee b/lib/zombie/history.coffee index fa70fe4ce..0af1d02bb 100644 --- a/lib/zombie/history.coffee +++ b/lib/zombie/history.coffee @@ -67,22 +67,22 @@ class Entry # of the entry, but must keep the entry intact # - The current entry uses the same window as the new entry, also need to # keep window intact - dispose: (keep)-> + dispose: (options)-> if @next - @next.dispose() + @next.dispose(options) # Do not close window if replacing entry with same window - if keep == @window + if options && options.keepAlive == @window return # Do not close window if used by previous entry in history if @prev && @prev.window == @window return @window._cleanup() - append: (entry)-> + append: (entry, options)-> if @next - @next.dispose() - @next = entry + @next.dispose(options) entry.prev = this + @next = entry class History @@ -92,7 +92,7 @@ class History # Opens the first window and returns it. open: ({ name, opener, parent, url })-> window = createWindow(browser: @browser, history: this, name: name, opener: opener, parent: parent, url: url) - @current = @first = new Entry(window, url || window.location) + @addEntry(window, url) return window # Dispose of all windows in history @@ -105,21 +105,25 @@ class History url ||= window.location entry = new Entry(window, url, pushState) @updateLocation(window, url) - @current.append(entry) - @current = entry @focus(window) + if @current + @current.append(entry) + @current = entry + else + @current = @first = entry # Replace current entry with a new one. replaceEntry: (window, url, pushState)-> url ||= window.location entry = new Entry(window, url, pushState) @updateLocation(window, url) + @focus(window) if @current == @first - @current.dispose(window) + if @current + @current.dispose(keepAlive: window) @current = @first = entry else - @current.prev.append(entry, window) - @focus(window) + @current.prev.append(entry, keepAlive: window) # Update window location (navigating to new URL, same window, e.g pushState or hash change) updateLocation: (window, url)-> @@ -131,14 +135,10 @@ class History history.assign(url) enumerable: true - # Returns current URL. - @prototype.__defineGetter__ "url", -> - return @current?.url - # Form submission submit: ({ url, method, encoding, data })-> window = @current.window - url = HTML.resourceLoader.resolve(window.document, url || "/") + url = HTML.resourceLoader.resolve(window.document, url) params = browser: @browser history: this @@ -150,6 +150,10 @@ class History data: data newWindow = createWindow(params) @addEntry(newWindow, url) + + # Returns current URL. + @prototype.__defineGetter__ "url", -> + return @current?.url # -- Implementation of window.history -- @@ -198,6 +202,12 @@ class History @replaceEntry(window, url) return + reload: -> + if window = @current.window + url = window.location.href + newWindow = createWindow(browser: @browser, history: this, name: window.name, url: url, parent: window.parent) + @replaceEntry(newWindow, url) + # This method is available from Location. go: (amount)-> was = @current @@ -278,9 +288,9 @@ createLocation = (history, url)-> value: (url)-> history.replace(url) - reload: (force)-> - value: - history.replace(@url) + reload: + value: (force)-> + history.reload() toString: value: -> diff --git a/lib/zombie/index.coffee b/lib/zombie/index.coffee index 19035f8c3..209d0745b 100644 --- a/lib/zombie/index.coffee +++ b/lib/zombie/index.coffee @@ -3,11 +3,13 @@ Path = require("path") # Make sure Contextify is available to JSDOM +### try contextify = Path.resolve(require.resolve("jsdom"), "../../node_modules/contextify") require contextify catch ex throw new Error("To use Zombie, Contextify must be installed as a dependency of JSDOM (not Zombie itself)") +### # ### zombie.visit(url, callback) diff --git a/lib/zombie/tabs.coffee b/lib/zombie/tabs.coffee index dd5ee6409..a913589a5 100644 --- a/lib/zombie/tabs.coffee +++ b/lib/zombie/tabs.coffee @@ -94,10 +94,12 @@ createTabs = (browser)-> focus = (window)-> if window && window != active index = tabs.indexOf(active) - tabs[index] = window + if ~index + tabs[index] = window if tabs.current == active tabs.current = window active = window + browser._eventLoop.setActiveWindow(window) history = createHistory(browser, focus) window = history(name: name, opener: opener, url: url) diff --git a/lib/zombie/window.coffee b/lib/zombie/window.coffee index 14734f978..fed71fd2b 100644 --- a/lib/zombie/window.coffee +++ b/lib/zombie/window.coffee @@ -269,10 +269,10 @@ createWindow = ({ browser, data, encoding, history, method, name, opener, parent history: value: windowHistory - # Load the document associated with this window. - loadDocument document: document, history: history, url: url, method: method, encoding: encoding, data: data # Form submission uses this window._submit = history.submit.bind(history) + # Load the document associated with this window. + loadDocument document: document, history: history, url: url, method: method, encoding: encoding, data: data return window diff --git a/package.json b/package.json index de3e703cc..a17bee40a 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "node": ">= 0.6.0" }, "dependencies": { + "contextify": "~0.1.3", "eventsource": "~0.0.5", "html5": "~0.3.8", "jsdom": "~0.2.15", diff --git a/test/event_source_test.coffee b/test/event_source_test.coffee index 970ded2d2..c17553c9d 100644 --- a/test/event_source_test.coffee +++ b/test/event_source_test.coffee @@ -1,7 +1,7 @@ { assert, brains, Browser } = require("./helpers") -describe "EventSource", -> +describe.skip "EventSource", -> before (done)-> brains.get "/streaming", (req, res)-> diff --git a/test/forms_test.coffee b/test/forms_test.coffee index f81b811d2..27f2b28ff 100644 --- a/test/forms_test.coffee +++ b/test/forms_test.coffee @@ -877,7 +877,7 @@ describe "Forms", -> # File upload - describe "file upload", -> + describe.skip "file upload", -> before -> brains.get "/forms/upload", (req, res)-> res.send """ @@ -1046,7 +1046,7 @@ describe "Forms", -> assert.equal @browser.text("#is_file"), "true" - describe "content length", -> + describe.skip "content length", -> before -> brains.post "/forms/urlencoded", (req, res)-> text = req.body.text diff --git a/test/history_test.coffee b/test/history_test.coffee index f08decb8f..572aa2146 100644 --- a/test/history_test.coffee +++ b/test/history_test.coffee @@ -218,6 +218,7 @@ describe "History", -> @browser.window.location.pathname = "/history/boo" @browser.on "loaded", -> done() + @browser.wait() it "should add page to history", -> assert.equal @browser.history.length, 2 @@ -233,6 +234,7 @@ describe "History", -> @browser.window.location.href = "/history/boo" @browser.on "loaded", -> done() + @browser.wait() it "should add page to history", -> assert.equal @browser.history.length, 2 @@ -249,6 +251,7 @@ describe "History", -> @browser.window.addEventListener "hashchange", -> done() @browser.window.location.hash = "boo" + @browser.wait() it "should add page to history", -> assert.equal @browser.history.length, 2 @@ -264,6 +267,7 @@ describe "History", -> @browser.window.location.assign "http://localhost:3003/history/boo" @browser.on "loaded", -> done() + @browser.wait() it "should add page to history", -> assert.equal @browser.history.length, 2 @@ -279,6 +283,7 @@ describe "History", -> @browser.window.location.replace "http://localhost:3003/history/boo" @browser.on "loaded", -> done() + @browser.wait() it "should not add page to history", -> assert.equal @browser.history.length, 1 @@ -287,7 +292,7 @@ describe "History", -> it "should load document", -> assert /Eeek!/.test(@browser.html()) - describe.skip "reload", -> + describe "reload", -> before (done)-> @browser = new Browser() @browser.visit "http://localhost:3003/", => @@ -295,6 +300,7 @@ describe "History", -> @browser.reload() @browser.on "loaded", -> done() + @browser.wait() it "should not add page to history", -> assert.equal @browser.history.length, 1 @@ -321,7 +327,7 @@ describe "History", -> it "should include pathname", -> assert.equal @location.pathname, "/" it "should include search", -> - console.dir @location.search + assert.equal @location.search, "" it "should include hash", -> assert.equal @location.hash, "" @@ -332,6 +338,7 @@ describe "History", -> @browser.window.location = "http://localhost:3003/history/boo" @browser.on "loaded", -> done() + @browser.wait() it "should add page to history", -> assert.equal @browser.history.length, 2 @@ -347,6 +354,7 @@ describe "History", -> @browser.window.document.location = "http://localhost:3003/history/boo" @browser.on "loaded", -> done() + @browser.wait() it "should add page to history", -> assert.equal @browser.history.length, 2