-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ba0fdd
Refactor FlutterRendererTest, prepare for ActivityScenarioRule.
matanlurey 4dbf657
++
matanlurey 358f0c2
Do not cache context.
matanlurey 0c52336
++
matanlurey 36bf5b4
++
matanlurey 8a2970f
++
matanlurey 7179852
++
matanlurey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterEngineRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Done.