browser.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. describe('BrowserService', function() {
  2. function testUserAgent(agent) {
  3. let $service;
  4. module('3ema.services');
  5. module(function($provide) {
  6. $provide.value('$window', {
  7. navigator: { userAgent: agent }
  8. });
  9. });
  10. inject(function(BrowserService) {
  11. $service = BrowserService;
  12. });
  13. return $service;
  14. }
  15. it('firefox', () => {
  16. const ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0';
  17. const service = testUserAgent(ua);
  18. const browser = service.getBrowser();
  19. expect(browser.chrome).toBe(false);
  20. expect(browser.firefox).toBe(true);
  21. expect(browser.ie).toBe(false);
  22. expect(browser.edge).toBe(false);
  23. expect(browser.opera).toBe(false);
  24. expect(browser.safari).toBe(false);
  25. expect(browser.name).toEqual('firefox');
  26. expect(browser.version).toEqual(59);
  27. expect(browser.mobile).toBe(false);
  28. expect(browser.textInfo).toEqual('Firefox 59');
  29. });
  30. it('chrome', () => {
  31. const ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36';
  32. const service = testUserAgent(ua);
  33. const browser = service.getBrowser();
  34. expect(browser.chrome).toBe(true);
  35. expect(browser.firefox).toBe(false);
  36. expect(browser.ie).toBe(false);
  37. expect(browser.edge).toBe(false);
  38. expect(browser.opera).toBe(false);
  39. expect(browser.safari).toBe(false);
  40. expect(browser.name).toEqual('chrome');
  41. expect(browser.version).toEqual(65);
  42. expect(browser.mobile).toBe(false);
  43. expect(browser.textInfo).toEqual('Chrome 65');
  44. });
  45. it('ie9', () => {
  46. const ua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)';
  47. const service = testUserAgent(ua);
  48. const browser = service.getBrowser();
  49. expect(browser.chrome).toBe(false);
  50. expect(browser.firefox).toBe(false);
  51. expect(browser.ie).toBe(true);
  52. expect(browser.edge).toBe(false);
  53. expect(browser.opera).toBe(false);
  54. expect(browser.safari).toBe(false);
  55. expect(browser.name).toEqual('ie');
  56. expect(browser.version).toEqual(9);
  57. expect(browser.mobile).toBe(false);
  58. expect(browser.textInfo).toEqual('Internet Explorer 9');
  59. });
  60. it('ie11', () => {
  61. const ua = 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
  62. const service = testUserAgent(ua);
  63. const browser = service.getBrowser();
  64. expect(browser.chrome).toBe(false);
  65. expect(browser.firefox).toBe(false);
  66. expect(browser.ie).toBe(true);
  67. expect(browser.edge).toBe(false);
  68. expect(browser.opera).toBe(false);
  69. expect(browser.safari).toBe(false);
  70. expect(browser.name).toEqual('ie');
  71. expect(browser.version).toEqual(11);
  72. expect(browser.mobile).toBe(false);
  73. expect(browser.textInfo).toEqual('Internet Explorer 11');
  74. });
  75. it('edge12', () => {
  76. const ua = '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';
  77. const service = testUserAgent(ua);
  78. const browser = service.getBrowser();
  79. expect(browser.chrome).toBe(false);
  80. expect(browser.firefox).toBe(false);
  81. expect(browser.ie).toBe(false);
  82. expect(browser.edge).toBe(true);
  83. expect(browser.opera).toBe(false);
  84. expect(browser.safari).toBe(false);
  85. expect(browser.name).toEqual('edge');
  86. expect(browser.version).toEqual(12);
  87. expect(browser.mobile).toBe(false);
  88. expect(browser.textInfo).toEqual('Edge 12');
  89. });
  90. it('opera', () => {
  91. const ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36 OPR/51.0.2830.55';
  92. const service = testUserAgent(ua);
  93. const browser = service.getBrowser();
  94. expect(browser.chrome).toBe(false);
  95. expect(browser.firefox).toBe(false);
  96. expect(browser.ie).toBe(false);
  97. expect(browser.edge).toBe(false);
  98. expect(browser.opera).toBe(true);
  99. expect(browser.safari).toBe(false);
  100. expect(browser.name).toEqual('opera');
  101. expect(browser.version).toEqual(51);
  102. expect(browser.mobile).toBe(false);
  103. expect(browser.textInfo).toEqual('Opera 51');
  104. });
  105. it('safari7', () => {
  106. const ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A';
  107. const service = testUserAgent(ua);
  108. const browser = service.getBrowser();
  109. expect(browser.chrome).toBe(false);
  110. expect(browser.firefox).toBe(false);
  111. expect(browser.ie).toBe(false);
  112. expect(browser.edge).toBe(false);
  113. expect(browser.opera).toBe(false);
  114. expect(browser.safari).toBe(true);
  115. expect(browser.name).toEqual('safari');
  116. expect(browser.version).toEqual(7);
  117. expect(browser.mobile).toBe(false);
  118. expect(browser.textInfo).toEqual('Safari 7');
  119. });
  120. it('safari11', () => {
  121. const ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6';
  122. const service = testUserAgent(ua);
  123. const browser = service.getBrowser();
  124. expect(browser.chrome).toBe(false);
  125. expect(browser.firefox).toBe(false);
  126. expect(browser.ie).toBe(false);
  127. expect(browser.edge).toBe(false);
  128. expect(browser.opera).toBe(false);
  129. expect(browser.safari).toBe(true);
  130. expect(browser.name).toEqual('safari');
  131. expect(browser.version).toEqual(11);
  132. expect(browser.mobile).toBe(false);
  133. expect(browser.textInfo).toEqual('Safari 11');
  134. });
  135. it('safari10Mobile', () => {
  136. const ua = 'Mozilla/5.0 (iPad; CPU OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A403 Safari/602.1';
  137. const service = testUserAgent(ua);
  138. const browser = service.getBrowser();
  139. expect(browser.chrome).toBe(false);
  140. expect(browser.firefox).toBe(false);
  141. expect(browser.ie).toBe(false);
  142. expect(browser.edge).toBe(false);
  143. expect(browser.opera).toBe(false);
  144. expect(browser.safari).toBe(true);
  145. expect(browser.name).toEqual('safari');
  146. expect(browser.version).toEqual(10);
  147. expect(browser.mobile).toBe(true);
  148. expect(browser.textInfo).toEqual('Safari 10 Mobile');
  149. });
  150. });