2
0

AnimationObjectGroup.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @author tschw
  3. */
  4. module( "AnimationObjectGroup" );
  5. var ObjectA = new THREE.Object3D(),
  6. ObjectB = new THREE.Object3D(),
  7. ObjectC = new THREE.Object3D(),
  8. PathA = 'object.position',
  9. PathB = 'object.rotation',
  10. PathC = 'object.scale',
  11. ParsedPathA = THREE.PropertyBinding.parseTrackName( PathA ),
  12. ParsedPathB = THREE.PropertyBinding.parseTrackName( PathB ),
  13. ParsedPathC = THREE.PropertyBinding.parseTrackName( PathC );
  14. test( "smoke test", function() {
  15. var expect = function expect( testIndex, group, bindings, path, cached, roots ) {
  16. var rootNodes = [], pathsOk = true, nodesOk = true;
  17. for ( var i = group.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
  18. if ( bindings[ i ].path !== path ) pathsOk = false;
  19. rootNodes.push( bindings[ i ].rootNode );
  20. }
  21. for ( var i = 0, n = roots.length; i !== n; ++ i ) {
  22. if ( rootNodes.indexOf( roots[ i ] ) === -1 ) nodesOk = false;
  23. }
  24. ok( pathsOk, testIndex + " paths" );
  25. ok( nodesOk, testIndex + " nodes");
  26. ok( group.nCachedObjects_ === cached, testIndex + " cache size" );
  27. ok( bindings.length - group.nCachedObjects_ === roots.length, testIndex + " object count" );
  28. };
  29. // initial state
  30. var groupA = new THREE.AnimationObjectGroup();
  31. ok( groupA instanceof THREE.AnimationObjectGroup, "constructor (w/o args)" );
  32. var bindingsAA = groupA.subscribe_( PathA, ParsedPathA );
  33. expect( 0, groupA, bindingsAA, PathA, 0, [] );
  34. var groupB = new THREE.AnimationObjectGroup( ObjectA, ObjectB );
  35. ok( groupB instanceof THREE.AnimationObjectGroup, "constructor (with args)" );
  36. var bindingsBB = groupB.subscribe_( PathB, ParsedPathB );
  37. expect( 1, groupB, bindingsBB, PathB, 0, [ ObjectA, ObjectB ] );
  38. // add
  39. groupA.add( ObjectA, ObjectB );
  40. expect( 2, groupA, bindingsAA, PathA, 0, [ ObjectA, ObjectB ] );
  41. groupB.add( ObjectC );
  42. expect( 3, groupB, bindingsBB, PathB, 0, [ ObjectA, ObjectB, ObjectC ] );
  43. // remove
  44. groupA.remove( ObjectA, ObjectC );
  45. expect( 4, groupA, bindingsAA, PathA, 1, [ ObjectB ] );
  46. groupB.remove( ObjectA, ObjectB, ObjectC );
  47. expect( 5, groupB, bindingsBB, PathB, 3, [] );
  48. // subscribe after re-add
  49. groupA.add( ObjectC );
  50. expect( 6, groupA, bindingsAA, PathA, 1, [ ObjectB, ObjectC ] );
  51. var bindingsAC = groupA.subscribe_( PathC, ParsedPathC );
  52. expect( 7, groupA, bindingsAC, PathC, 1, [ ObjectB, ObjectC ] );
  53. // re-add after subscribe
  54. var bindingsBC = groupB.subscribe_( PathC, ParsedPathC );
  55. groupB.add( ObjectA, ObjectB );
  56. expect( 8, groupB, bindingsBB, PathB, 1, [ ObjectA, ObjectB ] );
  57. // unsubscribe
  58. var copyOfBindingsBC = bindingsBC.slice();
  59. groupB.unsubscribe_( PathC );
  60. groupB.add( ObjectC );
  61. deepEqual( bindingsBC, copyOfBindingsBC, "no more update after unsubscribe" );
  62. // uncache active
  63. groupB.uncache( ObjectA );
  64. expect( 9, groupB, bindingsBB, PathB, 0, [ ObjectB, ObjectC ] );
  65. // uncache cached
  66. groupA.uncache( ObjectA );
  67. expect( 10, groupA, bindingsAC, PathC, 0, [ ObjectB, ObjectC ] );
  68. } );