Skip to content
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
Next Next commit
add integration test
  • Loading branch information
bparrishMines committed Apr 14, 2023
commit 2ec0a601fc13872966e2efda58f2b2a1d07f859a
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
Expand Down Expand Up @@ -35,7 +37,7 @@ public class InstanceManager {
// Host uses identifiers >= 2^16 and Dart is expected to use values n where,
// 0 <= n < 2^16.
private static final long MIN_HOST_CREATED_IDENTIFIER = 65536;
private static final long CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL = 30000;
@VisibleForTesting public static final long CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL = 30000;
private static final String TAG = "InstanceManager";

/** Interface for listening when a weak reference of an instance is removed from the manager. */
Expand Down Expand Up @@ -65,6 +67,7 @@ public interface FinalizationListener {
* @param finalizationListener the listener for garbage collected weak references.
* @return a new `InstanceManager`.
*/
@NonNull
public static InstanceManager create(FinalizationListener finalizationListener) {
return new InstanceManager(finalizationListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ flutter {
dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.2.0'
api 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
api 'androidx.test:core:1.4.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.cameraexample;

import static io.flutter.plugins.camerax.InstanceManager.CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.plugins.camerax.InstanceManager;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class InstanceManagerTest {
@Test
public void managerDoesNotTriggerFinalizationListenerWhenStopped() throws InterruptedException {
final boolean[] callbackTriggered = {false};
final InstanceManager instanceManager =
InstanceManager.create(identifier -> callbackTriggered[0] = true);
instanceManager.stopFinalizationListener();

Object object = new Object();
instanceManager.addDartCreatedInstance(object, 0);

assertEquals(object, instanceManager.remove(0));

// To allow for object to be garbage collected.
//noinspection UnusedAssignment
object = null;

Runtime.getRuntime().gc();

Thread.sleep(CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL);

assertNull(instanceManager.getInstance(0));
assertFalse(callbackTriggered[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies:
video_player: ^2.4.10

dev_dependencies:
espresso: ^0.2.0
flutter_test:
sdk: flutter
integration_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

* Fixes a bug where the native `WebView` wouldn't be traversed for autofill automatically.
* Updates minimum Flutter version to 3.3.
>>>>>>> 9852b41ff46771c24bca26919fc345d69bdd0b1a

## 3.4.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class InstanceManager {
// Host uses identifiers >= 2^16 and Dart is expected to use values n where,
// 0 <= n < 2^16.
private static final long MIN_HOST_CREATED_IDENTIFIER = 65536;
private static final long CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL = 30000;
@VisibleForTesting public static final long CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL = 3000;
private static final String TAG = "InstanceManager";

/** Interface for listening when a weak reference of an instance is removed from the manager. */
Expand Down Expand Up @@ -66,6 +67,7 @@ public interface FinalizationListener {
* @param finalizationListener the listener for garbage collected weak references.
* @return a new `InstanceManager`.
*/
@NonNull
public static InstanceManager create(@NonNull FinalizationListener finalizationListener) {
return new InstanceManager(finalizationListener);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.webviewflutterexample;

import static io.flutter.plugins.webviewflutter.InstanceManager.CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.plugins.webviewflutter.InstanceManager;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class InstanceManagerTest {
@Test
public void managerDoesNotTriggerFinalizationListenerWhenStopped() throws InterruptedException {
final boolean[] callbackTriggered = {false};
final InstanceManager instanceManager =
InstanceManager.create(identifier -> callbackTriggered[0] = true);
instanceManager.stopFinalizationListener();

Object object = new Object();
instanceManager.addDartCreatedInstance(object, 0);

assertEquals(object, instanceManager.remove(0));

// To allow for object to be garbage collected.
//noinspection UnusedAssignment
object = null;

Runtime.getRuntime().gc();

Thread.sleep(CLEAR_FINALIZED_WEAK_REFERENCES_INTERVAL);

assertNull(instanceManager.getInstance(0));
assertFalse(callbackTriggered[0]);
}
}