Bug 1210796 - Part 1: Add GetAnimationTypeForLonghand into nsIDOMWindowUtils to use in animationinspector of devtools. r=hiro draft
authorDaisuke Akatsuka <daisuke@mozilla-japan.org>
Tue, 18 Apr 2017 11:12:12 +0900
changeset 564113 475794a9ca872990485b4309749853547288feb7
parent 563802 a374c35469935a874fefe64d3e07003fc5bc8884
child 564114 2df4cd749dc9ee28e11c17e11d0883a5d50d88b5
push id54524
push userbmo:dakatsuka@mozilla.com
push dateTue, 18 Apr 2017 09:24:06 +0000
reviewershiro
bugs1210796
milestone55.0a1
Bug 1210796 - Part 1: Add GetAnimationTypeForLonghand into nsIDOMWindowUtils to use in animationinspector of devtools. r=hiro MozReview-Commit-ID: 5uxQr4hH0WP
dom/base/nsDOMWindowUtils.cpp
dom/base/test/test_domwindowutils.html
dom/interfaces/base/nsIDOMWindowUtils.idl
--- a/dom/base/nsDOMWindowUtils.cpp
+++ b/dom/base/nsDOMWindowUtils.cpp
@@ -2723,16 +2723,70 @@ nsDOMWindowUtils::ComputeAnimationDistan
   if (!StyleAnimationValue::ComputeDistance(property, v1, v2, styleContext,
                                             *aResult)) {
     return NS_ERROR_FAILURE;
   }
 
   return NS_OK;
 }
 
+NS_IMETHODIMP
+nsDOMWindowUtils::GetAnimationTypeForLonghand(const nsAString& aProperty,
+                                              nsAString& aResult)
+{
+  nsCSSPropertyID propertyID =
+    nsCSSProps::LookupProperty(aProperty, CSSEnabledState::eForAllContent);
+  if (propertyID == eCSSProperty_UNKNOWN) {
+    return NS_ERROR_INVALID_ARG;
+  }
+  if (nsCSSProps::IsShorthand(propertyID)) {
+    // The given property should be a longhand.
+    return NS_ERROR_INVALID_ARG;
+  }
+  switch (nsCSSProps::kAnimTypeTable[propertyID]) {
+    case eStyleAnimType_Custom:
+      aResult.AssignLiteral("custom");
+      break;
+    case eStyleAnimType_Coord:
+    case eStyleAnimType_Sides_Top:
+    case eStyleAnimType_Sides_Right:
+    case eStyleAnimType_Sides_Bottom:
+    case eStyleAnimType_Sides_Left:
+    case eStyleAnimType_Corner_TopLeft:
+    case eStyleAnimType_Corner_TopRight:
+    case eStyleAnimType_Corner_BottomRight:
+    case eStyleAnimType_Corner_BottomLeft:
+      aResult.AssignLiteral("coord");
+      break;
+    case eStyleAnimType_nscoord:
+      aResult.AssignLiteral("length");
+      break;
+    case eStyleAnimType_float:
+      aResult.AssignLiteral("float");
+      break;
+    case eStyleAnimType_Color:
+    case eStyleAnimType_ComplexColor:
+      aResult.AssignLiteral("color");
+      break;
+    case eStyleAnimType_PaintServer:
+      aResult.AssignLiteral("paintServer");
+      break;
+    case eStyleAnimType_Shadow:
+      aResult.AssignLiteral("shadow");
+      break;
+    case eStyleAnimType_Discrete:
+      aResult.AssignLiteral("discrete");
+      break;
+    case eStyleAnimType_None:
+      aResult.AssignLiteral("none");
+      break;
+  }
+  return NS_OK;
+}
+
 nsresult
 nsDOMWindowUtils::RenderDocument(const nsRect& aRect,
                                  uint32_t aFlags,
                                  nscolor aBackgroundColor,
                                  gfxContext* aThebesContext)
 {
     nsCOMPtr<nsIDocument> doc = GetDocument();
     NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
--- a/dom/base/test/test_domwindowutils.html
+++ b/dom/base/test/test_domwindowutils.html
@@ -50,19 +50,116 @@ function test_sendMouseEventOptionals() 
     SimpleTest.executeSoon(next);
   }, {once: true});
 
   // Check explicit value for optional args
   utils.sendMouseEvent("mouseup", x, y, button, clickCount, modifiers,
                        false, pressure, source, false);
 }
 
+function test_getAnimationType() {
+  [
+    {
+      propertyName: "align-content",
+      expectedType: "discrete"
+    },
+    {
+      propertyName: "animation-delay",
+      expectedType: "none"
+    },
+    {
+      propertyName: "background-color",
+      expectedType: "color"
+    },
+    {
+      propertyName: "background-size",
+      expectedType: "custom"
+    },
+    {
+      propertyName: "border-bottom-left-radius",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "border-bottom-right-radius",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "border-top-left-radius",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "border-top-right-radius",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "font-size",
+      expectedType: "length"
+    },
+    {
+      propertyName: "margin-top",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "margin-right",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "margin-bottom",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "margin-left",
+      expectedType: "coord"
+    },
+    {
+      propertyName: "opacity",
+      expectedType: "float"
+    },
+    {
+      propertyName: "stroke",
+      expectedType: "paintServer"
+    },
+    {
+      propertyName: "text-shadow",
+      expectedType: "shadow"
+    },
+    {
+      propertyName: "transform",
+      expectedType: "custom"
+    },
+    {
+      propertyName: "visibility",
+      expectedType: "discrete"
+    },
+    {
+      propertyName: "width",
+      expectedType: "coord"
+    }
+  ].forEach(({ propertyName, expectedType }) => {
+    is(utils.getAnimationTypeForLonghand(propertyName), expectedType,
+       `Animation type should be ${ expectedType }`);
+  });
+
+  SimpleTest.doesThrow(
+    () => utils.getAnimationTypeForLonghand("background"),
+    "NS_ERROR_ILLEGAL_VALUE",
+    "background property should throw");
+
+  SimpleTest.doesThrow(
+    () => utils.getAnimationTypeForLonghand("invalid"),
+    "NS_ERROR_ILLEGAL_VALUE",
+    "Invalid property should throw");
+
+  next();
+}
+
 var tests = [
   test_sendMouseEventDefaults,
-  test_sendMouseEventOptionals
+  test_sendMouseEventOptionals,
+  test_getAnimationType
 ];
 
 function next() {
   if (!tests.length) {
     SimpleTest.finish();
     return;
   }
 
--- a/dom/interfaces/base/nsIDOMWindowUtils.idl
+++ b/dom/interfaces/base/nsIDOMWindowUtils.idl
@@ -1553,16 +1553,23 @@ interface nsIDOMWindowUtils : nsISupport
    * property.
    */
   double computeAnimationDistance(in nsIDOMElement element,
                                   in AString property,
                                   in AString value1,
                                   in AString value2);
 
   /**
+   * Returns the animation type of the specified property (e.g. 'coord').
+   *
+   * @param aProperty A longhand CSS property (e.g. 'background-color').
+   */
+  AString getAnimationTypeForLonghand(in AString aProperty);
+
+  /**
    * Get the type of the currently focused html input, if any.
    */
   readonly attribute string focusedInputType;
 
   /**
    * Find the view ID for a given element. This is the reverse of
    * findElementWithViewId().
    */