Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- SVG -- GeoData Service - Unit Tests |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 19 | */ |
| 20 | describe('factory: fw/svg/geodata.js', function() { |
| 21 | var $log, $httpBackend, fs, gds, promise; |
| 22 | |
| 23 | beforeEach(module('onosUtil', 'onosSvg')); |
| 24 | |
| 25 | beforeEach(inject(function (_$log_, _$httpBackend_, FnService, GeoDataService) { |
| 26 | $log = _$log_; |
| 27 | $httpBackend = _$httpBackend_; |
| 28 | fs = FnService; |
| 29 | gds = GeoDataService; |
| 30 | gds.clearCache(); |
| 31 | })); |
| 32 | |
| 33 | |
| 34 | it('should define GeoDataService', function () { |
| 35 | expect(gds).toBeDefined(); |
| 36 | }); |
| 37 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 38 | it('should define api functions', function () { |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 39 | expect(fs.areFunctions(gds, [ |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 40 | 'clearCache', 'fetchTopoData', 'createPathGenerator', 'rescaleProjection' |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 41 | ])).toBeTruthy(); |
| 42 | }); |
| 43 | |
| 44 | it('should return null when no parameters given', function () { |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 45 | promise = gds.fetchTopoData(); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 46 | expect(promise).toBeNull(); |
| 47 | }); |
| 48 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 49 | it('should augment the id of a bundled map', function () { |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 50 | var id = '*foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 51 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 52 | expect(promise.meta).toBeDefined(); |
| 53 | expect(promise.meta.id).toBe(id); |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 54 | expect(promise.meta.url).toBe('data/map/foo.topojson'); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 55 | }); |
| 56 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 57 | it('should treat an external id as the url itself', function () { |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 58 | var id = 'some/path/to/foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 59 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 60 | expect(promise.meta).toBeDefined(); |
| 61 | expect(promise.meta.id).toBe(id); |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 62 | expect(promise.meta.url).toBe(id + '.topojson'); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 63 | }); |
| 64 | |
| 65 | it('should cache the returned objects', function () { |
| 66 | var id = 'foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 67 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 68 | expect(promise).toBeDefined(); |
| 69 | expect(promise.meta.wasCached).toBeFalsy(); |
| 70 | expect(promise.tagged).toBeUndefined(); |
| 71 | |
| 72 | promise.tagged = 'I woz here'; |
| 73 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 74 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 75 | expect(promise).toBeDefined(); |
| 76 | expect(promise.meta.wasCached).toBeTruthy(); |
| 77 | expect(promise.tagged).toEqual('I woz here'); |
| 78 | }); |
| 79 | |
| 80 | it('should clear the cache when asked', function () { |
| 81 | var id = 'foo'; |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 82 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 83 | expect(promise.meta.wasCached).toBeFalsy(); |
| 84 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 85 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 86 | expect(promise.meta.wasCached).toBeTruthy(); |
| 87 | |
| 88 | gds.clearCache(); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 89 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 90 | expect(promise.meta.wasCached).toBeFalsy(); |
| 91 | }); |
| 92 | |
| 93 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 94 | it('should log a warning if data fails to load', function () { |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 95 | var id = 'foo'; |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 96 | $httpBackend.expectGET('foo.topojson').respond(404, 'Not found'); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 97 | spyOn($log, 'warn'); |
| 98 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 99 | promise = gds.fetchTopoData(id); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 100 | $httpBackend.flush(); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 101 | expect(promise.topodata).toBeUndefined(); |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 102 | expect($log.warn) |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 103 | .toHaveBeenCalledWith('Failed to retrieve map TopoJSON data: foo.topojson', |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 104 | 404, 'Not found'); |
| 105 | }); |
| 106 | |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 107 | // --- path generator tests |
| 108 | |
| 109 | function simpleTopology(object) { |
| 110 | return { |
| 111 | type: "Topology", |
| 112 | transform: {scale: [1, 1], translate: [0, 0]}, |
| 113 | objects: {states: object}, |
| 114 | arcs: [ |
| 115 | [[0, 0], [1, 0], [0, 1], [-1, 0], [0, -1]], |
| 116 | [[0, 0], [1, 0], [0, 1]], |
| 117 | [[1, 1], [-1, 0], [0, -1]], |
| 118 | [[1, 1]], |
| 119 | [[0, 0]] |
| 120 | ] |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | function simpleLineStringTopo() { |
| 125 | return simpleTopology({type: "LineString", arcs: [1, 2]}); |
| 126 | } |
| 127 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 128 | it('should use default settings if none are supplied', function () { |
| 129 | var gen = gds.createPathGenerator(simpleLineStringTopo(), {adjustScale: true}); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 130 | expect(gen.settings.objectTag).toBe('states'); |
| 131 | expect(gen.settings.logicalSize).toBe(1000); |
| 132 | expect(gen.settings.mapFillScale).toBe(.95); |
| 133 | // best we can do for now is test that projection is a function ... |
| 134 | expect(fs.isF(gen.settings.projection)).toBeTruthy(); |
| 135 | }); |
| 136 | |
| 137 | it('should allow us to override default settings', function () { |
| 138 | var gen = gds.createPathGenerator(simpleLineStringTopo(), { |
| 139 | mapFillScale: .80 |
| 140 | }); |
| 141 | expect(gen.settings.objectTag).toBe('states'); |
| 142 | expect(gen.settings.logicalSize).toBe(1000); |
| 143 | expect(gen.settings.mapFillScale).toBe(.80); |
| 144 | }); |
| 145 | |
Matteo Scandolo | 231c754 | 2016-05-20 11:13:11 -0700 | [diff] [blame] | 146 | it('should create transformed geodata, and a path generator', function () { |
| 147 | var gen = gds.createPathGenerator(simpleLineStringTopo(), {adjustScale: true}); |
Simon Hunt | a7b6a6b | 2015-01-13 19:53:09 -0800 | [diff] [blame] | 148 | expect(fs.isO(gen.settings)).toBeTruthy(); |
| 149 | expect(fs.isO(gen.geodata)).toBeTruthy(); |
| 150 | expect(fs.isF(gen.pathgen)).toBeTruthy(); |
| 151 | }); |
| 152 | // NOTE: we probably should have more unit tests that assert stuff about |
| 153 | // the transformed data (geo data) -- though perhaps we can rely on |
| 154 | // the unit testing of TopoJSON? See... |
| 155 | // https://github.com/mbostock/topojson/blob/master/test/feature-test.js |
| 156 | // and, what about the path generator?, and the computed bounds? |
| 157 | // In summary, more work should be done here.. |
| 158 | |
Simon Hunt | f817338 | 2015-01-13 14:12:09 -0800 | [diff] [blame] | 159 | }); |