Skip to content

Commit

Permalink
fix(tests): Testing multi-threaded loading
Browse files Browse the repository at this point in the history
  • Loading branch information
breautek committed Oct 20, 2024
1 parent bb1a2fe commit 8c55ac1
Show file tree
Hide file tree
Showing 22 changed files with 391 additions and 410 deletions.
6 changes: 5 additions & 1 deletion android/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
local.properties
.gradle
fuse/build
fuseTestTools/build
fuseTestTools/build
.idea/caches
.idea/deploymentTargetSelector.xml
.idea/deviceManager.xml
.idea/androidTestResultsUserPreferences.xml
35 changes: 35 additions & 0 deletions android/.idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

329 changes: 0 additions & 329 deletions android/.idea/caches/deviceStreaming.xml

This file was deleted.

18 changes: 0 additions & 18 deletions android/.idea/deploymentTargetSelector.xml

This file was deleted.

4 changes: 4 additions & 0 deletions android/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.CountDownLatch;

@RunWith(AndroidJUnit4.class)
public class FuseAPITest {
Expand All @@ -44,74 +45,92 @@ public static void setUp() {}
public static void tearDown() {}

@Test
public void shouldHaveAPort() {
public void shouldHaveAPort() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
activityRule.getScenario().onActivity(activity -> {
int port = activity.getFuseContext().getAPIPort();
assertTrue(port >= 1024 && port <= 65535);
activity.setOnReadyCallback(() -> {
int port = activity.getFuseContext().getAPIPort();
assertTrue(port >= 1024 && port <= 65535);
latch.countDown();
});
});
latch.await();
}

@Test
public void shouldHaveASecret() {
public void shouldHaveASecret() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
activityRule.getScenario().onActivity(activity -> {
String secret = activity.getFuseContext().getAPISecret();
assertNotNull(secret);
activity.setOnReadyCallback(() -> {
String secret = activity.getFuseContext().getAPISecret();
assertNotNull(secret);
latch.countDown();
});
});
latch.await();
}

@Test
public void canDoSimpleEchoRequest() {
public void canDoSimpleEchoRequest() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
activityRule.getScenario().onActivity(activity -> {
int port = activity.getFuseContext().getAPIPort();
String secret = activity.getFuseContext().getAPISecret();

FuseTestAPIClient client;
try {
client = new FuseTestAPIClient.Builder()
.setFuseContext(activity.getFuseContext())
.setAPIPort(port)
.setAPISecret(secret)
.setPluginID("echo")
.setType("text/plain")
.setEndpoint("/echo")
.setContent("Hello Test!")
.build();
}
catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException(e);
}

FuseTestAPIClient.FuseAPITestResponse response = client.execute();
assertEquals(200, response.getStatus());
assertTrue(response.readAsString().contains("Hello Test!"));
activity.setOnReadyCallback(() -> {
int port = activity.getFuseContext().getAPIPort();
String secret = activity.getFuseContext().getAPISecret();

FuseTestAPIClient client;
try {
client = new FuseTestAPIClient.Builder()
.setFuseContext(activity.getFuseContext())
.setAPIPort(port)
.setAPISecret(secret)
.setPluginID("echo")
.setType("text/plain")
.setEndpoint("/echo")
.setContent("Hello Test!")
.build();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException(e);
}

FuseTestAPIClient.FuseAPITestResponse response = client.execute();
assertEquals(200, response.getStatus());
assertTrue(response.readAsString().contains("Hello Test!"));
latch.countDown();
});
});
latch.await();
}

@Test
public void canUseAnAPIThatSwitchesToMainThread() {
public void canUseAnAPIThatSwitchesToMainThread() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
activityRule.getScenario().onActivity(activity -> {
int port = activity.getFuseContext().getAPIPort();
String secret = activity.getFuseContext().getAPISecret();

FuseTestAPIClient client;
try {
client = new FuseTestAPIClient.Builder()
.setFuseContext(activity.getFuseContext())
.setAPIPort(port)
.setAPISecret(secret)
.setPluginID("echo")
.setType("text/plain")
.setEndpoint("/threadtest")
.setContent("Hello Test!")
.build();
}
catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException(e);
}

FuseTestAPIClient.FuseAPITestResponse response = client.execute();
assertEquals(200, response.getStatus());
assertTrue(response.readAsString().contains("Hello Test!"));
activity.setOnReadyCallback(() -> {
int port = activity.getFuseContext().getAPIPort();
String secret = activity.getFuseContext().getAPISecret();

FuseTestAPIClient client;
try {
client = new FuseTestAPIClient.Builder()
.setFuseContext(activity.getFuseContext())
.setAPIPort(port)
.setAPISecret(secret)
.setPluginID("echo")
.setType("text/plain")
.setEndpoint("/threadtest")
.setContent("Hello Test!")
.build();
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException(e);
}

FuseTestAPIClient.FuseAPITestResponse response = client.execute();
assertEquals(200, response.getStatus());
assertTrue(response.readAsString().contains("Hello Test!"));
latch.countDown();
});
});
latch.await();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@
import com.breautek.fuse.plugins.echo.EchoPlugin;

public class TestFuseActivity extends FuseTestActivity {
private FuseContext.IReadyCallback $callback;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FuseContext fuseContext = getFuseContext();
fuseContext.registerPlugin(new EchoPlugin(fuseContext));
}

public void setOnReadyCallback(FuseContext.IReadyCallback callback) {
$callback = callback;
}

@Override
protected void _onContextReady() {
$callback.onReady();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.breautek.fuse;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.ContentView;
Expand All @@ -30,7 +29,6 @@
import androidx.core.view.WindowInsetsControllerCompat;

import android.view.Window;
import android.view.WindowInsetsController;

import com.breautek.fuse.plugins.IFusePluginRegistrar;

Expand Down Expand Up @@ -66,18 +64,20 @@ public FuseActivity(@LayoutRes int contentLayoutId) {
}

private void $init() {
$fuseContext = new FuseContext(this);
$fuseContext = new FuseContext(this, this::_onContextReady);
$fuseContext.$pluginMapLock.writeLock().lock();
_registerFusePlugins(plugin -> $fuseContext.$registerPlugin(plugin));
$fuseContext.$pluginMapLock.writeLock().unlock();
}

protected void _onContextReady() {}

/**
* Can be overwritten by subclasses to register plugins
*
* @param registrar
*/
protected void _registerFusePlugins(IFusePluginRegistrar registrar) {};
protected void _registerFusePlugins(IFusePluginRegistrar registrar) {}

public FuseContext getFuseContext() {
return $fuseContext;
Expand Down
22 changes: 20 additions & 2 deletions android/fuse/src/main/java/com/breautek/fuse/FuseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import android.widget.FrameLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.webkit.WebViewAssetLoader;
import androidx.webkit.WebViewClientCompat;
import java.io.IOException;
Expand Down Expand Up @@ -94,14 +97,21 @@ public class FuseContext implements IProgressContextListener {
private ViewGroup $container;

private final FuseScreenUtils $screenUtils;
private final FuseRuntime $runtime;
private final IReadyCallback $readyCallback;

private static final String LOAD_CONTEXT_CORE = "FuseContext_core";
private static final String LOAD_CONTEXT_API_SERVER = "FuseContext_apiServer";
private static final String LOAD_CONTEXT_CORE_PLUGINS = "FuseContext_corePlugins";
private static final String LOAD_CONTEXT_WEBVIEW = "FuseContext_webview";

public FuseContext(AppCompatActivity context) {
public static interface IReadyCallback {
void onReady();
}

public FuseContext(AppCompatActivity context, IReadyCallback callback) {
$context = context;
$readyCallback = callback;
$loadProgress = new ProgressContext();

$loadProgress.createProgress(LOAD_CONTEXT_CORE);
Expand All @@ -126,7 +136,8 @@ public FuseContext(AppCompatActivity context) {
$apiRouter = new FuseAPIRouter(this);
$loadProgress.update(LOAD_CONTEXT_CORE, 1);

registerPlugin(new FuseRuntime(this));
$runtime = new FuseRuntime(this);
registerPlugin($runtime);

$loadProgress.update(LOAD_CONTEXT_CORE_PLUGINS, 1);
}
Expand Down Expand Up @@ -209,6 +220,11 @@ public void onCreate(Bundle bundle) {
$container.addView($webview);
$container.addView($splash);

ViewCompat.setOnApplyWindowInsetsListener($container, (v, insets) -> {
$runtime.onInsetChange(insets);
return insets;
});

final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
.addPathHandler("/assets/", new WebViewAssetLoader.AssetsPathHandler($context))
.setHttpAllowed(false)
Expand Down Expand Up @@ -255,6 +271,8 @@ public void onReceivedSslError(WebView webview, SslErrorHandler handler, SslErro
$loadProgress.update(LOAD_CONTEXT_API_SERVER, 1);
Log.i(TAG, "API Server Port: " + $apiServer.getPort());

$readyCallback.onReady();

self.runOnMainThread(() -> {
WebSettings settings = $webview.getSettings();
settings.setAllowFileAccess(false);
Expand Down
Loading

0 comments on commit 8c55ac1

Please sign in to comment.