1 |
|
2 | var stringify = require('../common/stringify')
|
3 |
|
4 |
|
5 | var ContextKarma = function (callParentKarmaMethod) {
|
6 |
|
7 | var hasError = false
|
8 | var self = this
|
9 |
|
10 |
|
11 |
|
12 | this.log = function (type, args) {
|
13 | var values = []
|
14 |
|
15 | for (var i = 0; i < args.length; i++) {
|
16 | values.push(this.stringify(args[i], 3))
|
17 | }
|
18 |
|
19 | this.info({log: values.join(', '), type: type})
|
20 | }
|
21 |
|
22 | this.stringify = stringify
|
23 |
|
24 |
|
25 |
|
26 | this.error = function () {
|
27 | hasError = true
|
28 | callParentKarmaMethod('error', [].slice.call(arguments))
|
29 | return false
|
30 | }
|
31 |
|
32 |
|
33 | var UNIMPLEMENTED_START = function () {
|
34 | this.error('You need to include some adapter that implements __karma__.start method!')
|
35 | }
|
36 |
|
37 | this.loaded = function () {
|
38 |
|
39 | if (!hasError) {
|
40 | this.start(this.config)
|
41 | }
|
42 |
|
43 |
|
44 | this.start = UNIMPLEMENTED_START
|
45 | }
|
46 |
|
47 |
|
48 | this.start = UNIMPLEMENTED_START
|
49 |
|
50 |
|
51 |
|
52 | var proxyMethods = ['complete', 'info', 'result']
|
53 | for (var i = 0; i < proxyMethods.length; i++) {
|
54 | (function bindProxyMethod (methodName) {
|
55 | self[methodName] = function boundProxyMethod () {
|
56 | callParentKarmaMethod(methodName, [].slice.call(arguments))
|
57 | }
|
58 | }(proxyMethods[i]))
|
59 | }
|
60 |
|
61 |
|
62 | this.setupContext = function (contextWindow) {
|
63 |
|
64 |
|
65 | if (self.config.clearContext && hasError) {
|
66 | return
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 | contextWindow.onerror = function () {
|
72 | return self.error.apply(self, arguments)
|
73 | }
|
74 |
|
75 | contextWindow.onbeforeunload = function (e, b) {
|
76 | callParentKarmaMethod('onbeforeunload', [])
|
77 | }
|
78 |
|
79 | contextWindow.dump = function () {
|
80 | self.log('dump', arguments)
|
81 | }
|
82 |
|
83 | var _confirm = contextWindow.confirm
|
84 | var _prompt = contextWindow.prompt
|
85 |
|
86 | contextWindow.alert = function (msg) {
|
87 | self.log('alert', [msg])
|
88 | }
|
89 |
|
90 | contextWindow.confirm = function (msg) {
|
91 | self.log('confirm', [msg])
|
92 | return _confirm(msg)
|
93 | }
|
94 |
|
95 | contextWindow.prompt = function (msg, defaultVal) {
|
96 | self.log('prompt', [msg, defaultVal])
|
97 | return _prompt(msg, defaultVal)
|
98 | }
|
99 |
|
100 |
|
101 | var getConsole = function (currentWindow) {
|
102 | return currentWindow.console || {
|
103 | log: function () {},
|
104 | info: function () {},
|
105 | warn: function () {},
|
106 | error: function () {},
|
107 | debug: function () {}
|
108 | }
|
109 | }
|
110 | if (self.config.captureConsole) {
|
111 |
|
112 | var localConsole = contextWindow.console = getConsole(contextWindow)
|
113 | var logMethods = ['log', 'info', 'warn', 'error', 'debug']
|
114 | var patchConsoleMethod = function (method) {
|
115 | var orig = localConsole[method]
|
116 | if (!orig) {
|
117 | return
|
118 | }
|
119 | localConsole[method] = function () {
|
120 | self.log(method, arguments)
|
121 | return Function.prototype.apply.call(orig, localConsole, arguments)
|
122 | }
|
123 | }
|
124 | for (var i = 0; i < logMethods.length; i++) {
|
125 | patchConsoleMethod(logMethods[i])
|
126 | }
|
127 | }
|
128 | }
|
129 | }
|
130 |
|
131 |
|
132 | ContextKarma.getDirectCallParentKarmaMethod = function (parentWindow) {
|
133 | return function directCallParentKarmaMethod (method, args) {
|
134 |
|
135 | if (!parentWindow.karma[method]) {
|
136 | parentWindow.karma.error('Expected Karma method "' + method + '" to exist but it doesn\'t')
|
137 | return
|
138 | }
|
139 |
|
140 |
|
141 | parentWindow.karma[method].apply(parentWindow.karma, args)
|
142 | }
|
143 | }
|
144 | ContextKarma.getPostMessageCallParentKarmaMethod = function (parentWindow) {
|
145 | return function postMessageCallParentKarmaMethod (method, args) {
|
146 | parentWindow.postMessage({__karmaMethod: method, __karmaArguments: args}, window.location.origin)
|
147 | }
|
148 | }
|
149 |
|
150 |
|
151 | module.exports = ContextKarma
|