Committed by
Gerrit Code Review
GUI -- Added isMobile() predicate to FnService.
- Mocked out $window in fn-spec.js and table-spec.js Change-Id: Ibe59c742b3955809ea9a64dabc9a7e3779dd6199
Showing
3 changed files
with
73 additions
and
5 deletions
| ... | @@ -102,6 +102,13 @@ | ... | @@ -102,6 +102,13 @@ |
| 102 | }; | 102 | }; |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | + // Returns true if current browser determined to be a mobile device | ||
| 106 | + function isMobile() { | ||
| 107 | + var ua = $window.navigator.userAgent, | ||
| 108 | + patt = /iPhone|iPod|iPad|Silk|Android|BlackBerry|Opera Mini|IEMobile/; | ||
| 109 | + return patt.test(ua); | ||
| 110 | + } | ||
| 111 | + | ||
| 105 | // search through an array of objects, looking for the one with the | 112 | // search through an array of objects, looking for the one with the |
| 106 | // tagged property matching the given key. tag defaults to 'id'. | 113 | // tagged property matching the given key. tag defaults to 'id'. |
| 107 | // returns the index of the matching object, or -1 for no match. | 114 | // returns the index of the matching object, or -1 for no match. |
| ... | @@ -171,6 +178,7 @@ | ... | @@ -171,6 +178,7 @@ |
| 171 | areFunctions: areFunctions, | 178 | areFunctions: areFunctions, |
| 172 | areFunctionsNonStrict: areFunctionsNonStrict, | 179 | areFunctionsNonStrict: areFunctionsNonStrict, |
| 173 | windowSize: windowSize, | 180 | windowSize: windowSize, |
| 181 | + isMobile: isMobile, | ||
| 174 | find: find, | 182 | find: find, |
| 175 | inArray: inArray, | 183 | inArray: inArray, |
| 176 | removeFromArray: removeFromArray, | 184 | removeFromArray: removeFromArray, | ... | ... |
| ... | @@ -30,12 +30,23 @@ describe('factory: fw/util/fn.js', function() { | ... | @@ -30,12 +30,23 @@ describe('factory: fw/util/fn.js', function() { |
| 30 | 30 | ||
| 31 | beforeEach(module('onosUtil')); | 31 | beforeEach(module('onosUtil')); |
| 32 | 32 | ||
| 33 | + var mockWindow = { | ||
| 34 | + innerWidth: 400, | ||
| 35 | + innerHeight: 200, | ||
| 36 | + navigator: { | ||
| 37 | + userAgent: 'defaultUA' | ||
| 38 | + } | ||
| 39 | + }; | ||
| 40 | + | ||
| 41 | + beforeEach(function () { | ||
| 42 | + module(function ($provide) { | ||
| 43 | + $provide.value('$window', mockWindow); | ||
| 44 | + }); | ||
| 45 | + }); | ||
| 46 | + | ||
| 33 | beforeEach(inject(function (_$window_, FnService) { | 47 | beforeEach(inject(function (_$window_, FnService) { |
| 34 | $window = _$window_; | 48 | $window = _$window_; |
| 35 | fs = FnService; | 49 | fs = FnService; |
| 36 | - | ||
| 37 | - $window.innerWidth = 400; | ||
| 38 | - $window.innerHeight = 200; | ||
| 39 | })); | 50 | })); |
| 40 | 51 | ||
| 41 | // === Tests for isF() | 52 | // === Tests for isF() |
| ... | @@ -201,8 +212,8 @@ describe('factory: fw/util/fn.js', function() { | ... | @@ -201,8 +212,8 @@ describe('factory: fw/util/fn.js', function() { |
| 201 | it('should define api functions', function () { | 212 | it('should define api functions', function () { |
| 202 | expect(fs.areFunctions(fs, [ | 213 | expect(fs.areFunctions(fs, [ |
| 203 | 'isF', 'isA', 'isS', 'isO', 'contains', | 214 | 'isF', 'isA', 'isS', 'isO', 'contains', |
| 204 | - 'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'find', | 215 | + 'areFunctions', 'areFunctionsNonStrict', 'windowSize', 'isMobile', |
| 205 | - 'inArray', 'removeFromArray', 'isEmptyObject', 'cap' | 216 | + 'find', 'inArray', 'removeFromArray', 'isEmptyObject', 'cap' |
| 206 | ])).toBeTruthy(); | 217 | ])).toBeTruthy(); |
| 207 | }); | 218 | }); |
| 208 | 219 | ||
| ... | @@ -232,6 +243,39 @@ describe('factory: fw/util/fn.js', function() { | ... | @@ -232,6 +243,39 @@ describe('factory: fw/util/fn.js', function() { |
| 232 | expect(dim.height).toEqual(99); | 243 | expect(dim.height).toEqual(99); |
| 233 | }); | 244 | }); |
| 234 | 245 | ||
| 246 | + // === Tests for isMobile() | ||
| 247 | + var uaMap = { | ||
| 248 | + chrome: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) " + | ||
| 249 | + "AppleWebKit/537.36 (KHTML, like Gecko) " + | ||
| 250 | + "Chrome/41.0.2272.89 Safari/537.36", | ||
| 251 | + | ||
| 252 | + iPad: "Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) " + | ||
| 253 | + "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " + | ||
| 254 | + "Mobile/11A465 Safari/9537.53", | ||
| 255 | + | ||
| 256 | + iPhone: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) " + | ||
| 257 | + "AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 " + | ||
| 258 | + "Mobile/11A465 Safari/9537.53" | ||
| 259 | + }; | ||
| 260 | + | ||
| 261 | + function setUa(key) { | ||
| 262 | + var str = uaMap[key]; | ||
| 263 | + expect(str).toBeTruthy(); | ||
| 264 | + mockWindow.navigator.userAgent = str; | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + it('isMobile(): should be false for Chrome on Mac OS X', function () { | ||
| 268 | + setUa('chrome'); | ||
| 269 | + expect(fs.isMobile()).toBe(false); | ||
| 270 | + }); | ||
| 271 | + it('isMobile(): should be true for Safari on iPad', function () { | ||
| 272 | + setUa('iPad'); | ||
| 273 | + expect(fs.isMobile()).toBe(true); | ||
| 274 | + }); | ||
| 275 | + it('isMobile(): should be true for Safari on iPhone', function () { | ||
| 276 | + setUa('iPhone'); | ||
| 277 | + expect(fs.isMobile()).toBe(true); | ||
| 278 | + }); | ||
| 235 | 279 | ||
| 236 | // === Tests for find() | 280 | // === Tests for find() |
| 237 | var dataset = [ | 281 | var dataset = [ | ... | ... |
| ... | @@ -75,6 +75,22 @@ describe('factory: fw/widget/table.js', function () { | ... | @@ -75,6 +75,22 @@ describe('factory: fw/widget/table.js', function () { |
| 75 | 75 | ||
| 76 | beforeEach(module('onosWidget', 'onosUtil', 'onosSvg')); | 76 | beforeEach(module('onosWidget', 'onosUtil', 'onosSvg')); |
| 77 | 77 | ||
| 78 | + var mockWindow = { | ||
| 79 | + innerWidth: 400, | ||
| 80 | + innerHeight: 200, | ||
| 81 | + navigator: { | ||
| 82 | + userAgent: 'defaultUA' | ||
| 83 | + }, | ||
| 84 | + on: function () {}, | ||
| 85 | + addEventListener: function () {} | ||
| 86 | + }; | ||
| 87 | + | ||
| 88 | + beforeEach(function () { | ||
| 89 | + module(function ($provide) { | ||
| 90 | + $provide.value('$window', mockWindow); | ||
| 91 | + }); | ||
| 92 | + }); | ||
| 93 | + | ||
| 78 | beforeEach(inject(function (_$log_, _$compile_, _$rootScope_, | 94 | beforeEach(inject(function (_$log_, _$compile_, _$rootScope_, |
| 79 | FnService, IconService) { | 95 | FnService, IconService) { |
| 80 | $log = _$log_; | 96 | $log = _$log_; | ... | ... |
-
Please register or login to post a comment