Debian Codegraph @

closure-library/closure/goog/useragent/useragenttestutil.js

raw
1 // Copyright 2006 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 Shared test function to reset the constants in
17 * goog.userAgent.*
18 */
19
20 goog.provide('goog.userAgentTestUtil');
21 goog.provide('goog.userAgentTestUtil.UserAgents');
22
23 goog.require('goog.labs.userAgent.browser');
24 goog.require('goog.labs.userAgent.engine');
25 goog.require('goog.labs.userAgent.platform');
26 goog.require('goog.object');
27 goog.require('goog.userAgent');
28 goog.require('goog.userAgent.keyboard');
29 goog.require('goog.userAgent.platform');
30 goog.require('goog.userAgent.product');
31 /** @suppress {extraRequire} */
32 goog.require('goog.userAgent.product.isVersion');
33
34 goog.setTestOnly('goog.userAgentTestUtil');
35
36
37 /**
38 * Rerun the initialization code to set all of the goog.userAgent constants.
39 * @suppress {accessControls}
40 */
41 goog.userAgentTestUtil.reinitializeUserAgent = function() {
42 // Unfortunately we can't isolate the useragent setting in a function
43 // we can call, because things rely on it compiling to nothing when
44 // one of the ASSUME flags is set, and the compiler isn't smart enough
45 // to do that when the setting is done inside a function that's inlined.
46 goog.userAgent.OPERA = goog.labs.userAgent.browser.isOpera();
47 goog.userAgent.IE = goog.labs.userAgent.browser.isIE();
48 goog.userAgent.EDGE = goog.labs.userAgent.engine.isEdge();
49 goog.userAgent.EDGE_OR_IE = goog.userAgent.EDGE || goog.userAgent.IE;
50 goog.userAgent.GECKO = goog.labs.userAgent.engine.isGecko();
51 goog.userAgent.WEBKIT = goog.labs.userAgent.engine.isWebKit();
52 goog.userAgent.MOBILE = goog.userAgent.isMobile_();
53 goog.userAgent.SAFARI = goog.userAgent.WEBKIT;
54
55 // Platform in goog.userAgent.
56 goog.userAgent.PLATFORM = goog.userAgent.determinePlatform_();
57
58 goog.userAgent.MAC = goog.labs.userAgent.platform.isMacintosh();
59 goog.userAgent.WINDOWS = goog.labs.userAgent.platform.isWindows();
60 goog.userAgent.LINUX = goog.userAgent.isLegacyLinux_();
61 goog.userAgent.X11 = goog.userAgent.isX11_();
62 goog.userAgent.ANDROID = goog.labs.userAgent.platform.isAndroid();
63 goog.userAgent.IPAD = goog.labs.userAgent.platform.isIpad();
64 goog.userAgent.IPHONE = goog.labs.userAgent.platform.isIphone();
65 goog.userAgent.IPOD = goog.labs.userAgent.platform.isIpod();
66 goog.userAgent.VERSION = goog.userAgent.determineVersion_();
67
68 // Platform in goog.userAgent.platform.
69 goog.userAgent.platform.VERSION = goog.userAgent.platform.determineVersion_();
70
71 // Update goog.userAgent.product
72 goog.userAgent.product.ANDROID =
73 goog.labs.userAgent.browser.isAndroidBrowser();
74 goog.userAgent.product.CHROME = goog.labs.userAgent.browser.isChrome();
75 goog.userAgent.product.EDGE = goog.labs.userAgent.browser.isEdge();
76 goog.userAgent.product.FIREFOX = goog.labs.userAgent.browser.isFirefox();
77 goog.userAgent.product.IE = goog.labs.userAgent.browser.isIE();
78 goog.userAgent.product.IPAD = goog.labs.userAgent.platform.isIpad();
79 goog.userAgent.product.IPHONE = goog.userAgent.product.isIphoneOrIpod_();
80 goog.userAgent.product.OPERA = goog.labs.userAgent.browser.isOpera();
81 goog.userAgent.product.SAFARI = goog.userAgent.product.isSafariDesktop_();
82
83 // Still uses its own implementation.
84 goog.userAgent.product.VERSION = goog.userAgent.product.determineVersion_();
85
86 // goog.userAgent.keyboard
87 goog.userAgent.keyboard.MAC_KEYBOARD =
88 goog.userAgent.keyboard.determineMacKeyboard_();
89
90 // Reset cache so calls to isVersionOrHigher don't use cached version.
91 goog.object.clear(goog.userAgent.isVersionOrHigherCache_);
92 };
93
94
95 /**
96 * Browser definitions.
97 * @enum {string}
98 */
99 goog.userAgentTestUtil.UserAgents = {
100 GECKO: 'GECKO',
101 IE: 'IE',
102 OPERA: 'OPERA',
103 WEBKIT: 'WEBKIT',
104 EDGE: 'EDGE'
105 };
106
107
108 /**
109 * Return whether a given user agent has been detected.
110 * @param {string} agent Value in UserAgents.
111 * @return {boolean} Whether the user agent has been detected.
112 */
113 goog.userAgentTestUtil.getUserAgentDetected = function(agent) {
114 switch (agent) {
115 case goog.userAgentTestUtil.UserAgents.GECKO:
116 return goog.userAgent.GECKO;
117 case goog.userAgentTestUtil.UserAgents.IE:
118 return goog.userAgent.IE;
119 case goog.userAgentTestUtil.UserAgents.EDGE:
120 return goog.userAgent.EDGE;
121 case goog.userAgentTestUtil.UserAgents.OPERA:
122 return goog.userAgent.OPERA;
123 case goog.userAgentTestUtil.UserAgents.WEBKIT:
124 return goog.userAgent.WEBKIT;
125 }
126
127 throw new Error('Unrecognized user agent');
128 };