/tests/js-tests/src/MemoryModelTest/MemoryModelTest.js

https://gitlab.com/0072016/cocos2d-x-2d · JavaScript · 263 lines · 155 code · 35 blank · 73 comment · 5 complexity · e3544468f0889b870c75265ee1547674 MD5 · raw file

  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. var memoryModelTestSceneIdx = -1;
  22. //------------------------------------------------------------------
  23. //
  24. // MemoryModelTestBase
  25. //
  26. //------------------------------------------------------------------
  27. var MemoryModelTestBase = BaseTestLayer.extend({
  28. _title:"",
  29. _subtitle:"",
  30. onRestartCallback:function (sender) {
  31. var s = new MemoryModelTestScene();
  32. s.addChild(restartMemoryModelTest());
  33. director.runScene(s);
  34. },
  35. onNextCallback:function (sender) {
  36. var s = new MemoryModelTestScene();
  37. s.addChild(nextMemoryModelTest());
  38. director.runScene(s);
  39. },
  40. onBackCallback:function (sender) {
  41. var s = new MemoryModelTestScene();
  42. s.addChild(previousMemoryModelTest());
  43. director.runScene(s);
  44. },
  45. // automation
  46. numberOfPendingTests:function() {
  47. return ( (arrayOfMemoryModelTest.length-1) - memoryModelTestSceneIdx );
  48. },
  49. getTestNumber:function() {
  50. return memoryModelTestSceneIdx;
  51. }
  52. });
  53. //------------------------------------------------------------------
  54. //
  55. // Set property on sprite
  56. //
  57. //------------------------------------------------------------------
  58. var SetPropertyMemoryModelTest = MemoryModelTestBase.extend({
  59. _title:"Set Property Test",
  60. _subtitle:"See console for possible errors",
  61. ctor:function () {
  62. cc.sys.garbageCollect();
  63. this._super();
  64. var sprite = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  65. var tag = 10;
  66. this.addChild(sprite, 0, tag);
  67. var x = winSize.width / 2;
  68. var y = winSize.height / 2;
  69. sprite.setPosition(x, y);
  70. // add random property
  71. sprite.randomProperty = "hello world";
  72. sprite = this.getChildByTag(tag);
  73. // should print "hello world"
  74. this.log(sprite.randomProperty);
  75. },
  76. });
  77. //------------------------------------------------------------------
  78. //
  79. // Using Ivar 1: from ctor to onEnter
  80. //
  81. //------------------------------------------------------------------
  82. var Ivar1MemoryModelTest = MemoryModelTestBase.extend({
  83. _title:"Using ivars to hold C++ objects",
  84. _subtitle:"From ctor to onEnter",
  85. ctor:function () {
  86. this._super();
  87. this.sprite = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  88. },
  89. onEnter:function() {
  90. this._super();
  91. this.addChild(this.sprite);
  92. var x = winSize.width / 2;
  93. var y = winSize.height / 2;
  94. this.sprite.setPosition(x, y);
  95. },
  96. });
  97. //------------------------------------------------------------------
  98. //
  99. // Using Ivar 2: from ctor to update
  100. //
  101. //------------------------------------------------------------------
  102. var Ivar2MemoryModelTest = MemoryModelTestBase.extend({
  103. _title:"Using ivars to hold C++ objects",
  104. _subtitle:"From ctor to update",
  105. ctor:function () {
  106. this._super();
  107. this.sprite = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  108. this.scheduleOnce(this.showSprite, 0.5);
  109. },
  110. showSprite:function() {
  111. this.addChild(this.sprite);
  112. var x = winSize.width / 2;
  113. var y = winSize.height / 2;
  114. this.sprite.setPosition(x, y);
  115. },
  116. });
  117. var MemoryModelTestScene = TestScene.extend({
  118. runThisTest:function (num) {
  119. memoryModelTestSceneIdx = (num || num == 0) ? (num - 1) : -1;
  120. var layer = nextMemoryModelTest();
  121. this.addChild(layer);
  122. director.runScene(this);
  123. }
  124. });
  125. //------------------------------------------------------------------
  126. //
  127. // Using Local vars
  128. //
  129. //------------------------------------------------------------------
  130. var LocalVarMemoryModelTest = MemoryModelTestBase.extend({
  131. _title:"Using local vars + GC",
  132. _subtitle:"native objects should get destroyed",
  133. ctor:function () {
  134. this._super();
  135. var sprite1 = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  136. var sprite2 = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  137. var sprite3 = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  138. var a = 10;
  139. this.addChild(sprite1);
  140. this.removeChild(sprite1);
  141. // this.addChild(sprite2);
  142. // this.removeChild(sprite2);
  143. this.addChild(sprite3);
  144. this.removeChild(sprite3);
  145. //cc.sys.dumpRoot();
  146. cc.sys.garbageCollect();
  147. cc.log(sprite1);
  148. cc.log(sprite2);
  149. cc.log(sprite3);
  150. cc.log(a);
  151. },
  152. });
  153. //------------------------------------------------------------------
  154. //
  155. // Using Local vars
  156. //
  157. //------------------------------------------------------------------
  158. var RetainRootsMemoryModelTest = MemoryModelTestBase.extend({
  159. _title:"retain must root",
  160. _subtitle:"native objects should not get destroyed",
  161. ctor:function () {
  162. this._super();
  163. var sprite = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  164. // addChild should root the sprite
  165. this.addChild(sprite);
  166. cc.sys.garbageCollect();
  167. var x = winSize.width / 2;
  168. var y = winSize.height / 2;
  169. sprite.setPosition(x, y);
  170. },
  171. });
  172. //------------------------------------------------------------------
  173. //
  174. // Testing Root/Unroot
  175. //
  176. //------------------------------------------------------------------
  177. var RootUnrootMemoryModelTest = MemoryModelTestBase.extend({
  178. _title:"root/unroot",
  179. _subtitle:"rooting/unrooting with GC memory model",
  180. ctor:function () {
  181. this._super();
  182. var sprite = new cc.Sprite(s_grossini_dance_atlas, cc.rect(0, 0, 85, 121));
  183. // addChild should root the sprite
  184. this.addChild(sprite);
  185. // should unroot the sprite
  186. this.removeChild(sprite)
  187. cc.sys.garbageCollect();
  188. },
  189. });
  190. //
  191. // Entry point
  192. //
  193. var MemoryModelTestScene = TestScene.extend({
  194. runThisTest:function (num) {
  195. memoryModelTestSceneIdx = (num || num == 0) ? (num - 1) : -1;
  196. var layer = nextMemoryModelTest();
  197. this.addChild(layer);
  198. director.runScene(this);
  199. }
  200. });
  201. //
  202. // Flow control
  203. //
  204. var arrayOfMemoryModelTest = [
  205. SetPropertyMemoryModelTest,
  206. Ivar1MemoryModelTest,
  207. Ivar2MemoryModelTest,
  208. LocalVarMemoryModelTest,
  209. RetainRootsMemoryModelTest,
  210. RootUnrootMemoryModelTest,
  211. ];
  212. var nextMemoryModelTest = function () {
  213. memoryModelTestSceneIdx++;
  214. memoryModelTestSceneIdx = memoryModelTestSceneIdx % arrayOfMemoryModelTest.length;
  215. return new arrayOfMemoryModelTest[memoryModelTestSceneIdx]();
  216. };
  217. var previousMemoryModelTest = function () {
  218. memoryModelTestSceneIdx--;
  219. if (memoryModelTestSceneIdx < 0)
  220. memoryModelTestSceneIdx += arrayOfMemoryModelTest.length;
  221. return new arrayOfMemoryModelTest[memoryModelTestSceneIdx]();
  222. };
  223. var restartMemoryModelTest = function () {
  224. return new arrayOfMemoryModelTest[memoryModelTestSceneIdx]();
  225. };