/*
 * Copyright 2014-present Facebook, Inc.
 *
 * 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.espresso;

import static com.facebook.litho.testing.espresso.ComponentHostMatchers.componentHost;
import static com.facebook.litho.testing.espresso.ComponentHostMatchers.componentHostWithText;
import static com.facebook.litho.testing.espresso.ComponentHostMatchers.withContentDescription;
import static com.facebook.litho.testing.espresso.ComponentHostMatchers.withLifecycle;
import static com.facebook.litho.testing.espresso.ComponentHostMatchers.withText;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.widget.TextView;
import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.ComponentTree;
import com.facebook.litho.LithoView;
import com.facebook.litho.widget.Text;
import com.facebook.testing.screenshot.ViewHelpers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Tests {@link ComponentHostMatchers}
 */
@RunWith(AndroidJUnit4.class)
public class ComponentHostMatchersTest {

  @Rule
  public UiThreadTestRule mUiThreadRule = new UiThreadTestRule();

  private LithoView mView;

  @Before
  public void before() throws Throwable {
    final ComponentContext mComponentContext = new ComponentContext(
        InstrumentationRegistry.getTargetContext());
    final Component mTextComponent = MyComponent.create(mComponentContext)
        .text("foobar")
        .customViewTag("zoidberg")
        .build();
    final ComponentTree tree = ComponentTree.create(
        mComponentContext,
        mTextComponent)
        .build();
    mView = new LithoView(mComponentContext);
    mView.setComponentTree(tree);
    ViewHelpers.setupView(mView)
        .setExactWidthPx(200)
        .setExactHeightPx(100)
        .layout();
  }

  @Test
  public void testContentDescriptionMatching() throws Throwable {

    assertThat(
        mView,
        componentHostWithText("foobar"));
    assertThat(
        mView,
        not(componentHostWithText("bar")));
    assertThat(
        mView,
        componentHostWithText(containsString("oob")));
  }

  @Test
  public void testIsComponentHost() throws Throwable {
    assertThat(
        new TextView(InstrumentationRegistry.getTargetContext()),
        is(not(componentHost())));
    assertThat(
        mView,
        is(componentHost()));
  }

  @Test
  public void testIsComponentHostWithMatcher() throws Throwable {
    assertThat(
        mView,
        is(componentHost(withText("foobar"))));
    assertThat(
        mView,
        is(not(componentHost(withText("blah")))));
  }

  @Test
  public void testContentDescription() throws Throwable {
    assertThat(
        mView,
        is(componentHost(withContentDescription("foobar2"))));
  }

  @Test
  public void testMountedComponent() throws Throwable {
    assertThat(
        mView,
        is(componentHost(withLifecycle(isA(Text.class)))));
  }
}

@@ -2582,16 +2582,132 @@
 ntext);%0A
+    mUiThreadRule.runOnUiThread(%0A        new Runnable() %7B%0A          @Override%0A          public void run() %7B%0A        
     mVie
@@ -2732,16 +2732,24 @@
 (tree);%0A
+        
     View
@@ -2772,25 +2772,16 @@
 w(mView)
-%0A        
 .setExac
@@ -2793,25 +2793,16 @@
 hPx(200)
-%0A        
 .setExac
@@ -2819,25 +2819,40 @@
 100)
-%0A        .layout(
+.layout();%0A          %7D%0A        %7D
 );%0A 
@@ -2929,17 +2929,16 @@
 wable %7B%0A
-%0A
     asse

Fix ComponentHostMatchersTest