|
| 1 | +// Copyright 2014 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 | + |
| 5 | +// ignore_for_file: invalid_use_of_internal_member |
| 6 | + |
| 7 | +import 'dart:async'; |
| 8 | +import 'dart:convert'; |
| 9 | +import 'dart:io'; |
| 10 | + |
| 11 | +import 'package:flutter/material.dart'; |
| 12 | +import 'package:flutter/src/widgets/_window.dart'; |
| 13 | +import 'package:flutter_driver/driver_extension.dart'; |
| 14 | + |
| 15 | +class _MainRegularWindowControllerDelegate |
| 16 | + extends RegularWindowControllerDelegate { |
| 17 | + @override |
| 18 | + void onWindowDestroyed() { |
| 19 | + super.onWindowDestroyed(); |
| 20 | + |
| 21 | + exit(0); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +late final RegularWindowController controller; |
| 26 | + |
| 27 | +void main() { |
| 28 | + final Completer<void> windowCreated = Completer(); |
| 29 | + enableFlutterDriverExtension( |
| 30 | + handler: (String? message) async { |
| 31 | + await windowCreated.future; |
| 32 | + if (message == null) { |
| 33 | + return ''; |
| 34 | + } |
| 35 | + |
| 36 | + final jsonMap = jsonDecode(message); |
| 37 | + if (!jsonMap.containsKey('type')) { |
| 38 | + throw ArgumentError('Message must contain a "type" field.'); |
| 39 | + } |
| 40 | + |
| 41 | + if (jsonMap['type'] == 'get_size') { |
| 42 | + return jsonEncode({ |
| 43 | + 'width': controller.contentSize.width, |
| 44 | + 'height': controller.contentSize.height, |
| 45 | + }); |
| 46 | + } else if (jsonMap['type'] == 'set_size') { |
| 47 | + final Size size = Size( |
| 48 | + jsonMap['width'].toDouble(), |
| 49 | + jsonMap['height'].toDouble(), |
| 50 | + ); |
| 51 | + controller.setSize(size); |
| 52 | + } else if (jsonMap['type'] == 'set_constraints') { |
| 53 | + final BoxConstraints constraints = BoxConstraints( |
| 54 | + minWidth: jsonMap['min_width'].toDouble(), |
| 55 | + minHeight: jsonMap['min_height'].toDouble(), |
| 56 | + maxWidth: jsonMap['max_width'].toDouble(), |
| 57 | + maxHeight: jsonMap['max_height'].toDouble(), |
| 58 | + ); |
| 59 | + controller.setConstraints(constraints); |
| 60 | + } else if (jsonMap['type'] == 'set_fullscreen') { |
| 61 | + controller.setFullscreen(true); |
| 62 | + } else if (jsonMap['type'] == 'unset_fullscreen') { |
| 63 | + controller.setFullscreen(false); |
| 64 | + } else if (jsonMap['type'] == 'get_fullscreen') { |
| 65 | + return jsonEncode({'isFullscreen': controller.isFullscreen}); |
| 66 | + } else if (jsonMap['type'] == 'set_maximized') { |
| 67 | + controller.setMaximized(true); |
| 68 | + } else if (jsonMap['type'] == 'unset_maximized') { |
| 69 | + controller.setMaximized(false); |
| 70 | + } else if (jsonMap['type'] == 'get_maximized') { |
| 71 | + return jsonEncode({'isMaximized': controller.isMaximized}); |
| 72 | + } else if (jsonMap['type'] == 'set_minimized') { |
| 73 | + controller.setMinimized(true); |
| 74 | + } else if (jsonMap['type'] == 'unset_minimized') { |
| 75 | + controller.setMinimized(false); |
| 76 | + } else if (jsonMap['type'] == 'get_minimized') { |
| 77 | + return jsonEncode({'isMinimized': controller.isMinimized}); |
| 78 | + } else if (jsonMap['type'] == 'set_title') { |
| 79 | + controller.setTitle(jsonMap['title']); |
| 80 | + } else if (jsonMap['type'] == 'get_title') { |
| 81 | + return jsonEncode({'title': controller.title}); |
| 82 | + } else if (jsonMap['type'] == 'set_activated') { |
| 83 | + controller.activate(); |
| 84 | + } else if (jsonMap['type'] == 'get_activated') { |
| 85 | + return jsonEncode({'isActivated': controller.isActivated}); |
| 86 | + } else { |
| 87 | + throw ArgumentError('Unknown message type: ${jsonMap['type']}'); |
| 88 | + } |
| 89 | + |
| 90 | + return ''; |
| 91 | + }, |
| 92 | + ); |
| 93 | + controller = RegularWindowController( |
| 94 | + preferredSize: Size(640, 480), |
| 95 | + title: 'Integration Test', |
| 96 | + delegate: _MainRegularWindowControllerDelegate(), |
| 97 | + ); |
| 98 | + windowCreated.complete(); |
| 99 | + |
| 100 | + runWidget(RegularWindow(controller: controller, child: MyApp())); |
| 101 | +} |
| 102 | + |
| 103 | +class MyApp extends StatelessWidget { |
| 104 | + const MyApp({super.key}); |
| 105 | + |
| 106 | + @override |
| 107 | + Widget build(BuildContext context) { |
| 108 | + return MaterialApp( |
| 109 | + title: 'Flutter Demo', |
| 110 | + theme: ThemeData( |
| 111 | + colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
| 112 | + ), |
| 113 | + home: const MyHomePage(title: 'Flutter Demo Home Page'), |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +class MyHomePage extends StatefulWidget { |
| 119 | + const MyHomePage({super.key, required this.title}); |
| 120 | + |
| 121 | + final String title; |
| 122 | + |
| 123 | + @override |
| 124 | + State<MyHomePage> createState() => _MyHomePageState(); |
| 125 | +} |
| 126 | + |
| 127 | +class _MyHomePageState extends State<MyHomePage> { |
| 128 | + int _counter = 0; |
| 129 | + |
| 130 | + void _incrementCounter() { |
| 131 | + setState(() { |
| 132 | + _counter++; |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + @override |
| 137 | + Widget build(BuildContext context) { |
| 138 | + return Scaffold( |
| 139 | + appBar: AppBar( |
| 140 | + backgroundColor: Theme.of(context).colorScheme.inversePrimary, |
| 141 | + title: Text(widget.title), |
| 142 | + ), |
| 143 | + body: Center( |
| 144 | + child: Column( |
| 145 | + mainAxisAlignment: MainAxisAlignment.center, |
| 146 | + children: <Widget>[ |
| 147 | + const Text('You have pushed the button this many times:'), |
| 148 | + Text( |
| 149 | + '$_counter', |
| 150 | + style: Theme.of(context).textTheme.headlineMedium, |
| 151 | + ), |
| 152 | + ], |
| 153 | + ), |
| 154 | + ), |
| 155 | + floatingActionButton: FloatingActionButton( |
| 156 | + onPressed: _incrementCounter, |
| 157 | + tooltip: 'Increment', |
| 158 | + child: const Icon(Icons.add), |
| 159 | + ), |
| 160 | + ); |
| 161 | + } |
| 162 | +} |
0 commit comments