|
| 1 | +package io.flutter.embedding.engine; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.runner.RunWith; |
| 8 | +import org.robolectric.RobolectricTestRunner; |
| 9 | +import org.robolectric.annotation.Config; |
| 10 | + |
| 11 | +import io.flutter.embedding.engine.loader.FlutterLoader; |
| 12 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 13 | +import io.flutter.plugin.platform.PlatformViewsController; |
| 14 | + |
| 15 | +import static junit.framework.TestCase.assertTrue; |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.assertFalse; |
| 18 | +import static org.mockito.Mockito.mock; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +// Run with Robolectric so that Log calls don't crash. |
| 22 | +@Config(manifest=Config.NONE) |
| 23 | +@RunWith(RobolectricTestRunner.class) |
| 24 | +public class FlutterEnginePluginRegistryTest { |
| 25 | + @Test |
| 26 | + public void itDoesNotRegisterTheSamePluginTwice() { |
| 27 | + Context context = mock(Context.class); |
| 28 | + |
| 29 | + FlutterEngine flutterEngine = mock(FlutterEngine.class); |
| 30 | + PlatformViewsController platformViewsController = mock(PlatformViewsController.class); |
| 31 | + when(flutterEngine.getPlatformViewsController()).thenReturn(platformViewsController); |
| 32 | + |
| 33 | + FlutterLoader flutterLoader = mock(FlutterLoader.class); |
| 34 | + |
| 35 | + FakeFlutterPlugin fakePlugin1 = new FakeFlutterPlugin(); |
| 36 | + FakeFlutterPlugin fakePlugin2 = new FakeFlutterPlugin(); |
| 37 | + |
| 38 | + FlutterEnginePluginRegistry registry = new FlutterEnginePluginRegistry( |
| 39 | + context, |
| 40 | + flutterEngine, |
| 41 | + flutterLoader |
| 42 | + ); |
| 43 | + |
| 44 | + // Verify that the registry doesn't think it contains our plugin yet. |
| 45 | + assertFalse(registry.has(fakePlugin1.getClass())); |
| 46 | + |
| 47 | + // Add our plugin to the registry. |
| 48 | + registry.add(fakePlugin1); |
| 49 | + |
| 50 | + // Verify that the registry now thinks it contains our plugin. |
| 51 | + assertTrue(registry.has(fakePlugin1.getClass())); |
| 52 | + assertEquals(1, fakePlugin1.attachmentCallCount); |
| 53 | + |
| 54 | + // Add a different instance of the same plugin class. |
| 55 | + registry.add(fakePlugin2); |
| 56 | + |
| 57 | + // Verify that the registry did not detach the 1st plugin, and |
| 58 | + // it did not attach the 2nd plugin. |
| 59 | + assertEquals(1, fakePlugin1.attachmentCallCount); |
| 60 | + assertEquals(0, fakePlugin1.detachmentCallCount); |
| 61 | + |
| 62 | + assertEquals(0, fakePlugin2.attachmentCallCount); |
| 63 | + assertEquals(0, fakePlugin2.detachmentCallCount); |
| 64 | + } |
| 65 | + |
| 66 | + private static class FakeFlutterPlugin implements FlutterPlugin { |
| 67 | + public int attachmentCallCount = 0; |
| 68 | + public int detachmentCallCount = 0; |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { |
| 72 | + attachmentCallCount += 1; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { |
| 77 | + detachmentCallCount += 1; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments