socketioMEANseed/www/bower_components/angular-material/angular-material-mocks.js
www/bower_components/angular-material/angular-material-mocks.jsRaw
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
17'use strict';
18
19 // Allow our code to know when they are running inside of a test so they can expose extra services
20 // that should NOT be exposed to the public but that should be tested.
21 //
22 // As an example, see input.js which exposes some animation-related methods.
23 window._mdMocksIncluded = true;
24
25/**
26 * @ngdoc module
27 * @name ngMaterial-mock
28 * @packageName angular-material-mocks
29 *
30 * @description
31 *
32 * The `ngMaterial-mock` module provides support
33 *
34 */
35angular.module('ngMaterial-mock', [
36 'ngMock',
37 'ngAnimateMock',
38 'material.core'
39 ])
40 .config(['$provide', function($provide) {
41
42 $provide.factory('$material', ['$animate', '$timeout', function($animate, $timeout) {
43 return {
44 flushOutstandingAnimations: function() {
45 // this code is placed in a try-catch statement
46 // since 1.3 and 1.4 handle their animations differently
47 // and there may be situations where follow-up animations
48 // are run in one version and not the other
49 try { $animate.flush(); } catch(e) {}
50 },
51 flushInterimElement: function() {
52 this.flushOutstandingAnimations();
53 $timeout.flush();
54 this.flushOutstandingAnimations();
55 $timeout.flush();
56 this.flushOutstandingAnimations();
57 $timeout.flush();
58 }
59 };
60 }]);
61
62 /**
63 * Angular Material dynamically generates Style tags
64 * based on themes and palletes; for each ng-app.
65 *
66 * For testing, we want to disable generation and
67 * <style> DOM injections. So we clear the huge THEME
68 * styles while testing...
69 */
70 $provide.constant('$MD_THEME_CSS', '/**/');
71
72 /**
73 * Add throttle() and wrap .flush() to catch `no callbacks present`
74 * errors
75 */
76 $provide.decorator('$$rAF', function throttleInjector($delegate){
77
78 $delegate.throttle = function(cb) {
79 return function() {
80 cb.apply(this, arguments);
81 };
82 };
83
84 var ngFlush = $delegate.flush;
85 $delegate.flush = function() {
86 try { ngFlush(); }
87 catch(e) { ; }
88 };
89
90 return $delegate;
91 });
92
93 /**
94 * Capture $timeout.flush() errors: "No deferred tasks to be flushed"
95 * errors
96 */
97 $provide.decorator('$timeout', function throttleInjector($delegate){
98
99 var ngFlush = $delegate.flush;
100 $delegate.flush = function() {
101 var args = Array.prototype.slice.call(arguments);
102 try { ngFlush.apply($delegate, args); }
103 catch(e) { }
104 };
105
106 return $delegate;
107 });
108
109 }]);
110
111 /**
112 * Stylesheet Mocks used by `animateCss.spec.js`
113 */
114 window.createMockStyleSheet = function createMockStyleSheet(doc, wind) {
115 doc = doc ? doc[0] : window.document;
116 wind = wind || window;
117
118 var node = doc.createElement('style');
119 var head = doc.getElementsByTagName('head')[0];
120 head.appendChild(node);
121
122 var ss = doc.styleSheets[doc.styleSheets.length - 1];
123
124 return {
125 addRule: function(selector, styles) {
126 styles = addVendorPrefix(styles);
127
128 try {
129 ss.insertRule(selector + '{ ' + styles + '}', 0);
130 }
131 catch (e) {
132 try {
133 ss.addRule(selector, styles);
134 }
135 catch (e2) {}
136 }
137 },
138
139 destroy: function() {
140 head.removeChild(node);
141 }
142 };
143
144 /**
145 * Decompose styles, attached specific vendor prefixes
146 * and recompose...
147 * e.g.
148 * 'transition:0.5s linear all; font-size:100px;'
149 * becomes
150 * '-webkit-transition:0.5s linear all; transition:0.5s linear all; font-size:100px;'
151 */
152 function addVendorPrefix(styles) {
153 var cache = { };
154
155 // Decompose into cache registry
156 styles
157 .match(/([\-A-Za-z]*)\w\:\w*([A-Za-z0-9\.\-\s]*)/gi)
158 .forEach(function(style){
159 var pair = style.split(":");
160 var key = pair[0];
161
162 switch(key) {
163 case 'transition':
164 case 'transform':
165 case 'animation':
166 case 'transition-duration':
167 case 'animation-duration':
168 cache[key] = cache['-webkit-' + key] = pair[1];
169 break;
170 default:
171 cache[key] = pair[1];
172 }
173 });
174
175 // Recompose full style object (as string)
176 styles = "";
177 angular.forEach(cache, function(value, key) {
178 styles = styles + key + ":" + value + "; ";
179 });
180
181 return styles;
182 }
183
184 };
185
186})(window, window.angular);