diff options
author | Bryan Berry <bryan@olenepal.org> | 2009-11-20 17:39:51 (GMT) |
---|---|---|
committer | Bryan Berry <bryan@olenepal.org> | 2009-11-20 17:39:51 (GMT) |
commit | 12dd211d05b69b0b852d0e0985e8f9e923263042 (patch) | |
tree | fb2a741909da23baccb16bd86341da9b0b458dd5 | |
parent | 516ce032de1b8a29b1d13e471695bfaf102912de (diff) |
fixed enabling and disabling buttons in adding_up_canvas. Fixed test for Karma.karma.ready() for whatever reason it only works if run last. I think it is an issue w/ qunit. Moved Karma.karma.reset to tests.js
-rwxr-xr-x | examples/adding_up_to_10_canvas/js/lesson.js | 57 | ||||
-rwxr-xr-x | js/karma.js | 32 | ||||
-rwxr-xr-x | tests/js/tests.js | 79 | ||||
-rw-r--r-- | tests/js/tests1.js | 67 |
4 files changed, 147 insertions, 88 deletions
diff --git a/examples/adding_up_to_10_canvas/js/lesson.js b/examples/adding_up_to_10_canvas/js/lesson.js index 43c1d0b..4a93451 100755 --- a/examples/adding_up_to_10_canvas/js/lesson.js +++ b/examples/adding_up_to_10_canvas/js/lesson.js @@ -33,8 +33,6 @@ $(document).ready( k.ready(function() { - - var imgNames = ["ball", "banana", "balloon","chilli", "fish", "flower"]; //game logic var total, level=0, time, n0, n1, correct; @@ -49,7 +47,7 @@ $(document).ready( var endTimerY = 100; var offsetTimerY = 5; var timerId; - var dispatchChoice; + var isGameRunning = false; var timerFn = function () { k.canvases['timer'].clear(); @@ -106,14 +104,6 @@ $(document).ready( var card = function (canvas, n, minx, miny, d ) { canvas.save(); - //var r = k.rectangle({x:minx, y:miny, width:maskd, height:maskd, - // stroke:false,fill:false}).draw(canvas); - - // var r = canvas.rectangle({x:minx, y:miny, width:maskd, height:maskd, - // stroke:false,fill:false} ); - - //do the clip - //canvas.clip(); var pos = []; var x, y, flag; @@ -174,7 +164,6 @@ $(document).ready( } else { k.sounds[ "incorrect" ].play(); } - //animate sad monkey animateChimp(false); } else { @@ -198,7 +187,6 @@ $(document).ready( var timerChimp; k.canvases["chimp"].clear(); if( answer === true){ - //k.images["happyChimp"].draw(k.canvases["chimp"], 0, 0); k.canvases["chimp"].drawImage( k.images["happyChimp"].media, 0, 0); } else { @@ -231,7 +219,7 @@ $(document).ready( var startStop = function (start) { score = level = 0; startTimerY = 10; - addChoiceButtons(); + isGameRunning = true; $.each(k.canvases, function () { if (this.name != "chimp"){ @@ -251,6 +239,7 @@ $(document).ready( var stop = function () { + isGameRunning = false; changeTimer('stop'); $.each(k.canvases, function () { @@ -258,7 +247,6 @@ $(document).ready( this.clear(); } }); - removeChoiceButtons(); }; var reset = function () { @@ -276,27 +264,22 @@ $(document).ready( buttons[ 2 ] = { "canvas": k.canvases["bottomRt"], "id": 2}; - var addChoiceButtons = function(){ - $.each(buttons, function( key, item ) { - item.canvas.node.addEventListener('click', - function dispatchChoice( ev ) { - if ( choices[ item.id ] === total){ - answer(true); - game(); - }else { - answer(false); - game(); - } - }, false); - }); - }; - - var removeChoiceButtons = function(){ - $.each(buttons, function( key, item ) { - item.canvas.node.removeEventListener('click', dispatchChoice, false); - }); - }; + $.each(buttons, function( key, item ) { + item.canvas.node + .addEventListener('click', + function ( ev ) { + if(isGameRunning === true){ + if ( choices[ item.id ] === total){ + answer(true); + game(); + }else { + answer(false); + game(); + } + } + }, false); + }); document.getElementById('start'). addEventListener('click', start, false); @@ -311,9 +294,7 @@ $(document).ready( k.canvases["chimp"].drawImage( k.images["normalChimp"].media, 0, 0); - //end of Karma.main - }); + }); - //end of ready });
\ No newline at end of file diff --git a/js/karma.js b/js/karma.js index fa1858f..a6c0a57 100755 --- a/js/karma.js +++ b/js/karma.js @@ -201,13 +201,13 @@ Karma.karma = { //ready checks to see if all assets loaded, then runs lesson code ready : function( cb ) { - that = this; + var that = this; if (Karma.karma.initialized !== true){ throw new Error("Karma.karma not initialized"); } if (this._counters.loaded !== this._counters.total){ - setTimeout(function(){ that.ready(cb);}, 100); + setTimeout(function(){ that.ready(cb);}, 10); } else if (cb) { //hide that loader status this.loaderDiv.setAttribute('style', 'display:none;'); @@ -261,33 +261,7 @@ Karma.karma = { return locale.length > 2 ? "" + lang + divider + country : lang; }, - //unit test suite uses this function - reset : function () { - if (this.statusDiv){ - this.statusDiv.parentNode.removeChild(this.statusDiv); - } - - var starterMsg = document.getElementById('starterMsg'); - if(starterMsg){ - starterMsg.parentNode.removeChild(starterMsg); - } - - this._assetPath = "assets/", - this.locale = undefined, - this._localized = false, - this._localePath = "", - this.images = {}, - this.canvases = {}, - this.sounds = {}, - this.svgs = {}, - this.videos = {}, - this.initialized = false, - this.statusDiv= undefined, - this._counters = { total : 0, errors : 0, loaded : 0}; - this.loaderDiv = undefined; - return this; - }, - + // Below are geometry and math helper methods /** diff --git a/tests/js/tests.js b/tests/js/tests.js index 789b988..d66af1a 100755 --- a/tests/js/tests.js +++ b/tests/js/tests.js @@ -34,6 +34,34 @@ return regex.test(errorMsg); }; + //unit test suite uses this function + Karma.karma.reset = function () { + if (this.statusDiv){ + this.statusDiv.parentNode.removeChild(this.statusDiv); + } + + var starterMsg = document.getElementById('starterMsg'); + if(starterMsg){ + starterMsg.parentNode.removeChild(starterMsg); + } + + this._assetPath = "assets/", + this.locale = undefined, + this._localized = false, + this._localePath = "", + this.images = {}, + this.canvases = {}, + this.sounds = {}, + this.svgs = {}, + this.videos = {}, + this.initialized = false, + this.statusDiv= undefined, + this._counters = { total : 0, errors : 0, loaded : 0}; + this.loaderDiv = undefined; + return this; + }; + + module("Module Helpers"); @@ -182,7 +210,8 @@ k.reset(); }); - + + test("Karma.karma.ready()", function () { expect(3); ok(shouldError(function () {k.ready();}), "Uninitialized karma instance " + @@ -207,25 +236,7 @@ k.reset(); }); - asyncTest("Karma.karma.ready() check callback execution", 2, - function(){ - //test that callback isn't called while asset isn't ready yet - var ninjaName = "Bruce Lee"; - var testCb = function () { ninjaName = "Chuck Norris";}; - k.reset().init(); - k._counters.total = 5000; - k.ready(testCb); - ok( ninjaName === "Bruce Lee", "callback not called before all assets loaded"); - k._counters.total = 0; - - //wait for callback to be called by ready - setTimeout(function() { - ok (ninjaName === "Chuck Norris", - "ready() calls callback after assets loaded"); - k.reset(); - start();}, - 200); - }); + test("karma.isValidLocale(locale)", function () { @@ -891,5 +902,31 @@ */ - + //for whatever reason, this test only works if run last + asyncTest("Karma.karma.ready() check callback execution", + function(){ + expect(2); + //test that callback isn't called while asset isn't ready yet + var foo = "bar"; + var testCb = function () { + foo = "baz"; + }; + + k.reset().init(); + k._counters.total = 5000; + k.ready(testCb); + ok( foo === "bar", "callback not called before all assets loaded"); + k._counters.total = k._counters.loaded = 0; + + //wait for callback to be called by ready + setTimeout(function() { + ok (foo === "baz", + "ready() calls callback after assets loaded"); + delete foo; + start(); + }, + 10); + }); + + });
\ No newline at end of file diff --git a/tests/js/tests1.js b/tests/js/tests1.js new file mode 100644 index 0000000..e79b743 --- /dev/null +++ b/tests/js/tests1.js @@ -0,0 +1,67 @@ + $(document).ready( + function(){ + // k is a shortcut for the Karma object + var k = Karma.karma; + + var hasProperties = function (properties) { + for ( prop in properties) { + if (!this[prop]){ + return false; + } + else { + return true; + } + } + }; + + var shouldError = function ( cb ){ + try { + cb(); + } + catch (e) { + return true; + } + return false; + }; + + var shouldNotError = function ( cb ) { + return !shouldError( cb ); + }; + + var checkErrorMsg = function(){ + var errorMsg = $('#errorList>li').text(); + var regex = new RegExp('error', 'i'); + return regex.test(errorMsg); + }; + + module("Module Helpers"); + + + asyncTest("Karma.karma.ready() check callback execution", + function(){ + expect(2); + //test that callback isn't called while asset isn't ready yet + k.foo = "bar"; + var testCb = function () { + k.foo = "baz"; + console.log(k.foo); + }; + + k.reset().init(); + k._counters.total = 5000; + k.ready(testCb); + ok( k.foo === "bar", "callback not called before all assets loaded"); + k._counters.total = k._counters.loaded = 0; + + //wait for callback to be called by ready + setTimeout(function() { + console.log(k.foo); + ok (k.foo === "baz", + "ready() calls callback after assets loaded"); + delete k.foo; + start(); + }, + 100); + }); + + });
\ No newline at end of file |