nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / angular-scenario / jstd-scenario-adapter.js
1 /**
2  * @license AngularJS v1.0.4
3  * (c) 2010-2012 Google, Inc. http://angularjs.org
4  * License: MIT
5  */
6 (function(window) {
7 'use strict';
8
9 /**
10  * JSTestDriver adapter for angular scenario tests
11  *
12  * Example of jsTestDriver.conf for running scenario tests with JSTD:
13   <pre>
14     server: http://localhost:9877
15
16     load:
17       - lib/angular-scenario.js
18       - lib/jstd-scenario-adapter-config.js
19       - lib/jstd-scenario-adapter.js
20       # your test files go here #
21
22     proxy:
23      - {matcher: "/your-prefix/*", server: "http://localhost:8000/"}
24   </pre>
25  *
26  * For more information on how to configure jstd proxy, see {@link http://code.google.com/p/js-test-driver/wiki/Proxy}
27  * Note the order of files - it's important !
28  *
29  * Example of jstd-scenario-adapter-config.js
30   <pre>
31     var jstdScenarioAdapter = {
32       relativeUrlPrefix: '/your-prefix/'
33     };
34   </pre>
35  *
36  * Whenever you use <code>browser().navigateTo('relativeUrl')</code> in your scenario test, the relativeUrlPrefix will be prepended.
37  * You have to configure this to work together with JSTD proxy.
38  *
39  * Let's assume you are using the above configuration (jsTestDriver.conf and jstd-scenario-adapter-config.js):
40  * Now, when you call <code>browser().navigateTo('index.html')</code> in your scenario test, the browser will open /your-prefix/index.html.
41  * That matches the proxy, so JSTD will proxy this request to http://localhost:8000/index.html.
42  */
43
44 /**
45  * Custom type of test case
46  *
47  * @const
48  * @see jstestdriver.TestCaseInfo
49  */
50 var SCENARIO_TYPE = 'scenario';
51
52 /**
53  * Plugin for JSTestDriver
54  * Connection point between scenario's jstd output and jstestdriver.
55  *
56  * @see jstestdriver.PluginRegistrar
57  */
58 function JstdPlugin() {
59   var nop = function() {};
60
61   this.reportResult = nop;
62   this.reportEnd = nop;
63   this.runScenario = nop;
64
65   this.name = 'Angular Scenario Adapter';
66
67   /**
68    * Called for each JSTD TestCase
69    *
70    * Handles only SCENARIO_TYPE test cases. There should be only one fake TestCase.
71    * Runs all scenario tests (under one fake TestCase) and report all results to JSTD.
72    *
73    * @param {jstestdriver.TestRunConfiguration} configuration
74    * @param {Function} onTestDone
75    * @param {Function} onAllTestsComplete
76    * @returns {boolean} True if this type of test is handled by this plugin, false otherwise
77    */
78   this.runTestConfiguration = function(configuration, onTestDone, onAllTestsComplete) {
79     if (configuration.getTestCaseInfo().getType() != SCENARIO_TYPE) return false;
80
81     this.reportResult = onTestDone;
82     this.reportEnd = onAllTestsComplete;
83     this.runScenario();
84
85     return true;
86   };
87
88   this.getTestRunsConfigurationFor = function(testCaseInfos, expressions, testRunsConfiguration) {
89     testRunsConfiguration.push(
90         new jstestdriver.TestRunConfiguration(
91             new jstestdriver.TestCaseInfo(
92                 'Angular Scenario Tests', function() {}, SCENARIO_TYPE), []));
93
94     return true;
95   };
96 }
97
98 /**
99  * Singleton instance of the plugin
100  * Accessed using closure by:
101  *  - jstd output (reports to this plugin)
102  *  - initScenarioAdapter (register the plugin to jstd)
103  */
104 var plugin = new JstdPlugin();
105
106 /**
107  * Initialise scenario jstd-adapter
108  * (only if jstestdriver is defined)
109  *
110  * @param {Object} jstestdriver Undefined when run from browser (without jstd)
111  * @param {Function} initScenarioAndRun Function that inits scenario and runs all the tests
112  * @param {Object=} config Configuration object, supported properties:
113  *  - relativeUrlPrefix: prefix for all relative links when navigateTo()
114  */
115 function initScenarioAdapter(jstestdriver, initScenarioAndRun, config) {
116   if (jstestdriver) {
117     // create and register ScenarioPlugin
118     jstestdriver.pluginRegistrar.register(plugin);
119     plugin.runScenario = initScenarioAndRun;
120
121     /**
122      * HACK (angular.scenario.Application.navigateTo)
123      *
124      * We need to navigate to relative urls when running from browser (without JSTD),
125      * because we want to allow running scenario tests without creating its own virtual host.
126      * For example: http://angular.local/build/docs/docs-scenario.html
127      *
128      * On the other hand, when running with JSTD, we need to navigate to absolute urls,
129      * because of JSTD proxy. (proxy, because of same domain policy)
130      *
131      * So this hack is applied only if running with JSTD and change all relative urls to absolute.
132      */
133     var appProto = angular.scenario.Application.prototype,
134         navigateTo = appProto.navigateTo,
135         relativeUrlPrefix = config && config.relativeUrlPrefix || '/';
136
137     appProto.navigateTo = function(url, loadFn, errorFn) {
138       if (url.charAt(0) != '/' && url.charAt(0) != '#' &&
139           url != 'about:blank' && !url.match(/^https?/)) {
140         url = relativeUrlPrefix + url;
141       }
142
143       return navigateTo.call(this, url, loadFn, errorFn);
144     };
145   }
146 }
147
148 /**
149  * Builds proper TestResult object from given model spec
150  *
151  * TODO(vojta) report error details
152  *
153  * @param {angular.scenario.ObjectModel.Spec} spec
154  * @returns {jstestdriver.TestResult}
155  */
156 function createTestResultFromSpec(spec) {
157   var map = {
158     success: 'PASSED',
159     error:   'ERROR',
160     failure: 'FAILED'
161   };
162
163   return new jstestdriver.TestResult(
164     spec.fullDefinitionName,
165     spec.name,
166     jstestdriver.TestResult.RESULT[map[spec.status]],
167     spec.error || '',
168     spec.line || '',
169     spec.duration);
170 }
171
172 /**
173  * Generates JSTD output (jstestdriver.TestResult)
174  */
175 angular.scenario.output('jstd', function(context, runner, model) {
176   model.on('SpecEnd', function(spec) {
177     plugin.reportResult(createTestResultFromSpec(spec));
178   });
179
180   model.on('RunnerEnd', function() {
181     plugin.reportEnd();
182   });
183 });
184 initScenarioAdapter(window.jstestdriver, angular.scenario.setUpAndRun, window.jstdScenarioAdapter);
185 })(window);