Debian Codegraph @

closure-library/closure/goog/testing/mockuseragent.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview MockUserAgent overrides goog.userAgent.getUserAgentString()
17 * depending on a specified configuration.
18 *
19 */
20
21 goog.setTestOnly('goog.testing.MockUserAgent');
22 goog.provide('goog.testing.MockUserAgent');
23
24 goog.require('goog.Disposable');
25 goog.require('goog.labs.userAgent.util');
26 goog.require('goog.testing.PropertyReplacer');
27 goog.require('goog.userAgent');
28
29
30
31 /**
32 * Class for unit testing code that uses goog.userAgent.
33 *
34 * @extends {goog.Disposable}
35 * @constructor
36 * @final
37 */
38 goog.testing.MockUserAgent = function() {
39 goog.Disposable.call(this);
40
41 /**
42 * Property replacer used to mock out User-Agent functions.
43 * @type {!goog.testing.PropertyReplacer}
44 * @private
45 */
46 this.propertyReplacer_ = new goog.testing.PropertyReplacer();
47
48 /**
49 * The userAgent string used by goog.userAgent.
50 * @type {?string}
51 * @private
52 */
53 this.userAgent_ = goog.userAgent.getUserAgentString();
54
55 /**
56 * The navigator object used by goog.userAgent
57 * @type {Object}
58 * @private
59 */
60 this.navigator_ = goog.userAgent.getNavigator();
61
62 /**
63 * The documentMode number used by goog.userAgent
64 * @type {number|undefined}
65 * @private
66 */
67 this.documentMode_ = goog.userAgent.DOCUMENT_MODE;
68 };
69 goog.inherits(goog.testing.MockUserAgent, goog.Disposable);
70
71
72 /**
73 * Whether this MockUserAgent has been installed.
74 * @type {boolean}
75 * @private
76 */
77 goog.testing.MockUserAgent.prototype.installed_;
78
79
80 /**
81 * Installs this MockUserAgent.
82 */
83 goog.testing.MockUserAgent.prototype.install = function() {
84 if (!this.installed_) {
85 // Stub out user agent functions.
86 this.propertyReplacer_.replace(
87 goog.userAgent, 'getUserAgentString',
88 goog.bind(this.getUserAgentString, this));
89
90 this.propertyReplacer_.replace(
91 goog.labs.userAgent.util, 'getUserAgent',
92 goog.bind(this.getUserAgentString, this));
93
94 // Stub out navigator functions.
95 this.propertyReplacer_.replace(
96 goog.userAgent, 'getNavigator', goog.bind(this.getNavigator, this));
97
98 // Stub out documentMode functions.
99 this.propertyReplacer_.replace(
100 goog.userAgent, 'getDocumentMode_',
101 goog.bind(this.getDocumentMode, this));
102
103 this.propertyReplacer_.replace(
104 goog.userAgent, 'DOCUMENT_MODE', this.getDocumentMode());
105
106 this.installed_ = true;
107 }
108 };
109
110
111 /**
112 * @return {?string} The userAgent set in this class.
113 */
114 goog.testing.MockUserAgent.prototype.getUserAgentString = function() {
115 return this.userAgent_;
116 };
117
118
119 /**
120 * @param {string} userAgent The desired userAgent string to use.
121 */
122 goog.testing.MockUserAgent.prototype.setUserAgentString = function(userAgent) {
123 this.userAgent_ = userAgent;
124 };
125
126
127 /**
128 * @return {Object} The Navigator set in this class.
129 */
130 goog.testing.MockUserAgent.prototype.getNavigator = function() {
131 return this.navigator_;
132 };
133
134
135 /**
136 * @param {Object} navigator The desired Navigator object to use.
137 */
138 goog.testing.MockUserAgent.prototype.setNavigator = function(navigator) {
139 this.navigator_ = navigator;
140 };
141
142 /**
143 * @return {number|undefined} The documentMode set in this class.
144 */
145 goog.testing.MockUserAgent.prototype.getDocumentMode = function() {
146 return this.documentMode_;
147 };
148
149 /**
150 * @param {number} documentMode The desired documentMode to use.
151 */
152 goog.testing.MockUserAgent.prototype.setDocumentMode = function(documentMode) {
153 this.documentMode_ = documentMode;
154 this.propertyReplacer_.set(goog.userAgent, 'DOCUMENT_MODE', documentMode);
155 };
156
157 /**
158 * Uninstalls the MockUserAgent.
159 */
160 goog.testing.MockUserAgent.prototype.uninstall = function() {
161 if (this.installed_) {
162 this.propertyReplacer_.reset();
163 this.installed_ = false;
164 }
165
166 };
167
168
169 /** @override */
170 goog.testing.MockUserAgent.prototype.disposeInternal = function() {
171 this.uninstall();
172 delete this.propertyReplacer_;
173 delete this.navigator_;
174 delete this.documentMode_;
175 goog.testing.MockUserAgent.base(this, 'disposeInternal');
176 };