Flutter Engine
The Flutter Engine
TestActivity.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package dev.flutter.scenarios;
6
7import android.content.Context;
8import android.content.Intent;
9import android.os.Bundle;
10import android.os.Handler;
11import android.os.Looper;
12import android.view.Window;
13import androidx.annotation.NonNull;
14import androidx.annotation.Nullable;
15import androidx.core.view.WindowCompat;
16import androidx.core.view.WindowInsetsCompat;
17import androidx.core.view.WindowInsetsControllerCompat;
18import io.flutter.FlutterInjector;
19import io.flutter.embedding.engine.FlutterShellArgs;
20import io.flutter.embedding.engine.loader.FlutterLoader;
21import io.flutter.plugin.common.JSONMethodCodec;
22import io.flutter.plugin.common.MethodChannel;
23import java.util.HashMap;
24import java.util.Map;
25import java.util.Objects;
26import java.util.concurrent.atomic.AtomicBoolean;
27
28public abstract class TestActivity extends TestableFlutterActivity {
29 @Override
30 protected void onCreate(@Nullable Bundle savedInstanceState) {
31 super.onCreate(savedInstanceState);
32 hideSystemBars(getWindow());
34 }
35
36 @Override
37 protected void onDestroy() {
38 super.onDestroy();
39 }
40
41 @Override
42 @NonNull
48 return args;
49 }
50
51 @Override
52 public void onFlutterUiDisplayed() {
53 final Intent launchIntent = getIntent();
54 MethodChannel channel =
55 new MethodChannel(
56 Objects.requireNonNull(getFlutterEngine()).getDartExecutor(),
57 "driver",
59 Map<String, Object> test = new HashMap<>(2);
60 if (launchIntent.hasExtra("scenario_name")) {
61 test.put("name", launchIntent.getStringExtra("scenario_name"));
62 } else {
63 test.put("name", "animated_color_square");
64 }
65 test.put("use_android_view", launchIntent.getBooleanExtra("use_android_view", false));
66 test.put(
67 "expect_android_view_fallback",
68 launchIntent.getBooleanExtra("expect_android_view_fallback", false));
69 test.put("view_type", launchIntent.getStringExtra("view_type"));
71 channel.invokeMethod("set_scenario", test);
72 }
73
74 /**
75 * Populates test-specific parameters that are sent to the Dart test scenario.
76 *
77 * @param args The map of test arguments
78 */
79 protected void getScenarioParams(@NonNull Map<String, Object> args) {}
80
81 /**
82 * This method verifies that {@link
83 * io.flutter.embedding.engine.loader.FlutterLoader#ensureInitializationCompleteAsync(Context,
84 * String[], Handler, Runnable)} invokes its callback when called after initialization.
85 */
87 FlutterLoader flutterLoader = FlutterInjector.instance().flutterLoader();
88
89 // Flutter is probably already loaded in this app based on
90 // code that ran before this method. Nonetheless, invoke the
91 // blocking initialization here to ensure it's initialized.
92 flutterLoader.startInitialization(getApplicationContext());
93 flutterLoader.ensureInitializationComplete(getApplication(), new String[] {});
94
95 // Now that Flutter is loaded, invoke ensureInitializationCompleteAsync with
96 // a callback and verify that the callback is invoked.
97 Handler mainHandler = new Handler(Looper.getMainLooper());
98
99 final AtomicBoolean didInvokeCallback = new AtomicBoolean(false);
100
102 getApplication(), new String[] {}, mainHandler, () -> didInvokeCallback.set(true));
103
104 mainHandler.post(
105 () -> {
106 if (!didInvokeCallback.get()) {
107 throw new RuntimeException(
108 "Failed test: FlutterLoader#ensureInitializationCompleteAsync() did not invoke its callback.");
109 }
110 });
111 }
112
113 private static void hideSystemBars(Window window) {
114 final WindowInsetsControllerCompat insetController =
115 WindowCompat.getInsetsController(window, window.getDecorView());
116 assert insetController != null;
117 insetController.setSystemBarsBehavior(
118 WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
119 insetController.hide(WindowInsetsCompat.Type.systemBars());
120 }
121}
FlutterShellArgs getFlutterShellArgs()
void onCreate(@Nullable Bundle savedInstanceState)
void getScenarioParams(@NonNull Map< String, Object > args)
static FlutterShellArgs fromIntent(@NonNull Intent intent)
void ensureInitializationComplete( @NonNull Context applicationContext, @Nullable String[] args)
void startInitialization(@NonNull Context applicationContext)
void ensureInitializationCompleteAsync( @NonNull Context applicationContext, @Nullable String[] args, @NonNull Handler callbackHandler, @NonNull Runnable callback)
static final JSONMethodCodec INSTANCE
void invokeMethod(@NonNull String method, @Nullable Object arguments)
GLFWwindow * window
Definition: main.cc:45
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args