"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/dev/integration_tests/ios_add2app_life_cycle/flutterapp/lib/main.dart" (13 Nov 2020, 2623 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 import 'dart:ui' as ui;
    6 import 'package:flutter/material.dart';
    7 import 'package:flutter/rendering.dart';
    8 import 'package:flutter/services.dart';
    9 import 'package:flutter/foundation.dart';
   10 import 'package:collection/collection.dart';
   11 
   12 VoidCallback originalSemanticsListener;
   13 
   14 void main() {
   15   WidgetsFlutterBinding.ensureInitialized();
   16   // Disconnects semantics listener for testing purposes.
   17   originalSemanticsListener = ui.window.onSemanticsEnabledChanged;
   18   ui.window.onSemanticsEnabledChanged = null;
   19   RendererBinding.instance.setSemanticsEnabled(false);
   20   // If the test passes, LifeCycleSpy will rewire the semantics listener back.
   21   runApp(const LifeCycleSpy());
   22 }
   23 
   24 /// A Test widget that spies on app life cycle changes.
   25 ///
   26 /// It will collect the AppLifecycleState sequence during its lifetime, and it
   27 /// will rewire semantics harness if the sequence it receives matches the
   28 /// expected list.
   29 ///
   30 /// Rewiring semantics is a signal to native IOS test that the test has passed.
   31 class LifeCycleSpy extends StatefulWidget {
   32   const LifeCycleSpy();
   33 
   34   @override
   35   _LifeCycleSpyState createState() => _LifeCycleSpyState();
   36 }
   37 
   38 class _LifeCycleSpyState extends State<LifeCycleSpy> with WidgetsBindingObserver {
   39   final List<AppLifecycleState> _expectedLifeCycleSequence = <AppLifecycleState>[
   40     AppLifecycleState.detached,
   41     AppLifecycleState.inactive,
   42     AppLifecycleState.resumed,
   43   ];
   44   List<AppLifecycleState> _actualLifeCycleSequence;
   45 
   46   @override
   47   void initState(){
   48     super.initState();
   49     WidgetsBinding.instance.addObserver(this);
   50     _actualLifeCycleSequence =  <AppLifecycleState>[
   51       ServicesBinding.instance.lifecycleState
   52     ];
   53   }
   54 
   55   @override
   56   void dispose() {
   57     WidgetsBinding.instance.removeObserver(this);
   58     super.dispose();
   59   }
   60 
   61   @override
   62   void didChangeAppLifecycleState(AppLifecycleState state) {
   63     setState(() {
   64       _actualLifeCycleSequence = List<AppLifecycleState>.from(_actualLifeCycleSequence);
   65       _actualLifeCycleSequence.add(state);
   66     });
   67   }
   68 
   69   @override
   70   Widget build(BuildContext context) {
   71     if (const ListEquality<AppLifecycleState>().equals(_actualLifeCycleSequence, _expectedLifeCycleSequence)) {
   72       // Rewires the semantics harness if test passes.
   73       RendererBinding.instance.setSemanticsEnabled(true);
   74       ui.window.onSemanticsEnabledChanged = originalSemanticsListener;
   75     }
   76     return const MaterialApp(
   77       title: 'Flutter View',
   78       home: Text('test'),
   79     );
   80   }
   81 }