Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
Summary: The core test has been broken for a while, the sample tests are finnicky with ui automation connections, so adding a fix for that too.

Reviewed By: sjkirby

Differential Revision: D27933139

fbshipit-source-id: ab2ad622cf63b9032fd46fff101f0aeb511ab172
  • Loading branch information
xiphirx authored and facebook-github-bot committed Apr 26, 2021
1 parent 081522d commit e52ba06
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -108,24 +105,11 @@ public void testRecordBuilderImplHasAHierarchyDumpFile() throws Throwable {
rb.record();
mScreenshot.flush();

ZipFile bundle =
new ZipFile(
new File(mScreenshotDirectories.get("verify-in-test"), "screenshot_bundle.zip"));
Enumeration<? extends ZipEntry> entries = bundle.entries();
ZipEntry hierarchyEntry = null;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().equals("blahblah_dump.json")) {
hierarchyEntry = entry;
break;
}
}

if (hierarchyEntry == null) {
throw new IllegalStateException("No hierarchy file found");
}
String fileName =
new File(mScreenshotDirectories.get("verify-in-test"), "blahblah_dump.json")
.getAbsolutePath();

InputStream is = bundle.getInputStream(hierarchyEntry);
InputStream is = new FileInputStream(fileName);

StringBuilder builder = new StringBuilder();
byte[] buffer = new byte[8 * 1024];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,30 @@

import android.content.Context;
import android.view.LayoutInflater;
import androidx.test.annotation.UiThreadTest;
import androidx.test.platform.app.InstrumentationRegistry;
import com.facebook.litho.LithoView;
import com.facebook.testing.screenshot.Screenshot;
import com.facebook.testing.screenshot.ViewHelpers;
import org.junit.Before;
import org.junit.Test;

public class ExampleScreenshotTest {
@Before
public void before() {
InstrumentationRegistry.getInstrumentation().getUiAutomation();
}

@Test
@UiThreadTest
public void testDefault() {
Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
LayoutInflater inflater = LayoutInflater.from(targetContext);
LithoView view = (LithoView) inflater.inflate(R.layout.litho_view, null, false);

view.setComponent(Example.create(view.getComponentContext()).build());

ViewHelpers.setupView(view).setExactWidthDp(300).layout();
ViewHelpers.setupView(view).setExactWidthDp(300).setExactHeightDp(300).layout();
Screenshot.snap(view).record();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@

import android.content.Context;
import android.view.LayoutInflater;
import androidx.test.annotation.UiThreadTest;
import androidx.test.platform.app.InstrumentationRegistry;
import com.facebook.litho.LithoView;
import com.facebook.testing.screenshot.Screenshot;
import com.facebook.testing.screenshot.ViewHelpers;
import org.junit.Before;
import org.junit.Test;

public class ImageRowScreenshotTest {
@Before
public void before() {
InstrumentationRegistry.getInstrumentation().getUiAutomation();
}

@Test
@UiThreadTest
public void testDefault() {
Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
LayoutInflater inflater = LayoutInflater.from(targetContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,23 @@ import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import com.facebook.testing.screenshot.Screenshot
import org.hamcrest.core.AllOf.allOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class MainActivityTest {
@get:Rule
var activityTestRule = ActivityTestRule<MainActivity>(MainActivity::class.java, false, false)

@Before
fun before() {
InstrumentationRegistry.getInstrumentation().getUiAutomation()
}

@Test
fun testScreenshotEntireActivity() {
val activity = activityTestRule.launchActivity(null)
Expand Down

0 comments on commit e52ba06

Please sign in to comment.