Add activity for connectivity test
Summary: This is quite hacky, but allows us to test that a plugin connects, sends some actions and then shuts down the app which we can use in a headless test. Reviewed By: jknoxville Differential Revision: D14482381 fbshipit-source-id: deafa8fa7e6d5a5ca1bb006e9d150b62295105af
This commit is contained in:
committed by
Facebook Github Bot
parent
f30ff068a7
commit
7c185ea771
@@ -21,6 +21,8 @@
|
||||
</activity>
|
||||
<activity android:name="com.facebook.flipper.android.diagnostics.FlipperDiagnosticActivity"
|
||||
android:exported="true"/>
|
||||
<activity android:name="com.facebook.flipper.connectivitytest.ConnectionTestActivity"
|
||||
android:exported="true"/>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.facebook.flipper.connectivitytest;
|
||||
|
||||
import android.os.Bundle;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import com.facebook.flipper.android.AndroidFlipperClient;
|
||||
import com.facebook.flipper.core.FlipperClient;
|
||||
import com.facebook.flipper.plugins.example.ExampleFlipperPlugin;
|
||||
import com.facebook.flipper.sample.RootComponent;
|
||||
import com.facebook.litho.ComponentContext;
|
||||
import com.facebook.litho.LithoView;
|
||||
|
||||
/**
|
||||
* Oh hai! This is probably not the kinda sample you want to copy to your application; we're just
|
||||
* using this to drive a test run and exit the app afterwards.
|
||||
*/
|
||||
public class ConnectionTestActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
final ComponentContext c = new ComponentContext(this);
|
||||
setContentView(LithoView.create(c, RootComponent.create(c).build()));
|
||||
|
||||
final FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized();
|
||||
if (client != null) {
|
||||
// As we're re-using the identifier, get rid of the default plugin first.
|
||||
final ExampleFlipperPlugin exampleFlipperPlugin =
|
||||
client.getPluginByClass(ExampleFlipperPlugin.class);
|
||||
client.removePlugin(exampleFlipperPlugin);
|
||||
|
||||
final ConnectionTestPlugin connectionTestPlugin = new ConnectionTestPlugin(this);
|
||||
client.addPlugin(connectionTestPlugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.flipper.connectivitytest;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.widget.Toast;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.flipper.core.FlipperConnection;
|
||||
import com.facebook.flipper.core.FlipperObject;
|
||||
import com.facebook.flipper.core.FlipperPlugin;
|
||||
import com.facebook.flipper.core.FlipperReceiver;
|
||||
import com.facebook.flipper.core.FlipperResponder;
|
||||
import com.facebook.flipper.sample.ExampleActions;
|
||||
|
||||
public class ConnectionTestPlugin implements FlipperPlugin {
|
||||
|
||||
// We are reusing the existing "Example" logic here. That's generally a pretty bad idea,
|
||||
// but in war and in testing everything is fair.
|
||||
private static final String ID = "Example";
|
||||
|
||||
private final Activity mActivity;
|
||||
|
||||
@Nullable private FlipperConnection mConnection;
|
||||
|
||||
public ConnectionTestPlugin(Activity activity) {
|
||||
mActivity = activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnect(FlipperConnection connection) {
|
||||
mConnection = connection;
|
||||
connection.receive(
|
||||
"displayMessage",
|
||||
new FlipperReceiver() {
|
||||
@Override
|
||||
public void onReceive(final FlipperObject params, FlipperResponder responder) {
|
||||
if (mActivity != null) {
|
||||
mActivity.runOnUiThread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Toast.makeText(mActivity, params.getString("message"), Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
responder.success(new FlipperObject.Builder().put("greeting", "Hello").build());
|
||||
}
|
||||
});
|
||||
|
||||
mActivity.runOnUiThread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ExampleActions.sendGetRequest();
|
||||
ExampleActions.sendPostRequest();
|
||||
// We want Flipper to properly disconnect at this point and actually shut down the app.
|
||||
mActivity.finish();
|
||||
android.os.Process.sendSignal(android.os.Process.myPid(), 15);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnect() {
|
||||
mConnection = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean runInBackground() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,9 @@ import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
final class ExampleActions {
|
||||
public final class ExampleActions {
|
||||
|
||||
static void sendPostRequest() {
|
||||
public static void sendPostRequest() {
|
||||
final RequestBody formBody =
|
||||
new FormBody.Builder().add("app", "Flipper").add("remarks", "Its awesome").build();
|
||||
|
||||
@@ -45,7 +45,7 @@ final class ExampleActions {
|
||||
});
|
||||
}
|
||||
|
||||
static void sendGetRequest() {
|
||||
public static void sendGetRequest() {
|
||||
final Request request =
|
||||
new Request.Builder().url("https://api.github.com/repos/facebook/yoga").get().build();
|
||||
FlipperSampleApplication.sOkHttpClient
|
||||
@@ -69,7 +69,7 @@ final class ExampleActions {
|
||||
});
|
||||
}
|
||||
|
||||
static void sendNotification() {
|
||||
public static void sendNotification() {
|
||||
final FlipperClient client = AndroidFlipperClient.getInstanceIfInitialized();
|
||||
if (client != null) {
|
||||
final ExampleFlipperPlugin plugin = client.getPluginByClass(ExampleFlipperPlugin.class);
|
||||
|
||||
@@ -28,12 +28,5 @@ public class MainActivity extends AppCompatActivity {
|
||||
final ExampleFlipperPlugin samplePlugin = client.getPluginByClass(ExampleFlipperPlugin.class);
|
||||
samplePlugin.setActivity(this);
|
||||
}
|
||||
|
||||
if (getIntent().getBooleanExtra("NON_INTERACTIVE_RUN", false)) {
|
||||
ExampleActions.sendGetRequest();
|
||||
ExampleActions.sendPostRequest();
|
||||
ExampleActions.sendNotification();
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user