forked from firka/flutter
Add basic codegen app to be used for integration testing and benchmarks (#27257)
This commit is contained in:
58
dev/integration_tests/codegen/lib/coffee_app.dart
Normal file
58
dev/integration_tests/codegen/lib/coffee_app.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:inject/inject.dart';
|
||||
|
||||
// This is a compile-time generated file and does not exist in source.
|
||||
import 'coffee_app.inject.dart' as generated; // ignore: uri_does_not_exist
|
||||
import 'src/coffee.dart';
|
||||
|
||||
@module
|
||||
class PourOverCoffeeModule {
|
||||
@provide
|
||||
@brandName
|
||||
String provideBrand() => 'Coffee by Flutter Inc.';
|
||||
|
||||
@provide
|
||||
@modelName
|
||||
String provideModel() => 'PourOverSupremeFiesta';
|
||||
|
||||
@provide
|
||||
@asynchronous
|
||||
Future<Heater> provideHeater() async => Stove();
|
||||
|
||||
@provide
|
||||
Pump providePump(Heater heater) => NoOpPump();
|
||||
}
|
||||
|
||||
class NoOpPump extends Pump {
|
||||
@override
|
||||
void pump() {
|
||||
print('nothing to pump...');
|
||||
}
|
||||
}
|
||||
|
||||
class Stove extends Heater {
|
||||
@override
|
||||
bool get isHot => _isHot;
|
||||
bool _isHot = false;
|
||||
|
||||
@override
|
||||
void off() {
|
||||
_isHot = true;
|
||||
}
|
||||
|
||||
@override
|
||||
void on() {
|
||||
_isHot = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Injector(<Type>[PourOverCoffeeModule])
|
||||
abstract class CoffeeApp {
|
||||
static final Future<CoffeeApp> Function(PourOverCoffeeModule) create = generated.CoffeeApp$Injector.create;
|
||||
|
||||
@provide
|
||||
CoffeeMaker getCoffeeMaker();
|
||||
}
|
||||
49
dev/integration_tests/codegen/lib/main.dart
Normal file
49
dev/integration_tests/codegen/lib/main.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_driver/driver_extension.dart';
|
||||
|
||||
import 'coffee_app.dart';
|
||||
import 'src/coffee.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
enableFlutterDriverExtension();
|
||||
coffeeApp = await CoffeeApp.create(PourOverCoffeeModule());
|
||||
runApp(ExampleWidget());
|
||||
}
|
||||
|
||||
CoffeeApp coffeeApp;
|
||||
|
||||
class ExampleWidget extends StatefulWidget {
|
||||
@override
|
||||
_ExampleWidgetState createState() => _ExampleWidgetState();
|
||||
}
|
||||
|
||||
class _ExampleWidgetState extends State<ExampleWidget> {
|
||||
String _message = '';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
RaisedButton(
|
||||
child: const Text('Press Button, Get Coffee'),
|
||||
onPressed: () async {
|
||||
final CoffeeMaker coffeeMaker = coffeeApp.getCoffeeMaker();
|
||||
setState(() {
|
||||
_message = coffeeMaker.brew();
|
||||
});
|
||||
},
|
||||
),
|
||||
Text(_message),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
41
dev/integration_tests/codegen/lib/src/coffee.dart
Normal file
41
dev/integration_tests/codegen/lib/src/coffee.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:inject/inject.dart';
|
||||
|
||||
const Qualifier brandName = Qualifier(#brandName);
|
||||
const Qualifier modelName = Qualifier(#modelName);
|
||||
|
||||
class CoffeeMaker {
|
||||
@provide
|
||||
CoffeeMaker(this._heater, this._pump, this._brand, this._model);
|
||||
|
||||
final Heater _heater;
|
||||
final Pump _pump;
|
||||
|
||||
@modelName
|
||||
final String _model;
|
||||
|
||||
@brandName
|
||||
final String _brand;
|
||||
|
||||
String brew() {
|
||||
_heater.on();
|
||||
_pump.pump();
|
||||
print(' [_]P coffee! [_]P');
|
||||
final String message = 'Thanks for using $_model by $_brand';
|
||||
_heater.off();
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Heater {
|
||||
void on();
|
||||
void off();
|
||||
bool get isHot;
|
||||
}
|
||||
|
||||
abstract class Pump {
|
||||
void pump();
|
||||
}
|
||||
Reference in New Issue
Block a user