summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLHeatmapStyleLayerTests.mm
blob: bde8a5b1c85b4fb83382828b88c1788ca0caa332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// This file is generated.
// Edit platform/darwin/scripts/generate-style-code.js, then run `make darwin-style-code`.

#import "MGLStyleLayerTests.h"
#import "../../darwin/src/NSDate+MGLAdditions.h"

#import "MGLStyleLayer_Private.h"

#include <mbgl/style/layers/heatmap_layer.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/expression/dsl.hpp>

@interface MGLHeatmapLayerTests : MGLStyleLayerTests
@end

@implementation MGLHeatmapLayerTests

+ (NSString *)layerType {
    return @"heatmap";
}

- (void)testPredicates {
    MGLPointFeature *feature = [[MGLPointFeature alloc] init];
    MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"sourceID" shape:feature options:nil];
    MGLHeatmapStyleLayer *layer = [[MGLHeatmapStyleLayer alloc] initWithIdentifier:@"layerID" source:source];

    XCTAssertNil(layer.sourceLayerIdentifier);
    layer.sourceLayerIdentifier = @"layerID";
    XCTAssertEqualObjects(layer.sourceLayerIdentifier, @"layerID");
    layer.sourceLayerIdentifier = nil;
    XCTAssertNil(layer.sourceLayerIdentifier);

    XCTAssertNil(layer.predicate);
    layer.predicate = [NSPredicate predicateWithFormat:@"$featureIdentifier = 1"];
    XCTAssertEqualObjects(layer.predicate,  [NSPredicate predicateWithFormat:@"$featureIdentifier = 1"]);
    layer.predicate = nil;
    XCTAssertNil(layer.predicate);
}

- (void)testProperties {
    MGLPointFeature *feature = [[MGLPointFeature alloc] init];
    MGLShapeSource *source = [[MGLShapeSource alloc] initWithIdentifier:@"sourceID" shape:feature options:nil];

    MGLHeatmapStyleLayer *layer = [[MGLHeatmapStyleLayer alloc] initWithIdentifier:@"layerID" source:source];
    XCTAssertNotEqual(layer.rawLayer, nullptr);
    XCTAssertEqual(layer.rawLayer->getType(), mbgl::style::LayerType::Heatmap);
    auto rawLayer = static_cast<mbgl::style::HeatmapLayer*>(layer.rawLayer);

    MGLTransition transitionTest = MGLTransitionMake(5, 4);


    // heatmap-intensity
    {
        XCTAssertTrue(rawLayer->getHeatmapIntensity().isUndefined(),
                      @"heatmap-intensity should be unset initially.");
        NSExpression *defaultExpression = layer.heatmapIntensity;

        NSExpression *constantExpression = [NSExpression expressionWithFormat:@"1"];
        layer.heatmapIntensity = constantExpression;
        mbgl::style::PropertyValue<float> propertyValue = { 1.0 };
        XCTAssertEqual(rawLayer->getHeatmapIntensity(), propertyValue,
                       @"Setting heatmapIntensity to a constant value expression should update heatmap-intensity.");
        XCTAssertEqualObjects(layer.heatmapIntensity, constantExpression,
                              @"heatmapIntensity should round-trip constant value expressions.");

        constantExpression = [NSExpression expressionWithFormat:@"1"];
        NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
        layer.heatmapIntensity = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                step(zoom(), literal(1.0), 18.0, literal(1.0))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapIntensity(), propertyValue,
                       @"Setting heatmapIntensity to a camera expression should update heatmap-intensity.");
        XCTAssertEqualObjects(layer.heatmapIntensity, functionExpression,
                              @"heatmapIntensity should round-trip camera expressions.");


        layer.heatmapIntensity = nil;
        XCTAssertTrue(rawLayer->getHeatmapIntensity().isUndefined(),
                      @"Unsetting heatmapIntensity should return heatmap-intensity to the default value.");
        XCTAssertEqualObjects(layer.heatmapIntensity, defaultExpression,
                              @"heatmapIntensity should return the default value after being unset.");

        functionExpression = [NSExpression expressionForKeyPath:@"bogus"];
        XCTAssertThrowsSpecificNamed(layer.heatmapIntensity = functionExpression, NSException, NSInvalidArgumentException, @"MGLHeatmapLayer should raise an exception if a camera-data expression is applied to a property that does not support key paths to feature attributes.");
        functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:(bogus, %@, %@)", constantExpression, @{@18: constantExpression}];
        functionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", @{@10: functionExpression}];
        XCTAssertThrowsSpecificNamed(layer.heatmapIntensity = functionExpression, NSException, NSInvalidArgumentException, @"MGLHeatmapLayer should raise an exception if a camera-data expression is applied to a property that does not support key paths to feature attributes.");
        // Transition property test
        layer.heatmapIntensityTransition = transitionTest;
        auto toptions = rawLayer->getHeatmapIntensityTransition();
        XCTAssert(toptions.delay && MGLTimeIntervalFromDuration(*toptions.delay) == transitionTest.delay);
        XCTAssert(toptions.duration && MGLTimeIntervalFromDuration(*toptions.duration) == transitionTest.duration);

        MGLTransition heatmapIntensityTransition = layer.heatmapIntensityTransition;
        XCTAssertEqual(heatmapIntensityTransition.delay, transitionTest.delay);
        XCTAssertEqual(heatmapIntensityTransition.duration, transitionTest.duration);
    }

    // heatmap-opacity
    {
        XCTAssertTrue(rawLayer->getHeatmapOpacity().isUndefined(),
                      @"heatmap-opacity should be unset initially.");
        NSExpression *defaultExpression = layer.heatmapOpacity;

        NSExpression *constantExpression = [NSExpression expressionWithFormat:@"1"];
        layer.heatmapOpacity = constantExpression;
        mbgl::style::PropertyValue<float> propertyValue = { 1.0 };
        XCTAssertEqual(rawLayer->getHeatmapOpacity(), propertyValue,
                       @"Setting heatmapOpacity to a constant value expression should update heatmap-opacity.");
        XCTAssertEqualObjects(layer.heatmapOpacity, constantExpression,
                              @"heatmapOpacity should round-trip constant value expressions.");

        constantExpression = [NSExpression expressionWithFormat:@"1"];
        NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
        layer.heatmapOpacity = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                step(zoom(), literal(1.0), 18.0, literal(1.0))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapOpacity(), propertyValue,
                       @"Setting heatmapOpacity to a camera expression should update heatmap-opacity.");
        XCTAssertEqualObjects(layer.heatmapOpacity, functionExpression,
                              @"heatmapOpacity should round-trip camera expressions.");


        layer.heatmapOpacity = nil;
        XCTAssertTrue(rawLayer->getHeatmapOpacity().isUndefined(),
                      @"Unsetting heatmapOpacity should return heatmap-opacity to the default value.");
        XCTAssertEqualObjects(layer.heatmapOpacity, defaultExpression,
                              @"heatmapOpacity should return the default value after being unset.");

        functionExpression = [NSExpression expressionForKeyPath:@"bogus"];
        XCTAssertThrowsSpecificNamed(layer.heatmapOpacity = functionExpression, NSException, NSInvalidArgumentException, @"MGLHeatmapLayer should raise an exception if a camera-data expression is applied to a property that does not support key paths to feature attributes.");
        functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:(bogus, %@, %@)", constantExpression, @{@18: constantExpression}];
        functionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", @{@10: functionExpression}];
        XCTAssertThrowsSpecificNamed(layer.heatmapOpacity = functionExpression, NSException, NSInvalidArgumentException, @"MGLHeatmapLayer should raise an exception if a camera-data expression is applied to a property that does not support key paths to feature attributes.");
        // Transition property test
        layer.heatmapOpacityTransition = transitionTest;
        auto toptions = rawLayer->getHeatmapOpacityTransition();
        XCTAssert(toptions.delay && MGLTimeIntervalFromDuration(*toptions.delay) == transitionTest.delay);
        XCTAssert(toptions.duration && MGLTimeIntervalFromDuration(*toptions.duration) == transitionTest.duration);

        MGLTransition heatmapOpacityTransition = layer.heatmapOpacityTransition;
        XCTAssertEqual(heatmapOpacityTransition.delay, transitionTest.delay);
        XCTAssertEqual(heatmapOpacityTransition.duration, transitionTest.duration);
    }

    // heatmap-radius
    {
        XCTAssertTrue(rawLayer->getHeatmapRadius().isUndefined(),
                      @"heatmap-radius should be unset initially.");
        NSExpression *defaultExpression = layer.heatmapRadius;

        NSExpression *constantExpression = [NSExpression expressionWithFormat:@"1"];
        layer.heatmapRadius = constantExpression;
        mbgl::style::PropertyValue<float> propertyValue = { 1.0 };
        XCTAssertEqual(rawLayer->getHeatmapRadius(), propertyValue,
                       @"Setting heatmapRadius to a constant value expression should update heatmap-radius.");
        XCTAssertEqualObjects(layer.heatmapRadius, constantExpression,
                              @"heatmapRadius should round-trip constant value expressions.");

        constantExpression = [NSExpression expressionWithFormat:@"1"];
        NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
        layer.heatmapRadius = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                step(zoom(), literal(1.0), 18.0, literal(1.0))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapRadius(), propertyValue,
                       @"Setting heatmapRadius to a camera expression should update heatmap-radius.");
        XCTAssertEqualObjects(layer.heatmapRadius, functionExpression,
                              @"heatmapRadius should round-trip camera expressions.");

        functionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:(keyName, 'linear', nil, %@)", @{@18: constantExpression}];
        layer.heatmapRadius = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                interpolate(linear(), number(get("keyName")), 18.0, literal(1.0))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapRadius(), propertyValue,
                       @"Setting heatmapRadius to a data expression should update heatmap-radius.");
        NSExpression *pedanticFunctionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:(CAST(keyName, 'NSNumber'), 'linear', nil, %@)", @{@18: constantExpression}];
        XCTAssertEqualObjects(layer.heatmapRadius, pedanticFunctionExpression,
                              @"heatmapRadius should round-trip data expressions.");

        functionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", @{@10: functionExpression}];
        layer.heatmapRadius = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                interpolate(linear(), zoom(), 10.0, interpolate(linear(), number(get("keyName")), 18.0, literal(1.0)))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapRadius(), propertyValue,
                       @"Setting heatmapRadius to a camera-data expression should update heatmap-radius.");
        pedanticFunctionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", @{@10: pedanticFunctionExpression}];
        XCTAssertEqualObjects(layer.heatmapRadius, pedanticFunctionExpression,
                              @"heatmapRadius should round-trip camera-data expressions.");

        layer.heatmapRadius = nil;
        XCTAssertTrue(rawLayer->getHeatmapRadius().isUndefined(),
                      @"Unsetting heatmapRadius should return heatmap-radius to the default value.");
        XCTAssertEqualObjects(layer.heatmapRadius, defaultExpression,
                              @"heatmapRadius should return the default value after being unset.");
        // Transition property test
        layer.heatmapRadiusTransition = transitionTest;
        auto toptions = rawLayer->getHeatmapRadiusTransition();
        XCTAssert(toptions.delay && MGLTimeIntervalFromDuration(*toptions.delay) == transitionTest.delay);
        XCTAssert(toptions.duration && MGLTimeIntervalFromDuration(*toptions.duration) == transitionTest.duration);

        MGLTransition heatmapRadiusTransition = layer.heatmapRadiusTransition;
        XCTAssertEqual(heatmapRadiusTransition.delay, transitionTest.delay);
        XCTAssertEqual(heatmapRadiusTransition.duration, transitionTest.duration);
    }

    // heatmap-weight
    {
        XCTAssertTrue(rawLayer->getHeatmapWeight().isUndefined(),
                      @"heatmap-weight should be unset initially.");
        NSExpression *defaultExpression = layer.heatmapWeight;

        NSExpression *constantExpression = [NSExpression expressionWithFormat:@"1"];
        layer.heatmapWeight = constantExpression;
        mbgl::style::PropertyValue<float> propertyValue = { 1.0 };
        XCTAssertEqual(rawLayer->getHeatmapWeight(), propertyValue,
                       @"Setting heatmapWeight to a constant value expression should update heatmap-weight.");
        XCTAssertEqualObjects(layer.heatmapWeight, constantExpression,
                              @"heatmapWeight should round-trip constant value expressions.");

        constantExpression = [NSExpression expressionWithFormat:@"1"];
        NSExpression *functionExpression = [NSExpression expressionWithFormat:@"mgl_step:from:stops:($zoomLevel, %@, %@)", constantExpression, @{@18: constantExpression}];
        layer.heatmapWeight = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                step(zoom(), literal(1.0), 18.0, literal(1.0))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapWeight(), propertyValue,
                       @"Setting heatmapWeight to a camera expression should update heatmap-weight.");
        XCTAssertEqualObjects(layer.heatmapWeight, functionExpression,
                              @"heatmapWeight should round-trip camera expressions.");

        functionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:(keyName, 'linear', nil, %@)", @{@18: constantExpression}];
        layer.heatmapWeight = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                interpolate(linear(), number(get("keyName")), 18.0, literal(1.0))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapWeight(), propertyValue,
                       @"Setting heatmapWeight to a data expression should update heatmap-weight.");
        NSExpression *pedanticFunctionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:(CAST(keyName, 'NSNumber'), 'linear', nil, %@)", @{@18: constantExpression}];
        XCTAssertEqualObjects(layer.heatmapWeight, pedanticFunctionExpression,
                              @"heatmapWeight should round-trip data expressions.");

        functionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", @{@10: functionExpression}];
        layer.heatmapWeight = functionExpression;

        {
            using namespace mbgl::style::expression::dsl;
            propertyValue = mbgl::style::PropertyExpression<float>(
                interpolate(linear(), zoom(), 10.0, interpolate(linear(), number(get("keyName")), 18.0, literal(1.0)))
            );
        }

        XCTAssertEqual(rawLayer->getHeatmapWeight(), propertyValue,
                       @"Setting heatmapWeight to a camera-data expression should update heatmap-weight.");
        pedanticFunctionExpression = [NSExpression expressionWithFormat:@"mgl_interpolate:withCurveType:parameters:stops:($zoomLevel, 'linear', nil, %@)", @{@10: pedanticFunctionExpression}];
        XCTAssertEqualObjects(layer.heatmapWeight, pedanticFunctionExpression,
                              @"heatmapWeight should round-trip camera-data expressions.");

        layer.heatmapWeight = nil;
        XCTAssertTrue(rawLayer->getHeatmapWeight().isUndefined(),
                      @"Unsetting heatmapWeight should return heatmap-weight to the default value.");
        XCTAssertEqualObjects(layer.heatmapWeight, defaultExpression,
                              @"heatmapWeight should return the default value after being unset.");
    }
}

- (void)testPropertyNames {
    [self testPropertyName:@"heatmap-intensity" isBoolean:NO];
    [self testPropertyName:@"heatmap-opacity" isBoolean:NO];
    [self testPropertyName:@"heatmap-radius" isBoolean:NO];
    [self testPropertyName:@"heatmap-weight" isBoolean:NO];
}

@end