env.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import env, { isEdge, isMac, isGecko, isSafari, isAndroid } from '../src/env';
  6. function toLowerCase( str ) {
  7. return str.toLowerCase();
  8. }
  9. describe( 'Env', () => {
  10. it( 'is an object', () => {
  11. expect( env ).to.be.an( 'object' );
  12. } );
  13. describe( 'isMac', () => {
  14. it( 'is a boolean', () => {
  15. expect( env.isMac ).to.be.a( 'boolean' );
  16. } );
  17. } );
  18. describe( 'isEdge', () => {
  19. it( 'is a boolean', () => {
  20. expect( env.isEdge ).to.be.a( 'boolean' );
  21. } );
  22. } );
  23. describe( 'isGecko', () => {
  24. it( 'is a boolean', () => {
  25. expect( env.isGecko ).to.be.a( 'boolean' );
  26. } );
  27. } );
  28. describe( 'isSafari', () => {
  29. it( 'is a boolean', () => {
  30. expect( env.isSafari ).to.be.a( 'boolean' );
  31. } );
  32. } );
  33. describe( 'isAndroid', () => {
  34. it( 'is a boolean', () => {
  35. expect( env.isAndroid ).to.be.a( 'boolean' );
  36. } );
  37. } );
  38. describe( 'isMac()', () => {
  39. it( 'returns true for macintosh UA strings', () => {
  40. expect( isMac( 'macintosh' ) ).to.be.true;
  41. expect( isMac( 'foo macintosh bar' ) ).to.be.true;
  42. expect( isMac( toLowerCase(
  43. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) ' +
  44. 'Chrome/61.0.3163.100 Safari/537.36'
  45. ) ) ).to.be.true;
  46. } );
  47. it( 'returns false for non–macintosh UA strings', () => {
  48. expect( isMac( '' ) ).to.be.false;
  49. expect( isMac( 'mac' ) ).to.be.false;
  50. expect( isMac( 'foo' ) ).to.be.false;
  51. } );
  52. } );
  53. describe( 'isEdge()', () => {
  54. it( 'returns true for Edge UA strings', () => {
  55. expect( isEdge( 'edge/12' ) ).to.be.true;
  56. expect( isEdge( 'foo edge/12 bar' ) ).to.be.true;
  57. expect( isEdge( toLowerCase(
  58. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' +
  59. 'Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393'
  60. ) ) ).to.be.true;
  61. } );
  62. it( 'returns false for non–Edge UA strings', () => {
  63. expect( isEdge( '' ) ).to.be.false;
  64. expect( isEdge( 'mac' ) ).to.be.false;
  65. expect( isEdge( 'foo' ) ).to.be.false;
  66. expect( isEdge( 'ledge' ) ).to.be.false;
  67. expect( isEdge( 'foo edge bar' ) ).to.be.false;
  68. // Chrome
  69. expect( isEdge( toLowerCase(
  70. 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
  71. ) ) ).to.be.false;
  72. // IE11
  73. expect( isEdge( toLowerCase(
  74. 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
  75. ) ) ).to.be.false;
  76. } );
  77. } );
  78. describe( 'isGecko()', () => {
  79. it( 'returns true for Firefox UA strings', () => {
  80. expect( isGecko( 'gecko/42' ) ).to.be.true;
  81. expect( isGecko( 'foo gecko/42 bar' ) ).to.be.true;
  82. expect( isGecko( toLowerCase(
  83. 'mozilla/5.0 (macintosh; intel mac os x 10.13; rv:62.0) gecko/20100101 firefox/62.0'
  84. ) ) ).to.be.true;
  85. } );
  86. it( 'returns false for non–Edge UA strings', () => {
  87. expect( isGecko( '' ) ).to.be.false;
  88. expect( isGecko( 'foo' ) ).to.be.false;
  89. expect( isGecko( 'Mozilla' ) ).to.be.false;
  90. // Chrome
  91. expect( isGecko( toLowerCase(
  92. 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
  93. ) ) ).to.be.false;
  94. } );
  95. } );
  96. describe( 'isSafari()', () => {
  97. /* eslint-disable max-len */
  98. it( 'returns true for Safari UA strings', () => {
  99. expect( isSafari( toLowerCase(
  100. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15'
  101. ) ) ).to.be.true;
  102. expect( isSafari( toLowerCase(
  103. 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1'
  104. ) ) ).to.be.true;
  105. } );
  106. it( 'returns false for non-Safari UA strings', () => {
  107. expect( isSafari( toLowerCase(
  108. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  109. ) ) ).to.be.false;
  110. expect( isSafari( toLowerCase(
  111. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
  112. ) ) ).to.be.false;
  113. expect( isSafari( toLowerCase(
  114. 'Mozilla/5.0 (Linux; Android 7.1; Mi A1 Build/N2G47H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36'
  115. ) ) ).to.be.false;
  116. } );
  117. /* eslint-enable max-len */
  118. } );
  119. describe( 'isAndroid()', () => {
  120. /* eslint-disable max-len */
  121. it( 'returns true for Android UA strings', () => {
  122. // Strings taken from https://developer.chrome.com/multidevice/user-agent.
  123. expect( isAndroid( toLowerCase(
  124. 'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>'
  125. ) ) ).to.be.true;
  126. expect( isAndroid( toLowerCase(
  127. 'Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev>(KHTML, like Gecko) Chrome/<Chrome Rev> Safari/<WebKit Rev>'
  128. ) ) ).to.be.true;
  129. } );
  130. it( 'returns false for non-Android UA strings', () => {
  131. expect( isAndroid( toLowerCase(
  132. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
  133. ) ) ).to.be.false;
  134. expect( isAndroid( toLowerCase(
  135. 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'
  136. ) ) ).to.be.false;
  137. expect( isAndroid( toLowerCase(
  138. 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
  139. ) ) ).to.be.false;
  140. } );
  141. /* eslint-enable max-len */
  142. } );
  143. } );