blossom
Version:
Modern, Cross-Platform Application Framework
134 lines (106 loc) • 4.51 kB
JavaScript
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2010 Apple Inc. and contributors.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
/*global module test equals context ok same Q$ CommonSetup */
var pane, r, view ;
CommonSetup = {
setup: function() {
pane = SC.Pane.create({
childViews: [SC.View]
});
view = pane.childViews[0];
pane.makeFirstResponder(view);
pane.append(); // make visible so it will have root responder
r = pane.get('rootResponder');
ok(r, 'has root responder');
},
teardown: function() {
pane.remove();
pane = r = view = null ;
}
};
// ..........................................................
// becomeKeyPane()
//
suite("SC.Pane#becomeKeyPane", CommonSetup);
test("should become keyPane if not already keyPane", function() {
ok(r.get('keyPane') !== pane, 'precond - pane is not currently key');
pane.becomeKeyPane();
equals(r.get('keyPane'), pane, 'pane should be keyPane');
equals(pane.get('isKeyPane'), true, 'isKeyPane should be set to true');
});
test("should do nothing if acceptsKeyPane is false", function() {
ok(r.get('keyPane') !== pane, 'precond - pane is not currently key');
pane.acceptsKeyPane = false ;
pane.becomeKeyPane();
ok(r.get('keyPane') !== pane, 'pane should not be keyPane');
equals(pane.get('isKeyPane'), false, 'isKeyPane should be false');
});
test("should invoke willBecomeKeyPane & didBecomeKeyPane", function() {
var callCount = 0 ;
pane.willBecomeKeyPaneFrom = pane.didBecomeKeyPaneFrom = function() {
callCount++;
};
pane.becomeKeyPane();
equals(callCount, 2, 'should invoke both callbacks');
callCount = 0;
pane.becomeKeyPane();
equals(callCount, 0, 'should not invoke callbacks if already key pane');
});
test("should invoke callbacks and update isKeyResponder state on firstResponder", function() {
var callCount = 0;
view.willBecomeKeyResponderFrom = view.didBecomeKeyResponderFrom =
function() { callCount++; };
equals(view.get('isKeyResponder'), false, 'precond - view is not keyResponder');
equals(view.get('isFirstResponder'), true, 'precond - view is firstResponder');
pane.becomeKeyPane();
equals(callCount, 2, 'should invoke both callbacks');
equals(view.get('isKeyResponder'), true, 'should be keyResponder');
});
// ..........................................................
// resignKeyPane()
//
suite("SC.Pane#resignKeyPane", CommonSetup);
test("should resign keyPane if keyPane", function() {
pane.becomeKeyPane();
ok(r.get('keyPane') === pane, 'precond - pane is currently key');
pane.resignKeyPane();
ok(r.get('keyPane') !== pane, 'pane should NOT be keyPane');
equals(pane.get('isKeyPane'), false, 'isKeyPane should be set to false');
});
// technically this shouldn't happen, but someone could screw up and change
// acceptsKeyPane to false once the pane has already become key
test("should still resign if acceptsKeyPane is false", function() {
pane.becomeKeyPane();
ok(r.get('keyPane') === pane, 'precond - pane is currently key');
pane.acceptsKeyPane = false ;
pane.resignKeyPane();
ok(r.get('keyPane') !== pane, 'pane should NOT be keyPane');
equals(pane.get('isKeyPane'), false, 'isKeyPane should be set to false');
});
test("should invoke willLoseKeyPaneTo & didLoseKeyPaneTo", function() {
pane.becomeKeyPane();
ok(r.get('keyPane') === pane, 'precond - pane is currently key');
var callCount = 0 ;
pane.willLoseKeyPaneTo = pane.didLoseKeyPaneTo = function() {
callCount++;
};
pane.resignKeyPane();
equals(callCount, 2, 'should invoke both callbacks');
callCount = 0;
pane.resignKeyPane();
equals(callCount, 0, 'should not invoke callbacks if already key pane');
});
test("should invoke callbacks and update isKeyResponder state on firstResponder", function() {
var callCount = 0;
view.willLoseKeyResponderTo = view.didLoseKeyResponderTo =
function() { callCount++; };
pane.becomeKeyPane();
equals(view.get('isKeyResponder'), true, 'precond - view is keyResponder');
equals(view.get('isFirstResponder'), true, 'precond - view is firstResponder');
pane.resignKeyPane();
equals(callCount, 2, 'should invoke both callbacks');
equals(view.get('isKeyResponder'), false, 'should be keyResponder');
});