Commit 0e304acf authored by Natalie Chouinard's avatar Natalie Chouinard Committed by Commit Bot

[GoogleSans] Fix AndroidFontLookupImplTest

After the AndroidFontLookup implementation was switched to using a
PostTask to fetch the font, and an Executor to run the callback, these
tests were only passing because the callback (checking assertions)
wasn't being run at all in tests.

This fixes that issue by initializing native to ensure PostTasks are
run, and adding a runLoop to run the Executor tasks (the latter mirrors
the Mojo ExecutorFactoryTest).

Bug: 1111148
Change-Id: I71eda8b1ab20ed2aabf8810303a6435b5007aaa8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2404073Reviewed-by: default avatarSky Malice <skym@chromium.org>
Commit-Queue: Natalie Chouinard <chouinard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806267}
parent 20f06509
...@@ -5,12 +5,13 @@ ...@@ -5,12 +5,13 @@
package org.chromium.content.browser.font; package org.chromium.content.browser.font;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks; import static org.mockito.MockitoAnnotations.initMocks;
...@@ -42,8 +43,10 @@ import org.mockito.stubbing.OngoingStubbing; ...@@ -42,8 +43,10 @@ import org.mockito.stubbing.OngoingStubbing;
import org.chromium.base.test.BaseJUnit4ClassRunner; import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.blink.mojom.AndroidFontLookup; import org.chromium.blink.mojom.AndroidFontLookup;
import org.chromium.blink.mojom.AndroidFontLookup.MatchLocalFontByUniqueNameResponse;
import org.chromium.content_public.browser.test.NativeLibraryTestUtils;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.mojo.MojoTestRule; import org.chromium.mojo.MojoTestRule;
import org.chromium.mojo_base.mojom.File;
/** /**
* Tests the {@link AndroidFontLookup} implementation. * Tests the {@link AndroidFontLookup} implementation.
...@@ -55,6 +58,7 @@ public final class AndroidFontLookupImplTest { ...@@ -55,6 +58,7 @@ public final class AndroidFontLookupImplTest {
private static final String AUTHORITY = "com.google.android.gms.fonts"; private static final String AUTHORITY = "com.google.android.gms.fonts";
private static final Uri URI = Uri.parse("content://com.google.android.gms.fonts/123"); private static final Uri URI = Uri.parse("content://com.google.android.gms.fonts/123");
private static final int FD = 42; private static final int FD = 42;
private static final long RUN_LOOP_TIMEOUT_MS = 50;
@Rule @Rule
public MojoTestRule mMojoTestRule = new MojoTestRule(MojoTestRule.MojoCore.INITIALIZE); public MojoTestRule mMojoTestRule = new MojoTestRule(MojoTestRule.MojoCore.INITIALIZE);
...@@ -65,12 +69,17 @@ public final class AndroidFontLookupImplTest { ...@@ -65,12 +69,17 @@ public final class AndroidFontLookupImplTest {
private ParcelFileDescriptor mMockFileDescriptor; private ParcelFileDescriptor mMockFileDescriptor;
private Context mMockContext; private Context mMockContext;
@Mock
private MatchLocalFontByUniqueNameResponse mMatchLocalFontByUniqueNameCallback;
private AndroidFontLookupImpl mAndroidFontLookup; private AndroidFontLookupImpl mAndroidFontLookup;
@Before @Before
public void setUp() { public void setUp() {
initMocks(this); initMocks(this);
NativeLibraryTestUtils.loadNativeLibraryNoBrowserProcess();
MockContentResolver resolver = new MockContentResolver(); MockContentResolver resolver = new MockContentResolver();
MockContext mockContext = new MockContext(); MockContext mockContext = new MockContext();
when(mMockFileDescriptor.detachFd()).thenReturn(FD); when(mMockFileDescriptor.detachFd()).thenReturn(FD);
...@@ -92,8 +101,12 @@ public final class AndroidFontLookupImplTest { ...@@ -92,8 +101,12 @@ public final class AndroidFontLookupImplTest {
@SmallTest @SmallTest
@Test @Test
public void testMatchLocalFontByUniqueName_UnsupportedFontName() { public void testMatchLocalFontByUniqueName_UnsupportedFontName() {
mAndroidFontLookup.matchLocalFontByUniqueName( mAndroidFontLookup.matchLocalFontByUniqueName("Bar", mMatchLocalFontByUniqueNameCallback);
"Bar", (File handle) -> { assertNull(handle); });
mMojoTestRule.runLoop(RUN_LOOP_TIMEOUT_MS);
verify(mMatchLocalFontByUniqueNameCallback,
timeout(CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL))
.call(isNull());
} }
@SmallTest @SmallTest
...@@ -104,7 +117,12 @@ public final class AndroidFontLookupImplTest { ...@@ -104,7 +117,12 @@ public final class AndroidFontLookupImplTest {
whenFetchFontsWith(FONT_QUERY).thenReturn(result); whenFetchFontsWith(FONT_QUERY).thenReturn(result);
mAndroidFontLookup.matchLocalFontByUniqueName( mAndroidFontLookup.matchLocalFontByUniqueName(
FULL_FONT_NAME, (File handle) -> { assertNull(handle); }); FULL_FONT_NAME, mMatchLocalFontByUniqueNameCallback);
mMojoTestRule.runLoop(RUN_LOOP_TIMEOUT_MS);
verify(mMatchLocalFontByUniqueNameCallback,
timeout(CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL))
.call(isNull());
} }
@SmallTest @SmallTest
...@@ -114,7 +132,12 @@ public final class AndroidFontLookupImplTest { ...@@ -114,7 +132,12 @@ public final class AndroidFontLookupImplTest {
whenFetchFontsWith(FONT_QUERY).thenReturn(result); whenFetchFontsWith(FONT_QUERY).thenReturn(result);
mAndroidFontLookup.matchLocalFontByUniqueName( mAndroidFontLookup.matchLocalFontByUniqueName(
FULL_FONT_NAME, (File handle) -> { assertNull(handle); }); FULL_FONT_NAME, mMatchLocalFontByUniqueNameCallback);
mMojoTestRule.runLoop(RUN_LOOP_TIMEOUT_MS);
verify(mMatchLocalFontByUniqueNameCallback,
timeout(CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL))
.call(isNull());
} }
@SmallTest @SmallTest
...@@ -126,22 +149,29 @@ public final class AndroidFontLookupImplTest { ...@@ -126,22 +149,29 @@ public final class AndroidFontLookupImplTest {
whenFetchFontsWith(FONT_QUERY).thenReturn(result); whenFetchFontsWith(FONT_QUERY).thenReturn(result);
mAndroidFontLookup.matchLocalFontByUniqueName( mAndroidFontLookup.matchLocalFontByUniqueName(
FULL_FONT_NAME, (File handle) -> { assertNull(handle); }); FULL_FONT_NAME, mMatchLocalFontByUniqueNameCallback);
mMojoTestRule.runLoop(RUN_LOOP_TIMEOUT_MS);
verify(mMatchLocalFontByUniqueNameCallback,
timeout(CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL))
.call(isNull());
} }
@SmallTest @SmallTest
@Test @Test
public void testMatchLocalFontByUniqueName_Success() throws NameNotFoundException { public void testMatchLocalFontByUniqueName_Success() throws NameNotFoundException {
int expectedHandle = 1;
FontInfo fontInfo = new FontInfo(URI, 0, 400, false, Columns.RESULT_CODE_OK); FontInfo fontInfo = new FontInfo(URI, 0, 400, false, Columns.RESULT_CODE_OK);
FontFamilyResult result = FontFamilyResult result =
new FontFamilyResult(FontFamilyResult.STATUS_OK, new FontInfo[] {fontInfo}); new FontFamilyResult(FontFamilyResult.STATUS_OK, new FontInfo[] {fontInfo});
whenFetchFontsWith(FONT_QUERY).thenReturn(result); whenFetchFontsWith(FONT_QUERY).thenReturn(result);
mAndroidFontLookup.matchLocalFontByUniqueName(FULL_FONT_NAME, (File handle) -> { mAndroidFontLookup.matchLocalFontByUniqueName(
assertNotNull(handle); FULL_FONT_NAME, mMatchLocalFontByUniqueNameCallback);
assertEquals(expectedHandle, handle.fd.releaseNativeHandle());
}); mMojoTestRule.runLoop(RUN_LOOP_TIMEOUT_MS);
verify(mMatchLocalFontByUniqueNameCallback,
timeout(CriteriaHelper.DEFAULT_MAX_TIME_TO_POLL))
.call(notNull());
} }
private OngoingStubbing<FontFamilyResult> whenFetchFontsWith(String query) private OngoingStubbing<FontFamilyResult> whenFetchFontsWith(String query)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment