From 36aaabf582c27b68b279c1cba7f50e798b27035d Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 16 Apr 2020 17:34:38 +0200 Subject: [PATCH] TestDriver: Take some more screenshots * Add some existing notes * Screenshot the checklist edit * Screenshot the folders view --- test_driver/isolates_workaround.dart | 67 +++++++++++++++++++ test_driver/main.dart | 99 ++++++++++++++++++++++++++++ test_driver/main_test.dart | 38 +++++++++-- 3 files changed, 200 insertions(+), 4 deletions(-) create mode 100644 test_driver/isolates_workaround.dart diff --git a/test_driver/isolates_workaround.dart b/test_driver/isolates_workaround.dart new file mode 100644 index 00000000..a0c6df0e --- /dev/null +++ b/test_driver/isolates_workaround.dart @@ -0,0 +1,67 @@ +import 'dart:async'; + +import 'package:flutter_driver/flutter_driver.dart'; + +/// Workaround for bug: https://github.com/flutter/flutter/issues/24703 +/// +/// USAGE +/// +/// ``` +/// FlutterDriver driver; +/// IsolatesWorkaround workaround; +/// +/// setUpAll(() async { +/// driver = await FlutterDriver.connect(); +/// workaround = IsolatesWorkaround(driver); +/// await workaround.resumeIsolates(); +/// }); +/// +/// tearDownAll(() async { +/// if (driver != null) { +/// await driver.close(); +/// await workaround.tearDown(); +/// } +/// }); +/// ``` +class IsolatesWorkaround { + IsolatesWorkaround(this._driver, {this.log = false}); + final FlutterDriver _driver; + final bool log; + StreamSubscription _streamSubscription; + + /// workaround for isolates + /// https://github.com/flutter/flutter/issues/24703 + Future resumeIsolates() async { + final vm = await _driver.serviceClient.getVM(); + // // unpause any paused isolated + for (final isolateRef in vm.isolates) { + final isolate = await isolateRef.load(); + if (isolate.isPaused) { + isolate.resume(); + if (log) { + print("Resuming isolate: ${isolate.numberAsString}:${isolate.name}"); + } + } + } + if (_streamSubscription != null) { + return; + } + _streamSubscription = _driver.serviceClient.onIsolateRunnable + .asBroadcastStream() + .listen((isolateRef) async { + final isolate = await isolateRef.load(); + if (isolate.isPaused) { + isolate.resume(); + if (log) { + print("Resuming isolate: ${isolate.numberAsString}:${isolate.name}"); + } + } + }); + } + + Future tearDown() async { + if (_streamSubscription != null) { + await _streamSubscription.cancel(); + } + } +} diff --git a/test_driver/main.dart b/test_driver/main.dart index f3d2b7c5..31210708 100644 --- a/test_driver/main.dart +++ b/test_driver/main.dart @@ -1,7 +1,16 @@ +import 'dart:io'; + import 'package:flutter_driver/driver_extension.dart'; +import 'package:gitjournal/apis/git.dart'; + import 'package:gitjournal/app.dart'; +import 'package:gitjournal/appstate.dart'; import 'package:gitjournal/settings.dart'; +import 'package:gitjournal/utils/datetime.dart'; + import 'package:shared_preferences/shared_preferences.dart'; +import 'package:path/path.dart' as p; +import 'package:dart_git/git.dart'; void main() async { enableFlutterDriverExtension(); @@ -9,5 +18,95 @@ void main() async { var pref = await SharedPreferences.getInstance(); Settings.instance.load(pref); + await populateWithData(pref); await JournalApp.main(pref); } + +// Generate lots of notes and folders better screenshots +Future populateWithData(SharedPreferences pref) async { + var dir = await getGitBaseDirectory(); + + var appState = AppState(pref); + appState.gitBaseDirectory = dir.path; + appState.localGitRepoConfigured = true; + appState.localGitRepoFolderName = "journal_local"; + appState.save(pref); + + var repoPath = p.join(dir.path, appState.localGitRepoFolderName); + await GitRepository.init(repoPath); + + print("Filling fake data in $repoPath"); + + // Write Folders + Directory(p.join(repoPath, "GitJournal")).createSync(); + Directory(p.join(repoPath, "Journal/Work")).createSync(recursive: true); + Directory(p.join(repoPath, "Journal/Personal")).createSync(recursive: true); + Directory(p.join(repoPath, "Food")).createSync(); + + // Write notes + createChecklist(p.join(repoPath, "checklist.md"), DateTime.now()); + createNoteWithTitle( + p.join(repoPath, "note1.md"), + DateTime.now(), + "Desire", + "Haven't you always wanted such an app?", + ); + createNote( + p.join(repoPath, "note2.md"), + DateTime.now(), + "There is not a pipe", + ); + + createNote( + p.join(repoPath, "note2.md"), + DateTime.now(), + "There is not a pipe", + ); +} + +void createNote(String filePath, DateTime dt, String body) { + var content = """--- +modified: ${toIso8601WithTimezone(dt)} +created: ${toIso8601WithTimezone(dt)} +--- + +$body +"""; + + File(filePath).writeAsStringSync(content); +} + +void createNoteWithTitle( + String filePath, + DateTime dt, + String title, + String body, +) { + var content = """--- +modified: ${toIso8601WithTimezone(dt)} +created: ${toIso8601WithTimezone(dt)} +title: $title +--- + +$body +"""; + + File(filePath).writeAsStringSync(content); +} + +void createChecklist(String filePath, DateTime dt) { + var content = """--- +modified: ${toIso8601WithTimezone(dt)} +created: ${toIso8601WithTimezone(dt)} +title: Shopping List +type: Checklist +--- + +[ ] Bananas +[ ] Rice +[ ] Cat Food +[x] Tomatoes +"""; + + File(filePath).writeAsStringSync(content); +} diff --git a/test_driver/main_test.dart b/test_driver/main_test.dart index 26817b16..e2299c55 100644 --- a/test_driver/main_test.dart +++ b/test_driver/main_test.dart @@ -2,22 +2,30 @@ import 'package:flutter_driver/flutter_driver.dart'; import 'package:test/test.dart'; import 'package:screenshots/screenshots.dart'; +import 'isolates_workaround.dart'; + void main() { group('Test', () { FlutterDriver driver; + IsolatesWorkaround workaround; + int screenshotNum = 0; final config = Config(); // Connect to the Flutter driver before running any tests setUpAll(() async { driver = await FlutterDriver.connect(); + workaround = IsolatesWorkaround(driver); + await workaround.resumeIsolates(); + await Future.delayed(const Duration(seconds: 5)); }); // Close the connection to the driver after the tests have completed tearDownAll(() async { if (driver != null) { - driver.close(); + await driver.close(); + await workaround.tearDown(); } }); @@ -52,13 +60,13 @@ void main() { await driver.tap(find.byValueKey("GetStarted")); // Main Screen - final loadedFinder = find.text("Let's add some notes?"); - await driver.waitFor(loadedFinder, timeout: const Duration(seconds: 5)); + //final loadedFinder = find.text("Let's add some notes?"); + // await driver.waitFor(loadedFinder, timeout: const Duration(seconds: 5)); // await _takeScreenshot(); // Create a new note var fab = find.byValueKey("FAB"); - await driver.waitFor(fab, timeout: const Duration(seconds: 2)); + await driver.waitFor(fab, timeout: const Duration(seconds: 5)); await driver.tap(fab); await driver.waitFor(find.text('Write here'), timeout: const Duration(seconds: 5)); @@ -149,12 +157,34 @@ void main() { await Future.delayed(const Duration(milliseconds: 100)); await _takeScreenshot(); + // Select the Checklist + var checklist = find.text("Shopping List"); + await driver.waitFor(checklist, timeout: const Duration(seconds: 2)); + await driver.tap(checklist); + await Future.delayed(const Duration(milliseconds: 100)); + await _takeScreenshot(); + await driver.tap(find.byValueKey("NewEntry")); + // Open the Drawer final drawerButtonFinder = find.byValueKey("DrawerButton"); await driver.tap(drawerButtonFinder); await Future.delayed(const Duration(milliseconds: 100)); await _takeScreenshot(); + // Folders View + var foldersButon = find.text("Folders"); + await driver.waitFor(foldersButon, timeout: const Duration(seconds: 2)); + await driver.tap(foldersButon); + + var rootFolder = find.text("Root Folder"); + await driver.waitFor(rootFolder, timeout: const Duration(seconds: 2)); + await _takeScreenshot(); + + // Open the Drawer + await driver.tap(drawerButtonFinder); + await Future.delayed(const Duration(milliseconds: 100)); + await _takeScreenshot(); + // The Git Host setup screen await driver.tap(find.text("Setup Git Host")); await driver.waitFor(find.text("GitHub"),