summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/animation_test_helper.cc
blob: a7bf2c2799063aaeae45e291cf3d2f597118480e (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/animation/animation_test_helper.h"

#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/core/animation/css_interpolation_environment.h"
#include "third_party/blink/renderer/core/animation/css_interpolation_types_map.h"
#include "third_party/blink/renderer/core/animation/invalidatable_interpolation.h"
#include "third_party/blink/renderer/core/css/css_pending_interpolation_value.h"
#include "third_party/blink/renderer/core/css/resolver/style_cascade.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/style/computed_style.h"

namespace blink {

namespace {

class TestAnimator : public StyleCascade::Animator {
  STACK_ALLOCATED();

 public:
  TestAnimator(StyleResolverState& state,
               StyleCascade& cascade,
               CSSInterpolationTypesMap& map,
               const ActiveInterpolations& interpolations)
      : state_(state),
        cascade_(cascade),
        map_(map),
        interpolations_(interpolations) {}

  void Apply(const CSSProperty&,
             const cssvalue::CSSPendingInterpolationValue& value,
             StyleCascade::Resolver& resolver) override {
    // Ignore CSSProperty here. We assume this function is only called once
    // for each invocation of EnsureInterpolatedValueCached.
    CSSInterpolationEnvironment environment(map_, state_, &cascade_, &resolver);
    InvalidatableInterpolation::ApplyStack(interpolations_, environment);
  }

 private:
  StyleResolverState& state_;
  StyleCascade& cascade_;
  CSSInterpolationTypesMap& map_;
  const ActiveInterpolations& interpolations_;
};

}  // namespace

void SetV8ObjectPropertyAsString(v8::Isolate* isolate,
                                 v8::Local<v8::Object> object,
                                 const StringView& name,
                                 const StringView& value) {
  object
      ->Set(isolate->GetCurrentContext(), V8String(isolate, name),
            V8String(isolate, value))
      .ToChecked();
}

void SetV8ObjectPropertyAsNumber(v8::Isolate* isolate,
                                 v8::Local<v8::Object> object,
                                 const StringView& name,
                                 double value) {
  object
      ->Set(isolate->GetCurrentContext(), V8String(isolate, name),
            v8::Number::New(isolate, value))
      .ToChecked();
}

void EnsureInterpolatedValueCached(const ActiveInterpolations& interpolations,
                                   Document& document,
                                   Element* element) {
  // TODO(smcgruer): We should be able to use a saner API approach like
  // document.EnsureStyleResolver().StyleForElement(element). However that would
  // require our callers to propertly register every animation they pass in
  // here, which the current tests do not do.
  auto style = ComputedStyle::Create();
  StyleResolverState state(document, *element, style.get(), style.get());
  state.SetStyle(style);
  CSSInterpolationTypesMap map(state.GetDocument().GetPropertyRegistry(),
                               state.GetDocument());
  if (RuntimeEnabledFeatures::CSSCascadeEnabled()) {
    // We must apply the animation effects via StyleCascade when the cascade
    // is enabled.
    StyleCascade cascade(state);
    auto type = cssvalue::CSSPendingInterpolationValue::Type::kCSSProperty;
    auto* pending = cssvalue::CSSPendingInterpolationValue::Create(type);
    auto origin = StyleCascade::Origin::kAuthor;
    cascade.Add(*CSSPropertyName::From("--unused"), pending, origin);

    TestAnimator animator(state, cascade, map, interpolations);
    cascade.Apply(animator);
  } else {
    CSSInterpolationEnvironment environment(map, state, nullptr);
    InvalidatableInterpolation::ApplyStack(interpolations, environment);
  }
}

}  // namespace blink