PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/java/client/src/org/openqa/selenium/Platform.java

https://gitlab.com/chrsmith/selenium
Java | 331 lines | 187 code | 37 blank | 107 comment | 20 complexity | c6a208f4148d10518e402bfe22ca8e7d MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, MIT, AGPL-1.0
  1. /*
  2. Copyright 2007-2011 Selenium committers
  3. Copyright 2011 Software Freedom Conservancy
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package org.openqa.selenium;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. /**
  18. * Represents the known and supported Platforms that WebDriver runs on. This is pretty close to the
  19. * Operating System, but differs slightly, because this class is used to extract information such as
  20. * program locations and line endings.
  21. */
  22. // Useful URLs:
  23. // http://hg.openjdk.java.net/jdk7/modules/jdk/file/a37326fa7f95/src/windows/native/java/lang/java_props_md.c
  24. public enum Platform {
  25. /**
  26. * Never returned, but can be used to request a browser running on any version of Windows.
  27. */
  28. WINDOWS("") {},
  29. /**
  30. * For versions of Windows that "feel like" Windows XP. These are ones that store files in
  31. * "\Program Files\" and documents under "\\documents and settings\\username"
  32. */
  33. XP("Windows Server 2003", "xp", "windows", "winnt") {
  34. @Override
  35. public Platform family() {
  36. return WINDOWS;
  37. }
  38. },
  39. /**
  40. * For versions of Windows that "feel like" Windows Vista.
  41. */
  42. VISTA("windows vista", "Windows Server 2008", "windows 7", "win7") {
  43. @Override
  44. public Platform family() {
  45. return WINDOWS;
  46. }
  47. },
  48. /**
  49. * For versions of Windows that "feel like" Windows 8.
  50. */
  51. WIN8("Windows Server 2012", "windows 8", "win8") {
  52. @Override
  53. public Platform family() {
  54. return WINDOWS;
  55. }
  56. },
  57. WIN8_1("windows 8.1", "win8.1") {
  58. @Override
  59. public Platform family() {
  60. return WINDOWS;
  61. }
  62. },
  63. MAC("mac", "darwin", "os x") {},
  64. SNOW_LEOPARD("snow leopard", "os x 10.6") {
  65. @Override
  66. public Platform family() {
  67. return MAC;
  68. }
  69. @Override
  70. public String toString() {
  71. return "OS X 10.6";
  72. }
  73. },
  74. MOUNTAIN_LION("mountain lion", "os x 10.8") {
  75. @Override
  76. public Platform family() {
  77. return MAC;
  78. }
  79. @Override
  80. public String toString() {
  81. return "OS X 10.8";
  82. }
  83. },
  84. MAVERICKS("mavericks", "os x 10.9") {
  85. @Override
  86. public Platform family() {
  87. return MAC;
  88. }
  89. @Override
  90. public String toString() {
  91. return "OS X 10.9";
  92. }
  93. },
  94. YOSEMITE("yosemite", "os x 10.10") {
  95. @Override
  96. public Platform family() {
  97. return MAC;
  98. }
  99. @Override
  100. public String toString() {
  101. return "OS X 10.10";
  102. }
  103. },
  104. /**
  105. * Many platforms have UNIX traits, amongst them LINUX, Solaris and BSD.
  106. */
  107. UNIX("solaris", "bsd") {},
  108. LINUX("linux") {
  109. @Override
  110. public Platform family() {
  111. return UNIX;
  112. }
  113. },
  114. ANDROID("android", "dalvik") {
  115. public String getLineEnding() {
  116. return "\n";
  117. }
  118. @Override
  119. public Platform family() {
  120. return LINUX;
  121. }
  122. },
  123. /**
  124. * Never returned, but can be used to request a browser running on any operating system.
  125. */
  126. ANY("") {
  127. @Override
  128. public boolean is(Platform compareWith) {
  129. return this == compareWith;
  130. }
  131. };
  132. private final String[] partOfOsName;
  133. private final int minorVersion;
  134. private final int majorVersion;
  135. private Platform(String... partOfOsName) {
  136. this.partOfOsName = partOfOsName;
  137. String version = System.getProperty("os.version", "0.0.0");
  138. int major = 0;
  139. int min = 0;
  140. Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+).*");
  141. Matcher matcher = pattern.matcher(version);
  142. if (matcher.matches()) {
  143. try {
  144. major = Integer.parseInt(matcher.group(1));
  145. min = Integer.parseInt(matcher.group(2));
  146. } catch (NumberFormatException e) {
  147. // These things happen
  148. }
  149. }
  150. majorVersion = major;
  151. minorVersion = min;
  152. }
  153. public String[] getPartOfOsName() {
  154. return partOfOsName;
  155. }
  156. /**
  157. * Get current platform (not necessarily the same as operating system).
  158. *
  159. * @return current platform
  160. */
  161. public static Platform getCurrent() {
  162. return extractFromSysProperty(System.getProperty("os.name"));
  163. }
  164. /**
  165. * Extracts platforms based on system properties in Java and uses a heuristic to determine the
  166. * most likely operating system. If unable to determine the operating system, it will default to
  167. * UNIX.
  168. *
  169. * @param osName the operating system name to determine the platform of
  170. * @return the most likely platform based on given operating system name
  171. */
  172. public static Platform extractFromSysProperty(String osName) {
  173. return extractFromSysProperty(osName, System.getProperty("os.version"));
  174. }
  175. /**
  176. * Extracts platforms based on system properties in Java and uses a heuristic to determine the
  177. * most likely operating system. If unable to determine the operating system, it will default to
  178. * UNIX.
  179. *
  180. * @param osName the operating system name to determine the platform of
  181. * @param osVersion the operating system version to determine the platform of
  182. * @return the most likely platform based on given operating system name and version
  183. */
  184. public static Platform extractFromSysProperty(String osName, String osVersion) {
  185. osName = osName.toLowerCase();
  186. // os.name for android is linux
  187. if ("dalvik".equalsIgnoreCase(System.getProperty("java.vm.name"))) {
  188. return Platform.ANDROID;
  189. }
  190. // Windows 8 can't be detected by osName alone
  191. if (osVersion.equals("6.2") && osName.startsWith("windows nt")) {
  192. return WIN8;
  193. }
  194. // Windows 8 can't be detected by osName alone
  195. if (osVersion.equals("6.3") && osName.startsWith("windows nt")) {
  196. return WIN8_1;
  197. }
  198. Platform mostLikely = UNIX;
  199. String previousMatch = null;
  200. for (Platform os : Platform.values()) {
  201. for (String matcher : os.partOfOsName) {
  202. if ("".equals(matcher)) {
  203. continue;
  204. }
  205. matcher = matcher.toLowerCase();
  206. if (os.isExactMatch(osName, matcher)) {
  207. return os;
  208. }
  209. if (os.isCurrentPlatform(osName, matcher) && isBetterMatch(previousMatch, matcher)) {
  210. previousMatch = matcher;
  211. mostLikely = os;
  212. }
  213. }
  214. }
  215. // Default to assuming we're on a UNIX variant (including LINUX)
  216. return mostLikely;
  217. }
  218. /**
  219. * Gets a platform with the name matching the parameter.
  220. *
  221. * @param name the platform name
  222. * @return the Platform enum value matching the parameter
  223. */
  224. public static Platform fromString(String name) {
  225. try {
  226. return Platform.valueOf(name);
  227. } catch (IllegalArgumentException ex) {
  228. for (Platform os : Platform.values()) {
  229. for (String matcher : os.partOfOsName) {
  230. if (name.toLowerCase().equals(matcher.toLowerCase())) {
  231. return os;
  232. }
  233. }
  234. }
  235. throw new WebDriverException("Unrecognized platform: " + name);
  236. }
  237. }
  238. /**
  239. * Decides whether the previous match is better or not than the current match. If previous match
  240. * is null, the newer match is always better.
  241. *
  242. * @param previous the previous match
  243. * @param matcher the newer match
  244. * @return true if newer match is better, false otherwise
  245. */
  246. private static boolean isBetterMatch(String previous, String matcher) {
  247. return previous == null || matcher.length() >= previous.length();
  248. }
  249. /**
  250. * Heuristic for comparing two platforms. If platforms (which is not the same thing as operating
  251. * systems) are found to be approximately similar in nature, this will return true. For instance
  252. * the LINUX platform is similar to UNIX, and will give a positive result if compared.
  253. *
  254. * @param compareWith the platform to compare with
  255. * @return true if platforms are approximately similar, false otherwise
  256. */
  257. public boolean is(Platform compareWith) {
  258. return this == compareWith || this.family().is(compareWith);
  259. }
  260. /**
  261. * Returns a platform that represents a family for the current platform. For instance
  262. * the LINUX if a part of the UNIX family, the XP is a part of the WINDOWS family.
  263. *
  264. * @return the family platform for the current one
  265. */
  266. public Platform family() {
  267. return ANY;
  268. }
  269. private boolean isCurrentPlatform(String osName, String matchAgainst) {
  270. return osName.contains(matchAgainst);
  271. }
  272. private boolean isExactMatch(String osName, String matchAgainst) {
  273. return matchAgainst.equals(osName);
  274. }
  275. /**
  276. * Returns the major version of this platform.
  277. *
  278. * @return the major version of specified platform
  279. */
  280. public int getMajorVersion() {
  281. return majorVersion;
  282. }
  283. /**
  284. * Returns the minor version of this platform.
  285. *
  286. * @return the minor version of specified platform
  287. */
  288. public int getMinorVersion() {
  289. return minorVersion;
  290. }
  291. }