browser.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const BrowserName = {
  2. Chrome: 'chrome',
  3. ChromeIos: 'chromeIos',
  4. Firefox: 'firefox',
  5. FirefoxIos: 'firefoxIos',
  6. InternetExplorer: 'ie',
  7. Edge: 'edge',
  8. Opera: 'opera',
  9. Safari: 'safari',
  10. };
  11. describe('BrowserService', function() {
  12. function testUserAgent(agent) {
  13. let $service;
  14. module('3ema.services');
  15. module(($provide) => {
  16. $provide.value('$window', {
  17. navigator: { userAgent: agent }
  18. });
  19. });
  20. inject(function(BrowserService) {
  21. $service = BrowserService;
  22. });
  23. return $service;
  24. }
  25. it('firefox', () => {
  26. const ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0';
  27. const service = testUserAgent(ua);
  28. const browser = service.getBrowser();
  29. expect(browser.name).toEqual(BrowserName.Firefox);
  30. expect(browser.version).toEqual(59);
  31. expect(browser.mobile).toBe(false);
  32. expect(browser.description()).toEqual('Firefox 59');
  33. });
  34. it('firefoxIosMobile', () => {
  35. const ua = 'Mozilla/5.0 (iPad; CPU OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) FxiOS/8.3b5826 Mobile/14A403 Safari/602.1.50';
  36. const service = testUserAgent(ua);
  37. const browser = service.getBrowser();
  38. expect(browser.name).toBe(BrowserName.FirefoxIos);
  39. expect(browser.version).toEqual(8);
  40. expect(browser.mobile).toBe(true);
  41. expect(browser.description()).toEqual('Firefox (iOS) 8 [Mobile]');
  42. });
  43. it('chrome', () => {
  44. const ua = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36';
  45. const service = testUserAgent(ua);
  46. const browser = service.getBrowser();
  47. expect(browser.name).toBe(BrowserName.Chrome);
  48. expect(browser.version).toEqual(65);
  49. expect(browser.mobile).toBe(false);
  50. expect(browser.description()).toEqual('Chrome 65');
  51. });
  52. it('chromeIosMobile', () => {
  53. const ua = 'Mozilla/5.0 (iPad; CPU OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/68.0.3440.83 Mobile/14A403 Safari/602.1';
  54. const service = testUserAgent(ua);
  55. const browser = service.getBrowser();
  56. expect(browser.name).toBe(BrowserName.ChromeIos);
  57. expect(browser.version).toEqual(68);
  58. expect(browser.mobile).toBe(true);
  59. expect(browser.description()).toEqual('Chrome (iOS) 68 [Mobile]');
  60. });
  61. it('ie9', () => {
  62. const ua = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)';
  63. const service = testUserAgent(ua);
  64. const browser = service.getBrowser();
  65. expect(browser.name).toBe(BrowserName.InternetExplorer);
  66. expect(browser.version).toEqual(9);
  67. expect(browser.mobile).toBe(false);
  68. expect(browser.description()).toEqual('Internet Explorer 9');
  69. });
  70. it('ie11', () => {
  71. const ua = 'Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
  72. const service = testUserAgent(ua);
  73. const browser = service.getBrowser();
  74. expect(browser.name).toBe(BrowserName.InternetExplorer);
  75. expect(browser.version).toEqual(11);
  76. expect(browser.mobile).toBe(false);
  77. expect(browser.description()).toEqual('Internet Explorer 11');
  78. });
  79. it('edge12', () => {
  80. 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';
  81. const service = testUserAgent(ua);
  82. const browser = service.getBrowser();
  83. expect(browser.name).toBe(BrowserName.Edge);
  84. expect(browser.version).toEqual(12);
  85. expect(browser.mobile).toBe(false);
  86. expect(browser.description()).toEqual('Edge 12');
  87. });
  88. it('opera', () => {
  89. 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';
  90. const service = testUserAgent(ua);
  91. const browser = service.getBrowser();
  92. expect(browser.name).toBe(BrowserName.Opera);
  93. expect(browser.version).toEqual(51);
  94. expect(browser.mobile).toBe(false);
  95. expect(browser.description()).toEqual('Opera 51');
  96. });
  97. it('safari7', () => {
  98. 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';
  99. const service = testUserAgent(ua);
  100. const browser = service.getBrowser();
  101. expect(browser.name).toBe(BrowserName.Safari);
  102. expect(browser.version).toEqual(7);
  103. expect(browser.mobile).toBe(false);
  104. expect(browser.description()).toEqual('Safari 7');
  105. });
  106. it('safari11', () => {
  107. 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';
  108. const service = testUserAgent(ua);
  109. const browser = service.getBrowser();
  110. expect(browser.name).toBe(BrowserName.Safari);
  111. expect(browser.version).toEqual(11);
  112. expect(browser.mobile).toBe(false);
  113. expect(browser.description()).toEqual('Safari 11');
  114. });
  115. it('safari10Mobile', () => {
  116. 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';
  117. const service = testUserAgent(ua);
  118. const browser = service.getBrowser();
  119. expect(browser.name).toBe(BrowserName.Safari);
  120. expect(browser.version).toEqual(10);
  121. expect(browser.mobile).toBe(true);
  122. expect(browser.description()).toEqual('Safari 10 [Mobile]');
  123. });
  124. });