├── .gitignore ├── Example ├── index.html └── master.css ├── MIT-LICENSE ├── README.md ├── plugin.xml ├── spec ├── gaplugin.tests.js ├── html │ ├── HtmlReporter.js │ ├── HtmlReporterHelpers.js │ ├── ReporterView.js │ ├── SpecView.js │ ├── SuiteView.js │ └── TrivialReporter.js ├── index.html ├── jasmine.css ├── jasmine.js └── test-runner.js ├── src ├── android │ ├── GAPlugin.java │ └── libGoogleAnalyticsV2.jar └── ios │ ├── GAI.h │ ├── GAIDictionaryBuilder.h │ ├── GAIFields.h │ ├── GAILogger.h │ ├── GAITrackedViewController.h │ ├── GAITracker.h │ ├── GAPlugin.h │ ├── GAPlugin.m │ └── libGoogleAnalyticsServices.a └── www └── GAPlugin.js /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | src/ios/.DS_Store 3 | 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /Example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | PhoneGap 7 | 8 | 9 | 10 | 64 | 65 | 66 | 67 |

Test Google Analytics Plugin

68 |
69 |
70 | Track Event 71 | Track Event with Variable 72 | Track Page 73 |
74 | 75 | 76 | -------------------------------------------------------------------------------- /Example/master.css: -------------------------------------------------------------------------------- 1 | /* 2 | Licensed to the Apache Software Foundation (ASF) under one 3 | or more contributor license agreements. See the NOTICE file 4 | distributed with this work for additional information 5 | regarding copyright ownership. The ASF licenses this file 6 | to you under the Apache License, Version 2.0 (the 7 | "License"); you may not use this file except in compliance 8 | with the License. You may obtain a copy of the License at 9 | 10 | http://www.apache.org/licenses/LICENSE-2.0 11 | 12 | Unless required by applicable law or agreed to in writing, 13 | software distributed under the License is distributed on an 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | KIND, either express or implied. See the License for the 16 | specific language governing permissions and limitations 17 | under the License. 18 | */ 19 | 20 | 21 | body { 22 | background:#222 none repeat scroll 0 0; 23 | color:#666; 24 | font-family:Helvetica; 25 | font-size:72%; 26 | line-height:1.5em; 27 | margin:0; 28 | border-top:1px solid #393939; 29 | } 30 | 31 | #info{ 32 | background:#ffa; 33 | border: 1px solid #ffd324; 34 | -webkit-border-radius: 5px; 35 | border-radius: 5px; 36 | clear:both; 37 | margin:15px 6px 0; 38 | width:295px; 39 | padding:4px 0px 2px 10px; 40 | } 41 | 42 | #info > h4{ 43 | font-size:.95em; 44 | margin:5px 0; 45 | } 46 | 47 | #stage.theme{ 48 | padding-top:3px; 49 | } 50 | 51 | /* Definition List */ 52 | #stage.theme > dl{ 53 | padding-top:10px; 54 | clear:both; 55 | margin:0; 56 | list-style-type:none; 57 | padding-left:10px; 58 | overflow:auto; 59 | } 60 | 61 | #stage.theme > dl > dt{ 62 | font-weight:bold; 63 | float:left; 64 | margin-left:5px; 65 | } 66 | 67 | #stage.theme > dl > dd{ 68 | width:45px; 69 | float:left; 70 | color:#a87; 71 | font-weight:bold; 72 | } 73 | 74 | /* Content Styling */ 75 | #stage.theme > h1, #stage.theme > h2, #stage.theme > p{ 76 | margin:1em 0 .5em 13px; 77 | } 78 | 79 | #stage.theme > h1{ 80 | color:#eee; 81 | font-size:1.6em; 82 | text-align:center; 83 | margin:0; 84 | margin-top:15px; 85 | padding:0; 86 | } 87 | 88 | #stage.theme > h2{ 89 | clear:both; 90 | margin:0; 91 | padding:3px; 92 | font-size:1em; 93 | text-align:center; 94 | } 95 | 96 | /* Stage Buttons */ 97 | #stage.theme a.btn{ 98 | border: 1px solid #555; 99 | -webkit-border-radius: 5px; 100 | border-radius: 5px; 101 | text-align:center; 102 | display:block; 103 | float:left; 104 | background:#444; 105 | width:150px; 106 | color:#9ab; 107 | font-size:1.1em; 108 | text-decoration:none; 109 | padding:1.2em 0; 110 | margin:3px 0px 3px 5px; 111 | } 112 | #stage.theme a.btn.large{ 113 | width:308px; 114 | padding:1.2em 0; 115 | } 116 | 117 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2012 Bob Easterday, Adobe Systems 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GAPlugin 2 | 3 | > Google Analytics plugin for iOS and Android. This allows you to post usage information to your Google Analytics account. 4 | 5 | ## Preparation: 6 | Before you can begin collecting metrics data, you need to set up a GoogleAnalytics Mobile App account so you can view them. When you do so, you will obtain an app tracking id which we'll use during session initialization. Start by going to the [Google Analytics](http://www.google.com/analytics/features/mobile-app-analytics.html) site and click on the **Create an Account** button. Once signed in, click on the **Admin** button and the **+ New Account** button under the **Accounts** tab. At the top of the resulting tab, select the **App** button in answer to the **What would you like to track?** query. Fill out the form as appropriate. Complete instructions can be found [here](http://www.google.com/analytics/features/mobile-app-analytics.html). 7 | 8 | ## Installation: 9 | 10 | ### local 11 | 12 | Add the following feature tag in your config.xml 13 | 14 | 15 | < param name="android-package" value="com.adobe.plugins.GAPlugin"/> 16 | 17 | 18 | This plugin is based on [plugman](https://github.com/apache/cordova-plugman). to install it to your app, 19 | simply execute plugman as follows; 20 | 21 | plugman install --platform [PLATFORM] --project [TARGET-PATH] --plugin [PLUGIN-PATH] 22 | 23 | where 24 | [PLATFORM] = ios or android 25 | [TARGET-PATH] = path to folder containing your xcode project 26 | [PLUGIN-PATH] = path to folder containing this plugin 27 | 28 | For additional info, take a look at the [Plugman Documentation](https://github.com/apache/cordova-plugman/blob/master/README.md) 29 | 30 | ### PhoneGap Build 31 | 32 | To use this plugin with PhoneGap Build, add the following plugin reference to your config.xml 33 | 34 | 35 | 36 | ## Usage 37 | The plugin creates the object `window.plugins.gaPlugin 38 | 39 | After onDeviceReady, create a local var and startup the plugin like so; 40 | 41 | var gaPlugin; 42 | 43 | function onDeviceReady() { 44 | gaPlugin = window.plugins.gaPlugin; 45 | gaPlugin.init(successHandler, errorHandler, "UA-12345678-1", 10); 46 | } 47 | 48 | To get things rolling you need to call init() when your device ready function fires. 49 | Init takes 4 arguments; 50 | 1) success - a function that will be called on success 51 | 2) fail - a function that will be called on error. 52 | 3) id - Your Google Analytics account ID of the form; UA-XXXXXXXX-X 53 | This is the account ID you were given when you signed up. 54 | 4) period - An integer containing the minimum number of seconds 55 | between upload of metrics. When metics are logged, they are enqued 56 | and are sent out in batches based on this value. You'll want to 57 | avoid setting this value too low, to limit the overhead of sending data. 58 | 59 | Example: 60 | 61 | gaPlugin.init(successHandler, errorHandler, "UA-12345678-1", 10); 62 | 63 | To track an event, call (oddly enough) trackEvent(). 64 | trackEvent takes 6 arguments; 65 | 66 | 1) resultHandler - a function that will be called on success 67 | 2) errorHandler - a function that will be called on error. 68 | 3) category - This is the type of event you are sending such as "Button", "Menu", etc. 69 | 4) eventAction - This is the type of event you are sending such as "Click", "Select". etc. 70 | 5) eventLabel - A label that describes the event such as Button title or Menu Item name. 71 | 6) eventValue - An application defined integer value that can mean whatever you want it to mean. 72 | 73 | Example: 74 | 75 | gaPlugin.trackEvent( nativePluginResultHandler, nativePluginErrorHandler, "Button", "Click", "event only", 1); 76 | 77 | TrackEvent covers most of what you need, but there may be cases where you want to pass arbitrary data. 78 | setVariable() lets you pass values by index (Up to 20, on free accounts). 79 | For free accounts, each variable is given an index from 1 - 20. Reusing an existing index simply overwrites 80 | the previous value. Passing an index out of range fails silently, with no data sent. The variables will be sent ONLY for the next trackEvent or trackPage, after which those indexes will be available for reuse. 81 | setVariable() accepts 4 arguments; 82 | 83 | 1) resultHandler - a function that will be called on success 84 | 2) errorHandler - a function that will be called on error. 85 | 3) index - the numerical index representing one of your variable slots (1-20). 86 | 4) value - Arbitrary string data associated with the index. 87 | 88 | Example: 89 | 90 | gaPlugin.setVariable( nativePluginResultHandler, nativePluginErrorHandler, 1, "Purple"); 91 | 92 | ####Important: 93 | Variable values are assigned to what Google calls Custom Dimensions in the dashboard. Prior to calling setVariable() in your client for a particular index, you need to create a slot in the GA dashboard. When you do so, you will be able to assign a name for the dimension, its index, and its scope. More info on creating Custom Dimensions can be found [here](https://support.google.com/analytics/answer/2709829?hl=en&ref_topic=2709827). 94 | The next event or page view you send after setVariable will contain your variable at Custom Dimension specified by the index value you passed in the setVariable call. This [Example](https://github.com/phonegap-build/GAPlugin/blob/master/Example/index.html) app shows how you might use that next event to specify a label for the variable you just set. 95 | 96 | In addition to events and variables, you can also log page visits with trackPage(). Unlike variables, however, page hits do not require a subsequent call to trackEvent() as they are considered unique events in and of themselves. 97 | trackPage() takes 3 arguments; 98 | 99 | 1) resultHandler - a function that will be called on success 100 | 2) errorHandler - a function that will be called on error. 101 | 3) url - The url of the page hit you are logging. 102 | 103 | Example: 104 | 105 | gaPlugin.trackPage( nativePluginResultHandler, nativePluginErrorHandler, "some.url.com"); 106 | 107 | Finally, when your app shuts down, you'll want to cleanup after yourself by calling exit(); 108 | exit() accepts 2 arguments; 109 | 110 | 1) resultHandler - a function that will be called on success 111 | 2) errorHandler - a function that will be called on error. 112 | Example: 113 | 114 | gaPlugin.exit(nativePluginResultHandler, nativePluginErrorHandler); 115 | 116 | This package includes an Example folder containing an index.html file showing how all of this fits together. 117 | Note that the contents of Examples does not get installed anywhere by pluginstall. Its just there to provide a usage example. 118 | 119 | ## More Info 120 | 121 | GAPlugin includes libraries from Google Analytics SDK for iOS and for Android. 122 | Use of those libraries is subject to [Google Analytics Terms of Service](http://www.google.com/analytics/terms/us.html) 123 | 124 | Also take a look at [Google Analytics Developer Guides](https://developers.google.com/analytics/devguides/) 125 | 126 | ## License ## 127 | 128 | The MIT License 129 | 130 | Copyright (c) 2012 Bob Easterday, Adobe Systems 131 | 132 | Permission is hereby granted, free of charge, to any person obtaining a copy 133 | of this software and associated documentation files (the "Software"), to deal 134 | in the Software without restriction, including without limitation the rights 135 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 136 | copies of the Software, and to permit persons to whom the Software is 137 | furnished to do so, subject to the following conditions: 138 | 139 | The above copyright notice and this permission notice shall be included in 140 | all copies or substantial portions of the Software. 141 | 142 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 143 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 144 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 145 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 146 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 147 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 148 | THE SOFTWARE. -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | GAPlugin 8 | Bob Easterday 9 | 10 | 11 | This plugin allows you to post usage information to your Google Analytics account. 12 | 13 | 14 | MIT 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /spec/gaplugin.tests.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one 4 | * or more contributor license agreements. See the NOTICE file 5 | * distributed with this work for additional information 6 | * regarding copyright ownership. The ASF licenses this file 7 | * to you under the Apache License, Version 2.0 (the 8 | * "License"); you may not use this file except in compliance 9 | * with the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, 14 | * software distributed under the License is distributed on an 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | * KIND, either express or implied. See the License for the 17 | * specific language governing permissions and limitations 18 | * under the License. 19 | * 20 | */ 21 | 22 | describe('Plugin object (window.plugins)', function () { 23 | it("should exist", function() { 24 | expect(window.plugins).toBeDefined(); 25 | }); 26 | 27 | it("should contain a gaPlugin object", function() { 28 | expect(window.plugins.gaPlugin).toBeDefined(); 29 | expect(typeof window.plugins.gaPlugin == 'object').toBe(true); 30 | }); 31 | 32 | it("should contain an init function", function() { 33 | expect(window.plugins.gaPlugin.init).toBeDefined(); 34 | expect(typeof window.plugins.gaPlugin.init == 'function').toBe(true); 35 | }); 36 | 37 | it("should contain a trackEvent function", function() { 38 | expect(window.plugins.gaPlugin.trackEvent).toBeDefined(); 39 | expect(typeof window.plugins.gaPlugin.trackEvent == 'function').toBe(true); 40 | }); 41 | 42 | it("should contain a setVariable function", function() { 43 | expect(window.plugins.gaPlugin.setVariable).toBeDefined(); 44 | expect(typeof window.plugins.gaPlugin.setVariable == 'function').toBe(true); 45 | }); 46 | 47 | it("should contain a trackPage function", function() { 48 | expect(window.plugins.gaPlugin.trackPage).toBeDefined(); 49 | expect(typeof window.plugins.gaPlugin.trackPage == 'function').toBe(true); 50 | }); 51 | 52 | it("should contain an exit function", function() { 53 | expect(window.plugins.gaPlugin.exit).toBeDefined(); 54 | expect(typeof window.plugins.gaPlugin.exit == 'function').toBe(true); 55 | }); 56 | }); 57 | -------------------------------------------------------------------------------- /spec/html/HtmlReporter.js: -------------------------------------------------------------------------------- 1 | jasmine.HtmlReporter = function(_doc) { 2 | var self = this; 3 | var doc = _doc || window.document; 4 | 5 | var reporterView; 6 | 7 | var dom = {}; 8 | 9 | // Jasmine Reporter Public Interface 10 | self.logRunningSpecs = false; 11 | 12 | self.reportRunnerStarting = function(runner) { 13 | var specs = runner.specs() || []; 14 | 15 | if (specs.length == 0) { 16 | return; 17 | } 18 | 19 | createReporterDom(runner.env.versionString()); 20 | doc.body.appendChild(dom.reporter); 21 | 22 | reporterView = new jasmine.HtmlReporter.ReporterView(dom); 23 | reporterView.addSpecs(specs, self.specFilter); 24 | }; 25 | 26 | self.reportRunnerResults = function(runner) { 27 | reporterView && reporterView.complete(); 28 | }; 29 | 30 | self.reportSuiteResults = function(suite) { 31 | reporterView.suiteComplete(suite); 32 | }; 33 | 34 | self.reportSpecStarting = function(spec) { 35 | if (self.logRunningSpecs) { 36 | self.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...'); 37 | } 38 | }; 39 | 40 | self.reportSpecResults = function(spec) { 41 | reporterView.specComplete(spec); 42 | }; 43 | 44 | self.log = function() { 45 | var console = jasmine.getGlobal().console; 46 | if (console && console.log) { 47 | if (console.log.apply) { 48 | console.log.apply(console, arguments); 49 | } else { 50 | console.log(arguments); // ie fix: console.log.apply doesn't exist on ie 51 | } 52 | } 53 | }; 54 | 55 | self.specFilter = function(spec) { 56 | if (!focusedSpecName()) { 57 | return true; 58 | } 59 | 60 | return spec.getFullName().indexOf(focusedSpecName()) === 0; 61 | }; 62 | 63 | return self; 64 | 65 | function focusedSpecName() { 66 | var specName; 67 | 68 | (function memoizeFocusedSpec() { 69 | if (specName) { 70 | return; 71 | } 72 | 73 | var paramMap = []; 74 | var params = doc.location.search.substring(1).split('&'); 75 | 76 | for (var i = 0; i < params.length; i++) { 77 | var p = params[i].split('='); 78 | paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]); 79 | } 80 | 81 | specName = paramMap.spec; 82 | })(); 83 | 84 | return specName; 85 | } 86 | 87 | function createReporterDom(version) { 88 | dom.reporter = self.createDom('div', { id: 'HTMLReporter', className: 'jasmine_reporter' }, 89 | dom.banner = self.createDom('div', { className: 'banner' }, 90 | self.createDom('span', { className: 'title' }, "Jasmine "), 91 | self.createDom('span', { className: 'version' }, version)), 92 | 93 | dom.symbolSummary = self.createDom('ul', {className: 'symbolSummary'}), 94 | dom.alert = self.createDom('div', {className: 'alert'}), 95 | dom.results = self.createDom('div', {className: 'results'}, 96 | dom.summary = self.createDom('div', { className: 'summary' }), 97 | dom.details = self.createDom('div', { id: 'details' })) 98 | ); 99 | } 100 | }; 101 | jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter); 102 | -------------------------------------------------------------------------------- /spec/html/HtmlReporterHelpers.js: -------------------------------------------------------------------------------- 1 | jasmine.HtmlReporterHelpers = {}; 2 | 3 | jasmine.HtmlReporterHelpers.createDom = function(type, attrs, childrenVarArgs) { 4 | var el = document.createElement(type); 5 | 6 | for (var i = 2; i < arguments.length; i++) { 7 | var child = arguments[i]; 8 | 9 | if (typeof child === 'string') { 10 | el.appendChild(document.createTextNode(child)); 11 | } else { 12 | if (child) { 13 | el.appendChild(child); 14 | } 15 | } 16 | } 17 | 18 | for (var attr in attrs) { 19 | if (attr == "className") { 20 | el[attr] = attrs[attr]; 21 | } else { 22 | el.setAttribute(attr, attrs[attr]); 23 | } 24 | } 25 | 26 | return el; 27 | }; 28 | 29 | jasmine.HtmlReporterHelpers.getSpecStatus = function(child) { 30 | var results = child.results(); 31 | var status = results.passed() ? 'passed' : 'failed'; 32 | if (results.skipped) { 33 | status = 'skipped'; 34 | } 35 | 36 | return status; 37 | }; 38 | 39 | jasmine.HtmlReporterHelpers.appendToSummary = function(child, childElement) { 40 | var parentDiv = this.dom.summary; 41 | var parentSuite = (typeof child.parentSuite == 'undefined') ? 'suite' : 'parentSuite'; 42 | var parent = child[parentSuite]; 43 | 44 | if (parent) { 45 | if (typeof this.views.suites[parent.id] == 'undefined') { 46 | this.views.suites[parent.id] = new jasmine.HtmlReporter.SuiteView(parent, this.dom, this.views); 47 | } 48 | parentDiv = this.views.suites[parent.id].element; 49 | } 50 | 51 | parentDiv.appendChild(childElement); 52 | }; 53 | 54 | 55 | jasmine.HtmlReporterHelpers.addHelpers = function(ctor) { 56 | for(var fn in jasmine.HtmlReporterHelpers) { 57 | ctor.prototype[fn] = jasmine.HtmlReporterHelpers[fn]; 58 | } 59 | }; 60 | 61 | -------------------------------------------------------------------------------- /spec/html/ReporterView.js: -------------------------------------------------------------------------------- 1 | jasmine.HtmlReporter.ReporterView = function(dom) { 2 | this.startedAt = new Date(); 3 | this.runningSpecCount = 0; 4 | this.completeSpecCount = 0; 5 | this.passedCount = 0; 6 | this.failedCount = 0; 7 | this.skippedCount = 0; 8 | 9 | this.createResultsMenu = function() { 10 | this.resultsMenu = this.createDom('span', {className: 'resultsMenu bar'}, 11 | this.summaryMenuItem = this.createDom('a', {className: 'summaryMenuItem', href: "#"}, '0 specs'), 12 | ' | ', 13 | this.detailsMenuItem = this.createDom('a', {className: 'detailsMenuItem', href: "#"}, '0 failing')); 14 | 15 | this.summaryMenuItem.onclick = function() { 16 | dom.reporter.className = dom.reporter.className.replace(/ showDetails/g, ''); 17 | }; 18 | 19 | this.detailsMenuItem.onclick = function() { 20 | showDetails(); 21 | }; 22 | }; 23 | 24 | this.addSpecs = function(specs, specFilter) { 25 | this.totalSpecCount = specs.length; 26 | 27 | this.views = { 28 | specs: {}, 29 | suites: {} 30 | }; 31 | 32 | for (var i = 0; i < specs.length; i++) { 33 | var spec = specs[i]; 34 | this.views.specs[spec.id] = new jasmine.HtmlReporter.SpecView(spec, dom, this.views); 35 | if (specFilter(spec)) { 36 | this.runningSpecCount++; 37 | } 38 | } 39 | }; 40 | 41 | this.specComplete = function(spec) { 42 | this.completeSpecCount++; 43 | 44 | if (isUndefined(this.views.specs[spec.id])) { 45 | this.views.specs[spec.id] = new jasmine.HtmlReporter.SpecView(spec, dom); 46 | } 47 | 48 | var specView = this.views.specs[spec.id]; 49 | 50 | switch (specView.status()) { 51 | case 'passed': 52 | this.passedCount++; 53 | break; 54 | 55 | case 'failed': 56 | this.failedCount++; 57 | break; 58 | 59 | case 'skipped': 60 | this.skippedCount++; 61 | break; 62 | } 63 | 64 | specView.refresh(); 65 | this.refresh(); 66 | }; 67 | 68 | this.suiteComplete = function(suite) { 69 | var suiteView = this.views.suites[suite.id]; 70 | if (isUndefined(suiteView)) { 71 | return; 72 | } 73 | suiteView.refresh(); 74 | }; 75 | 76 | this.refresh = function() { 77 | 78 | if (isUndefined(this.resultsMenu)) { 79 | this.createResultsMenu(); 80 | } 81 | 82 | // currently running UI 83 | if (isUndefined(this.runningAlert)) { 84 | this.runningAlert = this.createDom('a', {href: "?", className: "runningAlert bar"}); 85 | dom.alert.appendChild(this.runningAlert); 86 | } 87 | this.runningAlert.innerHTML = "Running " + this.completeSpecCount + " of " + specPluralizedFor(this.totalSpecCount); 88 | 89 | // skipped specs UI 90 | if (isUndefined(this.skippedAlert)) { 91 | this.skippedAlert = this.createDom('a', {href: "?", className: "skippedAlert bar"}); 92 | } 93 | 94 | this.skippedAlert.innerHTML = "Skipping " + this.skippedCount + " of " + specPluralizedFor(this.totalSpecCount) + " - run all"; 95 | 96 | if (this.skippedCount === 1 && isDefined(dom.alert)) { 97 | dom.alert.appendChild(this.skippedAlert); 98 | } 99 | 100 | // passing specs UI 101 | if (isUndefined(this.passedAlert)) { 102 | this.passedAlert = this.createDom('span', {href: "?", className: "passingAlert bar"}); 103 | } 104 | this.passedAlert.innerHTML = "Passing " + specPluralizedFor(this.passedCount); 105 | 106 | // failing specs UI 107 | if (isUndefined(this.failedAlert)) { 108 | this.failedAlert = this.createDom('span', {href: "?", className: "failingAlert bar"}); 109 | } 110 | this.failedAlert.innerHTML = "Failing " + specPluralizedFor(this.failedCount); 111 | 112 | if (this.failedCount === 1 && isDefined(dom.alert)) { 113 | dom.alert.appendChild(this.failedAlert); 114 | dom.alert.appendChild(this.resultsMenu); 115 | } 116 | 117 | // summary info 118 | this.summaryMenuItem.innerHTML = "" + specPluralizedFor(this.runningSpecCount); 119 | this.detailsMenuItem.innerHTML = "" + this.failedCount + " failing"; 120 | }; 121 | 122 | this.complete = function() { 123 | dom.alert.removeChild(this.runningAlert); 124 | 125 | this.skippedAlert.innerHTML = "Ran " + this.runningSpecCount + " of " + specPluralizedFor(this.totalSpecCount) + " - run all"; 126 | 127 | if (this.failedCount === 0) { 128 | dom.alert.appendChild(this.createDom('span', {className: 'passingAlert bar'}, "Passing " + specPluralizedFor(this.passedCount))); 129 | } else { 130 | showDetails(); 131 | } 132 | 133 | dom.banner.appendChild(this.createDom('span', {className: 'duration'}, "finished in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s")); 134 | }; 135 | 136 | return this; 137 | 138 | function showDetails() { 139 | if (dom.reporter.className.search(/showDetails/) === -1) { 140 | dom.reporter.className += " showDetails"; 141 | } 142 | } 143 | 144 | function isUndefined(obj) { 145 | return typeof obj === 'undefined'; 146 | } 147 | 148 | function isDefined(obj) { 149 | return !isUndefined(obj); 150 | } 151 | 152 | function specPluralizedFor(count) { 153 | var str = count + " spec"; 154 | if (count > 1) { 155 | str += "s" 156 | } 157 | return str; 158 | } 159 | 160 | }; 161 | 162 | jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter.ReporterView); 163 | 164 | 165 | -------------------------------------------------------------------------------- /spec/html/SpecView.js: -------------------------------------------------------------------------------- 1 | jasmine.HtmlReporter.SpecView = function(spec, dom, views) { 2 | this.spec = spec; 3 | this.dom = dom; 4 | this.views = views; 5 | 6 | this.symbol = this.createDom('li', { className: 'pending' }); 7 | this.dom.symbolSummary.appendChild(this.symbol); 8 | 9 | this.summary = this.createDom('div', { className: 'specSummary' }, 10 | this.createDom('a', { 11 | className: 'description', 12 | href: '?spec=' + encodeURIComponent(this.spec.getFullName()), 13 | title: this.spec.getFullName() 14 | }, this.spec.description) 15 | ); 16 | 17 | this.detail = this.createDom('div', { className: 'specDetail' }, 18 | this.createDom('a', { 19 | className: 'description', 20 | href: '?spec=' + encodeURIComponent(this.spec.getFullName()), 21 | title: this.spec.getFullName() 22 | }, this.spec.getFullName()) 23 | ); 24 | }; 25 | 26 | jasmine.HtmlReporter.SpecView.prototype.status = function() { 27 | return this.getSpecStatus(this.spec); 28 | }; 29 | 30 | jasmine.HtmlReporter.SpecView.prototype.refresh = function() { 31 | this.symbol.className = this.status(); 32 | 33 | switch (this.status()) { 34 | case 'skipped': 35 | break; 36 | 37 | case 'passed': 38 | this.appendSummaryToSuiteDiv(); 39 | break; 40 | 41 | case 'failed': 42 | this.appendSummaryToSuiteDiv(); 43 | this.appendFailureDetail(); 44 | break; 45 | } 46 | }; 47 | 48 | jasmine.HtmlReporter.SpecView.prototype.appendSummaryToSuiteDiv = function() { 49 | this.summary.className += ' ' + this.status(); 50 | this.appendToSummary(this.spec, this.summary); 51 | }; 52 | 53 | jasmine.HtmlReporter.SpecView.prototype.appendFailureDetail = function() { 54 | this.detail.className += ' ' + this.status(); 55 | 56 | var resultItems = this.spec.results().getItems(); 57 | var messagesDiv = this.createDom('div', { className: 'messages' }); 58 | 59 | for (var i = 0; i < resultItems.length; i++) { 60 | var result = resultItems[i]; 61 | 62 | if (result.type == 'log') { 63 | messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString())); 64 | } else if (result.type == 'expect' && result.passed && !result.passed()) { 65 | messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message)); 66 | 67 | if (result.trace.stack) { 68 | messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack)); 69 | } 70 | } 71 | } 72 | 73 | if (messagesDiv.childNodes.length > 0) { 74 | this.detail.appendChild(messagesDiv); 75 | this.dom.details.appendChild(this.detail); 76 | } 77 | }; 78 | 79 | jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter.SpecView); -------------------------------------------------------------------------------- /spec/html/SuiteView.js: -------------------------------------------------------------------------------- 1 | jasmine.HtmlReporter.SuiteView = function(suite, dom, views) { 2 | this.suite = suite; 3 | this.dom = dom; 4 | this.views = views; 5 | 6 | this.element = this.createDom('div', { className: 'suite' }, 7 | this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(this.suite.getFullName()) }, this.suite.description) 8 | ); 9 | 10 | this.appendToSummary(this.suite, this.element); 11 | }; 12 | 13 | jasmine.HtmlReporter.SuiteView.prototype.status = function() { 14 | return this.getSpecStatus(this.suite); 15 | }; 16 | 17 | jasmine.HtmlReporter.SuiteView.prototype.refresh = function() { 18 | this.element.className += " " + this.status(); 19 | }; 20 | 21 | jasmine.HtmlReporterHelpers.addHelpers(jasmine.HtmlReporter.SuiteView); 22 | 23 | -------------------------------------------------------------------------------- /spec/html/TrivialReporter.js: -------------------------------------------------------------------------------- 1 | /* @deprecated Use jasmine.HtmlReporter instead 2 | */ 3 | jasmine.TrivialReporter = function(doc) { 4 | this.document = doc || document; 5 | this.suiteDivs = {}; 6 | this.logRunningSpecs = false; 7 | }; 8 | 9 | jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) { 10 | var el = document.createElement(type); 11 | 12 | for (var i = 2; i < arguments.length; i++) { 13 | var child = arguments[i]; 14 | 15 | if (typeof child === 'string') { 16 | el.appendChild(document.createTextNode(child)); 17 | } else { 18 | if (child) { el.appendChild(child); } 19 | } 20 | } 21 | 22 | for (var attr in attrs) { 23 | if (attr == "className") { 24 | el[attr] = attrs[attr]; 25 | } else { 26 | el.setAttribute(attr, attrs[attr]); 27 | } 28 | } 29 | 30 | return el; 31 | }; 32 | 33 | jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) { 34 | var showPassed, showSkipped; 35 | 36 | this.outerDiv = this.createDom('div', { id: 'TrivialReporter', className: 'jasmine_reporter' }, 37 | this.createDom('div', { className: 'banner' }, 38 | this.createDom('div', { className: 'logo' }, 39 | this.createDom('span', { className: 'title' }, "Jasmine"), 40 | this.createDom('span', { className: 'version' }, runner.env.versionString())), 41 | this.createDom('div', { className: 'options' }, 42 | "Show ", 43 | showPassed = this.createDom('input', { id: "__jasmine_TrivialReporter_showPassed__", type: 'checkbox' }), 44 | this.createDom('label', { "for": "__jasmine_TrivialReporter_showPassed__" }, " passed "), 45 | showSkipped = this.createDom('input', { id: "__jasmine_TrivialReporter_showSkipped__", type: 'checkbox' }), 46 | this.createDom('label', { "for": "__jasmine_TrivialReporter_showSkipped__" }, " skipped") 47 | ) 48 | ), 49 | 50 | this.runnerDiv = this.createDom('div', { className: 'runner running' }, 51 | this.createDom('a', { className: 'run_spec', href: '?' }, "run all"), 52 | this.runnerMessageSpan = this.createDom('span', {}, "Running..."), 53 | this.finishedAtSpan = this.createDom('span', { className: 'finished-at' }, "")) 54 | ); 55 | 56 | this.document.body.appendChild(this.outerDiv); 57 | 58 | var suites = runner.suites(); 59 | for (var i = 0; i < suites.length; i++) { 60 | var suite = suites[i]; 61 | var suiteDiv = this.createDom('div', { className: 'suite' }, 62 | this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"), 63 | this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description)); 64 | this.suiteDivs[suite.id] = suiteDiv; 65 | var parentDiv = this.outerDiv; 66 | if (suite.parentSuite) { 67 | parentDiv = this.suiteDivs[suite.parentSuite.id]; 68 | } 69 | parentDiv.appendChild(suiteDiv); 70 | } 71 | 72 | this.startedAt = new Date(); 73 | 74 | var self = this; 75 | showPassed.onclick = function(evt) { 76 | if (showPassed.checked) { 77 | self.outerDiv.className += ' show-passed'; 78 | } else { 79 | self.outerDiv.className = self.outerDiv.className.replace(/ show-passed/, ''); 80 | } 81 | }; 82 | 83 | showSkipped.onclick = function(evt) { 84 | if (showSkipped.checked) { 85 | self.outerDiv.className += ' show-skipped'; 86 | } else { 87 | self.outerDiv.className = self.outerDiv.className.replace(/ show-skipped/, ''); 88 | } 89 | }; 90 | }; 91 | 92 | jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) { 93 | var results = runner.results(); 94 | var className = (results.failedCount > 0) ? "runner failed" : "runner passed"; 95 | this.runnerDiv.setAttribute("class", className); 96 | //do it twice for IE 97 | this.runnerDiv.setAttribute("className", className); 98 | var specs = runner.specs(); 99 | var specCount = 0; 100 | for (var i = 0; i < specs.length; i++) { 101 | if (this.specFilter(specs[i])) { 102 | specCount++; 103 | } 104 | } 105 | var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s"); 106 | message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s"; 107 | this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild); 108 | 109 | this.finishedAtSpan.appendChild(document.createTextNode("Finished at " + new Date().toString())); 110 | }; 111 | 112 | jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) { 113 | var results = suite.results(); 114 | var status = results.passed() ? 'passed' : 'failed'; 115 | if (results.totalCount === 0) { // todo: change this to check results.skipped 116 | status = 'skipped'; 117 | } 118 | this.suiteDivs[suite.id].className += " " + status; 119 | }; 120 | 121 | jasmine.TrivialReporter.prototype.reportSpecStarting = function(spec) { 122 | if (this.logRunningSpecs) { 123 | this.log('>> Jasmine Running ' + spec.suite.description + ' ' + spec.description + '...'); 124 | } 125 | }; 126 | 127 | jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) { 128 | var results = spec.results(); 129 | var status = results.passed() ? 'passed' : 'failed'; 130 | if (results.skipped) { 131 | status = 'skipped'; 132 | } 133 | var specDiv = this.createDom('div', { className: 'spec ' + status }, 134 | this.createDom('a', { className: 'run_spec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"), 135 | this.createDom('a', { 136 | className: 'description', 137 | href: '?spec=' + encodeURIComponent(spec.getFullName()), 138 | title: spec.getFullName() 139 | }, spec.description)); 140 | 141 | 142 | var resultItems = results.getItems(); 143 | var messagesDiv = this.createDom('div', { className: 'messages' }); 144 | for (var i = 0; i < resultItems.length; i++) { 145 | var result = resultItems[i]; 146 | 147 | if (result.type == 'log') { 148 | messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage log'}, result.toString())); 149 | } else if (result.type == 'expect' && result.passed && !result.passed()) { 150 | messagesDiv.appendChild(this.createDom('div', {className: 'resultMessage fail'}, result.message)); 151 | 152 | if (result.trace.stack) { 153 | messagesDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack)); 154 | } 155 | } 156 | } 157 | 158 | if (messagesDiv.childNodes.length > 0) { 159 | specDiv.appendChild(messagesDiv); 160 | } 161 | 162 | this.suiteDivs[spec.suite.id].appendChild(specDiv); 163 | }; 164 | 165 | jasmine.TrivialReporter.prototype.log = function() { 166 | var console = jasmine.getGlobal().console; 167 | if (console && console.log) { 168 | if (console.log.apply) { 169 | console.log.apply(console, arguments); 170 | } else { 171 | console.log(arguments); // ie fix: console.log.apply doesn't exist on ie 172 | } 173 | } 174 | }; 175 | 176 | jasmine.TrivialReporter.prototype.getLocation = function() { 177 | return this.document.location; 178 | }; 179 | 180 | jasmine.TrivialReporter.prototype.specFilter = function(spec) { 181 | var paramMap = {}; 182 | var params = this.getLocation().search.substring(1).split('&'); 183 | for (var i = 0; i < params.length; i++) { 184 | var p = params[i].split('='); 185 | paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]); 186 | } 187 | 188 | if (!paramMap.spec) { 189 | return true; 190 | } 191 | return spec.getFullName().indexOf(paramMap.spec) === 0; 192 | }; 193 | -------------------------------------------------------------------------------- /spec/index.html: -------------------------------------------------------------------------------- 1 | 2 | 22 | 23 | 24 | 25 | 26 | Cordova: API Specs 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 70 | 71 | 72 | 73 | Back 74 | 75 | 76 | -------------------------------------------------------------------------------- /spec/jasmine.css: -------------------------------------------------------------------------------- 1 | body { background-color: #eeeeee; padding: 0; margin: 5px; overflow-y: scroll; } 2 | 3 | #HTMLReporter { font-size: 11px; font-family: Monaco, "Lucida Console", monospace; line-height: 14px; color: #333333; } 4 | #HTMLReporter a { text-decoration: none; } 5 | #HTMLReporter a:hover { text-decoration: underline; } 6 | #HTMLReporter p, #HTMLReporter h1, #HTMLReporter h2, #HTMLReporter h3, #HTMLReporter h4, #HTMLReporter h5, #HTMLReporter h6 { margin: 0; line-height: 14px; } 7 | #HTMLReporter .banner, #HTMLReporter .symbolSummary, #HTMLReporter .summary, #HTMLReporter .resultMessage, #HTMLReporter .specDetail .description, #HTMLReporter .alert .bar, #HTMLReporter .stackTrace { padding-left: 9px; padding-right: 9px; } 8 | #HTMLReporter #jasmine_content { position: fixed; right: 100%; } 9 | #HTMLReporter .version { color: #aaaaaa; } 10 | #HTMLReporter .banner { margin-top: 14px; } 11 | #HTMLReporter .duration { color: #aaaaaa; float: right; } 12 | #HTMLReporter .symbolSummary { overflow: hidden; *zoom: 1; margin: 14px 0; } 13 | #HTMLReporter .symbolSummary li { display: block; float: left; height: 7px; width: 14px; margin-bottom: 7px; font-size: 16px; } 14 | #HTMLReporter .symbolSummary li.passed { font-size: 14px; } 15 | #HTMLReporter .symbolSummary li.passed:before { color: #5e7d00; content: "\02022"; } 16 | #HTMLReporter .symbolSummary li.failed { line-height: 9px; } 17 | #HTMLReporter .symbolSummary li.failed:before { color: #b03911; content: "x"; font-weight: bold; margin-left: -1px; } 18 | #HTMLReporter .symbolSummary li.skipped { font-size: 14px; } 19 | #HTMLReporter .symbolSummary li.skipped:before { color: #bababa; content: "\02022"; } 20 | #HTMLReporter .symbolSummary li.pending { line-height: 11px; } 21 | #HTMLReporter .symbolSummary li.pending:before { color: #aaaaaa; content: "-"; } 22 | #HTMLReporter .bar { line-height: 28px; font-size: 14px; display: block; color: #eee; } 23 | #HTMLReporter .runningAlert { background-color: #666666; } 24 | #HTMLReporter .skippedAlert { background-color: #aaaaaa; } 25 | #HTMLReporter .skippedAlert:first-child { background-color: #333333; } 26 | #HTMLReporter .skippedAlert:hover { text-decoration: none; color: white; text-decoration: underline; } 27 | #HTMLReporter .passingAlert { background-color: #a6b779; } 28 | #HTMLReporter .passingAlert:first-child { background-color: #5e7d00; } 29 | #HTMLReporter .failingAlert { background-color: #cf867e; } 30 | #HTMLReporter .failingAlert:first-child { background-color: #b03911; } 31 | #HTMLReporter .results { margin-top: 14px; } 32 | #HTMLReporter #details { display: none; } 33 | #HTMLReporter .resultsMenu, #HTMLReporter .resultsMenu a { background-color: #fff; color: #333333; } 34 | #HTMLReporter.showDetails .summaryMenuItem { font-weight: normal; text-decoration: inherit; } 35 | #HTMLReporter.showDetails .summaryMenuItem:hover { text-decoration: underline; } 36 | #HTMLReporter.showDetails .detailsMenuItem { font-weight: bold; text-decoration: underline; } 37 | #HTMLReporter.showDetails .summary { display: none; } 38 | #HTMLReporter.showDetails #details { display: block; } 39 | #HTMLReporter .summaryMenuItem { font-weight: bold; text-decoration: underline; } 40 | #HTMLReporter .summary { margin-top: 14px; } 41 | #HTMLReporter .summary .suite .suite, #HTMLReporter .summary .specSummary { margin-left: 14px; } 42 | #HTMLReporter .summary .specSummary.passed a { color: #5e7d00; } 43 | #HTMLReporter .summary .specSummary.failed a { color: #b03911; } 44 | #HTMLReporter .description + .suite { margin-top: 0; } 45 | #HTMLReporter .suite { margin-top: 14px; } 46 | #HTMLReporter .suite a { color: #333333; } 47 | #HTMLReporter #details .specDetail { margin-bottom: 28px; } 48 | #HTMLReporter #details .specDetail .description { display: block; color: white; background-color: #b03911; } 49 | #HTMLReporter .resultMessage { padding-top: 14px; color: #333333; } 50 | #HTMLReporter .resultMessage span.result { display: block; } 51 | #HTMLReporter .stackTrace { margin: 5px 0 0 0; max-height: 224px; overflow: auto; line-height: 18px; color: #666666; border: 1px solid #ddd; background: white; white-space: pre; } 52 | 53 | #TrivialReporter { padding: 8px 13px; position: absolute; top: 0; bottom: 0; left: 0; right: 0; overflow-y: scroll; background-color: white; font-family: "Helvetica Neue Light", "Lucida Grande", "Calibri", "Arial", sans-serif; /*.resultMessage {*/ /*white-space: pre;*/ /*}*/ } 54 | #TrivialReporter a:visited, #TrivialReporter a { color: #303; } 55 | #TrivialReporter a:hover, #TrivialReporter a:active { color: blue; } 56 | #TrivialReporter .run_spec { float: right; padding-right: 5px; font-size: .8em; text-decoration: none; } 57 | #TrivialReporter .banner { color: #303; background-color: #fef; padding: 5px; } 58 | #TrivialReporter .logo { float: left; font-size: 1.1em; padding-left: 5px; } 59 | #TrivialReporter .logo .version { font-size: .6em; padding-left: 1em; } 60 | #TrivialReporter .runner.running { background-color: yellow; } 61 | #TrivialReporter .options { text-align: right; font-size: .8em; } 62 | #TrivialReporter .suite { border: 1px outset gray; margin: 5px 0; padding-left: 1em; } 63 | #TrivialReporter .suite .suite { margin: 5px; } 64 | #TrivialReporter .suite.passed { background-color: #dfd; } 65 | #TrivialReporter .suite.failed { background-color: #fdd; } 66 | #TrivialReporter .spec { margin: 5px; padding-left: 1em; clear: both; } 67 | #TrivialReporter .spec.failed, #TrivialReporter .spec.passed, #TrivialReporter .spec.skipped { padding-bottom: 5px; border: 1px solid gray; } 68 | #TrivialReporter .spec.failed { background-color: #fbb; border-color: red; } 69 | #TrivialReporter .spec.passed { background-color: #bfb; border-color: green; } 70 | #TrivialReporter .spec.skipped { background-color: #bbb; } 71 | #TrivialReporter .messages { border-left: 1px dashed gray; padding-left: 1em; padding-right: 1em; } 72 | #TrivialReporter .passed { background-color: #cfc; display: none; } 73 | #TrivialReporter .failed { background-color: #fbb; } 74 | #TrivialReporter .skipped { color: #777; background-color: #eee; display: none; } 75 | #TrivialReporter .resultMessage span.result { display: block; line-height: 2em; color: black; } 76 | #TrivialReporter .resultMessage .mismatch { color: black; } 77 | #TrivialReporter .stackTrace { white-space: pre; font-size: .8em; margin-left: 10px; max-height: 5em; overflow: auto; border: 1px inset red; padding: 1em; background: #eef; } 78 | #TrivialReporter .finished-at { padding-left: 1em; font-size: .6em; } 79 | #TrivialReporter.show-passed .passed, #TrivialReporter.show-skipped .skipped { display: block; } 80 | #TrivialReporter #jasmine_content { position: fixed; right: 100%; } 81 | #TrivialReporter .runner { border: 1px solid gray; display: block; margin: 5px 0; padding: 2px 0 2px 10px; } 82 | -------------------------------------------------------------------------------- /spec/jasmine.js: -------------------------------------------------------------------------------- 1 | var isCommonJS = typeof window == "undefined"; 2 | 3 | /** 4 | * Top level namespace for Jasmine, a lightweight JavaScript BDD/spec/testing framework. 5 | * 6 | * @namespace 7 | */ 8 | var jasmine = {}; 9 | if (isCommonJS) exports.jasmine = jasmine; 10 | /** 11 | * @private 12 | */ 13 | jasmine.unimplementedMethod_ = function() { 14 | throw new Error("unimplemented method"); 15 | }; 16 | 17 | /** 18 | * Use jasmine.undefined instead of undefined, since undefined is just 19 | * a plain old variable and may be redefined by somebody else. 20 | * 21 | * @private 22 | */ 23 | jasmine.undefined = jasmine.___undefined___; 24 | 25 | /** 26 | * Show diagnostic messages in the console if set to true 27 | * 28 | */ 29 | jasmine.VERBOSE = false; 30 | 31 | /** 32 | * Default interval in milliseconds for event loop yields (e.g. to allow network activity or to refresh the screen with the HTML-based runner). Small values here may result in slow test running. Zero means no updates until all tests have completed. 33 | * 34 | */ 35 | jasmine.DEFAULT_UPDATE_INTERVAL = 250; 36 | 37 | /** 38 | * Default timeout interval in milliseconds for waitsFor() blocks. 39 | */ 40 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000; 41 | 42 | jasmine.getGlobal = function() { 43 | function getGlobal() { 44 | return this; 45 | } 46 | 47 | return getGlobal(); 48 | }; 49 | 50 | /** 51 | * Allows for bound functions to be compared. Internal use only. 52 | * 53 | * @ignore 54 | * @private 55 | * @param base {Object} bound 'this' for the function 56 | * @param name {Function} function to find 57 | */ 58 | jasmine.bindOriginal_ = function(base, name) { 59 | var original = base[name]; 60 | if (original.apply) { 61 | return function() { 62 | return original.apply(base, arguments); 63 | }; 64 | } else { 65 | // IE support 66 | return jasmine.getGlobal()[name]; 67 | } 68 | }; 69 | 70 | jasmine.setTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'setTimeout'); 71 | jasmine.clearTimeout = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearTimeout'); 72 | jasmine.setInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'setInterval'); 73 | jasmine.clearInterval = jasmine.bindOriginal_(jasmine.getGlobal(), 'clearInterval'); 74 | 75 | jasmine.MessageResult = function(values) { 76 | this.type = 'log'; 77 | this.values = values; 78 | this.trace = new Error(); // todo: test better 79 | }; 80 | 81 | jasmine.MessageResult.prototype.toString = function() { 82 | var text = ""; 83 | for (var i = 0; i < this.values.length; i++) { 84 | if (i > 0) text += " "; 85 | if (jasmine.isString_(this.values[i])) { 86 | text += this.values[i]; 87 | } else { 88 | text += jasmine.pp(this.values[i]); 89 | } 90 | } 91 | return text; 92 | }; 93 | 94 | jasmine.ExpectationResult = function(params) { 95 | this.type = 'expect'; 96 | this.matcherName = params.matcherName; 97 | this.passed_ = params.passed; 98 | this.expected = params.expected; 99 | this.actual = params.actual; 100 | this.message = this.passed_ ? 'Passed.' : params.message; 101 | 102 | var trace = (params.trace || new Error(this.message)); 103 | this.trace = this.passed_ ? '' : trace; 104 | }; 105 | 106 | jasmine.ExpectationResult.prototype.toString = function () { 107 | return this.message; 108 | }; 109 | 110 | jasmine.ExpectationResult.prototype.passed = function () { 111 | return this.passed_; 112 | }; 113 | 114 | /** 115 | * Getter for the Jasmine environment. Ensures one gets created 116 | */ 117 | jasmine.getEnv = function() { 118 | var env = jasmine.currentEnv_ = jasmine.currentEnv_ || new jasmine.Env(); 119 | return env; 120 | }; 121 | 122 | /** 123 | * @ignore 124 | * @private 125 | * @param value 126 | * @returns {Boolean} 127 | */ 128 | jasmine.isArray_ = function(value) { 129 | return jasmine.isA_("Array", value); 130 | }; 131 | 132 | /** 133 | * @ignore 134 | * @private 135 | * @param value 136 | * @returns {Boolean} 137 | */ 138 | jasmine.isString_ = function(value) { 139 | return jasmine.isA_("String", value); 140 | }; 141 | 142 | /** 143 | * @ignore 144 | * @private 145 | * @param value 146 | * @returns {Boolean} 147 | */ 148 | jasmine.isNumber_ = function(value) { 149 | return jasmine.isA_("Number", value); 150 | }; 151 | 152 | /** 153 | * @ignore 154 | * @private 155 | * @param {String} typeName 156 | * @param value 157 | * @returns {Boolean} 158 | */ 159 | jasmine.isA_ = function(typeName, value) { 160 | return Object.prototype.toString.apply(value) === '[object ' + typeName + ']'; 161 | }; 162 | 163 | /** 164 | * Pretty printer for expecations. Takes any object and turns it into a human-readable string. 165 | * 166 | * @param value {Object} an object to be outputted 167 | * @returns {String} 168 | */ 169 | jasmine.pp = function(value) { 170 | var stringPrettyPrinter = new jasmine.StringPrettyPrinter(); 171 | stringPrettyPrinter.format(value); 172 | return stringPrettyPrinter.string; 173 | }; 174 | 175 | /** 176 | * Returns true if the object is a DOM Node. 177 | * 178 | * @param {Object} obj object to check 179 | * @returns {Boolean} 180 | */ 181 | jasmine.isDomNode = function(obj) { 182 | return obj.nodeType > 0; 183 | }; 184 | 185 | /** 186 | * Returns a matchable 'generic' object of the class type. For use in expecations of type when values don't matter. 187 | * 188 | * @example 189 | * // don't care about which function is passed in, as long as it's a function 190 | * expect(mySpy).toHaveBeenCalledWith(jasmine.any(Function)); 191 | * 192 | * @param {Class} clazz 193 | * @returns matchable object of the type clazz 194 | */ 195 | jasmine.any = function(clazz) { 196 | return new jasmine.Matchers.Any(clazz); 197 | }; 198 | 199 | /** 200 | * Returns a matchable subset of a JSON object. For use in expectations when you don't care about all of the 201 | * attributes on the object. 202 | * 203 | * @example 204 | * // don't care about any other attributes than foo. 205 | * expect(mySpy).toHaveBeenCalledWith(jasmine.objectContaining({foo: "bar"}); 206 | * 207 | * @param sample {Object} sample 208 | * @returns matchable object for the sample 209 | */ 210 | jasmine.objectContaining = function (sample) { 211 | return new jasmine.Matchers.ObjectContaining(sample); 212 | }; 213 | 214 | /** 215 | * Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks. 216 | * 217 | * Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine 218 | * expectation syntax. Spies can be checked if they were called or not and what the calling params were. 219 | * 220 | * A Spy has the following fields: wasCalled, callCount, mostRecentCall, and argsForCall (see docs). 221 | * 222 | * Spies are torn down at the end of every spec. 223 | * 224 | * Note: Do not call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj. 225 | * 226 | * @example 227 | * // a stub 228 | * var myStub = jasmine.createSpy('myStub'); // can be used anywhere 229 | * 230 | * // spy example 231 | * var foo = { 232 | * not: function(bool) { return !bool; } 233 | * } 234 | * 235 | * // actual foo.not will not be called, execution stops 236 | * spyOn(foo, 'not'); 237 | 238 | // foo.not spied upon, execution will continue to implementation 239 | * spyOn(foo, 'not').andCallThrough(); 240 | * 241 | * // fake example 242 | * var foo = { 243 | * not: function(bool) { return !bool; } 244 | * } 245 | * 246 | * // foo.not(val) will return val 247 | * spyOn(foo, 'not').andCallFake(function(value) {return value;}); 248 | * 249 | * // mock example 250 | * foo.not(7 == 7); 251 | * expect(foo.not).toHaveBeenCalled(); 252 | * expect(foo.not).toHaveBeenCalledWith(true); 253 | * 254 | * @constructor 255 | * @see spyOn, jasmine.createSpy, jasmine.createSpyObj 256 | * @param {String} name 257 | */ 258 | jasmine.Spy = function(name) { 259 | /** 260 | * The name of the spy, if provided. 261 | */ 262 | this.identity = name || 'unknown'; 263 | /** 264 | * Is this Object a spy? 265 | */ 266 | this.isSpy = true; 267 | /** 268 | * The actual function this spy stubs. 269 | */ 270 | this.plan = function() { 271 | }; 272 | /** 273 | * Tracking of the most recent call to the spy. 274 | * @example 275 | * var mySpy = jasmine.createSpy('foo'); 276 | * mySpy(1, 2); 277 | * mySpy.mostRecentCall.args = [1, 2]; 278 | */ 279 | this.mostRecentCall = {}; 280 | 281 | /** 282 | * Holds arguments for each call to the spy, indexed by call count 283 | * @example 284 | * var mySpy = jasmine.createSpy('foo'); 285 | * mySpy(1, 2); 286 | * mySpy(7, 8); 287 | * mySpy.mostRecentCall.args = [7, 8]; 288 | * mySpy.argsForCall[0] = [1, 2]; 289 | * mySpy.argsForCall[1] = [7, 8]; 290 | */ 291 | this.argsForCall = []; 292 | this.calls = []; 293 | }; 294 | 295 | /** 296 | * Tells a spy to call through to the actual implemenatation. 297 | * 298 | * @example 299 | * var foo = { 300 | * bar: function() { // do some stuff } 301 | * } 302 | * 303 | * // defining a spy on an existing property: foo.bar 304 | * spyOn(foo, 'bar').andCallThrough(); 305 | */ 306 | jasmine.Spy.prototype.andCallThrough = function() { 307 | this.plan = this.originalValue; 308 | return this; 309 | }; 310 | 311 | /** 312 | * For setting the return value of a spy. 313 | * 314 | * @example 315 | * // defining a spy from scratch: foo() returns 'baz' 316 | * var foo = jasmine.createSpy('spy on foo').andReturn('baz'); 317 | * 318 | * // defining a spy on an existing property: foo.bar() returns 'baz' 319 | * spyOn(foo, 'bar').andReturn('baz'); 320 | * 321 | * @param {Object} value 322 | */ 323 | jasmine.Spy.prototype.andReturn = function(value) { 324 | this.plan = function() { 325 | return value; 326 | }; 327 | return this; 328 | }; 329 | 330 | /** 331 | * For throwing an exception when a spy is called. 332 | * 333 | * @example 334 | * // defining a spy from scratch: foo() throws an exception w/ message 'ouch' 335 | * var foo = jasmine.createSpy('spy on foo').andThrow('baz'); 336 | * 337 | * // defining a spy on an existing property: foo.bar() throws an exception w/ message 'ouch' 338 | * spyOn(foo, 'bar').andThrow('baz'); 339 | * 340 | * @param {String} exceptionMsg 341 | */ 342 | jasmine.Spy.prototype.andThrow = function(exceptionMsg) { 343 | this.plan = function() { 344 | throw exceptionMsg; 345 | }; 346 | return this; 347 | }; 348 | 349 | /** 350 | * Calls an alternate implementation when a spy is called. 351 | * 352 | * @example 353 | * var baz = function() { 354 | * // do some stuff, return something 355 | * } 356 | * // defining a spy from scratch: foo() calls the function baz 357 | * var foo = jasmine.createSpy('spy on foo').andCall(baz); 358 | * 359 | * // defining a spy on an existing property: foo.bar() calls an anonymnous function 360 | * spyOn(foo, 'bar').andCall(function() { return 'baz';} ); 361 | * 362 | * @param {Function} fakeFunc 363 | */ 364 | jasmine.Spy.prototype.andCallFake = function(fakeFunc) { 365 | this.plan = fakeFunc; 366 | return this; 367 | }; 368 | 369 | /** 370 | * Resets all of a spy's the tracking variables so that it can be used again. 371 | * 372 | * @example 373 | * spyOn(foo, 'bar'); 374 | * 375 | * foo.bar(); 376 | * 377 | * expect(foo.bar.callCount).toEqual(1); 378 | * 379 | * foo.bar.reset(); 380 | * 381 | * expect(foo.bar.callCount).toEqual(0); 382 | */ 383 | jasmine.Spy.prototype.reset = function() { 384 | this.wasCalled = false; 385 | this.callCount = 0; 386 | this.argsForCall = []; 387 | this.calls = []; 388 | this.mostRecentCall = {}; 389 | }; 390 | 391 | jasmine.createSpy = function(name) { 392 | 393 | var spyObj = function() { 394 | spyObj.wasCalled = true; 395 | spyObj.callCount++; 396 | var args = jasmine.util.argsToArray(arguments); 397 | spyObj.mostRecentCall.object = this; 398 | spyObj.mostRecentCall.args = args; 399 | spyObj.argsForCall.push(args); 400 | spyObj.calls.push({object: this, args: args}); 401 | return spyObj.plan.apply(this, arguments); 402 | }; 403 | 404 | var spy = new jasmine.Spy(name); 405 | 406 | for (var prop in spy) { 407 | spyObj[prop] = spy[prop]; 408 | } 409 | 410 | spyObj.reset(); 411 | 412 | return spyObj; 413 | }; 414 | 415 | /** 416 | * Determines whether an object is a spy. 417 | * 418 | * @param {jasmine.Spy|Object} putativeSpy 419 | * @returns {Boolean} 420 | */ 421 | jasmine.isSpy = function(putativeSpy) { 422 | return putativeSpy && putativeSpy.isSpy; 423 | }; 424 | 425 | /** 426 | * Creates a more complicated spy: an Object that has every property a function that is a spy. Used for stubbing something 427 | * large in one call. 428 | * 429 | * @param {String} baseName name of spy class 430 | * @param {Array} methodNames array of names of methods to make spies 431 | */ 432 | jasmine.createSpyObj = function(baseName, methodNames) { 433 | if (!jasmine.isArray_(methodNames) || methodNames.length === 0) { 434 | throw new Error('createSpyObj requires a non-empty array of method names to create spies for'); 435 | } 436 | var obj = {}; 437 | for (var i = 0; i < methodNames.length; i++) { 438 | obj[methodNames[i]] = jasmine.createSpy(baseName + '.' + methodNames[i]); 439 | } 440 | return obj; 441 | }; 442 | 443 | /** 444 | * All parameters are pretty-printed and concatenated together, then written to the current spec's output. 445 | * 446 | * Be careful not to leave calls to jasmine.log in production code. 447 | */ 448 | jasmine.log = function() { 449 | var spec = jasmine.getEnv().currentSpec; 450 | spec.log.apply(spec, arguments); 451 | }; 452 | 453 | /** 454 | * Function that installs a spy on an existing object's method name. Used within a Spec to create a spy. 455 | * 456 | * @example 457 | * // spy example 458 | * var foo = { 459 | * not: function(bool) { return !bool; } 460 | * } 461 | * spyOn(foo, 'not'); // actual foo.not will not be called, execution stops 462 | * 463 | * @see jasmine.createSpy 464 | * @param obj 465 | * @param methodName 466 | * @returns a Jasmine spy that can be chained with all spy methods 467 | */ 468 | var spyOn = function(obj, methodName) { 469 | return jasmine.getEnv().currentSpec.spyOn(obj, methodName); 470 | }; 471 | if (isCommonJS) exports.spyOn = spyOn; 472 | 473 | /** 474 | * Creates a Jasmine spec that will be added to the current suite. 475 | * 476 | * // TODO: pending tests 477 | * 478 | * @example 479 | * it('should be true', function() { 480 | * expect(true).toEqual(true); 481 | * }); 482 | * 483 | * @param {String} desc description of this specification 484 | * @param {Function} func defines the preconditions and expectations of the spec 485 | */ 486 | var it = function(desc, func) { 487 | return jasmine.getEnv().it(desc, func); 488 | }; 489 | if (isCommonJS) exports.it = it; 490 | 491 | /** 492 | * Creates a disabled Jasmine spec. 493 | * 494 | * A convenience method that allows existing specs to be disabled temporarily during development. 495 | * 496 | * @param {String} desc description of this specification 497 | * @param {Function} func defines the preconditions and expectations of the spec 498 | */ 499 | var xit = function(desc, func) { 500 | return jasmine.getEnv().xit(desc, func); 501 | }; 502 | if (isCommonJS) exports.xit = xit; 503 | 504 | /** 505 | * Starts a chain for a Jasmine expectation. 506 | * 507 | * It is passed an Object that is the actual value and should chain to one of the many 508 | * jasmine.Matchers functions. 509 | * 510 | * @param {Object} actual Actual value to test against and expected value 511 | */ 512 | var expect = function(actual) { 513 | return jasmine.getEnv().currentSpec.expect(actual); 514 | }; 515 | if (isCommonJS) exports.expect = expect; 516 | 517 | /** 518 | * Defines part of a jasmine spec. Used in cominbination with waits or waitsFor in asynchrnous specs. 519 | * 520 | * @param {Function} func Function that defines part of a jasmine spec. 521 | */ 522 | var runs = function(func) { 523 | jasmine.getEnv().currentSpec.runs(func); 524 | }; 525 | if (isCommonJS) exports.runs = runs; 526 | 527 | /** 528 | * Waits a fixed time period before moving to the next block. 529 | * 530 | * @deprecated Use waitsFor() instead 531 | * @param {Number} timeout milliseconds to wait 532 | */ 533 | var waits = function(timeout) { 534 | jasmine.getEnv().currentSpec.waits(timeout); 535 | }; 536 | if (isCommonJS) exports.waits = waits; 537 | 538 | /** 539 | * Waits for the latchFunction to return true before proceeding to the next block. 540 | * 541 | * @param {Function} latchFunction 542 | * @param {String} optional_timeoutMessage 543 | * @param {Number} optional_timeout 544 | */ 545 | var waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { 546 | jasmine.getEnv().currentSpec.waitsFor.apply(jasmine.getEnv().currentSpec, arguments); 547 | }; 548 | if (isCommonJS) exports.waitsFor = waitsFor; 549 | 550 | /** 551 | * A function that is called before each spec in a suite. 552 | * 553 | * Used for spec setup, including validating assumptions. 554 | * 555 | * @param {Function} beforeEachFunction 556 | */ 557 | var beforeEach = function(beforeEachFunction) { 558 | jasmine.getEnv().beforeEach(beforeEachFunction); 559 | }; 560 | if (isCommonJS) exports.beforeEach = beforeEach; 561 | 562 | /** 563 | * A function that is called after each spec in a suite. 564 | * 565 | * Used for restoring any state that is hijacked during spec execution. 566 | * 567 | * @param {Function} afterEachFunction 568 | */ 569 | var afterEach = function(afterEachFunction) { 570 | jasmine.getEnv().afterEach(afterEachFunction); 571 | }; 572 | if (isCommonJS) exports.afterEach = afterEach; 573 | 574 | /** 575 | * Defines a suite of specifications. 576 | * 577 | * Stores the description and all defined specs in the Jasmine environment as one suite of specs. Variables declared 578 | * are accessible by calls to beforeEach, it, and afterEach. Describe blocks can be nested, allowing for specialization 579 | * of setup in some tests. 580 | * 581 | * @example 582 | * // TODO: a simple suite 583 | * 584 | * // TODO: a simple suite with a nested describe block 585 | * 586 | * @param {String} description A string, usually the class under test. 587 | * @param {Function} specDefinitions function that defines several specs. 588 | */ 589 | var describe = function(description, specDefinitions) { 590 | return jasmine.getEnv().describe(description, specDefinitions); 591 | }; 592 | if (isCommonJS) exports.describe = describe; 593 | 594 | /** 595 | * Disables a suite of specifications. Used to disable some suites in a file, or files, temporarily during development. 596 | * 597 | * @param {String} description A string, usually the class under test. 598 | * @param {Function} specDefinitions function that defines several specs. 599 | */ 600 | var xdescribe = function(description, specDefinitions) { 601 | return jasmine.getEnv().xdescribe(description, specDefinitions); 602 | }; 603 | if (isCommonJS) exports.xdescribe = xdescribe; 604 | 605 | 606 | // Provide the XMLHttpRequest class for IE 5.x-6.x: 607 | jasmine.XmlHttpRequest = (typeof XMLHttpRequest == "undefined") ? function() { 608 | function tryIt(f) { 609 | try { 610 | return f(); 611 | } catch(e) { 612 | } 613 | return null; 614 | } 615 | 616 | var xhr = tryIt(function() { 617 | return new ActiveXObject("Msxml2.XMLHTTP.6.0"); 618 | }) || 619 | tryIt(function() { 620 | return new ActiveXObject("Msxml2.XMLHTTP.3.0"); 621 | }) || 622 | tryIt(function() { 623 | return new ActiveXObject("Msxml2.XMLHTTP"); 624 | }) || 625 | tryIt(function() { 626 | return new ActiveXObject("Microsoft.XMLHTTP"); 627 | }); 628 | 629 | if (!xhr) throw new Error("This browser does not support XMLHttpRequest."); 630 | 631 | return xhr; 632 | } : XMLHttpRequest; 633 | /** 634 | * @namespace 635 | */ 636 | jasmine.util = {}; 637 | 638 | /** 639 | * Declare that a child class inherit it's prototype from the parent class. 640 | * 641 | * @private 642 | * @param {Function} childClass 643 | * @param {Function} parentClass 644 | */ 645 | jasmine.util.inherit = function(childClass, parentClass) { 646 | /** 647 | * @private 648 | */ 649 | var subclass = function() { 650 | }; 651 | subclass.prototype = parentClass.prototype; 652 | childClass.prototype = new subclass(); 653 | }; 654 | 655 | jasmine.util.formatException = function(e) { 656 | var lineNumber; 657 | if (e.line) { 658 | lineNumber = e.line; 659 | } 660 | else if (e.lineNumber) { 661 | lineNumber = e.lineNumber; 662 | } 663 | 664 | var file; 665 | 666 | if (e.sourceURL) { 667 | file = e.sourceURL; 668 | } 669 | else if (e.fileName) { 670 | file = e.fileName; 671 | } 672 | 673 | var message = (e.name && e.message) ? (e.name + ': ' + e.message) : e.toString(); 674 | 675 | if (file && lineNumber) { 676 | message += ' in ' + file + ' (line ' + lineNumber + ')'; 677 | } 678 | 679 | return message; 680 | }; 681 | 682 | jasmine.util.htmlEscape = function(str) { 683 | if (!str) return str; 684 | return str.replace(/&/g, '&') 685 | .replace(//g, '>'); 687 | }; 688 | 689 | jasmine.util.argsToArray = function(args) { 690 | var arrayOfArgs = []; 691 | for (var i = 0; i < args.length; i++) arrayOfArgs.push(args[i]); 692 | return arrayOfArgs; 693 | }; 694 | 695 | jasmine.util.extend = function(destination, source) { 696 | for (var property in source) destination[property] = source[property]; 697 | return destination; 698 | }; 699 | 700 | /** 701 | * Environment for Jasmine 702 | * 703 | * @constructor 704 | */ 705 | jasmine.Env = function() { 706 | this.currentSpec = null; 707 | this.currentSuite = null; 708 | this.currentRunner_ = new jasmine.Runner(this); 709 | 710 | this.reporter = new jasmine.MultiReporter(); 711 | 712 | this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL; 713 | this.defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; 714 | this.lastUpdate = 0; 715 | this.specFilter = function() { 716 | return true; 717 | }; 718 | 719 | this.nextSpecId_ = 0; 720 | this.nextSuiteId_ = 0; 721 | this.equalityTesters_ = []; 722 | 723 | // wrap matchers 724 | this.matchersClass = function() { 725 | jasmine.Matchers.apply(this, arguments); 726 | }; 727 | jasmine.util.inherit(this.matchersClass, jasmine.Matchers); 728 | 729 | jasmine.Matchers.wrapInto_(jasmine.Matchers.prototype, this.matchersClass); 730 | }; 731 | 732 | 733 | jasmine.Env.prototype.setTimeout = jasmine.setTimeout; 734 | jasmine.Env.prototype.clearTimeout = jasmine.clearTimeout; 735 | jasmine.Env.prototype.setInterval = jasmine.setInterval; 736 | jasmine.Env.prototype.clearInterval = jasmine.clearInterval; 737 | 738 | /** 739 | * @returns an object containing jasmine version build info, if set. 740 | */ 741 | jasmine.Env.prototype.version = function () { 742 | if (jasmine.version_) { 743 | return jasmine.version_; 744 | } else { 745 | throw new Error('Version not set'); 746 | } 747 | }; 748 | 749 | /** 750 | * @returns string containing jasmine version build info, if set. 751 | */ 752 | jasmine.Env.prototype.versionString = function() { 753 | if (!jasmine.version_) { 754 | return "version unknown"; 755 | } 756 | 757 | var version = this.version(); 758 | var versionString = version.major + "." + version.minor + "." + version.build; 759 | if (version.release_candidate) { 760 | versionString += ".rc" + version.release_candidate; 761 | } 762 | versionString += " revision " + version.revision; 763 | return versionString; 764 | }; 765 | 766 | /** 767 | * @returns a sequential integer starting at 0 768 | */ 769 | jasmine.Env.prototype.nextSpecId = function () { 770 | return this.nextSpecId_++; 771 | }; 772 | 773 | /** 774 | * @returns a sequential integer starting at 0 775 | */ 776 | jasmine.Env.prototype.nextSuiteId = function () { 777 | return this.nextSuiteId_++; 778 | }; 779 | 780 | /** 781 | * Register a reporter to receive status updates from Jasmine. 782 | * @param {jasmine.Reporter} reporter An object which will receive status updates. 783 | */ 784 | jasmine.Env.prototype.addReporter = function(reporter) { 785 | this.reporter.addReporter(reporter); 786 | }; 787 | 788 | jasmine.Env.prototype.execute = function() { 789 | this.currentRunner_.execute(); 790 | }; 791 | 792 | jasmine.Env.prototype.describe = function(description, specDefinitions) { 793 | var suite = new jasmine.Suite(this, description, specDefinitions, this.currentSuite); 794 | 795 | var parentSuite = this.currentSuite; 796 | if (parentSuite) { 797 | parentSuite.add(suite); 798 | } else { 799 | this.currentRunner_.add(suite); 800 | } 801 | 802 | this.currentSuite = suite; 803 | 804 | var declarationError = null; 805 | try { 806 | specDefinitions.call(suite); 807 | } catch(e) { 808 | declarationError = e; 809 | } 810 | 811 | if (declarationError) { 812 | this.it("encountered a declaration exception", function() { 813 | throw declarationError; 814 | }); 815 | } 816 | 817 | this.currentSuite = parentSuite; 818 | 819 | return suite; 820 | }; 821 | 822 | jasmine.Env.prototype.beforeEach = function(beforeEachFunction) { 823 | if (this.currentSuite) { 824 | this.currentSuite.beforeEach(beforeEachFunction); 825 | } else { 826 | this.currentRunner_.beforeEach(beforeEachFunction); 827 | } 828 | }; 829 | 830 | jasmine.Env.prototype.currentRunner = function () { 831 | return this.currentRunner_; 832 | }; 833 | 834 | jasmine.Env.prototype.afterEach = function(afterEachFunction) { 835 | if (this.currentSuite) { 836 | this.currentSuite.afterEach(afterEachFunction); 837 | } else { 838 | this.currentRunner_.afterEach(afterEachFunction); 839 | } 840 | 841 | }; 842 | 843 | jasmine.Env.prototype.xdescribe = function(desc, specDefinitions) { 844 | return { 845 | execute: function() { 846 | } 847 | }; 848 | }; 849 | 850 | jasmine.Env.prototype.it = function(description, func) { 851 | var spec = new jasmine.Spec(this, this.currentSuite, description); 852 | this.currentSuite.add(spec); 853 | this.currentSpec = spec; 854 | 855 | if (func) { 856 | spec.runs(func); 857 | } 858 | 859 | return spec; 860 | }; 861 | 862 | jasmine.Env.prototype.xit = function(desc, func) { 863 | return { 864 | id: this.nextSpecId(), 865 | runs: function() { 866 | } 867 | }; 868 | }; 869 | 870 | jasmine.Env.prototype.compareObjects_ = function(a, b, mismatchKeys, mismatchValues) { 871 | if (a.__Jasmine_been_here_before__ === b && b.__Jasmine_been_here_before__ === a) { 872 | return true; 873 | } 874 | 875 | a.__Jasmine_been_here_before__ = b; 876 | b.__Jasmine_been_here_before__ = a; 877 | 878 | var hasKey = function(obj, keyName) { 879 | return obj !== null && obj[keyName] !== jasmine.undefined; 880 | }; 881 | 882 | for (var property in b) { 883 | if (!hasKey(a, property) && hasKey(b, property)) { 884 | mismatchKeys.push("expected has key '" + property + "', but missing from actual."); 885 | } 886 | } 887 | for (property in a) { 888 | if (!hasKey(b, property) && hasKey(a, property)) { 889 | mismatchKeys.push("expected missing key '" + property + "', but present in actual."); 890 | } 891 | } 892 | for (property in b) { 893 | if (property == '__Jasmine_been_here_before__') continue; 894 | if (!this.equals_(a[property], b[property], mismatchKeys, mismatchValues)) { 895 | mismatchValues.push("'" + property + "' was '" + (b[property] ? jasmine.util.htmlEscape(b[property].toString()) : b[property]) + "' in expected, but was '" + (a[property] ? jasmine.util.htmlEscape(a[property].toString()) : a[property]) + "' in actual."); 896 | } 897 | } 898 | 899 | if (jasmine.isArray_(a) && jasmine.isArray_(b) && a.length != b.length) { 900 | mismatchValues.push("arrays were not the same length"); 901 | } 902 | 903 | delete a.__Jasmine_been_here_before__; 904 | delete b.__Jasmine_been_here_before__; 905 | return (mismatchKeys.length === 0 && mismatchValues.length === 0); 906 | }; 907 | 908 | jasmine.Env.prototype.equals_ = function(a, b, mismatchKeys, mismatchValues) { 909 | mismatchKeys = mismatchKeys || []; 910 | mismatchValues = mismatchValues || []; 911 | 912 | for (var i = 0; i < this.equalityTesters_.length; i++) { 913 | var equalityTester = this.equalityTesters_[i]; 914 | var result = equalityTester(a, b, this, mismatchKeys, mismatchValues); 915 | if (result !== jasmine.undefined) return result; 916 | } 917 | 918 | if (a === b) return true; 919 | 920 | if (a === jasmine.undefined || a === null || b === jasmine.undefined || b === null) { 921 | return (a == jasmine.undefined && b == jasmine.undefined); 922 | } 923 | 924 | if (jasmine.isDomNode(a) && jasmine.isDomNode(b)) { 925 | return a === b; 926 | } 927 | 928 | if (a instanceof Date && b instanceof Date) { 929 | return a.getTime() == b.getTime(); 930 | } 931 | 932 | if (a.jasmineMatches) { 933 | return a.jasmineMatches(b); 934 | } 935 | 936 | if (b.jasmineMatches) { 937 | return b.jasmineMatches(a); 938 | } 939 | 940 | if (a instanceof jasmine.Matchers.ObjectContaining) { 941 | return a.matches(b); 942 | } 943 | 944 | if (b instanceof jasmine.Matchers.ObjectContaining) { 945 | return b.matches(a); 946 | } 947 | 948 | if (jasmine.isString_(a) && jasmine.isString_(b)) { 949 | return (a == b); 950 | } 951 | 952 | if (jasmine.isNumber_(a) && jasmine.isNumber_(b)) { 953 | return (a == b); 954 | } 955 | 956 | if (typeof a === "object" && typeof b === "object") { 957 | return this.compareObjects_(a, b, mismatchKeys, mismatchValues); 958 | } 959 | 960 | //Straight check 961 | return (a === b); 962 | }; 963 | 964 | jasmine.Env.prototype.contains_ = function(haystack, needle) { 965 | if (jasmine.isArray_(haystack)) { 966 | for (var i = 0; i < haystack.length; i++) { 967 | if (this.equals_(haystack[i], needle)) return true; 968 | } 969 | return false; 970 | } 971 | return haystack.indexOf(needle) >= 0; 972 | }; 973 | 974 | jasmine.Env.prototype.addEqualityTester = function(equalityTester) { 975 | this.equalityTesters_.push(equalityTester); 976 | }; 977 | /** No-op base class for Jasmine reporters. 978 | * 979 | * @constructor 980 | */ 981 | jasmine.Reporter = function() { 982 | }; 983 | 984 | //noinspection JSUnusedLocalSymbols 985 | jasmine.Reporter.prototype.reportRunnerStarting = function(runner) { 986 | }; 987 | 988 | //noinspection JSUnusedLocalSymbols 989 | jasmine.Reporter.prototype.reportRunnerResults = function(runner) { 990 | }; 991 | 992 | //noinspection JSUnusedLocalSymbols 993 | jasmine.Reporter.prototype.reportSuiteResults = function(suite) { 994 | }; 995 | 996 | //noinspection JSUnusedLocalSymbols 997 | jasmine.Reporter.prototype.reportSpecStarting = function(spec) { 998 | }; 999 | 1000 | //noinspection JSUnusedLocalSymbols 1001 | jasmine.Reporter.prototype.reportSpecResults = function(spec) { 1002 | }; 1003 | 1004 | //noinspection JSUnusedLocalSymbols 1005 | jasmine.Reporter.prototype.log = function(str) { 1006 | }; 1007 | 1008 | /** 1009 | * Blocks are functions with executable code that make up a spec. 1010 | * 1011 | * @constructor 1012 | * @param {jasmine.Env} env 1013 | * @param {Function} func 1014 | * @param {jasmine.Spec} spec 1015 | */ 1016 | jasmine.Block = function(env, func, spec) { 1017 | this.env = env; 1018 | this.func = func; 1019 | this.spec = spec; 1020 | }; 1021 | 1022 | jasmine.Block.prototype.execute = function(onComplete) { 1023 | try { 1024 | this.func.apply(this.spec); 1025 | } catch (e) { 1026 | this.spec.fail(e); 1027 | } 1028 | onComplete(); 1029 | }; 1030 | /** JavaScript API reporter. 1031 | * 1032 | * @constructor 1033 | */ 1034 | jasmine.JsApiReporter = function() { 1035 | this.started = false; 1036 | this.finished = false; 1037 | this.suites_ = []; 1038 | this.results_ = {}; 1039 | }; 1040 | 1041 | jasmine.JsApiReporter.prototype.reportRunnerStarting = function(runner) { 1042 | this.started = true; 1043 | var suites = runner.topLevelSuites(); 1044 | for (var i = 0; i < suites.length; i++) { 1045 | var suite = suites[i]; 1046 | this.suites_.push(this.summarize_(suite)); 1047 | } 1048 | }; 1049 | 1050 | jasmine.JsApiReporter.prototype.suites = function() { 1051 | return this.suites_; 1052 | }; 1053 | 1054 | jasmine.JsApiReporter.prototype.summarize_ = function(suiteOrSpec) { 1055 | var isSuite = suiteOrSpec instanceof jasmine.Suite; 1056 | var summary = { 1057 | id: suiteOrSpec.id, 1058 | name: suiteOrSpec.description, 1059 | type: isSuite ? 'suite' : 'spec', 1060 | children: [] 1061 | }; 1062 | 1063 | if (isSuite) { 1064 | var children = suiteOrSpec.children(); 1065 | for (var i = 0; i < children.length; i++) { 1066 | summary.children.push(this.summarize_(children[i])); 1067 | } 1068 | } 1069 | return summary; 1070 | }; 1071 | 1072 | jasmine.JsApiReporter.prototype.results = function() { 1073 | return this.results_; 1074 | }; 1075 | 1076 | jasmine.JsApiReporter.prototype.resultsForSpec = function(specId) { 1077 | return this.results_[specId]; 1078 | }; 1079 | 1080 | //noinspection JSUnusedLocalSymbols 1081 | jasmine.JsApiReporter.prototype.reportRunnerResults = function(runner) { 1082 | this.finished = true; 1083 | }; 1084 | 1085 | //noinspection JSUnusedLocalSymbols 1086 | jasmine.JsApiReporter.prototype.reportSuiteResults = function(suite) { 1087 | }; 1088 | 1089 | //noinspection JSUnusedLocalSymbols 1090 | jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) { 1091 | this.results_[spec.id] = { 1092 | messages: spec.results().getItems(), 1093 | result: spec.results().failedCount > 0 ? "failed" : "passed" 1094 | }; 1095 | }; 1096 | 1097 | //noinspection JSUnusedLocalSymbols 1098 | jasmine.JsApiReporter.prototype.log = function(str) { 1099 | }; 1100 | 1101 | jasmine.JsApiReporter.prototype.resultsForSpecs = function(specIds){ 1102 | var results = {}; 1103 | for (var i = 0; i < specIds.length; i++) { 1104 | var specId = specIds[i]; 1105 | results[specId] = this.summarizeResult_(this.results_[specId]); 1106 | } 1107 | return results; 1108 | }; 1109 | 1110 | jasmine.JsApiReporter.prototype.summarizeResult_ = function(result){ 1111 | var summaryMessages = []; 1112 | var messagesLength = result.messages.length; 1113 | for (var messageIndex = 0; messageIndex < messagesLength; messageIndex++) { 1114 | var resultMessage = result.messages[messageIndex]; 1115 | summaryMessages.push({ 1116 | text: resultMessage.type == 'log' ? resultMessage.toString() : jasmine.undefined, 1117 | passed: resultMessage.passed ? resultMessage.passed() : true, 1118 | type: resultMessage.type, 1119 | message: resultMessage.message, 1120 | trace: { 1121 | stack: resultMessage.passed && !resultMessage.passed() ? resultMessage.trace.stack : jasmine.undefined 1122 | } 1123 | }); 1124 | } 1125 | 1126 | return { 1127 | result : result.result, 1128 | messages : summaryMessages 1129 | }; 1130 | }; 1131 | 1132 | /** 1133 | * @constructor 1134 | * @param {jasmine.Env} env 1135 | * @param actual 1136 | * @param {jasmine.Spec} spec 1137 | */ 1138 | jasmine.Matchers = function(env, actual, spec, opt_isNot) { 1139 | this.env = env; 1140 | this.actual = actual; 1141 | this.spec = spec; 1142 | this.isNot = opt_isNot || false; 1143 | this.reportWasCalled_ = false; 1144 | }; 1145 | 1146 | // todo: @deprecated as of Jasmine 0.11, remove soon [xw] 1147 | jasmine.Matchers.pp = function(str) { 1148 | throw new Error("jasmine.Matchers.pp() is no longer supported, please use jasmine.pp() instead!"); 1149 | }; 1150 | 1151 | // todo: @deprecated Deprecated as of Jasmine 0.10. Rewrite your custom matchers to return true or false. [xw] 1152 | jasmine.Matchers.prototype.report = function(result, failing_message, details) { 1153 | throw new Error("As of jasmine 0.11, custom matchers must be implemented differently -- please see jasmine docs"); 1154 | }; 1155 | 1156 | jasmine.Matchers.wrapInto_ = function(prototype, matchersClass) { 1157 | for (var methodName in prototype) { 1158 | if (methodName == 'report') continue; 1159 | var orig = prototype[methodName]; 1160 | matchersClass.prototype[methodName] = jasmine.Matchers.matcherFn_(methodName, orig); 1161 | } 1162 | }; 1163 | 1164 | jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) { 1165 | return function() { 1166 | var matcherArgs = jasmine.util.argsToArray(arguments); 1167 | var result = matcherFunction.apply(this, arguments); 1168 | 1169 | if (this.isNot) { 1170 | result = !result; 1171 | } 1172 | 1173 | if (this.reportWasCalled_) return result; 1174 | 1175 | var message; 1176 | if (!result) { 1177 | if (this.message) { 1178 | message = this.message.apply(this, arguments); 1179 | if (jasmine.isArray_(message)) { 1180 | message = message[this.isNot ? 1 : 0]; 1181 | } 1182 | } else { 1183 | var englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) { return ' ' + s.toLowerCase(); }); 1184 | message = "Expected " + jasmine.pp(this.actual) + (this.isNot ? " not " : " ") + englishyPredicate; 1185 | if (matcherArgs.length > 0) { 1186 | for (var i = 0; i < matcherArgs.length; i++) { 1187 | if (i > 0) message += ","; 1188 | message += " " + jasmine.pp(matcherArgs[i]); 1189 | } 1190 | } 1191 | message += "."; 1192 | } 1193 | } 1194 | var expectationResult = new jasmine.ExpectationResult({ 1195 | matcherName: matcherName, 1196 | passed: result, 1197 | expected: matcherArgs.length > 1 ? matcherArgs : matcherArgs[0], 1198 | actual: this.actual, 1199 | message: message 1200 | }); 1201 | this.spec.addMatcherResult(expectationResult); 1202 | return jasmine.undefined; 1203 | }; 1204 | }; 1205 | 1206 | 1207 | 1208 | 1209 | /** 1210 | * toBe: compares the actual to the expected using === 1211 | * @param expected 1212 | */ 1213 | jasmine.Matchers.prototype.toBe = function(expected) { 1214 | return this.actual === expected; 1215 | }; 1216 | 1217 | /** 1218 | * toNotBe: compares the actual to the expected using !== 1219 | * @param expected 1220 | * @deprecated as of 1.0. Use not.toBe() instead. 1221 | */ 1222 | jasmine.Matchers.prototype.toNotBe = function(expected) { 1223 | return this.actual !== expected; 1224 | }; 1225 | 1226 | /** 1227 | * toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc. 1228 | * 1229 | * @param expected 1230 | */ 1231 | jasmine.Matchers.prototype.toEqual = function(expected) { 1232 | return this.env.equals_(this.actual, expected); 1233 | }; 1234 | 1235 | /** 1236 | * toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual 1237 | * @param expected 1238 | * @deprecated as of 1.0. Use not.toEqual() instead. 1239 | */ 1240 | jasmine.Matchers.prototype.toNotEqual = function(expected) { 1241 | return !this.env.equals_(this.actual, expected); 1242 | }; 1243 | 1244 | /** 1245 | * Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes 1246 | * a pattern or a String. 1247 | * 1248 | * @param expected 1249 | */ 1250 | jasmine.Matchers.prototype.toMatch = function(expected) { 1251 | return new RegExp(expected).test(this.actual); 1252 | }; 1253 | 1254 | /** 1255 | * Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch 1256 | * @param expected 1257 | * @deprecated as of 1.0. Use not.toMatch() instead. 1258 | */ 1259 | jasmine.Matchers.prototype.toNotMatch = function(expected) { 1260 | return !(new RegExp(expected).test(this.actual)); 1261 | }; 1262 | 1263 | /** 1264 | * Matcher that compares the actual to jasmine.undefined. 1265 | */ 1266 | jasmine.Matchers.prototype.toBeDefined = function() { 1267 | return (this.actual !== jasmine.undefined); 1268 | }; 1269 | 1270 | /** 1271 | * Matcher that compares the actual to jasmine.undefined. 1272 | */ 1273 | jasmine.Matchers.prototype.toBeUndefined = function() { 1274 | return (this.actual === jasmine.undefined); 1275 | }; 1276 | 1277 | /** 1278 | * Matcher that compares the actual to null. 1279 | */ 1280 | jasmine.Matchers.prototype.toBeNull = function() { 1281 | return (this.actual === null); 1282 | }; 1283 | 1284 | /** 1285 | * Matcher that boolean not-nots the actual. 1286 | */ 1287 | jasmine.Matchers.prototype.toBeTruthy = function() { 1288 | return !!this.actual; 1289 | }; 1290 | 1291 | 1292 | /** 1293 | * Matcher that boolean nots the actual. 1294 | */ 1295 | jasmine.Matchers.prototype.toBeFalsy = function() { 1296 | return !this.actual; 1297 | }; 1298 | 1299 | 1300 | /** 1301 | * Matcher that checks to see if the actual, a Jasmine spy, was called. 1302 | */ 1303 | jasmine.Matchers.prototype.toHaveBeenCalled = function() { 1304 | if (arguments.length > 0) { 1305 | throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith'); 1306 | } 1307 | 1308 | if (!jasmine.isSpy(this.actual)) { 1309 | throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); 1310 | } 1311 | 1312 | this.message = function() { 1313 | return [ 1314 | "Expected spy " + this.actual.identity + " to have been called.", 1315 | "Expected spy " + this.actual.identity + " not to have been called." 1316 | ]; 1317 | }; 1318 | 1319 | return this.actual.wasCalled; 1320 | }; 1321 | 1322 | /** @deprecated Use expect(xxx).toHaveBeenCalled() instead */ 1323 | jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.prototype.toHaveBeenCalled; 1324 | 1325 | /** 1326 | * Matcher that checks to see if the actual, a Jasmine spy, was not called. 1327 | * 1328 | * @deprecated Use expect(xxx).not.toHaveBeenCalled() instead 1329 | */ 1330 | jasmine.Matchers.prototype.wasNotCalled = function() { 1331 | if (arguments.length > 0) { 1332 | throw new Error('wasNotCalled does not take arguments'); 1333 | } 1334 | 1335 | if (!jasmine.isSpy(this.actual)) { 1336 | throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); 1337 | } 1338 | 1339 | this.message = function() { 1340 | return [ 1341 | "Expected spy " + this.actual.identity + " to not have been called.", 1342 | "Expected spy " + this.actual.identity + " to have been called." 1343 | ]; 1344 | }; 1345 | 1346 | return !this.actual.wasCalled; 1347 | }; 1348 | 1349 | /** 1350 | * Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters. 1351 | * 1352 | * @example 1353 | * 1354 | */ 1355 | jasmine.Matchers.prototype.toHaveBeenCalledWith = function() { 1356 | var expectedArgs = jasmine.util.argsToArray(arguments); 1357 | if (!jasmine.isSpy(this.actual)) { 1358 | throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); 1359 | } 1360 | this.message = function() { 1361 | if (this.actual.callCount === 0) { 1362 | // todo: what should the failure message for .not.toHaveBeenCalledWith() be? is this right? test better. [xw] 1363 | return [ 1364 | "Expected spy " + this.actual.identity + " to have been called with " + jasmine.pp(expectedArgs) + " but it was never called.", 1365 | "Expected spy " + this.actual.identity + " not to have been called with " + jasmine.pp(expectedArgs) + " but it was." 1366 | ]; 1367 | } else { 1368 | return [ 1369 | "Expected spy " + this.actual.identity + " to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall), 1370 | "Expected spy " + this.actual.identity + " not to have been called with " + jasmine.pp(expectedArgs) + " but was called with " + jasmine.pp(this.actual.argsForCall) 1371 | ]; 1372 | } 1373 | }; 1374 | 1375 | return this.env.contains_(this.actual.argsForCall, expectedArgs); 1376 | }; 1377 | 1378 | /** @deprecated Use expect(xxx).toHaveBeenCalledWith() instead */ 1379 | jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.prototype.toHaveBeenCalledWith; 1380 | 1381 | /** @deprecated Use expect(xxx).not.toHaveBeenCalledWith() instead */ 1382 | jasmine.Matchers.prototype.wasNotCalledWith = function() { 1383 | var expectedArgs = jasmine.util.argsToArray(arguments); 1384 | if (!jasmine.isSpy(this.actual)) { 1385 | throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.'); 1386 | } 1387 | 1388 | this.message = function() { 1389 | return [ 1390 | "Expected spy not to have been called with " + jasmine.pp(expectedArgs) + " but it was", 1391 | "Expected spy to have been called with " + jasmine.pp(expectedArgs) + " but it was" 1392 | ]; 1393 | }; 1394 | 1395 | return !this.env.contains_(this.actual.argsForCall, expectedArgs); 1396 | }; 1397 | 1398 | /** 1399 | * Matcher that checks that the expected item is an element in the actual Array. 1400 | * 1401 | * @param {Object} expected 1402 | */ 1403 | jasmine.Matchers.prototype.toContain = function(expected) { 1404 | return this.env.contains_(this.actual, expected); 1405 | }; 1406 | 1407 | /** 1408 | * Matcher that checks that the expected item is NOT an element in the actual Array. 1409 | * 1410 | * @param {Object} expected 1411 | * @deprecated as of 1.0. Use not.toContain() instead. 1412 | */ 1413 | jasmine.Matchers.prototype.toNotContain = function(expected) { 1414 | return !this.env.contains_(this.actual, expected); 1415 | }; 1416 | 1417 | jasmine.Matchers.prototype.toBeLessThan = function(expected) { 1418 | return this.actual < expected; 1419 | }; 1420 | 1421 | jasmine.Matchers.prototype.toBeGreaterThan = function(expected) { 1422 | return this.actual > expected; 1423 | }; 1424 | 1425 | /** 1426 | * Matcher that checks that the expected item is equal to the actual item 1427 | * up to a given level of decimal precision (default 2). 1428 | * 1429 | * @param {Number} expected 1430 | * @param {Number} precision 1431 | */ 1432 | jasmine.Matchers.prototype.toBeCloseTo = function(expected, precision) { 1433 | if (!(precision === 0)) { 1434 | precision = precision || 2; 1435 | } 1436 | var multiplier = Math.pow(10, precision); 1437 | var actual = Math.round(this.actual * multiplier); 1438 | expected = Math.round(expected * multiplier); 1439 | return expected == actual; 1440 | }; 1441 | 1442 | /** 1443 | * Matcher that checks that the expected exception was thrown by the actual. 1444 | * 1445 | * @param {String} expected 1446 | */ 1447 | jasmine.Matchers.prototype.toThrow = function(expected) { 1448 | var result = false; 1449 | var exception; 1450 | if (typeof this.actual != 'function') { 1451 | throw new Error('Actual is not a function'); 1452 | } 1453 | try { 1454 | this.actual(); 1455 | } catch (e) { 1456 | exception = e; 1457 | } 1458 | if (exception) { 1459 | result = (expected === jasmine.undefined || this.env.equals_(exception.message || exception, expected.message || expected)); 1460 | } 1461 | 1462 | var not = this.isNot ? "not " : ""; 1463 | 1464 | this.message = function() { 1465 | if (exception && (expected === jasmine.undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) { 1466 | return ["Expected function " + not + "to throw", expected ? expected.message || expected : "an exception", ", but it threw", exception.message || exception].join(' '); 1467 | } else { 1468 | return "Expected function to throw an exception."; 1469 | } 1470 | }; 1471 | 1472 | return result; 1473 | }; 1474 | 1475 | jasmine.Matchers.Any = function(expectedClass) { 1476 | this.expectedClass = expectedClass; 1477 | }; 1478 | 1479 | jasmine.Matchers.Any.prototype.jasmineMatches = function(other) { 1480 | if (this.expectedClass == String) { 1481 | return typeof other == 'string' || other instanceof String; 1482 | } 1483 | 1484 | if (this.expectedClass == Number) { 1485 | return typeof other == 'number' || other instanceof Number; 1486 | } 1487 | 1488 | if (this.expectedClass == Function) { 1489 | return typeof other == 'function' || other instanceof Function; 1490 | } 1491 | 1492 | if (this.expectedClass == Object) { 1493 | return typeof other == 'object'; 1494 | } 1495 | 1496 | return other instanceof this.expectedClass; 1497 | }; 1498 | 1499 | jasmine.Matchers.Any.prototype.jasmineToString = function() { 1500 | return ''; 1501 | }; 1502 | 1503 | jasmine.Matchers.ObjectContaining = function (sample) { 1504 | this.sample = sample; 1505 | }; 1506 | 1507 | jasmine.Matchers.ObjectContaining.prototype.jasmineMatches = function(other, mismatchKeys, mismatchValues) { 1508 | mismatchKeys = mismatchKeys || []; 1509 | mismatchValues = mismatchValues || []; 1510 | 1511 | var env = jasmine.getEnv(); 1512 | 1513 | var hasKey = function(obj, keyName) { 1514 | return obj != null && obj[keyName] !== jasmine.undefined; 1515 | }; 1516 | 1517 | for (var property in this.sample) { 1518 | if (!hasKey(other, property) && hasKey(this.sample, property)) { 1519 | mismatchKeys.push("expected has key '" + property + "', but missing from actual."); 1520 | } 1521 | else if (!env.equals_(this.sample[property], other[property], mismatchKeys, mismatchValues)) { 1522 | mismatchValues.push("'" + property + "' was '" + (other[property] ? jasmine.util.htmlEscape(other[property].toString()) : other[property]) + "' in expected, but was '" + (this.sample[property] ? jasmine.util.htmlEscape(this.sample[property].toString()) : this.sample[property]) + "' in actual."); 1523 | } 1524 | } 1525 | 1526 | return (mismatchKeys.length === 0 && mismatchValues.length === 0); 1527 | }; 1528 | 1529 | jasmine.Matchers.ObjectContaining.prototype.jasmineToString = function () { 1530 | return ""; 1531 | }; 1532 | // Mock setTimeout, clearTimeout 1533 | // Contributed by Pivotal Computer Systems, www.pivotalsf.com 1534 | 1535 | jasmine.FakeTimer = function() { 1536 | this.reset(); 1537 | 1538 | var self = this; 1539 | self.setTimeout = function(funcToCall, millis) { 1540 | self.timeoutsMade++; 1541 | self.scheduleFunction(self.timeoutsMade, funcToCall, millis, false); 1542 | return self.timeoutsMade; 1543 | }; 1544 | 1545 | self.setInterval = function(funcToCall, millis) { 1546 | self.timeoutsMade++; 1547 | self.scheduleFunction(self.timeoutsMade, funcToCall, millis, true); 1548 | return self.timeoutsMade; 1549 | }; 1550 | 1551 | self.clearTimeout = function(timeoutKey) { 1552 | self.scheduledFunctions[timeoutKey] = jasmine.undefined; 1553 | }; 1554 | 1555 | self.clearInterval = function(timeoutKey) { 1556 | self.scheduledFunctions[timeoutKey] = jasmine.undefined; 1557 | }; 1558 | 1559 | }; 1560 | 1561 | jasmine.FakeTimer.prototype.reset = function() { 1562 | this.timeoutsMade = 0; 1563 | this.scheduledFunctions = {}; 1564 | this.nowMillis = 0; 1565 | }; 1566 | 1567 | jasmine.FakeTimer.prototype.tick = function(millis) { 1568 | var oldMillis = this.nowMillis; 1569 | var newMillis = oldMillis + millis; 1570 | this.runFunctionsWithinRange(oldMillis, newMillis); 1571 | this.nowMillis = newMillis; 1572 | }; 1573 | 1574 | jasmine.FakeTimer.prototype.runFunctionsWithinRange = function(oldMillis, nowMillis) { 1575 | var scheduledFunc; 1576 | var funcsToRun = []; 1577 | for (var timeoutKey in this.scheduledFunctions) { 1578 | scheduledFunc = this.scheduledFunctions[timeoutKey]; 1579 | if (scheduledFunc != jasmine.undefined && 1580 | scheduledFunc.runAtMillis >= oldMillis && 1581 | scheduledFunc.runAtMillis <= nowMillis) { 1582 | funcsToRun.push(scheduledFunc); 1583 | this.scheduledFunctions[timeoutKey] = jasmine.undefined; 1584 | } 1585 | } 1586 | 1587 | if (funcsToRun.length > 0) { 1588 | funcsToRun.sort(function(a, b) { 1589 | return a.runAtMillis - b.runAtMillis; 1590 | }); 1591 | for (var i = 0; i < funcsToRun.length; ++i) { 1592 | try { 1593 | var funcToRun = funcsToRun[i]; 1594 | this.nowMillis = funcToRun.runAtMillis; 1595 | funcToRun.funcToCall(); 1596 | if (funcToRun.recurring) { 1597 | this.scheduleFunction(funcToRun.timeoutKey, 1598 | funcToRun.funcToCall, 1599 | funcToRun.millis, 1600 | true); 1601 | } 1602 | } catch(e) { 1603 | } 1604 | } 1605 | this.runFunctionsWithinRange(oldMillis, nowMillis); 1606 | } 1607 | }; 1608 | 1609 | jasmine.FakeTimer.prototype.scheduleFunction = function(timeoutKey, funcToCall, millis, recurring) { 1610 | this.scheduledFunctions[timeoutKey] = { 1611 | runAtMillis: this.nowMillis + millis, 1612 | funcToCall: funcToCall, 1613 | recurring: recurring, 1614 | timeoutKey: timeoutKey, 1615 | millis: millis 1616 | }; 1617 | }; 1618 | 1619 | /** 1620 | * @namespace 1621 | */ 1622 | jasmine.Clock = { 1623 | defaultFakeTimer: new jasmine.FakeTimer(), 1624 | 1625 | reset: function() { 1626 | jasmine.Clock.assertInstalled(); 1627 | jasmine.Clock.defaultFakeTimer.reset(); 1628 | }, 1629 | 1630 | tick: function(millis) { 1631 | jasmine.Clock.assertInstalled(); 1632 | jasmine.Clock.defaultFakeTimer.tick(millis); 1633 | }, 1634 | 1635 | runFunctionsWithinRange: function(oldMillis, nowMillis) { 1636 | jasmine.Clock.defaultFakeTimer.runFunctionsWithinRange(oldMillis, nowMillis); 1637 | }, 1638 | 1639 | scheduleFunction: function(timeoutKey, funcToCall, millis, recurring) { 1640 | jasmine.Clock.defaultFakeTimer.scheduleFunction(timeoutKey, funcToCall, millis, recurring); 1641 | }, 1642 | 1643 | useMock: function() { 1644 | if (!jasmine.Clock.isInstalled()) { 1645 | var spec = jasmine.getEnv().currentSpec; 1646 | spec.after(jasmine.Clock.uninstallMock); 1647 | 1648 | jasmine.Clock.installMock(); 1649 | } 1650 | }, 1651 | 1652 | installMock: function() { 1653 | jasmine.Clock.installed = jasmine.Clock.defaultFakeTimer; 1654 | }, 1655 | 1656 | uninstallMock: function() { 1657 | jasmine.Clock.assertInstalled(); 1658 | jasmine.Clock.installed = jasmine.Clock.real; 1659 | }, 1660 | 1661 | real: { 1662 | setTimeout: jasmine.getGlobal().setTimeout, 1663 | clearTimeout: jasmine.getGlobal().clearTimeout, 1664 | setInterval: jasmine.getGlobal().setInterval, 1665 | clearInterval: jasmine.getGlobal().clearInterval 1666 | }, 1667 | 1668 | assertInstalled: function() { 1669 | if (!jasmine.Clock.isInstalled()) { 1670 | throw new Error("Mock clock is not installed, use jasmine.Clock.useMock()"); 1671 | } 1672 | }, 1673 | 1674 | isInstalled: function() { 1675 | return jasmine.Clock.installed == jasmine.Clock.defaultFakeTimer; 1676 | }, 1677 | 1678 | installed: null 1679 | }; 1680 | jasmine.Clock.installed = jasmine.Clock.real; 1681 | 1682 | //else for IE support 1683 | jasmine.getGlobal().setTimeout = function(funcToCall, millis) { 1684 | if (jasmine.Clock.installed.setTimeout.apply) { 1685 | return jasmine.Clock.installed.setTimeout.apply(this, arguments); 1686 | } else { 1687 | return jasmine.Clock.installed.setTimeout(funcToCall, millis); 1688 | } 1689 | }; 1690 | 1691 | jasmine.getGlobal().setInterval = function(funcToCall, millis) { 1692 | if (jasmine.Clock.installed.setInterval.apply) { 1693 | return jasmine.Clock.installed.setInterval.apply(this, arguments); 1694 | } else { 1695 | return jasmine.Clock.installed.setInterval(funcToCall, millis); 1696 | } 1697 | }; 1698 | 1699 | jasmine.getGlobal().clearTimeout = function(timeoutKey) { 1700 | if (jasmine.Clock.installed.clearTimeout.apply) { 1701 | return jasmine.Clock.installed.clearTimeout.apply(this, arguments); 1702 | } else { 1703 | return jasmine.Clock.installed.clearTimeout(timeoutKey); 1704 | } 1705 | }; 1706 | 1707 | jasmine.getGlobal().clearInterval = function(timeoutKey) { 1708 | if (jasmine.Clock.installed.clearTimeout.apply) { 1709 | return jasmine.Clock.installed.clearInterval.apply(this, arguments); 1710 | } else { 1711 | return jasmine.Clock.installed.clearInterval(timeoutKey); 1712 | } 1713 | }; 1714 | 1715 | /** 1716 | * @constructor 1717 | */ 1718 | jasmine.MultiReporter = function() { 1719 | this.subReporters_ = []; 1720 | }; 1721 | jasmine.util.inherit(jasmine.MultiReporter, jasmine.Reporter); 1722 | 1723 | jasmine.MultiReporter.prototype.addReporter = function(reporter) { 1724 | this.subReporters_.push(reporter); 1725 | }; 1726 | 1727 | (function() { 1728 | var functionNames = [ 1729 | "reportRunnerStarting", 1730 | "reportRunnerResults", 1731 | "reportSuiteResults", 1732 | "reportSpecStarting", 1733 | "reportSpecResults", 1734 | "log" 1735 | ]; 1736 | for (var i = 0; i < functionNames.length; i++) { 1737 | var functionName = functionNames[i]; 1738 | jasmine.MultiReporter.prototype[functionName] = (function(functionName) { 1739 | return function() { 1740 | for (var j = 0; j < this.subReporters_.length; j++) { 1741 | var subReporter = this.subReporters_[j]; 1742 | if (subReporter[functionName]) { 1743 | subReporter[functionName].apply(subReporter, arguments); 1744 | } 1745 | } 1746 | }; 1747 | })(functionName); 1748 | } 1749 | })(); 1750 | /** 1751 | * Holds results for a set of Jasmine spec. Allows for the results array to hold another jasmine.NestedResults 1752 | * 1753 | * @constructor 1754 | */ 1755 | jasmine.NestedResults = function() { 1756 | /** 1757 | * The total count of results 1758 | */ 1759 | this.totalCount = 0; 1760 | /** 1761 | * Number of passed results 1762 | */ 1763 | this.passedCount = 0; 1764 | /** 1765 | * Number of failed results 1766 | */ 1767 | this.failedCount = 0; 1768 | /** 1769 | * Was this suite/spec skipped? 1770 | */ 1771 | this.skipped = false; 1772 | /** 1773 | * @ignore 1774 | */ 1775 | this.items_ = []; 1776 | }; 1777 | 1778 | /** 1779 | * Roll up the result counts. 1780 | * 1781 | * @param result 1782 | */ 1783 | jasmine.NestedResults.prototype.rollupCounts = function(result) { 1784 | this.totalCount += result.totalCount; 1785 | this.passedCount += result.passedCount; 1786 | this.failedCount += result.failedCount; 1787 | }; 1788 | 1789 | /** 1790 | * Adds a log message. 1791 | * @param values Array of message parts which will be concatenated later. 1792 | */ 1793 | jasmine.NestedResults.prototype.log = function(values) { 1794 | this.items_.push(new jasmine.MessageResult(values)); 1795 | }; 1796 | 1797 | /** 1798 | * Getter for the results: message & results. 1799 | */ 1800 | jasmine.NestedResults.prototype.getItems = function() { 1801 | return this.items_; 1802 | }; 1803 | 1804 | /** 1805 | * Adds a result, tracking counts (total, passed, & failed) 1806 | * @param {jasmine.ExpectationResult|jasmine.NestedResults} result 1807 | */ 1808 | jasmine.NestedResults.prototype.addResult = function(result) { 1809 | if (result.type != 'log') { 1810 | if (result.items_) { 1811 | this.rollupCounts(result); 1812 | } else { 1813 | this.totalCount++; 1814 | if (result.passed()) { 1815 | this.passedCount++; 1816 | } else { 1817 | this.failedCount++; 1818 | } 1819 | } 1820 | } 1821 | this.items_.push(result); 1822 | }; 1823 | 1824 | /** 1825 | * @returns {Boolean} True if everything below passed 1826 | */ 1827 | jasmine.NestedResults.prototype.passed = function() { 1828 | return this.passedCount === this.totalCount; 1829 | }; 1830 | /** 1831 | * Base class for pretty printing for expectation results. 1832 | */ 1833 | jasmine.PrettyPrinter = function() { 1834 | this.ppNestLevel_ = 0; 1835 | }; 1836 | 1837 | /** 1838 | * Formats a value in a nice, human-readable string. 1839 | * 1840 | * @param value 1841 | */ 1842 | jasmine.PrettyPrinter.prototype.format = function(value) { 1843 | if (this.ppNestLevel_ > 40) { 1844 | throw new Error('jasmine.PrettyPrinter: format() nested too deeply!'); 1845 | } 1846 | 1847 | this.ppNestLevel_++; 1848 | try { 1849 | if (value === jasmine.undefined) { 1850 | this.emitScalar('undefined'); 1851 | } else if (value === null) { 1852 | this.emitScalar('null'); 1853 | } else if (value === jasmine.getGlobal()) { 1854 | this.emitScalar(''); 1855 | } else if (value.jasmineToString) { 1856 | this.emitScalar(value.jasmineToString()); 1857 | } else if (typeof value === 'string') { 1858 | this.emitString(value); 1859 | } else if (jasmine.isSpy(value)) { 1860 | this.emitScalar("spy on " + value.identity); 1861 | } else if (value instanceof RegExp) { 1862 | this.emitScalar(value.toString()); 1863 | } else if (typeof value === 'function') { 1864 | this.emitScalar('Function'); 1865 | } else if (typeof value.nodeType === 'number') { 1866 | this.emitScalar('HTMLNode'); 1867 | } else if (value instanceof Date) { 1868 | this.emitScalar('Date(' + value + ')'); 1869 | } else if (value.__Jasmine_been_here_before__) { 1870 | this.emitScalar(''); 1871 | } else if (jasmine.isArray_(value) || typeof value == 'object') { 1872 | value.__Jasmine_been_here_before__ = true; 1873 | if (jasmine.isArray_(value)) { 1874 | this.emitArray(value); 1875 | } else { 1876 | this.emitObject(value); 1877 | } 1878 | delete value.__Jasmine_been_here_before__; 1879 | } else { 1880 | this.emitScalar(value.toString()); 1881 | } 1882 | } finally { 1883 | this.ppNestLevel_--; 1884 | } 1885 | }; 1886 | 1887 | jasmine.PrettyPrinter.prototype.iterateObject = function(obj, fn) { 1888 | for (var property in obj) { 1889 | if (property == '__Jasmine_been_here_before__') continue; 1890 | fn(property, obj.__lookupGetter__ ? (obj.__lookupGetter__(property) !== jasmine.undefined && 1891 | obj.__lookupGetter__(property) !== null) : false); 1892 | } 1893 | }; 1894 | 1895 | jasmine.PrettyPrinter.prototype.emitArray = jasmine.unimplementedMethod_; 1896 | jasmine.PrettyPrinter.prototype.emitObject = jasmine.unimplementedMethod_; 1897 | jasmine.PrettyPrinter.prototype.emitScalar = jasmine.unimplementedMethod_; 1898 | jasmine.PrettyPrinter.prototype.emitString = jasmine.unimplementedMethod_; 1899 | 1900 | jasmine.StringPrettyPrinter = function() { 1901 | jasmine.PrettyPrinter.call(this); 1902 | 1903 | this.string = ''; 1904 | }; 1905 | jasmine.util.inherit(jasmine.StringPrettyPrinter, jasmine.PrettyPrinter); 1906 | 1907 | jasmine.StringPrettyPrinter.prototype.emitScalar = function(value) { 1908 | this.append(value); 1909 | }; 1910 | 1911 | jasmine.StringPrettyPrinter.prototype.emitString = function(value) { 1912 | this.append("'" + value + "'"); 1913 | }; 1914 | 1915 | jasmine.StringPrettyPrinter.prototype.emitArray = function(array) { 1916 | this.append('[ '); 1917 | for (var i = 0; i < array.length; i++) { 1918 | if (i > 0) { 1919 | this.append(', '); 1920 | } 1921 | this.format(array[i]); 1922 | } 1923 | this.append(' ]'); 1924 | }; 1925 | 1926 | jasmine.StringPrettyPrinter.prototype.emitObject = function(obj) { 1927 | var self = this; 1928 | this.append('{ '); 1929 | var first = true; 1930 | 1931 | this.iterateObject(obj, function(property, isGetter) { 1932 | if (first) { 1933 | first = false; 1934 | } else { 1935 | self.append(', '); 1936 | } 1937 | 1938 | self.append(property); 1939 | self.append(' : '); 1940 | if (isGetter) { 1941 | self.append(''); 1942 | } else { 1943 | self.format(obj[property]); 1944 | } 1945 | }); 1946 | 1947 | this.append(' }'); 1948 | }; 1949 | 1950 | jasmine.StringPrettyPrinter.prototype.append = function(value) { 1951 | this.string += value; 1952 | }; 1953 | jasmine.Queue = function(env) { 1954 | this.env = env; 1955 | this.blocks = []; 1956 | this.running = false; 1957 | this.index = 0; 1958 | this.offset = 0; 1959 | this.abort = false; 1960 | }; 1961 | 1962 | jasmine.Queue.prototype.addBefore = function(block) { 1963 | this.blocks.unshift(block); 1964 | }; 1965 | 1966 | jasmine.Queue.prototype.add = function(block) { 1967 | this.blocks.push(block); 1968 | }; 1969 | 1970 | jasmine.Queue.prototype.insertNext = function(block) { 1971 | this.blocks.splice((this.index + this.offset + 1), 0, block); 1972 | this.offset++; 1973 | }; 1974 | 1975 | jasmine.Queue.prototype.start = function(onComplete) { 1976 | this.running = true; 1977 | this.onComplete = onComplete; 1978 | this.next_(); 1979 | }; 1980 | 1981 | jasmine.Queue.prototype.isRunning = function() { 1982 | return this.running; 1983 | }; 1984 | 1985 | jasmine.Queue.LOOP_DONT_RECURSE = true; 1986 | 1987 | jasmine.Queue.prototype.next_ = function() { 1988 | var self = this; 1989 | var goAgain = true; 1990 | 1991 | while (goAgain) { 1992 | goAgain = false; 1993 | 1994 | if (self.index < self.blocks.length && !this.abort) { 1995 | var calledSynchronously = true; 1996 | var completedSynchronously = false; 1997 | 1998 | var onComplete = function () { 1999 | if (jasmine.Queue.LOOP_DONT_RECURSE && calledSynchronously) { 2000 | completedSynchronously = true; 2001 | return; 2002 | } 2003 | 2004 | if (self.blocks[self.index].abort) { 2005 | self.abort = true; 2006 | } 2007 | 2008 | self.offset = 0; 2009 | self.index++; 2010 | 2011 | var now = new Date().getTime(); 2012 | if (self.env.updateInterval && now - self.env.lastUpdate > self.env.updateInterval) { 2013 | self.env.lastUpdate = now; 2014 | self.env.setTimeout(function() { 2015 | self.next_(); 2016 | }, 0); 2017 | } else { 2018 | if (jasmine.Queue.LOOP_DONT_RECURSE && completedSynchronously) { 2019 | goAgain = true; 2020 | } else { 2021 | self.next_(); 2022 | } 2023 | } 2024 | }; 2025 | self.blocks[self.index].execute(onComplete); 2026 | 2027 | calledSynchronously = false; 2028 | if (completedSynchronously) { 2029 | onComplete(); 2030 | } 2031 | 2032 | } else { 2033 | self.running = false; 2034 | if (self.onComplete) { 2035 | self.onComplete(); 2036 | } 2037 | } 2038 | } 2039 | }; 2040 | 2041 | jasmine.Queue.prototype.results = function() { 2042 | var results = new jasmine.NestedResults(); 2043 | for (var i = 0; i < this.blocks.length; i++) { 2044 | if (this.blocks[i].results) { 2045 | results.addResult(this.blocks[i].results()); 2046 | } 2047 | } 2048 | return results; 2049 | }; 2050 | 2051 | 2052 | /** 2053 | * Runner 2054 | * 2055 | * @constructor 2056 | * @param {jasmine.Env} env 2057 | */ 2058 | jasmine.Runner = function(env) { 2059 | var self = this; 2060 | self.env = env; 2061 | self.queue = new jasmine.Queue(env); 2062 | self.before_ = []; 2063 | self.after_ = []; 2064 | self.suites_ = []; 2065 | }; 2066 | 2067 | jasmine.Runner.prototype.execute = function() { 2068 | var self = this; 2069 | if (self.env.reporter.reportRunnerStarting) { 2070 | self.env.reporter.reportRunnerStarting(this); 2071 | } 2072 | self.queue.start(function () { 2073 | self.finishCallback(); 2074 | }); 2075 | }; 2076 | 2077 | jasmine.Runner.prototype.beforeEach = function(beforeEachFunction) { 2078 | beforeEachFunction.typeName = 'beforeEach'; 2079 | this.before_.splice(0,0,beforeEachFunction); 2080 | }; 2081 | 2082 | jasmine.Runner.prototype.afterEach = function(afterEachFunction) { 2083 | afterEachFunction.typeName = 'afterEach'; 2084 | this.after_.splice(0,0,afterEachFunction); 2085 | }; 2086 | 2087 | 2088 | jasmine.Runner.prototype.finishCallback = function() { 2089 | this.env.reporter.reportRunnerResults(this); 2090 | }; 2091 | 2092 | jasmine.Runner.prototype.addSuite = function(suite) { 2093 | this.suites_.push(suite); 2094 | }; 2095 | 2096 | jasmine.Runner.prototype.add = function(block) { 2097 | if (block instanceof jasmine.Suite) { 2098 | this.addSuite(block); 2099 | } 2100 | this.queue.add(block); 2101 | }; 2102 | 2103 | jasmine.Runner.prototype.specs = function () { 2104 | var suites = this.suites(); 2105 | var specs = []; 2106 | for (var i = 0; i < suites.length; i++) { 2107 | specs = specs.concat(suites[i].specs()); 2108 | } 2109 | return specs; 2110 | }; 2111 | 2112 | jasmine.Runner.prototype.suites = function() { 2113 | return this.suites_; 2114 | }; 2115 | 2116 | jasmine.Runner.prototype.topLevelSuites = function() { 2117 | var topLevelSuites = []; 2118 | for (var i = 0; i < this.suites_.length; i++) { 2119 | if (!this.suites_[i].parentSuite) { 2120 | topLevelSuites.push(this.suites_[i]); 2121 | } 2122 | } 2123 | return topLevelSuites; 2124 | }; 2125 | 2126 | jasmine.Runner.prototype.results = function() { 2127 | return this.queue.results(); 2128 | }; 2129 | /** 2130 | * Internal representation of a Jasmine specification, or test. 2131 | * 2132 | * @constructor 2133 | * @param {jasmine.Env} env 2134 | * @param {jasmine.Suite} suite 2135 | * @param {String} description 2136 | */ 2137 | jasmine.Spec = function(env, suite, description) { 2138 | if (!env) { 2139 | throw new Error('jasmine.Env() required'); 2140 | } 2141 | if (!suite) { 2142 | throw new Error('jasmine.Suite() required'); 2143 | } 2144 | var spec = this; 2145 | spec.id = env.nextSpecId ? env.nextSpecId() : null; 2146 | spec.env = env; 2147 | spec.suite = suite; 2148 | spec.description = description; 2149 | spec.queue = new jasmine.Queue(env); 2150 | 2151 | spec.afterCallbacks = []; 2152 | spec.spies_ = []; 2153 | 2154 | spec.results_ = new jasmine.NestedResults(); 2155 | spec.results_.description = description; 2156 | spec.matchersClass = null; 2157 | }; 2158 | 2159 | jasmine.Spec.prototype.getFullName = function() { 2160 | return this.suite.getFullName() + ' ' + this.description + '.'; 2161 | }; 2162 | 2163 | 2164 | jasmine.Spec.prototype.results = function() { 2165 | return this.results_; 2166 | }; 2167 | 2168 | /** 2169 | * All parameters are pretty-printed and concatenated together, then written to the spec's output. 2170 | * 2171 | * Be careful not to leave calls to jasmine.log in production code. 2172 | */ 2173 | jasmine.Spec.prototype.log = function() { 2174 | return this.results_.log(arguments); 2175 | }; 2176 | 2177 | jasmine.Spec.prototype.runs = function (func) { 2178 | var block = new jasmine.Block(this.env, func, this); 2179 | this.addToQueue(block); 2180 | return this; 2181 | }; 2182 | 2183 | jasmine.Spec.prototype.addToQueue = function (block) { 2184 | if (this.queue.isRunning()) { 2185 | this.queue.insertNext(block); 2186 | } else { 2187 | this.queue.add(block); 2188 | } 2189 | }; 2190 | 2191 | /** 2192 | * @param {jasmine.ExpectationResult} result 2193 | */ 2194 | jasmine.Spec.prototype.addMatcherResult = function(result) { 2195 | this.results_.addResult(result); 2196 | }; 2197 | 2198 | jasmine.Spec.prototype.expect = function(actual) { 2199 | var positive = new (this.getMatchersClass_())(this.env, actual, this); 2200 | positive.not = new (this.getMatchersClass_())(this.env, actual, this, true); 2201 | return positive; 2202 | }; 2203 | 2204 | /** 2205 | * Waits a fixed time period before moving to the next block. 2206 | * 2207 | * @deprecated Use waitsFor() instead 2208 | * @param {Number} timeout milliseconds to wait 2209 | */ 2210 | jasmine.Spec.prototype.waits = function(timeout) { 2211 | var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); 2212 | this.addToQueue(waitsFunc); 2213 | return this; 2214 | }; 2215 | 2216 | /** 2217 | * Waits for the latchFunction to return true before proceeding to the next block. 2218 | * 2219 | * @param {Function} latchFunction 2220 | * @param {String} optional_timeoutMessage 2221 | * @param {Number} optional_timeout 2222 | */ 2223 | jasmine.Spec.prototype.waitsFor = function(latchFunction, optional_timeoutMessage, optional_timeout) { 2224 | var latchFunction_ = null; 2225 | var optional_timeoutMessage_ = null; 2226 | var optional_timeout_ = null; 2227 | 2228 | for (var i = 0; i < arguments.length; i++) { 2229 | var arg = arguments[i]; 2230 | switch (typeof arg) { 2231 | case 'function': 2232 | latchFunction_ = arg; 2233 | break; 2234 | case 'string': 2235 | optional_timeoutMessage_ = arg; 2236 | break; 2237 | case 'number': 2238 | optional_timeout_ = arg; 2239 | break; 2240 | } 2241 | } 2242 | 2243 | var waitsForFunc = new jasmine.WaitsForBlock(this.env, optional_timeout_, latchFunction_, optional_timeoutMessage_, this); 2244 | this.addToQueue(waitsForFunc); 2245 | return this; 2246 | }; 2247 | 2248 | jasmine.Spec.prototype.fail = function (e) { 2249 | var expectationResult = new jasmine.ExpectationResult({ 2250 | passed: false, 2251 | message: e ? jasmine.util.formatException(e) : 'Exception', 2252 | trace: { stack: e.stack } 2253 | }); 2254 | this.results_.addResult(expectationResult); 2255 | }; 2256 | 2257 | jasmine.Spec.prototype.getMatchersClass_ = function() { 2258 | return this.matchersClass || this.env.matchersClass; 2259 | }; 2260 | 2261 | jasmine.Spec.prototype.addMatchers = function(matchersPrototype) { 2262 | var parent = this.getMatchersClass_(); 2263 | var newMatchersClass = function() { 2264 | parent.apply(this, arguments); 2265 | }; 2266 | jasmine.util.inherit(newMatchersClass, parent); 2267 | jasmine.Matchers.wrapInto_(matchersPrototype, newMatchersClass); 2268 | this.matchersClass = newMatchersClass; 2269 | }; 2270 | 2271 | jasmine.Spec.prototype.finishCallback = function() { 2272 | this.env.reporter.reportSpecResults(this); 2273 | }; 2274 | 2275 | jasmine.Spec.prototype.finish = function(onComplete) { 2276 | this.removeAllSpies(); 2277 | this.finishCallback(); 2278 | if (onComplete) { 2279 | onComplete(); 2280 | } 2281 | }; 2282 | 2283 | jasmine.Spec.prototype.after = function(doAfter) { 2284 | if (this.queue.isRunning()) { 2285 | this.queue.add(new jasmine.Block(this.env, doAfter, this)); 2286 | } else { 2287 | this.afterCallbacks.unshift(doAfter); 2288 | } 2289 | }; 2290 | 2291 | jasmine.Spec.prototype.execute = function(onComplete) { 2292 | var spec = this; 2293 | if (!spec.env.specFilter(spec)) { 2294 | spec.results_.skipped = true; 2295 | spec.finish(onComplete); 2296 | return; 2297 | } 2298 | 2299 | this.env.reporter.reportSpecStarting(this); 2300 | 2301 | spec.env.currentSpec = spec; 2302 | 2303 | spec.addBeforesAndAftersToQueue(); 2304 | 2305 | spec.queue.start(function () { 2306 | spec.finish(onComplete); 2307 | }); 2308 | }; 2309 | 2310 | jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { 2311 | var runner = this.env.currentRunner(); 2312 | var i; 2313 | 2314 | for (var suite = this.suite; suite; suite = suite.parentSuite) { 2315 | for (i = 0; i < suite.before_.length; i++) { 2316 | this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this)); 2317 | } 2318 | } 2319 | for (i = 0; i < runner.before_.length; i++) { 2320 | this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this)); 2321 | } 2322 | for (i = 0; i < this.afterCallbacks.length; i++) { 2323 | this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); 2324 | } 2325 | for (suite = this.suite; suite; suite = suite.parentSuite) { 2326 | for (i = 0; i < suite.after_.length; i++) { 2327 | this.queue.add(new jasmine.Block(this.env, suite.after_[i], this)); 2328 | } 2329 | } 2330 | for (i = 0; i < runner.after_.length; i++) { 2331 | this.queue.add(new jasmine.Block(this.env, runner.after_[i], this)); 2332 | } 2333 | }; 2334 | 2335 | jasmine.Spec.prototype.explodes = function() { 2336 | throw 'explodes function should not have been called'; 2337 | }; 2338 | 2339 | jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) { 2340 | if (obj == jasmine.undefined) { 2341 | throw "spyOn could not find an object to spy upon for " + methodName + "()"; 2342 | } 2343 | 2344 | if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) { 2345 | throw methodName + '() method does not exist'; 2346 | } 2347 | 2348 | if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) { 2349 | throw new Error(methodName + ' has already been spied upon'); 2350 | } 2351 | 2352 | var spyObj = jasmine.createSpy(methodName); 2353 | 2354 | this.spies_.push(spyObj); 2355 | spyObj.baseObj = obj; 2356 | spyObj.methodName = methodName; 2357 | spyObj.originalValue = obj[methodName]; 2358 | 2359 | obj[methodName] = spyObj; 2360 | 2361 | return spyObj; 2362 | }; 2363 | 2364 | jasmine.Spec.prototype.removeAllSpies = function() { 2365 | for (var i = 0; i < this.spies_.length; i++) { 2366 | var spy = this.spies_[i]; 2367 | spy.baseObj[spy.methodName] = spy.originalValue; 2368 | } 2369 | this.spies_ = []; 2370 | }; 2371 | 2372 | /** 2373 | * Internal representation of a Jasmine suite. 2374 | * 2375 | * @constructor 2376 | * @param {jasmine.Env} env 2377 | * @param {String} description 2378 | * @param {Function} specDefinitions 2379 | * @param {jasmine.Suite} parentSuite 2380 | */ 2381 | jasmine.Suite = function(env, description, specDefinitions, parentSuite) { 2382 | var self = this; 2383 | self.id = env.nextSuiteId ? env.nextSuiteId() : null; 2384 | self.description = description; 2385 | self.queue = new jasmine.Queue(env); 2386 | self.parentSuite = parentSuite; 2387 | self.env = env; 2388 | self.before_ = []; 2389 | self.after_ = []; 2390 | self.children_ = []; 2391 | self.suites_ = []; 2392 | self.specs_ = []; 2393 | }; 2394 | 2395 | jasmine.Suite.prototype.getFullName = function() { 2396 | var fullName = this.description; 2397 | for (var parentSuite = this.parentSuite; parentSuite; parentSuite = parentSuite.parentSuite) { 2398 | fullName = parentSuite.description + ' ' + fullName; 2399 | } 2400 | return fullName; 2401 | }; 2402 | 2403 | jasmine.Suite.prototype.finish = function(onComplete) { 2404 | this.env.reporter.reportSuiteResults(this); 2405 | this.finished = true; 2406 | if (typeof(onComplete) == 'function') { 2407 | onComplete(); 2408 | } 2409 | }; 2410 | 2411 | jasmine.Suite.prototype.beforeEach = function(beforeEachFunction) { 2412 | beforeEachFunction.typeName = 'beforeEach'; 2413 | this.before_.unshift(beforeEachFunction); 2414 | }; 2415 | 2416 | jasmine.Suite.prototype.afterEach = function(afterEachFunction) { 2417 | afterEachFunction.typeName = 'afterEach'; 2418 | this.after_.unshift(afterEachFunction); 2419 | }; 2420 | 2421 | jasmine.Suite.prototype.results = function() { 2422 | return this.queue.results(); 2423 | }; 2424 | 2425 | jasmine.Suite.prototype.add = function(suiteOrSpec) { 2426 | this.children_.push(suiteOrSpec); 2427 | if (suiteOrSpec instanceof jasmine.Suite) { 2428 | this.suites_.push(suiteOrSpec); 2429 | this.env.currentRunner().addSuite(suiteOrSpec); 2430 | } else { 2431 | this.specs_.push(suiteOrSpec); 2432 | } 2433 | this.queue.add(suiteOrSpec); 2434 | }; 2435 | 2436 | jasmine.Suite.prototype.specs = function() { 2437 | return this.specs_; 2438 | }; 2439 | 2440 | jasmine.Suite.prototype.suites = function() { 2441 | return this.suites_; 2442 | }; 2443 | 2444 | jasmine.Suite.prototype.children = function() { 2445 | return this.children_; 2446 | }; 2447 | 2448 | jasmine.Suite.prototype.execute = function(onComplete) { 2449 | var self = this; 2450 | this.queue.start(function () { 2451 | self.finish(onComplete); 2452 | }); 2453 | }; 2454 | jasmine.WaitsBlock = function(env, timeout, spec) { 2455 | this.timeout = timeout; 2456 | jasmine.Block.call(this, env, null, spec); 2457 | }; 2458 | 2459 | jasmine.util.inherit(jasmine.WaitsBlock, jasmine.Block); 2460 | 2461 | jasmine.WaitsBlock.prototype.execute = function (onComplete) { 2462 | if (jasmine.VERBOSE) { 2463 | this.env.reporter.log('>> Jasmine waiting for ' + this.timeout + ' ms...'); 2464 | } 2465 | this.env.setTimeout(function () { 2466 | onComplete(); 2467 | }, this.timeout); 2468 | }; 2469 | /** 2470 | * A block which waits for some condition to become true, with timeout. 2471 | * 2472 | * @constructor 2473 | * @extends jasmine.Block 2474 | * @param {jasmine.Env} env The Jasmine environment. 2475 | * @param {Number} timeout The maximum time in milliseconds to wait for the condition to become true. 2476 | * @param {Function} latchFunction A function which returns true when the desired condition has been met. 2477 | * @param {String} message The message to display if the desired condition hasn't been met within the given time period. 2478 | * @param {jasmine.Spec} spec The Jasmine spec. 2479 | */ 2480 | jasmine.WaitsForBlock = function(env, timeout, latchFunction, message, spec) { 2481 | this.timeout = timeout || env.defaultTimeoutInterval; 2482 | this.latchFunction = latchFunction; 2483 | this.message = message; 2484 | this.totalTimeSpentWaitingForLatch = 0; 2485 | jasmine.Block.call(this, env, null, spec); 2486 | }; 2487 | jasmine.util.inherit(jasmine.WaitsForBlock, jasmine.Block); 2488 | 2489 | jasmine.WaitsForBlock.TIMEOUT_INCREMENT = 10; 2490 | 2491 | jasmine.WaitsForBlock.prototype.execute = function(onComplete) { 2492 | if (jasmine.VERBOSE) { 2493 | this.env.reporter.log('>> Jasmine waiting for ' + (this.message || 'something to happen')); 2494 | } 2495 | var latchFunctionResult; 2496 | try { 2497 | latchFunctionResult = this.latchFunction.apply(this.spec); 2498 | } catch (e) { 2499 | this.spec.fail(e); 2500 | onComplete(); 2501 | return; 2502 | } 2503 | 2504 | if (latchFunctionResult) { 2505 | onComplete(); 2506 | } else if (this.totalTimeSpentWaitingForLatch >= this.timeout) { 2507 | var message = 'timed out after ' + this.timeout + ' msec waiting for ' + (this.message || 'something to happen'); 2508 | this.spec.fail({ 2509 | name: 'timeout', 2510 | message: message 2511 | }); 2512 | 2513 | this.abort = true; 2514 | onComplete(); 2515 | } else { 2516 | this.totalTimeSpentWaitingForLatch += jasmine.WaitsForBlock.TIMEOUT_INCREMENT; 2517 | var self = this; 2518 | this.env.setTimeout(function() { 2519 | self.execute(onComplete); 2520 | }, jasmine.WaitsForBlock.TIMEOUT_INCREMENT); 2521 | } 2522 | }; 2523 | 2524 | jasmine.version_= { 2525 | "major": 1, 2526 | "minor": 2, 2527 | "build": 0, 2528 | "revision": 1333310630, 2529 | "release_candidate": 1 2530 | }; 2531 | -------------------------------------------------------------------------------- /spec/test-runner.js: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Licensed to the Apache Software Foundation (ASF) under one 4 | * or more contributor license agreements. See the NOTICE file 5 | * distributed with this work for additional information 6 | * regarding copyright ownership. The ASF licenses this file 7 | * to you under the Apache License, Version 2.0 (the 8 | * "License"); you may not use this file except in compliance 9 | * with the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, 14 | * software distributed under the License is distributed on an 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | * KIND, either express or implied. See the License for the 17 | * specific language governing permissions and limitations 18 | * under the License. 19 | * 20 | */ 21 | 22 | if (window.sessionStorage != null) { 23 | window.sessionStorage.clear(); 24 | } 25 | 26 | // Timeout is 2 seconds to allow physical devices enough 27 | // time to query the response. This is important for some 28 | // Android devices. 29 | var Tests = function() {}; 30 | Tests.TEST_TIMEOUT = 7500; 31 | 32 | // Creates a spy that will fail if called. 33 | function createDoNotCallSpy(name, opt_extraMessage) { 34 | return jasmine.createSpy().andCallFake(function() { 35 | var errorMessage = name + ' should not have been called.'; 36 | if (arguments.length) { 37 | errorMessage += ' Got args: ' + JSON.stringify(arguments); 38 | } 39 | if (opt_extraMessage) { 40 | errorMessage += '\n' + opt_extraMessage; 41 | } 42 | expect(false).toBe(true, errorMessage); 43 | }); 44 | } 45 | 46 | // Waits for any of the given spys to be called. 47 | // Last param may be a custom timeout duration. 48 | function waitsForAny() { 49 | var spys = [].slice.call(arguments); 50 | var timeout = Tests.TEST_TIMEOUT; 51 | if (typeof spys[spys.length - 1] == 'number') { 52 | timeout = spys.pop(); 53 | } 54 | waitsFor(function() { 55 | for (var i = 0; i < spys.length; ++i) { 56 | if (spys[i].wasCalled) { 57 | return true; 58 | } 59 | } 60 | return false; 61 | }, "Expecting callbacks to be called.", timeout); 62 | } 63 | -------------------------------------------------------------------------------- /src/android/GAPlugin.java: -------------------------------------------------------------------------------- 1 | package com.adobe.plugins; 2 | 3 | import org.apache.cordova.CallbackContext; 4 | import org.apache.cordova.CordovaPlugin; 5 | import org.json.JSONArray; 6 | 7 | import com.google.analytics.tracking.android.GAServiceManager; 8 | import com.google.analytics.tracking.android.GoogleAnalytics; 9 | import com.google.analytics.tracking.android.Tracker; 10 | 11 | public class GAPlugin extends CordovaPlugin { 12 | @Override 13 | public boolean execute(String action, JSONArray args, CallbackContext callback) { 14 | GoogleAnalytics ga = GoogleAnalytics.getInstance(cordova.getActivity()); 15 | Tracker tracker = ga.getDefaultTracker(); 16 | 17 | if (action.equals("initGA")) { 18 | try { 19 | tracker = ga.getTracker(args.getString(0)); 20 | GAServiceManager.getInstance().setDispatchPeriod(args.getInt(1)); 21 | ga.setDefaultTracker(tracker); 22 | callback.success("initGA - id = " + args.getString(0) + "; interval = " + args.getInt(1) + " seconds"); 23 | return true; 24 | } catch (final Exception e) { 25 | callback.error(e.getMessage()); 26 | } 27 | } else if (action.equals("exitGA")) { 28 | try { 29 | GAServiceManager.getInstance().dispatch(); 30 | callback.success("exitGA"); 31 | return true; 32 | } catch (final Exception e) { 33 | callback.error(e.getMessage()); 34 | } 35 | } else if (action.equals("trackEvent")) { 36 | try { 37 | tracker.sendEvent(args.getString(0), args.getString(1), args.getString(2), args.getLong(3)); 38 | callback.success("trackEvent - category = " + args.getString(0) + "; action = " + args.getString(1) + "; label = " + args.getString(2) + "; value = " + args.getInt(3)); 39 | return true; 40 | } catch (final Exception e) { 41 | callback.error(e.getMessage()); 42 | } 43 | } else if (action.equals("trackPage")) { 44 | try { 45 | tracker.sendView(args.getString(0)); 46 | callback.success("trackPage - url = " + args.getString(0)); 47 | return true; 48 | } catch (final Exception e) { 49 | callback.error(e.getMessage()); 50 | } 51 | } else if (action.equals("setVariable")) { 52 | try { 53 | tracker.setCustomDimension(args.getInt(0), args.getString(1)); 54 | callback.success("setVariable passed - index = " + args.getInt(0) + "; value = " + args.getString(1)); 55 | return true; 56 | } catch (final Exception e) { 57 | callback.error(e.getMessage()); 58 | } 59 | } 60 | else if (action.equals("setDimension")) { 61 | try { 62 | tracker.setCustomDimension(args.getInt(0), args.getString(1)); 63 | callback.success("setDimension passed - index = " + args.getInt(0) + "; value = " + args.getString(1)); 64 | return true; 65 | } catch (final Exception e) { 66 | callback.error(e.getMessage()); 67 | } 68 | } 69 | else if (action.equals("setMetric")) { 70 | try { 71 | tracker.setCustomMetric(args.getInt(0), args.getLong(1)); 72 | callback.success("setVariable passed - index = " + args.getInt(2) + "; key = " + args.getString(0) + "; value = " + args.getString(1)); 73 | return true; 74 | } catch (final Exception e) { 75 | callback.error(e.getMessage()); 76 | } 77 | } 78 | return false; 79 | } 80 | } 81 | 82 | -------------------------------------------------------------------------------- /src/android/libGoogleAnalyticsV2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phonegap-build/GAPlugin/3a68d38a98bdf6485f306c0809c90a7ccfe4eebb/src/android/libGoogleAnalyticsV2.jar -------------------------------------------------------------------------------- /src/ios/GAI.h: -------------------------------------------------------------------------------- 1 | /*! 2 | @header GAI.h 3 | @abstract Google Analytics iOS SDK Header 4 | @version 3.0 5 | @copyright Copyright 2013 Google Inc. All rights reserved. 6 | */ 7 | 8 | #import 9 | #import "GAILogger.h" 10 | #import "GAITracker.h" 11 | #import "GAITrackedViewController.h" 12 | 13 | /*! Google Analytics product string. */ 14 | extern NSString *const kGAIProduct; 15 | 16 | /*! Google Analytics version string. */ 17 | extern NSString *const kGAIVersion; 18 | 19 | /*! 20 | NSError objects returned by the Google Analytics SDK may have this error domain 21 | to indicate that the error originated in the Google Analytics SDK. 22 | */ 23 | extern NSString *const kGAIErrorDomain; 24 | 25 | /*! Google Analytics error codes. */ 26 | typedef enum { 27 | // This error code indicates that there was no error. Never used. 28 | kGAINoError = 0, 29 | 30 | // This error code indicates that there was a database-related error. 31 | kGAIDatabaseError, 32 | 33 | // This error code indicates that there was a network-related error. 34 | kGAINetworkError, 35 | } GAIErrorCode; 36 | 37 | /*! 38 | Google Analytics iOS top-level class. Provides facilities to create trackers 39 | and set behaviorial flags. 40 | */ 41 | @interface GAI : NSObject 42 | 43 | /*! 44 | For convenience, this class exposes a default tracker instance. 45 | This is initialized to `nil` and will be set to the first tracker that is 46 | instantiated in trackerWithTrackingId:. It may be overridden as desired. 47 | 48 | The GAITrackedViewController class will, by default, use this tracker instance. 49 | */ 50 | @property(nonatomic, assign) id defaultTracker; 51 | 52 | /*! 53 | The GAILogger to use. 54 | */ 55 | @property(nonatomic, retain) id logger; 56 | 57 | /*! 58 | When this is true, no tracking information will be gathered; tracking calls 59 | will effectively become no-ops. When set to true, all tracking information that 60 | has not yet been submitted. The value of this flag will be persisted 61 | automatically by the SDK. Developers can optionally use this flag to implement 62 | an opt-out setting in the app to allows users to opt out of Google Analytics 63 | tracking. 64 | 65 | This is set to `NO` the first time the Google Analytics SDK is used on a 66 | device, and is persisted thereafter. 67 | */ 68 | @property(nonatomic, assign) BOOL optOut; 69 | 70 | /*! 71 | If this value is positive, tracking information will be automatically 72 | dispatched every dispatchInterval seconds. Otherwise, tracking information must 73 | be sent manually by calling dispatch. 74 | 75 | By default, this is set to `120`, which indicates tracking information should 76 | be dispatched automatically every 120 seconds. 77 | */ 78 | @property(nonatomic, assign) NSTimeInterval dispatchInterval; 79 | 80 | /*! 81 | When set to true, the SDK will record the currently registered uncaught 82 | exception handler, and then register an uncaught exception handler which tracks 83 | the exceptions that occurred using defaultTracker. If defaultTracker is not 84 | `nil`, this function will track the exception on the tracker and attempt to 85 | dispatch any outstanding tracking information for 5 seconds. It will then call 86 | the previously registered exception handler, if any. When set back to false, 87 | the previously registered uncaught exception handler will be restored. 88 | */ 89 | @property(nonatomic, assign) BOOL trackUncaughtExceptions; 90 | 91 | /*! 92 | When this is 'YES', no tracking information will be sent. Defaults to 'NO'. 93 | */ 94 | @property(nonatomic, assign) BOOL dryRun; 95 | 96 | /*! Get the shared instance of the Google Analytics for iOS class. */ 97 | + (GAI *)sharedInstance; 98 | 99 | /*! 100 | Creates or retrieves a GAITracker implementation with the specified name and 101 | tracking ID. If the tracker for the specified name does not already exist, then 102 | it will be created and returned; otherwise, the existing tracker will be 103 | returned. If the existing tracker for the respective name has a different 104 | tracking ID, that tracking ID is not changed by this method. If defaultTracker 105 | is not set, it will be set to the tracker instance returned here. 106 | 107 | @param name The name of this tracker. Must not be `nil` or empty. 108 | 109 | @param trackingID The tracking ID to use for this tracker. It should be of 110 | the form `UA-xxxxx-y`. 111 | 112 | @return A GAITracker associated with the specified name. The tracker 113 | can be used to send tracking data to Google Analytics. The first time this 114 | method is called with a particular name, the tracker for that name will be 115 | returned, and subsequent calls with the same name will return the same 116 | instance. It is not necessary to retain the tracker because the tracker will be 117 | retained internally by the library. 118 | 119 | If an error occurs or the name is not valid, this method will return 120 | `nil`. 121 | */ 122 | - (id)trackerWithName:(NSString *)name 123 | trackingId:(NSString *)trackingId; 124 | 125 | /*! 126 | Creates or retrieves a GAITracker implementation with name equal to 127 | the specified tracking ID. If the tracker for the respective name does not 128 | already exist, it is created, has it's tracking ID set to |trackingId|, 129 | and is returned; otherwise, the existing tracker is returned. If the existing 130 | tracker for the respective name has a different tracking ID, that tracking ID 131 | is not changed by this method. If defaultTracker is not set, it is set to the 132 | tracker instance returned here. 133 | 134 | @param trackingID The tracking ID to use for this tracker. It should be of 135 | the form `UA-xxxxx-y`. The name of the tracker will be the same as trackingID. 136 | 137 | @return A GAITracker associated with the specified trackingID. The tracker 138 | can be used to send tracking data to Google Analytics. The first time this 139 | method is called with a particular trackingID, the tracker for the respective 140 | name will be returned, and subsequent calls with the same trackingID 141 | will return the same instance. It is not necessary to retain the tracker 142 | because the tracker will be retained internally by the library. 143 | 144 | If an error occurs or the trackingId is not valid, this method will return 145 | `nil`. 146 | */ 147 | - (id)trackerWithTrackingId:(NSString *)trackingId; 148 | 149 | /*! 150 | Remove a tracker from the trackers dictionary. If it is the default tracker, 151 | clears the default tracker as well. 152 | 153 | @param name The name of the tracker. 154 | */ 155 | - (void)removeTrackerByName:(NSString *)name; 156 | 157 | /*! 158 | Dispatches any pending tracking information. 159 | 160 | It would be wise to call this when application is exiting to initiate the 161 | submission of any unsubmitted tracking information. Note that this does not 162 | have any effect on dispatchInterval, and can be used in conjuntion with 163 | periodic dispatch. */ 164 | - (void)dispatch; 165 | 166 | @end 167 | -------------------------------------------------------------------------------- /src/ios/GAIDictionaryBuilder.h: -------------------------------------------------------------------------------- 1 | /*! 2 | @header GAIDictionaryBuilder.h 3 | @abstract Google Analytics iOS SDK Hit Format Header 4 | @copyright Copyright 2013 Google Inc. All rights reserved. 5 | */ 6 | 7 | #import 8 | 9 | /*! 10 | * Helper class to build a dictionary of hit parameters and values. 11 | *
12 | * Examples: 13 | * 14 | * id t = // get a tracker. 15 | * [t send:[[[GAIDictionaryBuilder createEventWithCategory:@"EventCategory" 16 | * action:@"EventAction" 17 | * label:nil 18 | * value:nil] 19 | * set:@"dimension1" forKey:[GAIFields customDimensionForIndex:1]] build]]; 20 | * 21 | * This will send an event hit type with the specified parameters 22 | * and a custom dimension parameter. 23 | *
24 | * If you want to send a parameter with all hits, set it on GAITracker directly. 25 | * 26 | * [t set:kGAIScreenName value:@"Home"]; 27 | * [t send:[[GAIDictionaryBuilder createSocialWithNetwork:@"Google+" 28 | * action:@"PlusOne" 29 | * target:@"SOME_URL"] build]]; 30 | * [t send:[[GAIDictionaryBuilder createSocialWithNetwork:@"Google+" 31 | * action:@"Share" 32 | * target:@"SOME_POST"] build]]; 33 | * [t send:[[GAIDictionaryBuilder createSocialWithNetwork:@"Google+" 34 | * action:@"HangOut" 35 | * target:@"SOME_CIRCLE"] 36 | * build]]; 37 | * 38 | * You can override a value set on the tracker by adding it to the dictionary. 39 | * 40 | * [t set:kGAIScreenName value:@"Home"]; 41 | * [t send:...]; 42 | * [t send[[[GAIDictionaryBuilder createEventWithCategory:@"click" 43 | * action:@"popup" 44 | * label:nil 45 | * value:nil] 46 | * set:@"popup title" forKey:kGAIScreenName] build]]; 47 | * 48 | * The values set via [GAIDictionaryBuilder set] or 49 | * [GAIDictionaryBuilder setAll] will override any existing values in the 50 | * GAIDictionaryBuilder object (i.e. initialized by 51 | * [GAIDictionaryBuilder createXYZ]). e.g. 52 | * 53 | * GAIDictionaryBuilder *m = 54 | * GAIDictionaryBuilder createTimingWithCategory:@"category" 55 | * interval:@0 56 | * name:@"name" 57 | * label:nil]; 58 | * [t send:[m.set:@"10" forKey:kGAITimingVar] build]; 59 | * [t send:[m.set:@"20" forKey:kGAITimingVar] build]; 60 | * 61 | */ 62 | @interface GAIDictionaryBuilder : NSObject 63 | 64 | - (GAIDictionaryBuilder *)set:(NSString *)value 65 | forKey:(NSString *)key; 66 | 67 | /*! 68 | * Copies all the name-value pairs from params into this object, ignoring any 69 | * keys that are not NSString and any values that are neither NSString or 70 | * NSNull. 71 | */ 72 | - (GAIDictionaryBuilder *)setAll:(NSDictionary *)params; 73 | 74 | /*! 75 | * Returns the value for the input parameter paramName, or nil if paramName 76 | * is not present. 77 | */ 78 | - (NSString *)get:(NSString *)paramName; 79 | 80 | /*! 81 | * Return an NSMutableDictionary object with all the parameters set in this 82 | */ 83 | - (NSMutableDictionary *)build; 84 | 85 | /*! 86 | * Parses and translates utm campaign parameters to analytics campaign param 87 | * and returns them as a map. 88 | * 89 | * @param params url containing utm campaign parameters. 90 | * 91 | * Valid campaign parameters are: 92 | *
    93 | *
  • utm_id
  • 94 | *
  • utm_campaign
  • 95 | *
  • utm_content
  • 96 | *
  • utm_medium
  • 97 | *
  • utm_source
  • 98 | *
  • utm_term
  • 99 | *
  • dclid
  • 100 | *
  • gclid
  • 101 | *
  • gmob_t
  • 102 | *
103 | *

104 | * Example: 105 | * http://my.site.com/index.html?utm_campaign=wow&utm_source=source 106 | * utm_campaign=wow&utm_source=source. 107 | *

108 | * For more information on auto-tagging, see 109 | * http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55590 110 | *

111 | * For more information on manual tagging, see 112 | * http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55518 113 | */ 114 | - (GAIDictionaryBuilder *)setCampaignParametersFromUrl:(NSString *)urlString; 115 | 116 | /*! 117 | Returns a GAIDictionaryBuilder object with parameters specific to an appview 118 | hit. 119 | 120 | Note that using this method will not set the screen name for followon hits. To 121 | do that you need to call set:kGAIDescription value: on the 122 | GAITracker instance. 123 | */ 124 | + (GAIDictionaryBuilder *)createAppView; 125 | 126 | /*! 127 | Returns a GAIDictionaryBuilder object with parameters specific to an event hit. 128 | */ 129 | + (GAIDictionaryBuilder *)createEventWithCategory:(NSString *)category 130 | action:(NSString *)action 131 | label:(NSString *)label 132 | value:(NSNumber *)value; 133 | 134 | /*! 135 | Returns a GAIDictionaryBuilder object with parameters specific to an exception 136 | hit. 137 | */ 138 | + (GAIDictionaryBuilder *)createExceptionWithDescription:(NSString *)description 139 | withFatal:(NSNumber *)fatal; 140 | 141 | /*! 142 | Returns a GAIDictionaryBuilder object with parameters specific to an item hit. 143 | */ 144 | + (GAIDictionaryBuilder *)createItemWithTransactionId:(NSString *)transactionId 145 | name:(NSString *)name 146 | sku:(NSString *)sku 147 | category:(NSString *)category 148 | price:(NSNumber *)price 149 | quantity:(NSNumber *)quantity 150 | currencyCode:(NSString *)currencyCode; 151 | 152 | /*! 153 | Returns a GAIDictionaryBuilder object with parameters specific to a social hit. 154 | */ 155 | + (GAIDictionaryBuilder *)createSocialWithNetwork:(NSString *)network 156 | action:(NSString *)action 157 | target:(NSString *)target; 158 | 159 | /*! 160 | Returns a GAIDictionaryBuilder object with parameters specific to a timing hit. 161 | */ 162 | + (GAIDictionaryBuilder *)createTimingWithCategory:(NSString *)category 163 | interval:(NSNumber *)intervalMillis 164 | name:(NSString *)name 165 | label:(NSString *)label; 166 | 167 | /*! 168 | Returns a GAIDictionaryBuilder object with parameters specific to a transaction 169 | hit. 170 | */ 171 | + (GAIDictionaryBuilder *)createTransactionWithId:(NSString *)transactionId 172 | affiliation:(NSString *)affiliation 173 | revenue:(NSNumber *)revenue 174 | tax:(NSNumber *)tax 175 | shipping:(NSNumber *)shipping 176 | currencyCode:(NSString *)currencyCode; 177 | 178 | @end 179 | -------------------------------------------------------------------------------- /src/ios/GAIFields.h: -------------------------------------------------------------------------------- 1 | /*! 2 | @header GAIFields.h 3 | @abstract Google Analytics iOS SDK Hit Format Header 4 | @copyright Copyright 2013 Google Inc. All rights reserved. 5 | */ 6 | 7 | #import 8 | 9 | /*! 10 | These fields can be used for the wire format parameter names required by 11 | the |GAITracker| get, set and send methods as well as the set methods in the 12 | |GAIDictionaryBuilder| class. 13 | */ 14 | extern NSString *const kGAIUseSecure; 15 | 16 | extern NSString *const kGAIHitType; 17 | extern NSString *const kGAITrackingId; 18 | extern NSString *const kGAIClientId; 19 | extern NSString *const kGAIAnonymizeIp; 20 | extern NSString *const kGAISessionControl; 21 | extern NSString *const kGAIScreenResolution; 22 | extern NSString *const kGAIViewportSize; 23 | extern NSString *const kGAIEncoding; 24 | extern NSString *const kGAIScreenColors; 25 | extern NSString *const kGAILanguage; 26 | extern NSString *const kGAIJavaEnabled; 27 | extern NSString *const kGAIFlashVersion; 28 | extern NSString *const kGAINonInteraction; 29 | extern NSString *const kGAIReferrer; 30 | extern NSString *const kGAILocation; 31 | extern NSString *const kGAIHostname; 32 | extern NSString *const kGAIPage; 33 | extern NSString *const kGAIDescription; // synonym for kGAIScreenName 34 | extern NSString *const kGAIScreenName; // synonym for kGAIDescription 35 | extern NSString *const kGAITitle; 36 | extern NSString *const kGAIAppName; 37 | extern NSString *const kGAIAppVersion; 38 | extern NSString *const kGAIAppId; 39 | extern NSString *const kGAIAppInstallerId; 40 | 41 | extern NSString *const kGAIEventCategory; 42 | extern NSString *const kGAIEventAction; 43 | extern NSString *const kGAIEventLabel; 44 | extern NSString *const kGAIEventValue; 45 | 46 | extern NSString *const kGAISocialNetwork; 47 | extern NSString *const kGAISocialAction; 48 | extern NSString *const kGAISocialTarget; 49 | 50 | extern NSString *const kGAITransactionId; 51 | extern NSString *const kGAITransactionAffiliation; 52 | extern NSString *const kGAITransactionRevenue; 53 | extern NSString *const kGAITransactionShipping; 54 | extern NSString *const kGAITransactionTax; 55 | extern NSString *const kGAICurrencyCode; 56 | 57 | extern NSString *const kGAIItemPrice; 58 | extern NSString *const kGAIItemQuantity; 59 | extern NSString *const kGAIItemSku; 60 | extern NSString *const kGAIItemName; 61 | extern NSString *const kGAIItemCategory; 62 | 63 | extern NSString *const kGAICampaignSource; 64 | extern NSString *const kGAICampaignMedium; 65 | extern NSString *const kGAICampaignName; 66 | extern NSString *const kGAICampaignKeyword; 67 | extern NSString *const kGAICampaignContent; 68 | extern NSString *const kGAICampaignId; 69 | 70 | extern NSString *const kGAITimingCategory; 71 | extern NSString *const kGAITimingVar; 72 | extern NSString *const kGAITimingValue; 73 | extern NSString *const kGAITimingLabel; 74 | 75 | extern NSString *const kGAIExDescription; 76 | extern NSString *const kGAIExFatal; 77 | 78 | extern NSString *const kGAISampleRate; 79 | 80 | extern NSString *const kGAIIdfa; 81 | extern NSString *const kGAIAdTargetingEnabled; 82 | 83 | // hit types 84 | extern NSString *const kGAIAppView; 85 | extern NSString *const kGAIEvent; 86 | extern NSString *const kGAISocial; 87 | extern NSString *const kGAITransaction; 88 | extern NSString *const kGAIItem; 89 | extern NSString *const kGAIException; 90 | extern NSString *const kGAITiming; 91 | 92 | /*! 93 | This class provides several fields and methods useful as wire format parameter 94 | names. The methods are used for wire format parameter names that are indexed. 95 | */ 96 | 97 | @interface GAIFields : NSObject 98 | 99 | /*! 100 | Generates the correct parameter name for a content group with an index. 101 | 102 | @param index the index of the content group. 103 | 104 | @return an NSString representing the content group parameter for the index. 105 | */ 106 | + (NSString *)contentGroupForIndex:(NSUInteger)index; 107 | 108 | /*! 109 | Generates the correct parameter name for a custon dimension with an index. 110 | 111 | @param index the index of the custom dimension. 112 | 113 | @return an NSString representing the custom dimension parameter for the index. 114 | */ 115 | + (NSString *)customDimensionForIndex:(NSUInteger)index; 116 | 117 | /*! 118 | Generates the correct parameter name for a custom metric with an index. 119 | 120 | @param index the index of the custom metric. 121 | 122 | @return an NSString representing the custom metric parameter for the index. 123 | */ 124 | + (NSString *)customMetricForIndex:(NSUInteger)index; 125 | 126 | @end 127 | -------------------------------------------------------------------------------- /src/ios/GAILogger.h: -------------------------------------------------------------------------------- 1 | /*! 2 | @header GAILogger.h 3 | @abstract Google Analytics iOS SDK Source 4 | @copyright Copyright 2011 Google Inc. All rights reserved. 5 | */ 6 | 7 | #import 8 | 9 | typedef NS_ENUM(NSUInteger, GAILogLevel) { 10 | kGAILogLevelNone = 0, 11 | kGAILogLevelError = 1, 12 | kGAILogLevelWarning = 2, 13 | kGAILogLevelInfo = 3, 14 | kGAILogLevelVerbose = 4 15 | }; 16 | 17 | /*! 18 | Protocol to be used for logging debug and informational messages from the SDK. 19 | Implementations of this protocol can be provided to the |GAI| class, 20 | to be used as the logger by the SDK. See the |logger| property in GAI.h. 21 | */ 22 | @protocol GAILogger 23 | @required 24 | 25 | /*! 26 | Only messages of |logLevel| and below are logged. 27 | */ 28 | @property (nonatomic, assign) GAILogLevel logLevel; 29 | 30 | /*! 31 | Logs message with log level |kGAILogLevelVerbose|. 32 | */ 33 | - (void)verbose:(NSString *)message; 34 | 35 | /*! 36 | Logs message with log level |kGAILogLevelInfo|. 37 | */ 38 | - (void)info:(NSString *)message; 39 | 40 | /*! 41 | Logs message with log level |kGAILogLevelWarning|. 42 | */ 43 | - (void)warning:(NSString *)message; 44 | 45 | /*! 46 | Logs message with log level |kGAILogLevelError|. 47 | */ 48 | - (void)error:(NSString *)message; 49 | @end 50 | -------------------------------------------------------------------------------- /src/ios/GAITrackedViewController.h: -------------------------------------------------------------------------------- 1 | /*! 2 | @header GAITrackedViewController.h 3 | @abstract Google Analytics for iOS Tracked View Controller Header 4 | @version 2.0 5 | @copyright Copyright 2012 Google Inc. All rights reserved. 6 | */ 7 | 8 | #import 9 | #import 10 | 11 | @protocol GAITracker; 12 | 13 | /*! 14 | Extends UIViewController to generate Google Analytics appview calls 15 | whenever the view appears; this is done by overriding the `viewDidAppear:` 16 | method. The screen name must be set for any tracking calls to be made. 17 | 18 | By default, this will use [GAI defaultTracker] for tracking calls, but one can 19 | override this by setting the tracker property. 20 | */ 21 | @interface GAITrackedViewController : UIViewController 22 | 23 | /*! 24 | The tracker on which view tracking calls are be made, or `nil`, in which case 25 | [GAI defaultTracker] will be used. 26 | */ 27 | @property(nonatomic, assign) id tracker; 28 | /*! 29 | The screen name, for purposes of Google Analytics tracking. If this is `nil`, 30 | no tracking calls will be made. 31 | */ 32 | @property(nonatomic, copy) NSString *screenName; 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /src/ios/GAITracker.h: -------------------------------------------------------------------------------- 1 | /*! 2 | @header GAITracker.h 3 | @abstract Google Analytics iOS SDK Tracker Header 4 | @version 3.0 5 | @copyright Copyright 2013 Google Inc. All rights reserved. 6 | */ 7 | 8 | #import 9 | 10 | /*! 11 | Google Analytics tracking interface. Obtain instances of this interface from 12 | [GAI trackerWithTrackingId:] to track screens, events, transactions, timing, 13 | and exceptions. The implementation of this interface is thread-safe, and no 14 | calls are expected to block or take a long time. All network and disk activity 15 | will take place in the background. 16 | */ 17 | @protocol GAITracker 18 | 19 | /*! 20 | Name of this tracker. 21 | */ 22 | @property(nonatomic, readonly) NSString *name; 23 | 24 | /*! 25 | Set a tracking parameter. 26 | 27 | @param parameterName The parameter name. 28 | 29 | @param value The value to set for the parameter. If this is nil, the 30 | value for the parameter will be cleared. 31 | */ 32 | - (void)set:(NSString *)parameterName 33 | value:(NSString *)value; 34 | 35 | /*! 36 | Get a tracking parameter. 37 | 38 | @param parameterName The parameter name. 39 | 40 | @returns The parameter value, or nil if no value for the given parameter is 41 | set. 42 | */ 43 | - (NSString *)get:(NSString *)parameterName; 44 | 45 | /*! 46 | Queue tracking information with the given parameter values. 47 | 48 | @param parameters A map from parameter names to parameter values which will be 49 | set just for this piece of tracking information, or nil for none. 50 | */ 51 | - (void)send:(NSDictionary *)parameters; 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /src/ios/GAPlugin.h: -------------------------------------------------------------------------------- 1 | // 2 | // GAPlugin.h 3 | // GoSocial 4 | // 5 | // Created by Bob Easterday on 10/9/12. 6 | // Copyright (c) 2012 Adobe Systems, Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "GAI.h" 11 | 12 | @interface GAPlugin : CDVPlugin 13 | { 14 | BOOL inited; 15 | NSMutableDictionary* _customDimensions; 16 | } 17 | 18 | - (void) initGA:(CDVInvokedUrlCommand*)command; 19 | - (void) exitGA:(CDVInvokedUrlCommand*)command; 20 | - (void) trackEvent:(CDVInvokedUrlCommand*)command; 21 | - (void) trackPage:(CDVInvokedUrlCommand*)command; 22 | - (void) setVariable:(CDVInvokedUrlCommand*)command; 23 | 24 | - (void) successWithMessage:(NSString*)message toID:(NSString*)callbackID; 25 | - (void) failWithMessage:(NSString*)message toID:(NSString*)callbackID withError:(NSError*) error; 26 | 27 | @end -------------------------------------------------------------------------------- /src/ios/GAPlugin.m: -------------------------------------------------------------------------------- 1 | #import "GAPlugin.h" 2 | #import "AppDelegate.h" 3 | #import "GAIDictionaryBuilder.h" 4 | #import "GAIFields.h" 5 | 6 | @implementation GAPlugin 7 | 8 | - (void) initGA:(CDVInvokedUrlCommand*)command 9 | { 10 | NSString* callbackId = command.callbackId; 11 | NSString* accountID = [command.arguments objectAtIndex:0]; 12 | NSInteger dispatchPeriod = [[command.arguments objectAtIndex:1] intValue]; 13 | 14 | [GAI sharedInstance].trackUncaughtExceptions = YES; 15 | // Optional: set Google Analytics dispatch interval to e.g. 20 seconds. 16 | [GAI sharedInstance].dispatchInterval = dispatchPeriod; 17 | // Optional: set debug to YES for extra debugging information. 18 | //[GAI sharedInstance].debug = YES; 19 | // Create tracker instance. 20 | [[GAI sharedInstance] trackerWithTrackingId:accountID]; 21 | inited = YES; 22 | 23 | [self successWithMessage:[NSString stringWithFormat:@"initGA: accountID = %@; Interval = %ld seconds",accountID, (long)dispatchPeriod] toID:callbackId]; 24 | } 25 | 26 | - (void)addCustomDimensionsToTracker: (id)tracker 27 | { 28 | if (_customDimensions) { 29 | for (NSString* key in _customDimensions) { 30 | NSString* value = [_customDimensions objectForKey:key]; 31 | 32 | /* NSLog(@"Setting tracker dimension slot %@: <%@>", key, value); */ 33 | [tracker set:[GAIFields customDimensionForIndex:[key intValue]] 34 | value:value]; 35 | } 36 | } 37 | } 38 | 39 | - (void) setVariable:(CDVInvokedUrlCommand *)command 40 | { 41 | CDVPluginResult* pluginResult = nil; 42 | NSString* key = [command.arguments objectAtIndex:0]; 43 | NSString* value = [command.arguments objectAtIndex:1]; 44 | 45 | if ( ! _customDimensions) { 46 | _customDimensions = [[NSMutableDictionary alloc] init]; 47 | } 48 | 49 | _customDimensions[key] = value; 50 | [self addCustomDimensionsToTracker:[[GAI sharedInstance] defaultTracker]]; 51 | 52 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; 53 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 54 | } 55 | 56 | - (void) exitGA:(CDVInvokedUrlCommand*)command 57 | { 58 | if (inited) { 59 | [[GAI sharedInstance] dispatch]; 60 | } 61 | 62 | [self successWithMessage:@"exitGA" toID:command.callbackId]; 63 | } 64 | 65 | - (void) trackEvent:(CDVInvokedUrlCommand*)command 66 | { 67 | @try { 68 | NSString* category = [command.arguments objectAtIndex:0]; 69 | NSString* eventAction = [command.arguments objectAtIndex:1]; 70 | NSString* eventLabel = [command.arguments objectAtIndex:2]; 71 | id eventValueObject = [command.arguments objectAtIndex:3]; 72 | NSNumber* eventValue = nil; 73 | 74 | if (eventValueObject != [NSNull null]) { 75 | eventValue = [NSNumber numberWithInteger:[eventValueObject integerValue]]; 76 | } 77 | 78 | if (inited) { 79 | id tracker = [[GAI sharedInstance] defaultTracker]; 80 | 81 | [self addCustomDimensionsToTracker:tracker]; 82 | 83 | [tracker send:[[GAIDictionaryBuilder 84 | createEventWithCategory: category //required 85 | action: eventAction //required 86 | label: eventLabel 87 | value: eventValueObject] build]]; 88 | 89 | [self successWithMessage:[NSString stringWithFormat:@"trackEvent: category = %@; action = %@; label = %@; value = %d", category, eventAction, eventLabel, [eventValue intValue]] toID:command.callbackId]; 90 | 91 | } else { 92 | [self failWithMessage:@"trackEvent failed - not initialized" toID:command.callbackId withError:nil]; 93 | } 94 | 95 | } @catch (NSException* exception) { 96 | [self failWithMessage:[NSString stringWithFormat:@"trackEvent failed - exception name: %@, reason: %@", exception.name, exception.reason] toID:command.callbackId withError:nil]; 97 | } 98 | } 99 | 100 | - (void) trackPage:(CDVInvokedUrlCommand*)command 101 | { 102 | NSString* pageURL = [command.arguments objectAtIndex:0]; 103 | 104 | if (inited) { 105 | id tracker = [[GAI sharedInstance] defaultTracker]; 106 | 107 | [tracker set:kGAIScreenName value:pageURL]; 108 | [tracker send:[[GAIDictionaryBuilder createAppView] build]]; 109 | 110 | [self successWithMessage:[NSString stringWithFormat:@"trackPage: url = %@", pageURL] toID:command.callbackId]; 111 | } else { 112 | [self failWithMessage:@"trackPage failed - not initialized" toID:command.callbackId withError:nil]; 113 | } 114 | } 115 | 116 | - (void) successWithMessage:(NSString *)message toID:(NSString *)callbackID 117 | { 118 | CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; 119 | 120 | [self.commandDelegate sendPluginResult:commandResult callbackId:callbackID]; 121 | } 122 | 123 | - (void) failWithMessage:(NSString *)message toID:(NSString *)callbackID withError:(NSError *)error 124 | { 125 | NSString *errorMessage = (error) ? [NSString stringWithFormat:@"%@ - %@", message, [error localizedDescription]] : message; 126 | CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage]; 127 | 128 | [self.commandDelegate sendPluginResult:commandResult callbackId:callbackID]; 129 | } 130 | 131 | - (void)dealloc 132 | { 133 | [[GAI sharedInstance] dispatch]; 134 | } 135 | 136 | @end -------------------------------------------------------------------------------- /src/ios/libGoogleAnalyticsServices.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phonegap-build/GAPlugin/3a68d38a98bdf6485f306c0809c90a7ccfe4eebb/src/ios/libGoogleAnalyticsServices.a -------------------------------------------------------------------------------- /www/GAPlugin.js: -------------------------------------------------------------------------------- 1 | (function(){ 2 | var cordovaRef = window.PhoneGap || window.cordova || window.Cordova; 3 | 4 | function GAPlugin() { } 5 | 6 | // initialize google analytics with an account ID and the min number of seconds between posting 7 | // 8 | // id = the GA account ID of the form 'UA-00000000-0' 9 | // period = the minimum interval for transmitting tracking events if any exist in the queue 10 | GAPlugin.prototype.init = function(success, fail, id, period) { 11 | return cordovaRef.exec(success, fail, 'GAPlugin', 'initGA', [id, period]); 12 | }; 13 | 14 | // log an event 15 | // 16 | // category = The event category. This parameter is required to be non-empty. 17 | // eventAction = The event action. This parameter is required to be non-empty. 18 | // eventLabel = The event label. This parameter may be a blank string to indicate no label. 19 | // eventValue = The event value. This parameter may be -1 to indicate no value. 20 | GAPlugin.prototype.trackEvent = function(success, fail, category, eventAction, eventLabel, eventValue) { 21 | return cordovaRef.exec(success, fail, 'GAPlugin', 'trackEvent', [category, eventAction, eventLabel, eventValue]); 22 | }; 23 | 24 | 25 | // log a page view 26 | // 27 | // pageURL = the URL of the page view 28 | GAPlugin.prototype.trackPage = function(success, fail, pageURL) { 29 | return cordovaRef.exec(success, fail, 'GAPlugin', 'trackPage', [pageURL]); 30 | }; 31 | 32 | // Set a custom variable. The variable set is included with 33 | // the next event only. If there is an existing custom variable at the specified 34 | // index, it will be overwritten by this one. 35 | // 36 | // value = the value of the variable you are logging 37 | // index = the numerical index of the dimension to which this variable will be assigned (1 - 20) 38 | // Standard accounts support up to 20 custom dimensions. 39 | GAPlugin.prototype.setVariable = function(success, fail, index, value) { 40 | return cordovaRef.exec(success, fail, 'GAPlugin', 'setVariable', [index, value]); 41 | }; 42 | 43 | GAPlugin.prototype.exit = function(success, fail) { 44 | return cordovaRef.exec(success, fail, 'GAPlugin', 'exitGA', []); 45 | }; 46 | 47 | if (cordovaRef && cordovaRef.addConstructor) { 48 | cordovaRef.addConstructor(init); 49 | } 50 | else { 51 | init(); 52 | } 53 | 54 | function init () { 55 | if(!window.plugins) { 56 | window.plugins = {}; 57 | } 58 | if(!window.plugins.gaPlugin) { 59 | window.plugins.gaPlugin = new GAPlugin(); 60 | } 61 | } 62 | 63 | if (typeof module != 'undefined' && module.exports) { 64 | module.exports = new GAPlugin(); 65 | } 66 | })(); /* End of Temporary Scope. */ 67 | --------------------------------------------------------------------------------