You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.7 KiB

  1. /**
  2. *
  3. * Angular-Material-Mocks
  4. *
  5. * Developers interested in running their own custom unit tests WITH angular-material.js loaded...
  6. * must also include this *mocks* file. Similar to `angular-mocks.js`, `angular-material-mocks.js`
  7. * will override and disable specific Angular Material performance settings:
  8. *
  9. * - Disabled Theme CSS rule generations
  10. * - Forces $mdAria.expectWithText() to be synchronous
  11. * - Mocks $$rAF.throttle()
  12. * - Captures flush exceptions from $$rAF
  13. *
  14. */
  15. (function(window, angular, undefined) {
  16. 'use strict';
  17. /**
  18. * @ngdoc module
  19. * @name ngMaterial-mock
  20. * @packageName angular-material-mocks
  21. *
  22. * @description
  23. *
  24. * The `ngMaterial-mock` module provides support
  25. *
  26. */
  27. angular.module('ngMaterial-mock', [
  28. 'ngMock',
  29. 'ngAnimateMock',
  30. 'material.core'
  31. ])
  32. .config(['$provide', function($provide) {
  33. $provide.factory('$material', ['$animate', '$timeout', function($animate, $timeout) {
  34. return {
  35. flushOutstandingAnimations: function() {
  36. // this code is placed in a try-catch statement
  37. // since 1.3 and 1.4 handle their animations differently
  38. // and there may be situations where follow-up animations
  39. // are run in one version and not the other
  40. try { $animate.flush(); } catch(e) {}
  41. },
  42. flushInterimElement: function() {
  43. this.flushOutstandingAnimations();
  44. $timeout.flush();
  45. this.flushOutstandingAnimations();
  46. $timeout.flush();
  47. this.flushOutstandingAnimations();
  48. $timeout.flush();
  49. }
  50. };
  51. }]);
  52. /**
  53. * Angular Material dynamically generates Style tags
  54. * based on themes and palletes; for each ng-app.
  55. *
  56. * For testing, we want to disable generation and
  57. * <style> DOM injections. So we clear the huge THEME
  58. * styles while testing...
  59. */
  60. $provide.constant('$MD_THEME_CSS', '/**/');
  61. /**
  62. * Add throttle() and wrap .flush() to catch `no callbacks present`
  63. * errors
  64. */
  65. $provide.decorator('$$rAF', function throttleInjector($delegate){
  66. $delegate.throttle = function(cb) {
  67. return function() {
  68. cb.apply(this, arguments);
  69. };
  70. };
  71. var ngFlush = $delegate.flush;
  72. $delegate.flush = function() {
  73. try { ngFlush(); }
  74. catch(e) { ; }
  75. };
  76. return $delegate;
  77. });
  78. /**
  79. * Capture $timeout.flush() errors: "No deferred tasks to be flushed"
  80. * errors
  81. */
  82. $provide.decorator('$timeout', function throttleInjector($delegate){
  83. var ngFlush = $delegate.flush;
  84. $delegate.flush = function() {
  85. var args = Array.prototype.slice.call(arguments);
  86. try { ngFlush.apply($delegate, args); }
  87. catch(e) { }
  88. };
  89. return $delegate;
  90. });
  91. }]);
  92. /**
  93. * Stylesheet Mocks used by `animateCss.spec.js`
  94. */
  95. window.createMockStyleSheet = function createMockStyleSheet(doc, wind) {
  96. doc = doc ? doc[0] : window.document;
  97. wind = wind || window;
  98. var node = doc.createElement('style');
  99. var head = doc.getElementsByTagName('head')[0];
  100. head.appendChild(node);
  101. var ss = doc.styleSheets[doc.styleSheets.length - 1];
  102. return {
  103. addRule: function(selector, styles) {
  104. styles = addVendorPrefix(styles);
  105. try {
  106. ss.insertRule(selector + '{ ' + styles + '}', 0);
  107. }
  108. catch (e) {
  109. try {
  110. ss.addRule(selector, styles);
  111. }
  112. catch (e2) {}
  113. }
  114. },
  115. destroy: function() {
  116. head.removeChild(node);
  117. }
  118. };
  119. /**
  120. * Decompose styles, attached specific vendor prefixes
  121. * and recompose...
  122. * e.g.
  123. * 'transition:0.5s linear all; font-size:100px;'
  124. * becomes
  125. * '-webkit-transition:0.5s linear all; transition:0.5s linear all; font-size:100px;'
  126. */
  127. function addVendorPrefix(styles) {
  128. var cache = { };
  129. // Decompose into cache registry
  130. styles
  131. .match(/([\-A-Za-z]*)\w\:\w*([A-Za-z0-9\.\-\s]*)/gi)
  132. .forEach(function(style){
  133. var pair = style.split(":");
  134. var key = pair[0];
  135. switch(key) {
  136. case 'transition':
  137. case 'transform':
  138. case 'animation':
  139. case 'transition-duration':
  140. case 'animation-duration':
  141. cache[key] = cache['-webkit-' + key] = pair[1];
  142. break;
  143. default:
  144. cache[key] = pair[1];
  145. }
  146. });
  147. // Recompose full style object (as string)
  148. styles = "";
  149. angular.forEach(cache, function(value, key) {
  150. styles = styles + key + ":" + value + "; ";
  151. });
  152. return styles;
  153. }
  154. };
  155. })(window, window.angular);