Permalink
Browse files

Added isNormal() method to Device interface and updated tests

  • Loading branch information...
1 parent 429bf79 commit 7cff3ca312a8f6c563398ed42c04a904222b3e65 @royclarkson royclarkson committed Apr 20, 2012
View
5 spring-mobile-device/src/main/java/org/springframework/mobile/device/Device.java
@@ -23,6 +23,11 @@
* @author Scott Rossillo
*/
public interface Device {
+
+ /**
+ * True if this device is not a mobile or tablet device.
+ */
+ boolean isNormal();
/**
* True if this device is a mobile device such as an Apple iPhone or an Nexus One Android.
View
4 spring-mobile-device/src/main/java/org/springframework/mobile/device/DeviceType.java
@@ -27,12 +27,12 @@
NORMAL,
/**
- * Represents a mobile sized device, such as an iPhone
+ * Represents a mobile device, such as an iPhone
*/
MOBILE,
/**
- * Represents a tablet sized device, such as an iPad
+ * Represents a tablet device, such as an iPad
*/
TABLET
}
View
12 spring-mobile-device/src/main/java/org/springframework/mobile/device/LiteDevice.java
@@ -29,15 +29,23 @@
public static final LiteDevice TABLET_INSTANCE = new LiteDevice(DeviceType.TABLET);
public static final LiteDevice NORMAL_INSTANCE = new LiteDevice(DeviceType.NORMAL);
+
+ public boolean isNormal() {
+ return this.deviceType == DeviceType.NORMAL;
+ }
public boolean isMobile() {
- return deviceType == DeviceType.MOBILE;
+ return this.deviceType == DeviceType.MOBILE;
}
public boolean isTablet() {
- return deviceType == DeviceType.TABLET;
+ return this.deviceType == DeviceType.TABLET;
}
+ public DeviceType getDeviceType() {
+ return this.deviceType;
+ }
+
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[LiteDevice ");
View
162 ...mobile-device/src/test/java/org/springframework/mobile/device/LiteDeviceResolverTest.java
@@ -15,7 +15,7 @@
private LiteDeviceResolver resolver = new LiteDeviceResolver();
private MockHttpServletRequest request = new MockHttpServletRequest();
-
+
@Before
public void setUp() {
resolver.getNormalUserAgentKeywords().clear();
@@ -25,24 +25,21 @@ public void setUp() {
public void wapProfileHeader() {
request.addHeader("x-wap-profile", "http://nds.nokia.com/uaprof/N3650r100.xml");
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void profileHeader() {
request.addHeader("Profile", "http://nds.nokia.com/uaprof/N3650r100.xml");
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void userAgentHeaderPrefix() {
request.addHeader("User-Agent", UserAgent.PalmCentro);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
@@ -51,179 +48,150 @@ public void acceptHeader() {
"Accept",
"application/vnd.wap.wmlscriptc, text/vnd.wap.wml, application/vnd.wap.xhtml+xml, application/xhtml+xml, text/html, multipart/mixed, */*");
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void userAgentKeyword() {
request.addHeader("User-Agent", UserAgent.iPhone_iOS5);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void operaMini() {
request.addHeader("X-OperaMini-Phone-UA",
"X-OperaMini-Phone-UA: SonyEricssonK750i/R1AA Browser/SEMC-Browser/4.2 Profile/MIDP-2.0 Configuration/CLDC-1.1");
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
- }
-
- @Test
- public void notMobile() {
- request.addHeader("User-Agent", UserAgent.FireFox3_6_Mac10_6);
- Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void notMobileNoHeaders() {
Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
- assertFalse(device.isTablet());
+ assertNormal(device);
}
@Test
- public void mobileToString() {
- request.addHeader("User-Agent", UserAgent.iPhone_iOS5);
- Device device = resolver.resolveDevice(request);
- assertEquals(device.toString(), MOBILE_TO_STRING);
- }
-
- @Test
- public void tabletToString() {
- request.addHeader("User-Agent", UserAgent.MotorolaXoom_Android3_1);
- Device device = resolver.resolveDevice(request);
- assertEquals(device.toString(), TABLET_TO_STRING);
- }
-
- @Test
- public void normalToString() {
- request.addHeader("User-Agent", UserAgent.Safari5_1_5_Mac10_7_3);
- Device device = resolver.resolveDevice(request);
- assertEquals(device.toString(), NORMAL_TO_STRING);
- }
-
- @Test
public void normalDeviceMobileOverride() {
String[] normalDevices = new String[] { "android", "iphone" };
resolver.getNormalUserAgentKeywords().addAll(Arrays.asList(normalDevices));
request.addHeader("User-Agent", UserAgent.iPhone_iOS5);
Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
+ assertNormal(device);
}
-
+
@Test
public void normalDeviceTabletOverride() {
String[] normalDevices = new String[] { "ipad" };
resolver.getNormalUserAgentKeywords().addAll(Arrays.asList(normalDevices));
request.addHeader("User-Agent", UserAgent.iPad_iOS5);
Device device = resolver.resolveDevice(request);
- assertFalse(device.isTablet());
+ assertNormal(device);
}
-
+
@Test
- public void constructor() {
+ public void constructorNormalDeviceOverride() {
String[] normalDevices = new String[] { "android", "iphone" };
- LiteDeviceResolver resolver2 = new LiteDeviceResolver(Arrays.asList(normalDevices));
+ LiteDeviceResolver resolver2 = new LiteDeviceResolver(Arrays.asList(normalDevices));
request.addHeader("User-Agent", UserAgent.iPhone_iOS5);
Device device = resolver2.resolveDevice(request);
- assertFalse(device.isMobile());
+ assertNormal(device);
+ }
+
+ // Normal device User-Agent tests
+
+ @Test
+ public void safari5_1_5_Mac10_7_3() {
+ request.addHeader("User-Agent", UserAgent.Safari5_1_5_Mac10_7_3);
+ Device device = resolver.resolveDevice(request);
+ assertEquals(device.toString(), NORMAL_TO_STRING);
}
+ @Test
+ public void fireFox3_6_Mac10_6() {
+ request.addHeader("User-Agent", UserAgent.FireFox3_6_Mac10_6);
+ Device device = resolver.resolveDevice(request);
+ assertNormal(device);
+ }
+
// Mobile device User-Agent tests
@Test
public void iPodTouch() {
request.addHeader("User-Agent", UserAgent.iPodTouch_iOS1_1_3);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void iPhone() {
request.addHeader("User-Agent", UserAgent.iPhone_iOS5);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
- assertEquals(device.toString(), MOBILE_TO_STRING);
+ assertMobile(device);
}
@Test
public void googleNexusOne() {
request.addHeader("User-Agent", UserAgent.GoogleNexusOne_Android2_1);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void googleNexusS() {
request.addHeader("User-Agent", UserAgent.GoogleNexusS_Android2_3);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void blackBerry9850() {
request.addHeader("User-Agent", UserAgent.BlackBerry9850_OS7);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void blackBerry9800() {
request.addHeader("User-Agent", UserAgent.BlackBerry9800_OS5);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void palmCentro() {
request.addHeader("User-Agent", UserAgent.PalmCentro);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void palmPre() {
request.addHeader("User-Agent", UserAgent.PalmPre_webOS1_4);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void palmPre2() {
request.addHeader("User-Agent", UserAgent.PalmPre2_webOS2_1);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void hpPre3() {
request.addHeader("User-Agent", UserAgent.HPPre3_webOS2_1);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void palmPixi() {
request.addHeader("User-Agent", UserAgent.PalmPixi_webOS1_4);
Device device = resolver.resolveDevice(request);
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
// Tablet device User-Agent tests
@@ -232,62 +200,78 @@ public void palmPixi() {
public void iPad() {
request.addHeader("User-Agent", UserAgent.iPad_iOS3_2);
Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
- assertTrue(device.isTablet());
+ assertTablet(device);
}
@Test
public void samsungGalaxyTab() {
request.addHeader("User-Agent", UserAgent.SamsungGalaxyTab_Android2_2);
Device device = resolver.resolveDevice(request);
// Device reports standard Android user agent, indicating it as a mobile device
- assertTrue(device.isMobile());
- assertFalse(device.isTablet());
+ assertMobile(device);
}
@Test
public void samsungGalaxyTab10_1V() {
request.addHeader("User-Agent", UserAgent.SamsungGalaxyTab10_1V_Android3_0);
Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
- assertTrue(device.isTablet());
+ assertTablet(device);
}
-
+
@Test
public void samsungGalaxyTab10_1() {
request.addHeader("User-Agent", UserAgent.SamsungGalaxyTab10_1_Android3_1);
Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
- assertTrue(device.isTablet());
+ assertTablet(device);
}
@Test
public void motorolaXoom() {
request.addHeader("User-Agent", UserAgent.MotorolaXoom_Android3_1);
Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
- assertTrue(device.isTablet());
+ assertTablet(device);
}
@Test
public void blackBerryPlaybook() {
request.addHeader("User-Agent", UserAgent.BlackBerryPlaybook);
Device device = resolver.resolveDevice(request);
- assertFalse(device.isMobile());
- assertTrue(device.isTablet());
+ assertTablet(device);
}
@Test
public void hpTouchPad() {
request.addHeader("User-Agent", UserAgent.HPTouchPad_webOS3);
Device device = resolver.resolveDevice(request);
+ assertTablet(device);
+ }
+
+ // helpers
+
+ private static void assertNormal(Device device) {
+ assertTrue(device.isNormal());
+ assertFalse(device.isMobile());
+ assertFalse(device.isTablet());
+ assertEquals(device.toString(), NORMAL_TO_STRING);
+ }
+
+ private static void assertMobile(Device device) {
+ assertFalse(device.isNormal());
+ assertTrue(device.isMobile());
+ assertFalse(device.isTablet());
+ assertEquals(device.toString(), MOBILE_TO_STRING);
+ }
+
+ private static void assertTablet(Device device) {
+ assertFalse(device.isNormal());
assertFalse(device.isMobile());
assertTrue(device.isTablet());
+ assertEquals(device.toString(), TABLET_TO_STRING);
}
private static final String MOBILE_TO_STRING = "[LiteDevice type=MOBILE]";
-
+
private static final String TABLET_TO_STRING = "[LiteDevice type=TABLET]";
-
+
private static final String NORMAL_TO_STRING = "[LiteDevice type=NORMAL]";
}
View
8 spring-mobile-device/src/test/java/org/springframework/mobile/device/StubDevice.java
@@ -13,13 +13,17 @@ public StubDevice() {
public StubDevice(DeviceType deviceType) {
this.deviceType = deviceType;
}
+
+ public boolean isNormal() {
+ return this.deviceType == DeviceType.NORMAL;
+ }
public boolean isMobile() {
- return deviceType == DeviceType.MOBILE;
+ return this.deviceType == DeviceType.MOBILE;
}
public boolean isTablet() {
- return deviceType == DeviceType.TABLET;
+ return this.deviceType == DeviceType.TABLET;
}
public void setMobile(boolean mobile) {

0 comments on commit 7cff3ca

Please sign in to comment.