Skip to content

Commit

Permalink
Fixing platform comparison. Fixes issue 7522
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Jun 27, 2014
1 parent c7b0193 commit 52197cf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
46 changes: 25 additions & 21 deletions java/client/src/org/openqa/selenium/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,16 @@ public enum Platform {
/**
* Never returned, but can be used to request a browser running on any version of Windows.
*/
WINDOWS("") {
@Override
public boolean is(Platform compareWith) {
return compareWith == WINDOWS || compareWith == XP
|| compareWith == VISTA || compareWith == WIN8;
}
},
WINDOWS("") {},

/**
* For versions of Windows that "feel like" Windows XP. These are ones that store files in
* "\Program Files\" and documents under "\\documents and settings\\username"
*/
XP("Windows Server 2003", "xp", "windows", "winnt") {
@Override
public boolean is(Platform compareWith) {
return compareWith == WINDOWS || compareWith == XP;
public Platform family() {
return WINDOWS;
}
},

Expand All @@ -56,8 +50,8 @@ public boolean is(Platform compareWith) {
*/
VISTA("windows vista", "Windows Server 2008", "windows 7", "win7") {
@Override
public boolean is(Platform compareWith) {
return compareWith == WINDOWS || compareWith == VISTA;
public Platform family() {
return WINDOWS;
}
},

Expand All @@ -66,15 +60,15 @@ public boolean is(Platform compareWith) {
*/
WIN8("Windows Server 2012", "windows 8", "win8") {
@Override
public boolean is(Platform compareWith) {
return compareWith == WINDOWS || compareWith == WIN8;
public Platform family() {
return WINDOWS;
}
},

WIN8_1("windows 8.1", "win8.1") {
@Override
public boolean is(Platform compareWith) {
return compareWith == WINDOWS || compareWith == WIN8_1;
public Platform family() {
return WINDOWS;
}
},

Expand All @@ -87,8 +81,8 @@ public boolean is(Platform compareWith) {

LINUX("linux") {
@Override
public boolean is(Platform compareWith) {
return compareWith == UNIX || compareWith == LINUX;
public Platform family() {
return UNIX;
}
},

Expand All @@ -98,8 +92,8 @@ public String getLineEnding() {
}

@Override
public boolean is(Platform compareWith) {
return compareWith == LINUX || compareWith == ANDROID;
public Platform family() {
return LINUX;
}
},

Expand All @@ -109,7 +103,7 @@ public boolean is(Platform compareWith) {
ANY("") {
@Override
public boolean is(Platform compareWith) {
return true;
return this == compareWith;
}
};

Expand Down Expand Up @@ -230,7 +224,17 @@ private static boolean isBetterMatch(String previous, String matcher) {
* @return true if platforms are approximately similar, false otherwise
*/
public boolean is(Platform compareWith) {
return this.equals(compareWith);
return this == compareWith || this.family().is(compareWith);
}

/**
* Returns a platform that represents a family for the current platform. For instance
* the LINUX if a part of the UNIX family, the XP is a part of the WINDOWS family.
*
* @return the family platform for the current one
*/
public Platform family() {
return ANY;
}

private boolean isCurrentPlatform(String osName, String matchAgainst) {
Expand Down
44 changes: 33 additions & 11 deletions java/client/test/org/openqa/selenium/PlatformTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@

public class PlatformTest {

@Test
public void testShouldIdentifyWindowsVariants() {
assertAllAre(Platform.WINDOWS, "Windows 2003");
}

@Test
public void testXpIsWindows() {
assertTrue(Platform.XP.is(Platform.WINDOWS));
Expand All @@ -55,6 +50,36 @@ public void testLinuxIsUnix() {
assertTrue(Platform.LINUX.is(Platform.UNIX));
}

@Test
public void testUnixIsNotLinux() {
assertFalse(Platform.UNIX.is(Platform.LINUX));
}

@Test
public void testXpIsAny() {
assertTrue(Platform.XP.is(Platform.ANY));
}

@Test
public void testWindowsIsAny() {
assertTrue(Platform.WINDOWS.is(Platform.ANY));
}

@Test
public void testLinuxIsAny() {
assertTrue(Platform.LINUX.is(Platform.ANY));
}

@Test
public void testUnixIsAny() {
assertTrue(Platform.UNIX.is(Platform.ANY));
}

@Test
public void testShouldIdentifyXPVariants() {
assertAllAre(Platform.WINDOWS, "Windows 2003", "xp", "windows", "winnt");
}

@Test
public void testShouldIdentifyVistaVariants() {
assertAllAre(Platform.VISTA, "Windows Vista", "windows server 2008", "Windows 7", "win7");
Expand Down Expand Up @@ -82,12 +107,9 @@ public void testWindows8Detection() {
}

@Test
public void testShouldDistinctUnixFromLinux() {
Platform linPlatform = Platform.extractFromSysProperty("Linux");
assertTrue("Linux should be identified as Unix", linPlatform.is(Platform.UNIX));

Platform anyUnixPlatform = Platform.extractFromSysProperty("solaris");
assertFalse("Unix should NOT be identified as Linux", anyUnixPlatform.is(Platform.LINUX));
public void testWindows81Detection() {
assertEquals("Windows NT with os version 6.3 should be detected as Windows 8.1",
Platform.WIN8_1, Platform.extractFromSysProperty("windows nt (unknown)", "6.3"));
}

private void assertAllAre(Platform platform, String... osNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ protected DefaultDriverSessions(Platform runningOn, DriverFactory factory) {
private void registerDefaults(Platform current) {
for (Map.Entry<Capabilities, String> entry : defaultDrivers.entrySet()) {
Capabilities caps = entry.getKey();
if (caps.getPlatform() != null && caps.getPlatform().is(current)) {
registerDriver(caps, entry.getValue());
} else if (caps.getPlatform() == null) {
if (caps.getPlatform() == null || caps.getPlatform() == Platform.ANY || current.is(caps.getPlatform())) {
registerDriver(caps, entry.getValue());
} else {
log.info("Default driver " + entry.getValue() + " registration is skipped: registration capabilities "
Expand Down

0 comments on commit 52197cf

Please sign in to comment.