@@ -14,12 +14,132 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- import { DeviceType , parseUserAgent } from "../../../src/utils/device/parseUserAgent" ;
17
+ import { DeviceType , ExtendedDeviceInformation , parseUserAgent } from "../../../src/utils/device/parseUserAgent" ;
18
+
19
+ const makeDeviceExtendedInfo = (
20
+ deviceType : DeviceType ,
21
+ deviceModel ?: string ,
22
+ deviceOperatingSystem ?: string ,
23
+ clientName ?: string ,
24
+ clientVersion ?: string ,
25
+ ) : ExtendedDeviceInformation => ( {
26
+ deviceType,
27
+ deviceModel,
28
+ deviceOperatingSystem,
29
+ client : clientName && [ clientName , clientVersion ] . filter ( Boolean ) . join ( ' ' ) ,
30
+ } ) ;
31
+
32
+ /* eslint-disable max-len */
33
+ const ANDROID_UA = [
34
+ // New User Agent Implementation
35
+ "Element dbg/1.5.0-dev (Xiaomi Mi 9T; Android 11; RKQ1.200826.002 test-keys; Flavour GooglePlay; MatrixAndroidSdk2 1.5.2)" ,
36
+ "Element/1.5.0 (Samsung SM-G960F; Android 6.0.1; RKQ1.200826.002; Flavour FDroid; MatrixAndroidSdk2 1.5.2)" ,
37
+ "Element/1.5.0 (Google Nexus 5; Android 7.0; RKQ1.200826.002 test test; Flavour FDroid; MatrixAndroidSdk2 1.5.2)" ,
38
+ "Element/1.5.0 (Google (Nexus) 5; Android 7.0; RKQ1.200826.002 test test; Flavour FDroid; MatrixAndroidSdk2 1.5.2)" ,
39
+ "Element/1.5.0 (Google (Nexus) (5); Android 7.0; RKQ1.200826.002 test test; Flavour FDroid; MatrixAndroidSdk2 1.5.2)" ,
40
+ // Legacy User Agent Implementation
41
+ "Element/1.0.0 (Linux; U; Android 6.0.1; SM-A510F Build/MMB29; Flavour GPlay; MatrixAndroidSdk2 1.0)" ,
42
+ "Element/1.0.0 (Linux; Android 7.0; SM-G610M Build/NRD90M; Flavour GPlay; MatrixAndroidSdk2 1.0)" ,
43
+ ] ;
44
+
45
+ const ANDROID_EXPECTED_RESULT = [
46
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Xiaomi Mi 9T" , "Android 11" ) ,
47
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Samsung SM-G960F" , "Android 6.0.1" ) ,
48
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "LG Nexus 5" , "Android 7.0" ) ,
49
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Google (Nexus) 5" , "Android 7.0" ) ,
50
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Google (Nexus) (5)" , "Android 7.0" ) ,
51
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Samsung SM-A510F" , "Android 6.0.1" ) ,
52
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Samsung SM-G610M" , "Android 7.0" ) ,
53
+ ] ;
54
+
55
+ const IOS_UA = [
56
+ "Element/1.8.21 (iPhone; iOS 15.2; Scale/3.00)" ,
57
+ "Element/1.8.21 (iPhone XS Max; iOS 15.2; Scale/3.00)" ,
58
+ "Element/1.8.21 (iPad Pro (11-inch); iOS 15.2; Scale/3.00)" ,
59
+ "Element/1.8.21 (iPad Pro (12.9-inch) (3rd generation); iOS 15.2; Scale/3.00)" ,
60
+ ] ;
61
+ const IOS_EXPECTED_RESULT = [
62
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Apple iPhone" , "iOS 15.2" ) ,
63
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "Apple iPhone XS Max" , "iOS 15.2" ) ,
64
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "iPad Pro (11-inch)" , "iOS 15.2" ) ,
65
+ makeDeviceExtendedInfo ( DeviceType . Mobile , "iPad Pro (12.9-inch) (3rd generation)" , "iOS 15.2" ) ,
66
+ ] ;
67
+ const DESKTOP_UA = [
68
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2022091301 Chrome/104.0.5112.102" +
69
+ " Electron/20.1.1 Safari/537.36" ,
70
+ "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2022091301 Chrome/104.0.5112.102 Electron/20.1.1 Safari/537.36" ,
71
+ ] ;
72
+ const DESKTOP_EXPECTED_RESULT = [
73
+ makeDeviceExtendedInfo ( DeviceType . Desktop , undefined , "Mac OS 10.15.7" , "Electron" , "20" ) ,
74
+ makeDeviceExtendedInfo ( DeviceType . Desktop , undefined , "Windows 10" , "Electron" , "20" ) ,
75
+ ] ;
76
+
77
+ const WEB_UA = [
78
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36" ,
79
+ "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36" ,
80
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0" ,
81
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18" ,
82
+ "Mozilla/5.0 (Windows NT 6.0; rv:40.0) Gecko/20100101 Firefox/40.0" ,
83
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246" ,
84
+ // using mobile browser
85
+ "Mozilla/5.0 (iPad; CPU OS 8_4_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4" ,
86
+ "Mozilla/5.0 (iPhone; CPU iPhone OS 8_4_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4" ,
87
+ "Mozilla/5.0 (Linux; Android 9; SM-G973U Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36" ,
88
+ ] ;
89
+
90
+ const WEB_EXPECTED_RESULT = [
91
+ makeDeviceExtendedInfo ( DeviceType . Web , undefined , "Mac OS 10.15.7" , "Chrome" , "104" ) ,
92
+ makeDeviceExtendedInfo ( DeviceType . Web , undefined , "Windows 10" , "Chrome" , "104" ) ,
93
+ makeDeviceExtendedInfo ( DeviceType . Web , undefined , "Mac OS 10.10" , "Firefox" , "39" ) ,
94
+ makeDeviceExtendedInfo ( DeviceType . Web , undefined , "Mac OS 10.10.2" , "Safari" , "8" ) ,
95
+ makeDeviceExtendedInfo ( DeviceType . Web , undefined , "Windows Vista" , "Firefox" , "40" ) ,
96
+ makeDeviceExtendedInfo ( DeviceType . Web , undefined , "Windows 10" , "Edge" , "12" ) ,
97
+ // using mobile browser
98
+ makeDeviceExtendedInfo ( DeviceType . Web , "Apple iPad" , "iOS 8.4.1" , "Mobile Safari" , "8" ) ,
99
+ makeDeviceExtendedInfo ( DeviceType . Web , "Apple iPhone" , "iOS 8.4.1" , "Mobile Safari" , "8" ) ,
100
+ makeDeviceExtendedInfo ( DeviceType . Web , "Samsung SM-G973U" , "Android 9" , "Chrome" , "69" ) ,
101
+
102
+ ] ;
103
+
104
+ const MISC_UA = [
105
+ "AppleTV11,1/11.1" ,
106
+ "Curl Client/1.0" ,
107
+ "banana" ,
108
+ "" ,
109
+ ] ;
110
+
111
+ const MISC_EXPECTED_RESULT = [
112
+ makeDeviceExtendedInfo ( DeviceType . Unknown , "Apple Apple TV" , undefined , undefined , undefined ) ,
113
+ makeDeviceExtendedInfo ( DeviceType . Unknown , undefined , undefined , undefined , undefined ) ,
114
+ makeDeviceExtendedInfo ( DeviceType . Unknown , undefined , undefined , undefined , undefined ) ,
115
+ makeDeviceExtendedInfo ( DeviceType . Unknown , undefined , undefined , undefined , undefined ) ,
116
+ ] ;
117
+ /* eslint-disable max-len */
18
118
19
119
describe ( 'parseUserAgent()' , ( ) => {
20
120
it ( 'returns deviceType unknown when user agent is falsy' , ( ) => {
21
121
expect ( parseUserAgent ( undefined ) ) . toEqual ( {
22
122
deviceType : DeviceType . Unknown ,
23
123
} ) ;
24
124
} ) ;
125
+
126
+ type TestCase = [ string , ExtendedDeviceInformation ] ;
127
+
128
+ const testPlatform = ( platform : string , userAgents : string [ ] , results : ExtendedDeviceInformation [ ] ) : void => {
129
+ const testCases : TestCase [ ] = userAgents . map ( ( userAgent , index ) => [ userAgent , results [ index ] ] ) ;
130
+
131
+ describe ( platform , ( ) => {
132
+ it . each (
133
+ testCases ,
134
+ ) ( 'Parses user agent correctly - %s' , ( userAgent , expectedResult ) => {
135
+ expect ( parseUserAgent ( userAgent ) ) . toEqual ( expectedResult ) ;
136
+ } ) ;
137
+ } ) ;
138
+ } ;
139
+
140
+ testPlatform ( 'Android' , ANDROID_UA , ANDROID_EXPECTED_RESULT ) ;
141
+ testPlatform ( 'iOS' , IOS_UA , IOS_EXPECTED_RESULT ) ;
142
+ testPlatform ( 'Desktop' , DESKTOP_UA , DESKTOP_EXPECTED_RESULT ) ;
143
+ testPlatform ( 'Web' , WEB_UA , WEB_EXPECTED_RESULT ) ;
144
+ testPlatform ( 'Misc' , MISC_UA , MISC_EXPECTED_RESULT ) ;
25
145
} ) ;
0 commit comments