Skip to content

Commit

Permalink
Update Client Hints Windows version detection (Issue #13317)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgibson committed Jul 6, 2023
1 parent fe629b3 commit 9900c56
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 27 deletions.
29 changes: 18 additions & 11 deletions media/js/base/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,29 @@
},

// Madness from https://docs.microsoft.com/microsoft-edge/web-platform/how-to-detect-win11
// and https://wicg.github.io/ua-client-hints/#sec-ch-ua-platform-version
getWindowsVersionClientHint: function (version) {
var fullPlatformVersion = version ? version.toString() : '0';
var majorPlatformVersion = parseInt(
fullPlatformVersion.split('.')[0],
10
);
if (majorPlatformVersion >= 13) {
var platformVersion = parseFloat(fullPlatformVersion);

if (platformVersion >= 13.0) {
// Windows 11 or later.
return '11.0.0';
} else if (majorPlatformVersion > 0) {
return '11.0';
} else if (platformVersion >= 1.0 && platformVersion < 13.0) {
// Windows 10
return '10.0.0';
return '10.0';
} else if (platformVersion === 0.3) {
// Windows 8.1
return '6.3';
} else if (platformVersion === 0.2) {
// Windows 8
return '6.2';
} else if (platformVersion === 0.1) {
// Windows 7
return '6.1';
} else {
// Might be any Windows version less than 10, who knows.
// Rather than return undefined here, fallback to UA string.
return site.getPlatformVersion();
// Windows versions older than 7 are not reported, so return zero.
return '0';
}
},

Expand Down
89 changes: 73 additions & 16 deletions tests/unit/spec/base/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
describe('site.js', function () {
describe('getPlatform', function () {
it('should identify Windows', function () {
expect(
window.site.getPlatform(
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
'Win64'
)
).toBe('windows');
expect(
window.site.getPlatform(
'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
Expand Down Expand Up @@ -375,40 +381,91 @@ describe('site.js', function () {
describe('getWindowsVersionClientHint', function () {
it('should identify 13 and up as Windows 11 or later', function () {
expect(window.site.getWindowsVersionClientHint('13.0.0')).toBe(
'11.0.0'
'11.0'
);
expect(window.site.getWindowsVersionClientHint('14.0.0')).toBe(
'11.0.0'
'11.0'
);
});

it('should identify 1 through 12 as Windows 10', function () {
expect(window.site.getWindowsVersionClientHint('12.0.0')).toBe(
'10.0.0'
'10.0'
);
expect(window.site.getWindowsVersionClientHint('12.1.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('11.0.0')).toBe(
'10.0.0'
'10.0'
);
expect(window.site.getWindowsVersionClientHint('10.0.0')).toBe(
'10.0.0'
'10.0'
);
expect(window.site.getWindowsVersionClientHint('9.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('8.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('7.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('6.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('5.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('4.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('3.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('2.0.0')).toBe(
'10.0'
);
expect(window.site.getWindowsVersionClientHint('1.0.0')).toBe(
'10.0.0'
'10.0'
);
});

it('should identify 0.3 as Windows 8.1', function () {
expect(window.site.getWindowsVersionClientHint('0.3')).toBe('6.3');
expect(window.site.getWindowsVersionClientHint('0.3.1')).toBe(
'6.3'
);
});

it('should identify 0.2 as Windows 8', function () {
expect(window.site.getWindowsVersionClientHint('0.2')).toBe('6.2');
expect(window.site.getWindowsVersionClientHint('0.2.0')).toBe(
'6.2'
);
expect(window.site.getWindowsVersionClientHint('1')).toBe('10.0.0');
expect(window.site.getWindowsVersionClientHint(1.0)).toBe('10.0.0');
expect(window.site.getWindowsVersionClientHint(12)).toBe('10.0.0');
});

it('should return fallback to using UA string for unknown Windows versions', function () {
spyOn(window.site, 'getPlatformVersion').and.returnValue('8.1');
it('should identify 0.1 as Windows 7', function () {
expect(window.site.getWindowsVersionClientHint('0.1')).toBe('6.1');
expect(window.site.getWindowsVersionClientHint('0.1.2')).toBe(
'6.1'
);
});

expect(window.site.getWindowsVersionClientHint('0.0.0')).toBe(
'8.1'
it('should identify 0.1 as Windows 7', function () {
expect(window.site.getWindowsVersionClientHint('0.1')).toBe('6.1');
expect(window.site.getWindowsVersionClientHint('0.1.2')).toBe(
'6.1'
);
expect(window.site.getWindowsVersionClientHint(0.0)).toBe('8.1');
expect(window.site.getWindowsVersionClientHint('0')).toBe('8.1');
expect(window.site.getWindowsVersionClientHint('')).toBe('8.1');
});

it('should return anything else as zero / unknown', function () {
expect(window.site.getWindowsVersionClientHint('0.9')).toBe('0');
expect(window.site.getWindowsVersionClientHint('0.8')).toBe('0');
expect(window.site.getWindowsVersionClientHint('0.7')).toBe('0');
expect(window.site.getWindowsVersionClientHint('0.6')).toBe('0');
expect(window.site.getWindowsVersionClientHint('0.5')).toBe('0');
expect(window.site.getWindowsVersionClientHint('0.4')).toBe('0');
expect(window.site.getWindowsVersionClientHint('0.0.1')).toBe('0');
});
});

Expand Down

0 comments on commit 9900c56

Please sign in to comment.