summaryrefslogtreecommitdiff
path: root/test/style/paint_property.test.cpp
blob: fcca05f3bd18ef6a86c5ae6e5f9abd6fe567c373 (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
#include <mbgl/test/util.hpp>

#include <mbgl/style/paint_property.hpp>
#include <mbgl/renderer/transitioning_property.hpp>

using namespace mbgl;
using namespace mbgl::style;
using namespace std::literals::chrono_literals;

float evaluate(TransitioningProperty<PropertyValue<float>>& property, Duration delta = Duration::zero()) {
    ZoomHistory zoomHistory;
    zoomHistory.update(0, TimePoint::min() + delta);

    PropertyEvaluationParameters parameters {
        zoomHistory,
        TimePoint::min() + delta,
        Duration::zero()
    };

    PropertyEvaluator<float> evaluator {
        parameters,
        0.0f
    };

    return property.evaluate(evaluator, parameters.now);
}

PossiblyEvaluatedPropertyValue<float> evaluate(TransitioningProperty<DataDrivenPropertyValue<float>>& property, Duration delta = Duration::zero()) {
    ZoomHistory zoomHistory;
    zoomHistory.update(0, TimePoint::min() + delta);

    PropertyEvaluationParameters parameters {
        zoomHistory,
        TimePoint::min() + delta,
        Duration::zero()
    };
    
    DataDrivenPropertyEvaluator<float> evaluator {
        parameters,
        0.0f
    };
    
    return property.evaluate(evaluator, parameters.now);
}

TEST(TransitioningProperty, EvaluateDefaultValue) {
    TransitioningProperty<PropertyValue<float>> property;
    ASSERT_EQ(0.0f, evaluate(property));
}

TEST(TransitioningProperty, EvaluateUntransitionedConstant) {
    TransitioningProperty<PropertyValue<float>> property {
        PropertyValue<float>(1.0f),
        TransitioningProperty<PropertyValue<float>>(),
        TransitionOptions(),
        TimePoint::min()
    };

    ASSERT_EQ(1.0f, evaluate(property));
}

TEST(TransitioningProperty, EvaluateTransitionedConstantWithoutDelay) {
    TransitionOptions transition;
    transition.duration = { 1000ms };

    TransitioningProperty<PropertyValue<float>> t0 {
        PropertyValue<float>(0.0f),
        TransitioningProperty<PropertyValue<float>>(),
        TransitionOptions(),
        TimePoint::min()
    };

    TransitioningProperty<PropertyValue<float>> t1 {
        PropertyValue<float>(1.0f),
        t0,
        transition,
        TimePoint::min()
    };

    ASSERT_FLOAT_EQ(0.0f, evaluate(t1, 0ms));
    ASSERT_FLOAT_EQ(0.823099f, evaluate(t1, 500ms));
    ASSERT_FLOAT_EQ(1.0f, evaluate(t1, 1500ms));
}

TEST(TransitioningProperty, EvaluateTransitionedConstantWithDelay) {
    TransitionOptions transition;
    transition.delay = { 1000ms };
    transition.duration = { 1000ms };

    TransitioningProperty<PropertyValue<float>> t0 {
        PropertyValue<float>(0.0f),
        TransitioningProperty<PropertyValue<float>>(),
        TransitionOptions(),
        TimePoint::min()
    };

    TransitioningProperty<PropertyValue<float>> t1 {
        PropertyValue<float>(1.0f),
        t0,
        transition,
        TimePoint::min()
    };

    ASSERT_FLOAT_EQ(0.0f, evaluate(t1, 0ms));
    ASSERT_FLOAT_EQ(0.0f, evaluate(t1, 500ms));
    ASSERT_FLOAT_EQ(0.0f, evaluate(t1, 612ms));
    ASSERT_FLOAT_EQ(0.823099f, evaluate(t1, 1500ms));
    ASSERT_FLOAT_EQ(1.0f, evaluate(t1, 2500ms));
}

TEST(TransitioningProperty, EvaluateDataDrivenValue) {
    TransitionOptions transition;
    transition.delay = { 1000ms };
    transition.duration = { 1000ms };
    
    TransitioningProperty<DataDrivenPropertyValue<float>> t0 {
        DataDrivenPropertyValue<float>(0.0f),
        TransitioningProperty<DataDrivenPropertyValue<float>>(),
        TransitionOptions(),
        TimePoint::min()
    };
    
    SourceFunction<float> sourceFunction = {
        "property_name",
        IdentityStops<float>()
    };
    
    TransitioningProperty<DataDrivenPropertyValue<float>> t1 {
        DataDrivenPropertyValue<float>(sourceFunction),
        t0,
        transition,
        TimePoint::min()
    };
    
    ASSERT_TRUE(evaluate(t0, 0ms).isConstant());
    ASSERT_FALSE(evaluate(t1, 0ms).isConstant()) <<
        "A paint property transition to a data-driven evaluates immediately to the final value (see https://github.com/mapbox/mapbox-gl-native/issues/8237).";
}