blob: 20406dd40af273707b08d6d8ee6fccc92e31e5d2 [file] [log] [blame]
Simon Huntf8173382015-01-13 14:12:09 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Huntf8173382015-01-13 14:12:09 -08003 *
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 Huntf8173382015-01-13 14:12:09 -080019 */
20describe('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 Scandolo812aa5a2016-04-19 18:12:45 -070038 xit('should define api functions', function () {
Simon Huntf8173382015-01-13 14:12:09 -080039 expect(fs.areFunctions(gds, [
Simon Hunta7b6a6b2015-01-13 19:53:09 -080040 'clearCache', 'fetchTopoData', 'createPathGenerator'
Simon Huntf8173382015-01-13 14:12:09 -080041 ])).toBeTruthy();
42 });
43
44 it('should return null when no parameters given', function () {
Simon Hunta7b6a6b2015-01-13 19:53:09 -080045 promise = gds.fetchTopoData();
Simon Huntf8173382015-01-13 14:12:09 -080046 expect(promise).toBeNull();
47 });
48
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070049 xit('should augment the id of a bundled map', function () {
Simon Huntf8173382015-01-13 14:12:09 -080050 var id = '*foo';
Simon Hunta7b6a6b2015-01-13 19:53:09 -080051 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -080052 expect(promise.meta).toBeDefined();
53 expect(promise.meta.id).toBe(id);
Thomas Vachuskae95da772015-02-23 15:50:11 -080054 expect(promise.meta.url).toBe('data/map/foo.json');
Simon Huntf8173382015-01-13 14:12:09 -080055 });
56
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070057 xit('should treat an external id as the url itself', function () {
Simon Huntf8173382015-01-13 14:12:09 -080058 var id = 'some/path/to/foo';
Simon Hunta7b6a6b2015-01-13 19:53:09 -080059 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -080060 expect(promise.meta).toBeDefined();
61 expect(promise.meta.id).toBe(id);
62 expect(promise.meta.url).toBe(id + '.json');
63 });
64
65 it('should cache the returned objects', function () {
66 var id = 'foo';
Simon Hunta7b6a6b2015-01-13 19:53:09 -080067 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -080068 expect(promise).toBeDefined();
69 expect(promise.meta.wasCached).toBeFalsy();
70 expect(promise.tagged).toBeUndefined();
71
72 promise.tagged = 'I woz here';
73
Simon Hunta7b6a6b2015-01-13 19:53:09 -080074 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -080075 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 Hunta7b6a6b2015-01-13 19:53:09 -080082 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -080083 expect(promise.meta.wasCached).toBeFalsy();
84
Simon Hunta7b6a6b2015-01-13 19:53:09 -080085 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -080086 expect(promise.meta.wasCached).toBeTruthy();
87
88 gds.clearCache();
Simon Hunta7b6a6b2015-01-13 19:53:09 -080089 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -080090 expect(promise.meta.wasCached).toBeFalsy();
91 });
92
93
Matteo Scandolo812aa5a2016-04-19 18:12:45 -070094 xit('should log a warning if data fails to load', function () {
Simon Huntf8173382015-01-13 14:12:09 -080095 var id = 'foo';
96 $httpBackend.expectGET('foo.json').respond(404, 'Not found');
97 spyOn($log, 'warn');
98
Simon Hunta7b6a6b2015-01-13 19:53:09 -080099 promise = gds.fetchTopoData(id);
Simon Huntf8173382015-01-13 14:12:09 -0800100 $httpBackend.flush();
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800101 expect(promise.topodata).toBeUndefined();
Simon Huntf8173382015-01-13 14:12:09 -0800102 expect($log.warn)
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800103 .toHaveBeenCalledWith('Failed to retrieve map TopoJSON data: foo.json',
Simon Huntf8173382015-01-13 14:12:09 -0800104 404, 'Not found');
105 });
106
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800107 // --- 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 Scandolo812aa5a2016-04-19 18:12:45 -0700128 xit('should use default settings if none are supplied', function () {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800129 var gen = gds.createPathGenerator(simpleLineStringTopo());
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 Scandolo812aa5a2016-04-19 18:12:45 -0700146 xit('should create transformed geodata, and a path generator', function () {
Simon Hunta7b6a6b2015-01-13 19:53:09 -0800147 var gen = gds.createPathGenerator(simpleLineStringTopo());
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 Huntf8173382015-01-13 14:12:09 -0800159});