Skip to content

Commit

Permalink
Fire focus and blur events when switching windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed May 25, 2012
1 parent e9e9c77 commit 3f60fcb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ zombie.js-changelog(7) -- Changelog

Tweak to in-line script processing to fix a problem no one reported.

Fire `focus` and `blur` events when switching windows.


## Version 1.1.5 2012-05-24

Expand Down
20 changes: 16 additions & 4 deletions lib/zombie/windows.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Windows

# If this is a top window, it becomes the current browser window
unless options.parent
@_current = window
@select window
return window

# Returns specific window by its name or position (e.g. "foo" returns the
Expand Down Expand Up @@ -98,16 +98,28 @@ class Windows
# If we closed the currently open window, switch to the previous window.
if window == @_current
if index > 0
@_current = @_stack[index - 1]
@select @_stack[index - 1]
else
@_current = @_stack[0]
@select @_stack[0]
return

# Select specified window as the current window.
select: (window)->
window = @_named[window] || @_stack[window] || window
return unless ~@_stack.indexOf(window)
@_current = window
[previous, @_current] = [@_current, window]
unless previous == window
# Fire onfocus and onblur event
onfocus = window.document.createEvent("HTMLEvents")
onfocus.initEvent "focus", false, false
process.nextTick ->
window.dispatchEvent onfocus
if previous
onblur = window.document.createEvent("HTMLEvents")
onblur.initEvent "blur", false, false
process.nextTick ->
previous.dispatchEvent onblur
return

# Returns the currently open window.
@prototype.__defineGetter__ "current", ->
Expand Down
110 changes: 70 additions & 40 deletions test/window_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,8 @@

describe "Window", ->

describe ".title", ->
browser = new Browser()

before (done)->
brains.get "/window/title", (req, res)->
res.send """
<html>
<head>
<title>Whatever</title>
</head>
<body>Hello World</body>
</html>
"""
brains.ready ->
browser.visit "/window/title", done

it "should return the document's title", ->
assert.equal browser.window.title, "Whatever"
it "should set the document's title", ->
browser.window.title = "Overwritten"
assert.equal browser.window.title, browser.document.title


# -- Alert, confirm and popup; form when we let browsers handle our UI --

describe ".alert", ->
browser = new Browser()

Expand Down Expand Up @@ -121,6 +100,31 @@ describe "Window", ->
assert !browser.prompted("not asked")


# -- This part deals with various windows properties ---

describe ".title", ->
browser = new Browser()

before (done)->
brains.get "/window/title", (req, res)->
res.send """
<html>
<head>
<title>Whatever</title>
</head>
<body>Hello World</body>
</html>
"""
brains.ready ->
browser.visit "/window/title", done

it "should return the document's title", ->
assert.equal browser.window.title, "Whatever"
it "should set the document's title", ->
browser.window.title = "Overwritten"
assert.equal browser.window.title, browser.document.title


describe ".screen", ->
browser = new Browser()

Expand Down Expand Up @@ -198,48 +202,74 @@ describe "Window", ->
assert.equal window.btoa("Hello, world"), "SGVsbG8sIHdvcmxk"


# -- Opening/closing/switching windows --

describe "windows", ->
browser = new Browser(name: "first")

before ->
browser.open(name: "second")
browser.open(name: "third")
assert.equal browser.windows.count, 3
@browser = new Browser(name: "first")
@browser.open(name: "second")
@browser.open(name: "third")
assert.equal @browser.windows.count, 3
@windows = @browser.windows.all()

describe "select", ->
it "should pick window by name", ->
browser.windows.select("second")
assert.equal browser.window.name, "second"
@browser.windows.select("second")
assert.equal @browser.window.name, "second"

it "should pick window by index", ->
browser.windows.select(2)
assert.equal browser.window.name, "third"
@browser.windows.select(2)
assert.equal @browser.window.name, "third"

it "should be able to select specific window", ->
browser.windows.select(browser.windows.all()[0])
assert.equal browser.window.name, "first"
@browser.windows.select(@windows[0])
assert.equal @browser.window.name, "first"

it "should fire onfocus event", (done)->
@browser.windows.select @windows[0]
@windows[1].addEventListener "focus", ->
done()
done = null # will be called multiple times from other tests
@browser.windows.select @windows[1]

it "should fire onblur event", (done)->
@browser.windows.select @windows[0]
@windows[0].addEventListener "blur", ->
done()
done = null # will be called multiple times from other tests
@browser.windows.select @windows[2]


describe "close", ->
before ->
browser.windows.close(1)
@browser.windows.select @windows[0]
@browser.windows.close(1)

it "should discard one window", ->
assert.equal browser.windows.count, 2
assert.equal @browser.windows.count, 2

it "should discard specified window", ->
assert.deepEqual browser.windows.all().map((w)-> w.name), ["first", "third"]
assert.deepEqual @browser.windows.all().map((w)-> w.name), ["first", "third"]

it "should select previous window", ->
assert.equal browser.window.name, "first"
assert.equal @browser.window.name, "first"


describe "close first", ->
before ->
browser.windows.close()
assert.equal browser.windows.count, 1
@windows[2].addEventListener "focus", =>
@onfocus = true
@browser.windows.close()

it "should close one window", ->
assert.equal @browser.windows.count, 1

it "should select next available window", ->
assert.equal browser.window.name, "third"
assert.equal @browser.window.name, "third"

it "should fire onfocus event", ->
assert @onfocus


describe "onload", ->
Expand Down

0 comments on commit 3f60fcb

Please sign in to comment.