2022-12-20 03:14:42 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-05-21 10:53:59 +00:00
|
|
|
import 'package:appflowy/core/config/kv_keys.dart';
|
2023-06-15 14:43:07 +00:00
|
|
|
import 'package:appflowy/startup/entry_point.dart';
|
|
|
|
import 'package:appflowy/startup/startup.dart';
|
2023-02-26 08:27:17 +00:00
|
|
|
import 'package:appflowy/startup/tasks/prelude.dart';
|
2023-06-12 06:36:55 +00:00
|
|
|
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
|
2022-12-20 03:14:42 +00:00
|
|
|
import 'package:flutter/gestures.dart';
|
2023-06-15 14:43:07 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-12-20 03:14:42 +00:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
|
|
class TestFolder {
|
|
|
|
/// Location / Path
|
|
|
|
|
|
|
|
/// Set a given AppFlowy data storage location under test environment.
|
|
|
|
///
|
|
|
|
/// To pass null means clear the location.
|
|
|
|
///
|
|
|
|
/// The file_picker is a system component and can't be tapped, so using logic instead of tapping.
|
|
|
|
///
|
|
|
|
static Future<void> setTestLocation(String? name) async {
|
|
|
|
final location = await testLocation(name);
|
|
|
|
SharedPreferences.setMockInitialValues({
|
2023-05-21 10:53:59 +00:00
|
|
|
KVKeys.pathLocation: location.path,
|
2022-12-20 03:14:42 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clean the location.
|
|
|
|
static Future<void> cleanTestLocation(String? name) async {
|
|
|
|
final dir = await testLocation(name);
|
|
|
|
await dir.delete(recursive: true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get current using location.
|
|
|
|
static Future<String> currentLocation() async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
2023-05-21 10:53:59 +00:00
|
|
|
return prefs.getString(KVKeys.pathLocation)!;
|
2022-12-20 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get default location under development environment.
|
|
|
|
static Future<String> defaultDevelopmentLocation() async {
|
|
|
|
final dir = await appFlowyDocumentDirectory();
|
|
|
|
return dir.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get default location under test environment.
|
|
|
|
static Future<Directory> testLocation(String? name) async {
|
|
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
|
|
var path = '${dir.path}/flowy_test';
|
|
|
|
if (name != null) {
|
|
|
|
path += '/$name';
|
|
|
|
}
|
|
|
|
return Directory(path).create(recursive: true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AppFlowyTestBase on WidgetTester {
|
|
|
|
Future<void> initializeAppFlowy() async {
|
2023-05-28 04:09:39 +00:00
|
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
2023-06-15 14:43:07 +00:00
|
|
|
.setMockMethodCallHandler(const MethodChannel('hotkey_manager'),
|
|
|
|
(MethodCall methodCall) async {
|
|
|
|
if (methodCall.method == 'unregisterAll') {
|
|
|
|
// do nothing
|
|
|
|
}
|
2023-05-28 04:09:39 +00:00
|
|
|
|
2023-06-15 14:43:07 +00:00
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
await FlowyRunner.run(FlowyApp(), IntegrationMode.integrationTest);
|
2022-12-20 03:14:42 +00:00
|
|
|
|
|
|
|
await wait(3000);
|
|
|
|
await pumpAndSettle(const Duration(seconds: 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> tapButton(
|
|
|
|
Finder finder, {
|
|
|
|
int? pointer,
|
|
|
|
int buttons = kPrimaryButton,
|
|
|
|
bool warnIfMissed = true,
|
|
|
|
int milliseconds = 500,
|
|
|
|
}) async {
|
|
|
|
await tap(finder);
|
|
|
|
await pumpAndSettle(Duration(milliseconds: milliseconds));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> tapButtonWithName(
|
|
|
|
String tr, {
|
|
|
|
int milliseconds = 500,
|
|
|
|
}) async {
|
2023-06-12 06:36:55 +00:00
|
|
|
Finder button = find.text(
|
|
|
|
tr,
|
|
|
|
findRichText: true,
|
|
|
|
skipOffstage: false,
|
|
|
|
);
|
|
|
|
if (button.evaluate().isEmpty) {
|
|
|
|
button = find.byWidgetPredicate(
|
|
|
|
(widget) => widget is FlowyText && widget.title == tr,
|
|
|
|
);
|
|
|
|
}
|
2022-12-20 03:14:42 +00:00
|
|
|
await tapButton(
|
|
|
|
button,
|
|
|
|
milliseconds: milliseconds,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> tapButtonWithTooltip(
|
|
|
|
String tr, {
|
|
|
|
int milliseconds = 500,
|
|
|
|
}) async {
|
|
|
|
final button = find.byTooltip(tr);
|
|
|
|
await tapButton(
|
|
|
|
button,
|
|
|
|
milliseconds: milliseconds,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> wait(int milliseconds) async {
|
|
|
|
await pumpAndSettle(Duration(milliseconds: milliseconds));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 08:33:44 +00:00
|
|
|
|
|
|
|
extension AppFlowyFinderTestBase on CommonFinders {
|
|
|
|
Finder findTextInFlowyText(String text) {
|
|
|
|
return find.byWidgetPredicate(
|
|
|
|
(widget) => widget is FlowyText && widget.title == text,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|