Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a FlutterEngineRule (JUnit TestRule) and use it in FlutterRendererTest #53361

Merged
merged 7 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.flutter.embedding.engine.renderer;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.Intent;
import androidx.test.core.app.ApplicationProvider;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;
import io.flutter.embedding.engine.FlutterJNI;
import io.flutter.embedding.engine.loader.FlutterLoader;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
* Prepares and returns a {@link FlutterEngine} and {@link Intent} primed with an engine for tests.
*/
public final class FlutterEngineRule extends TestWatcher {
private static final Context ctx = ApplicationProvider.getApplicationContext();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error: Do not place Android context classes in static fields; this is a memory leak [StaticFieldLeak]
private static final Context ctx = ApplicationProvider.getApplicationContext();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done.

private static final String cachedEngineId = "flutter_engine_rule_cached_engine";
private FlutterJNI flutterJNI;
private FlutterEngine flutterEngine;
private boolean jniIsAttached = true;

@Override
protected void starting(Description description) {
// Setup mock JNI.
flutterJNI = mock(FlutterJNI.class);
when(flutterJNI.isAttached()).thenAnswer(i -> jniIsAttached);

// We will not try to load plugins in these tests.
FlutterLoader mockFlutterLoader = mock(FlutterLoader.class);
when(mockFlutterLoader.automaticallyRegisterPlugins()).thenReturn(false);

// Create an engine.
flutterEngine = new FlutterEngine(ctx, mockFlutterLoader, flutterJNI);

// Place it in the engine cache.
FlutterEngineCache.getInstance().put(cachedEngineId, flutterEngine);
}

@Override
protected void finished(Description description) {
FlutterEngineCache.getInstance().clear();
}

/**
* Returns a Mockito-mocked version of {@link FlutterJNI}.
*
* @return an instance that is already considered attached.
*/
FlutterJNI getFlutterJNI() {
return this.flutterJNI;
}

/**
* Returns a pre-configured engine.
*
* @return flutter engine using the mock provided by {{@link #getFlutterJNI()}}.
*/
FlutterEngine getFlutterEngine() {
return this.flutterEngine;
}

/**
* Sets what {@link FlutterJNI#isAttached()} returns. If not invoked, defaults to true.
*
* @param isAttached whether to consider JNI attached.
*/
void setJniIsAttached(boolean isAttached) {
this.jniIsAttached = isAttached;
}

/**
* Creates an intent with {@link FlutterEngine} instance already provided.
*
* @return intent, i.e. to use with {@link androidx.test.ext.junit.rules.ActivityScenarioRule}.
*/
Intent makeIntent() {
return FlutterActivity.withCachedEngine(cachedEngineId).build(ctx);
}
}
Loading