browser.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.textInfo).toEqual('Firefox 59');
  28. });
  29. it('chrome', () => {
  30. const ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36';
  31. const service = testUserAgent(ua);
  32. const browser = service.getBrowser();
  33. expect(browser.chrome).toBe(true);
  34. expect(browser.firefox).toBe(false);
  35. expect(browser.ie).toBe(false);
  36. expect(browser.edge).toBe(false);
  37. expect(browser.opera).toBe(false);
  38. expect(browser.safari).toBe(false);
  39. expect(browser.name).toEqual('chrome');
  40. expect(browser.version).toEqual(65);
  41. expect(browser.textInfo).toEqual('Chrome 65');
  42. });
  43. it('ie9', () => {
  44. const ua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)';
  45. const service = testUserAgent(ua);
  46. const browser = service.getBrowser();
  47. expect(browser.chrome).toBe(false);
  48. expect(browser.firefox).toBe(false);
  49. expect(browser.ie).toBe(true);
  50. expect(browser.edge).toBe(false);
  51. expect(browser.opera).toBe(false);
  52. expect(browser.safari).toBe(false);
  53. expect(browser.name).toEqual('ie');
  54. expect(browser.version).toEqual(9);
  55. expect(browser.textInfo).toEqual('Internet Explorer 9');
  56. });
  57. it('ie11', () => {
  58. const ua = 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
  59. const service = testUserAgent(ua);
  60. const browser = service.getBrowser();
  61. expect(browser.chrome).toBe(false);
  62. expect(browser.firefox).toBe(false);
  63. expect(browser.ie).toBe(true);
  64. expect(browser.edge).toBe(false);
  65. expect(browser.opera).toBe(false);
  66. expect(browser.safari).toBe(false);
  67. expect(browser.name).toEqual('ie');
  68. expect(browser.version).toEqual(11);
  69. expect(browser.textInfo).toEqual('Internet Explorer 11');
  70. });
  71. it('edge12', () => {
  72. 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';
  73. const service = testUserAgent(ua);
  74. const browser = service.getBrowser();
  75. expect(browser.chrome).toBe(false);
  76. expect(browser.firefox).toBe(false);
  77. expect(browser.ie).toBe(false);
  78. expect(browser.edge).toBe(true);
  79. expect(browser.opera).toBe(false);
  80. expect(browser.safari).toBe(false);
  81. expect(browser.name).toEqual('edge');
  82. expect(browser.version).toEqual(12);
  83. expect(browser.textInfo).toEqual('Edge 12');
  84. });
  85. it('opera', () => {
  86. 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';
  87. const service = testUserAgent(ua);
  88. const browser = service.getBrowser();
  89. expect(browser.chrome).toBe(false);
  90. expect(browser.firefox).toBe(false);
  91. expect(browser.ie).toBe(false);
  92. expect(browser.edge).toBe(false);
  93. expect(browser.opera).toBe(true);
  94. expect(browser.safari).toBe(false);
  95. expect(browser.name).toEqual('opera');
  96. expect(browser.version).toEqual(51);
  97. expect(browser.textInfo).toEqual('Opera 51');
  98. });
  99. it('safari7', () => {
  100. 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';
  101. const service = testUserAgent(ua);
  102. const browser = service.getBrowser();
  103. expect(browser.chrome).toBe(false);
  104. expect(browser.firefox).toBe(false);
  105. expect(browser.ie).toBe(false);
  106. expect(browser.edge).toBe(false);
  107. expect(browser.opera).toBe(false);
  108. expect(browser.safari).toBe(true);
  109. expect(browser.name).toEqual('safari');
  110. expect(browser.version).toEqual(7);
  111. expect(browser.textInfo).toEqual('Safari 7');
  112. });
  113. it('safari11', () => {
  114. 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';
  115. const service = testUserAgent(ua);
  116. const browser = service.getBrowser();
  117. expect(browser.chrome).toBe(false);
  118. expect(browser.firefox).toBe(false);
  119. expect(browser.ie).toBe(false);
  120. expect(browser.edge).toBe(false);
  121. expect(browser.opera).toBe(false);
  122. expect(browser.safari).toBe(true);
  123. expect(browser.name).toEqual('safari');
  124. expect(browser.version).toEqual(11);
  125. expect(browser.textInfo).toEqual('Safari 11');
  126. });
  127. });