Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
autoformat
  • Loading branch information
xster committed Aug 28, 2020
commit efcc33e5dd2f8de54d674e3f8347e66dc2efc7ef
40 changes: 20 additions & 20 deletions shell/platform/android/io/flutter/FlutterInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
import io.flutter.embedding.engine.loader.FlutterLoader;

/**
* This class is a simple dependency injector for the relatively thin Android part of the Flutter engine.
* This class is a simple dependency injector for the relatively thin Android part of the Flutter
* engine.
*
* This simple solution is used facilitate testability without bringing in heavier app-development
* centric dependency injection frameworks such as Guice or Dagger2 or spreading construction
* injection everywhere.
* <p>This simple solution is used facilitate testability without bringing in heavier
* app-development centric dependency injection frameworks such as Guice or Dagger2 or spreading
* construction injection everywhere.
*/
public final class FlutterInjector {

private static FlutterInjector instance;
private static boolean accessed;

/**
* Use {@link FlutterInjector.Builder} to specify members to be injected via the static
* {@code FlutterInjector}.
* Use {@link FlutterInjector.Builder} to specify members to be injected via the static {@code
* FlutterInjector}.
*
* This can only be called at the beginning of the program before the {@link #instance()} is
* <p>This can only be called at the beginning of the program before the {@link #instance()} is
* accessed.
*/
public static void setInstance(@NonNull FlutterInjector injector) {
Expand All @@ -40,9 +41,9 @@ public static void setInstance(@NonNull FlutterInjector injector) {
/**
* Retrieve the static instance of the {@code FlutterInjector} to use in your program.
*
* Once you access it, you can no longer change the values injected.
* <p>Once you access it, you can no longer change the values injected.
*
* If no override is provided for the injector, reasonable defaults are provided.
* <p>If no override is provided for the injector, reasonable defaults are provided.
*/
public static FlutterInjector instance() {
accessed = true;
Expand Down Expand Up @@ -71,35 +72,33 @@ private FlutterInjector(boolean shouldLoadNative, @NonNull FlutterLoader flutter
/**
* Returns whether the Flutter Android engine embedding should load the native C++ engine.
*
* Useful for testing since JVM tests via Robolectric can't load native libraries.
* <p>Useful for testing since JVM tests via Robolectric can't load native libraries.
*/
public boolean shouldLoadNative() {
return shouldLoadNative;
}

/**
* Returns the {@link FlutterLoader} instance to use for the Flutter Android engine embedding.
*/
/** Returns the {@link FlutterLoader} instance to use for the Flutter Android engine embedding. */
@NonNull
public FlutterLoader flutterLoader() {
return flutterLoader;
}

/**
* Builder used to supply a custom FlutterInjector instance to
* {@link FlutterInjector#setInstance(FlutterInjector)}.
* Builder used to supply a custom FlutterInjector instance to {@link
* FlutterInjector#setInstance(FlutterInjector)}.
*
* Non-overriden values have reasonable defaults.
* <p>Non-overriden values have reasonable defaults.
*/
public static final class Builder {

private boolean shouldLoadNative = true;
/**
* Sets whether the Flutter Android engine embedding should load the native C++ engine.
*
* Useful for testing since JVM tests via Robolectric can't load native libraries.
* <p>Useful for testing since JVM tests via Robolectric can't load native libraries.
*
* Defaults to true.
* <p>Defaults to true.
*/
public Builder setShouldLoadNative(boolean shouldLoadNative) {
this.shouldLoadNative = shouldLoadNative;
Expand All @@ -110,7 +109,7 @@ public Builder setShouldLoadNative(boolean shouldLoadNative) {
/**
* Sets a {@link FlutterLoader} override.
*
* A reasonable default will be used if unspecified.
* <p>A reasonable default will be used if unspecified.
*/
public Builder setFlutterLoader(@NonNull FlutterLoader flutterLoader) {
this.flutterLoader = flutterLoader;
Expand All @@ -123,7 +122,8 @@ private void fillDefaults() {
}
}

/** Builds a {@link FlutterInjector} from the builder. Unspecified properties will have
/**
* Builds a {@link FlutterInjector} from the builder. Unspecified properties will have
* reasonable defaults.
*/
public FlutterInjector build() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ public static DartEntrypoint createDefault() {
FlutterLoader flutterLoader = FlutterInjector.instance().flutterLoader();

if (!flutterLoader.initialized()) {
throw new AssertionError("DartEntrypoints can only be created once a FlutterEngine is created.");
throw new AssertionError(
"DartEntrypoints can only be created once a FlutterEngine is created.");
}
return new DartEntrypoint(
flutterLoader.findAppBundlePath(), "main");
return new DartEntrypoint(flutterLoader.findAppBundlePath(), "main");
}

/** The path within the AssetManager where the app will look for assets. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import io.flutter.BuildConfig;
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterJNI;
Expand Down Expand Up @@ -295,9 +294,7 @@ public void run() {
});
}

/**
* Returns whether the FlutterLoader has finished loading the native library.
*/
/** Returns whether the FlutterLoader has finished loading the native library. */
public boolean initialized() {
return initialized;
}
Expand Down
11 changes: 6 additions & 5 deletions shell/platform/android/test/io/flutter/FlutterInjectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.loader.FlutterLoader;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -52,9 +51,11 @@ public void canPartiallyOverride() {
public void cannotBeChangedOnceRead() {
FlutterInjector.instance();

assertThrows(IllegalStateException.class, () -> {
FlutterInjector.setInstance(
new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
});
assertThrows(
IllegalStateException.class,
() -> {
FlutterInjector.setInstance(
new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
package test.io.flutter.embedding.engine;

import static junit.framework.TestCase.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import androidx.annotation.NonNull;
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterJNI;
import io.flutter.embedding.engine.loader.FlutterApplicationInfo;
import io.flutter.embedding.engine.loader.FlutterLoader;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.dart.DartExecutor.DartEntrypoint;
import io.flutter.embedding.engine.loader.FlutterLoader;

import java.nio.ByteBuffer;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -70,15 +68,19 @@ public void itNotifiesLowMemoryWarning() {

@Test
public void itThrowsWhenCreatingADefaultDartEntrypointWithAnUninitializedFlutterLoader() {
assertThrows(AssertionError.class, () -> {
DartEntrypoint.createDefault();
});
assertThrows(
AssertionError.class,
() -> {
DartEntrypoint.createDefault();
});
}

@Test
public void itHasReasonableDefaultsWhenFlutterLoaderIsInitialized() {
when(mockFlutterLoader.initialized()).thenReturn(true);
when(mockFlutterLoader.findAppBundlePath()).thenReturn("my/custom/path");
FlutterInjector.setInstance(
new FlutterInjector.Builder().setFlutterLoader(mockFlutterLoader).build());
DartEntrypoint entrypoint = DartEntrypoint.createDefault();
assertEquals(entrypoint.pathToBundle, "my/custom/path");
assertEquals(entrypoint.dartEntrypointFunctionName, "main");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,8 @@

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.res.AssetManager;
import io.flutter.FlutterInjector;
import io.flutter.embedding.engine.FlutterJNI;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.loader.FlutterLoader;

import java.nio.ByteBuffer;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down