utility.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. "use strict";
  2. // Copyright 2012-13 United States Government, as represented by the Secretary of Defense, Under
  3. // Secretary of Defense (Personnel & Readiness).
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed under the License
  11. // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  12. // or implied. See the License for the specific language governing permissions and limitations under
  13. // the License.
  14. /// Test utility functions.
  15. ///
  16. /// @module test/utility
  17. define( [ "module" ], function( module ) {
  18. return {
  19. /// Create a unique name from a base.
  20. ///
  21. /// @param {String} base
  22. /// A string to serve as the base part of the name.
  23. ///
  24. /// @returns {String}
  25. /// A variant of `base` different from any other `uniqueName` result for the same base.
  26. uniqueName: function( base ) {
  27. if ( uniqueNameSequence === undefined ) {
  28. uniqueNameSequence = 0;
  29. }
  30. return base + "-" + ++uniqueNameSequence;
  31. },
  32. /// Run assertions against actions on the future queue. Call QUnit's `start` when completed
  33. /// to resume testing.
  34. ///
  35. /// The assertion functions run when time leaves the time specified in the `assertions`
  36. /// objects. For example, if actions are scheduled for times t1 and t2, an assertion
  37. /// intended to test the first action should specify t1. The action at t1 will execute, time
  38. /// will move towards t2, the assertion will run, then the action at t2 will execute. If
  39. /// actions are scheduled for t1, t1, and t2, then the assertion will run after both t1
  40. /// actions execute.
  41. ///
  42. /// @param {Function} [tocked]
  43. /// A function to call each time the time changes, just before any assertions scheduled
  44. /// for that time are run.
  45. /// @param {Array} assertions
  46. /// Assertions to run at certain points in the future. Each object in `assertions` should
  47. /// contain an `assertion` field that references a function that runs QUnit assertions,
  48. /// and either an `absolute` or `relative` field specifying the time to call the function.
  49. /// Relative times are calculated from the time that `runFutureAssertions` is called.
  50. /// @param {Function} [cleanup]
  51. /// A function that may be used to clean up the test environment. If provided, `cleanup`
  52. /// runs after the assertion functions have run, just before `start` is called.
  53. runFutureAssertions: function( tocked, assertions, cleanup ) {
  54. // Interpret runFutureAssertions( assertions, cleanup ) as
  55. // runFutureAssertions( undefined, assertions, cleanup )
  56. if ( typeof tocked != "function" && ! ( tocked instanceof Function ) ) {
  57. cleanup = assertions;
  58. assertions = tocked;
  59. tocked = undefined;
  60. }
  61. // Listen for the kernel tock.
  62. window.vwf_view.tocked = function( time ) {
  63. if ( next < assertions.length ) {
  64. // Call our caller's function every time the time changes. The function runs
  65. // just before any assertions.
  66. tocked && tocked( time );
  67. // Run any assertions that are ready.
  68. while ( next < assertions.length && ready( assertions[next], time ) ) {
  69. assertions[next++].assertion();
  70. }
  71. } else {
  72. // When finished, unlisten for the kernel tock, run the cleanup function, and
  73. // tell qunit that we're done.
  74. window.vwf_view.tocked = undefined;
  75. cleanup && cleanup();
  76. start();
  77. }
  78. }
  79. var reference = vwf.time();
  80. var next = 0;
  81. // Time to run the next one?
  82. function ready( assertion, time ) {
  83. return assertion.absolute !== undefined && time > assertion.absolute ||
  84. assertion.relative !== undefined && time > reference + assertions[next].relative;
  85. }
  86. },
  87. /// Convert a component descriptor to a data URI component.
  88. dataURIFromDescriptor: function( descriptor ) {
  89. return "data:application/json;base64," + btoa( JSON.stringify( descriptor ) );
  90. },
  91. /// Convert a component descriptor to a data URI component.
  92. dataURIFromScriptText: function( scriptText ) {
  93. return "data:application/javascript;base64," + btoa( scriptText );
  94. },
  95. };
  96. /// Sequence counter for `uniqueName`.
  97. var uniqueNameSequence;
  98. } );