/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.facebook.litho.testing;

import android.app.Activity;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.R;
import com.facebook.litho.StateHandler;
import com.facebook.litho.testing.assertj.LithoRepresentation;
import org.assertj.core.api.Assertions;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.robolectric.Robolectric;

public class ComponentsRule implements TestRule {

  private ComponentContext mContext;

  @Override
  public Statement apply(final Statement base, final Description description) {
    return new Statement() {
      @Override
      public void evaluate() throws Throwable {
        final Activity activity = Robolectric.buildActivity(Activity.class).create().get();
        mContext = new ComponentContext(activity, new StateHandler());
        setComponentStyleableAttributes();

        Assertions.useRepresentation(new LithoRepresentation(mContext));

        try {
          base.evaluate();
        } finally {
          Assertions.useDefaultRepresentation();
        }
      }
    };
  }

  /** Get a Component Context for this test instance. */
  public ComponentContext getContext() {
    return mContext;
  }

  /**
   * TODO natthu: this is just a hack around a BUCK issue whereby BUCK only generates random array
   * values. Component tests require the correct values for the ComponentLayout styleable array in
   * order to bind props to the layout. The array that we define here needs to be kept up-to-date
   * with the attrs defined in litho-core/src/main/res/*attrs.xml files.
   */
  static void setComponentStyleableAttributes() {

    System.arraycopy(
        new int[] {
          R.attr.flex_direction,
          R.attr.flex_layoutDirection,
          R.attr.flex_justifyContent,
          R.attr.flex_alignItems,
          R.attr.flex_alignSelf,
          R.attr.flex_positionType,
          R.attr.flex_wrap,
          R.attr.flex_left,
          R.attr.flex_top,
          R.attr.flex_right,
          R.attr.flex_bottom,
          R.attr.flex,
          android.R.attr.layout_width,
          android.R.attr.layout_height,
          android.R.attr.padding,
          android.R.attr.paddingLeft,
          android.R.attr.paddingTop,
          android.R.attr.paddingRight,
          android.R.attr.paddingBottom,
          android.R.attr.paddingStart,
          android.R.attr.paddingEnd,
          android.R.attr.layout_margin,
          android.R.attr.layout_marginLeft,
          android.R.attr.layout_marginTop,
          android.R.attr.layout_marginRight,
          android.R.attr.layout_marginBottom,
          android.R.attr.layout_marginStart,
          android.R.attr.layout_marginEnd,
          android.R.attr.contentDescription,
          android.R.attr.background,
          android.R.attr.foreground,
          android.R.attr.minHeight,
          android.R.attr.minWidth,
          android.R.attr.importantForAccessibility,
          android.R.attr.duplicateParentState,
        },
        /* srcPos */ 0,
        R.styleable.ComponentLayout,
        /* destPos */ 0,
        R.styleable.ComponentLayout.length);

    System.arraycopy(
        new int[] {
          android.R.attr.minLines,
          android.R.attr.maxLines,
          android.R.attr.textColor,
          android.R.attr.textColorLink,
          android.R.attr.textColorHighlight,
          android.R.attr.textSize,
          android.R.attr.lineSpacingExtra,
          android.R.attr.lineSpacingMultiplier,
          android.R.attr.ellipsize,
          android.R.attr.textAlignment,
          android.R.attr.textStyle,
          android.R.attr.text,
          android.R.attr.includeFontPadding,
          android.R.attr.singleLine,
          android.R.attr.shadowColor,
          android.R.attr.shadowDx,
          android.R.attr.shadowDy,
          android.R.attr.shadowRadius,
          android.R.attr.gravity,
          android.R.attr.minEms,
          android.R.attr.maxEms,
          android.R.attr.minWidth,
          android.R.attr.maxWidth,
          android.R.attr.inputType,
          android.R.attr.fontFamily,
          android.R.attr.imeOptions,
          android.R.attr.breakStrategy,
          android.R.attr.hyphenationFrequency,
          android.R.attr.justificationMode,
        },
        /* srcPos */ 0,
        R.styleable.Text,
        /* destPos */ 0,
        R.styleable.Text.length);

    System.arraycopy(
        new int[] {
          android.R.attr.src, android.R.attr.scaleType,
        },
        /* srcPos */ 0,
        R.styleable.Image,
        /* destPos */ 0,
        R.styleable.Image.length);
  }
}

@@ -725,37 +725,8 @@
 xt;%0A
-import com.facebook.litho.R;%0A
 impo
@@ -761,16 +761,16 @@
 andler;%0A
+
 import c
@@ -1453,51 +1453,8 @@
 ());
-%0A        setComponentStyleableAttributes();
 %0A%0A  
@@ -1759,24 +1759,24 @@
 Context() %7B%0A
+
     return m
@@ -1791,3428 +1791,7 @@
 %0A  %7D
-%0A%0A  /**%0A   * TODO natthu: this is just a hack around a BUCK issue whereby BUCK only generates random array%0A   * values. Component tests require the correct values for the ComponentLayout styleable array in%0A   * order to bind props to the layout. The array that we define here needs to be kept up-to-date%0A   * with the attrs defined in litho-core/src/main/res/*attrs.xml files.%0A   */%0A  static void setComponentStyleableAttributes() %7B%0A%0A    System.arraycopy(%0A        new int%5B%5D %7B%0A          R.attr.flex_direction,%0A          R.attr.flex_layoutDirection,%0A          R.attr.flex_justifyContent,%0A          R.attr.flex_alignItems,%0A          R.attr.flex_alignSelf,%0A          R.attr.flex_positionType,%0A          R.attr.flex_wrap,%0A          R.attr.flex_left,%0A          R.attr.flex_top,%0A          R.attr.flex_right,%0A          R.attr.flex_bottom,%0A          R.attr.flex,%0A          android.R.attr.layout_width,%0A          android.R.attr.layout_height,%0A          android.R.attr.padding,%0A          android.R.attr.paddingLeft,%0A          android.R.attr.paddingTop,%0A          android.R.attr.paddingRight,%0A          android.R.attr.paddingBottom,%0A          android.R.attr.paddingStart,%0A          android.R.attr.paddingEnd,%0A          android.R.attr.layout_margin,%0A          android.R.attr.layout_marginLeft,%0A          android.R.attr.layout_marginTop,%0A          android.R.attr.layout_marginRight,%0A          android.R.attr.layout_marginBottom,%0A          android.R.attr.layout_marginStart,%0A          android.R.attr.layout_marginEnd,%0A          android.R.attr.contentDescription,%0A          android.R.attr.background,%0A          android.R.attr.foreground,%0A          android.R.attr.minHeight,%0A          android.R.attr.minWidth,%0A          android.R.attr.importantForAccessibility,%0A          android.R.attr.duplicateParentState,%0A        %7D,%0A        /* srcPos */ 0,%0A        R.styleable.ComponentLayout,%0A        /* destPos */ 0,%0A        R.styleable.ComponentLayout.length);%0A%0A    System.arraycopy(%0A        new int%5B%5D %7B%0A          android.R.attr.minLines,%0A          android.R.attr.maxLines,%0A          android.R.attr.textColor,%0A          android.R.attr.textColorLink,%0A          android.R.attr.textColorHighlight,%0A          android.R.attr.textSize,%0A          android.R.attr.lineSpacingExtra,%0A          android.R.attr.lineSpacingMultiplier,%0A          android.R.attr.ellipsize,%0A          android.R.attr.textAlignment,%0A          android.R.attr.textStyle,%0A          android.R.attr.text,%0A          android.R.attr.includeFontPadding,%0A          android.R.attr.singleLine,%0A          android.R.attr.shadowColor,%0A          android.R.attr.shadowDx,%0A          android.R.attr.shadowDy,%0A          android.R.attr.shadowRadius,%0A          android.R.attr.gravity,%0A          android.R.attr.minEms,%0A          android.R.attr.maxEms,%0A          android.R.attr.minWidth,%0A          android.R.attr.maxWidth,%0A          android.R.attr.inputType,%0A          android.R.attr.fontFamily,%0A          android.R.attr.imeOptions,%0A          android.R.attr.breakStrategy,%0A          android.R.attr.hyphenationFrequency,%0A          android.R.attr.justificationMode,%0A        %7D,%0A        /* srcPos */ 0,%0A        R.styleable.Text,%0A        /* destPos */ 0,%0A        R.styleable.Text.length);%0A%0A    System.arraycopy(%0A        new int%5B%5D %7B%0A          android.R.attr.src, android.R.attr.scaleType,%0A        %7D,%0A        /* srcPos */ 0,%0A        R.styleable.Image,%0A        /* destPos */ 0,%0A        R.styleable.Image.length);%0A  %7D
 %0A%7D%0A

Remove Litho resource remapping buck hack