"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/dev/integration_tests/android_semantics_testing/lib/src/common.dart" (13 Nov 2020, 7003 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:convert';
    6 
    7 import 'package:meta/meta.dart';
    8 import 'constants.dart';
    9 
   10 /// A semantics node created from Android accessibility information.
   11 ///
   12 /// This object represents Android accessibility information derived from an
   13 /// [AccessibilityNodeInfo](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo)
   14 /// object. The purpose is to verify in integration
   15 /// tests that our semantics framework produces the correct accessibility info
   16 /// on Android.
   17 ///
   18 /// See also:
   19 ///
   20 ///   * [AccessibilityNodeInfo](https://developer.android.com/reference/android/view/accessibility/AccessibilityNodeInfo)
   21 class AndroidSemanticsNode  {
   22   AndroidSemanticsNode._(this._values);
   23 
   24   /// Deserializes a new [AndroidSemanticsNode] from a json map.
   25   ///
   26   /// The structure of the JSON:
   27   ///
   28   ///     {
   29   ///       "flags": {
   30   ///         "isChecked": bool,
   31   ///         "isCheckable": bool,
   32   ///         "isEditable": bool,
   33   ///         "isEnabled": bool,
   34   ///         "isFocusable": bool,
   35   ///         "isFocused": bool,
   36   ///         "isHeading": bool,
   37   ///         "isPassword": bool,
   38   ///         "isLongClickable": bool,
   39   ///       },
   40   ///       "text": String,
   41   ///       "contentDescription": String,
   42   ///       "className": String,
   43   ///       "id": int,
   44   ///       "rect": {
   45   ///         left: int,
   46   ///         top: int,
   47   ///         right: int,
   48   ///         bottom: int,
   49   ///       },
   50   ///       actions: [
   51   ///         int,
   52   ///       ]
   53   ///     }
   54   factory AndroidSemanticsNode.deserialize(String value) {
   55     return AndroidSemanticsNode._(json.decode(value) as Map<String, Object>);
   56   }
   57 
   58   final Map<String, Object> _values;
   59   final List<AndroidSemanticsNode> _children = <AndroidSemanticsNode>[];
   60 
   61   Map<String, Object> get _flags => _values['flags'] as Map<String, Object>;
   62 
   63   /// The text value of the semantics node.
   64   ///
   65   /// This is produced by combining the value, label, and hint fields from
   66   /// the Flutter [SemanticsNode].
   67   String get text => _values['text'] as String;
   68 
   69   /// The contentDescription of the semantics node.
   70   ///
   71   /// This field is used for the Switch, Radio, and Checkbox widgets
   72   /// instead of [text]. If the text property is used for these, TalkBack
   73   /// will not read out the "checked" or "not checked" label by default.
   74   ///
   75   /// This is produced by combining the value, label, and hint fields from
   76   /// the Flutter [SemanticsNode].
   77   String get contentDescription => _values['contentDescription'] as String;
   78 
   79   /// The className of the semantics node.
   80   ///
   81   /// Certain kinds of Flutter semantics are mapped to Android classes to
   82   /// use their default semantic behavior, such as checkboxes and images.
   83   ///
   84   /// If a more specific value isn't provided, it defaults to
   85   /// "android.view.View".
   86   String get className => _values['className'] as String;
   87 
   88   /// The identifier for this semantics node.
   89   int get id => _values['id'] as int;
   90 
   91   /// The children of this semantics node.
   92   List<AndroidSemanticsNode> get children => _children;
   93 
   94   /// Whether the node is currently in a checked state.
   95   ///
   96   /// Equivalent to [SemanticsFlag.isChecked].
   97   bool get isChecked => _flags['isChecked'] as bool;
   98 
   99   /// Whether the node can be in a checked state.
  100   ///
  101   /// Equivalent to [SemanticsFlag.hasCheckedState]
  102   bool get isCheckable => _flags['isCheckable'] as bool;
  103 
  104   /// Whether the node is editable.
  105   ///
  106   /// This is usually only applied to text fields, which map
  107   /// to "android.widget.EditText".
  108   bool get isEditable => _flags['isEditable'] as bool;
  109 
  110   /// Whether the node is enabled.
  111   bool get isEnabled => _flags['isEnabled'] as bool;
  112 
  113   /// Whether the node is focusable.
  114   bool get isFocusable => _flags['isFocusable'] as bool;
  115 
  116   /// Whether the node is focused.
  117   bool get isFocused => _flags['isFocused'] as bool;
  118 
  119   /// Whether the node is considered a heading.
  120   bool get isHeading => _flags['isHeading'] as bool;
  121 
  122   /// Whether the node represents a password field.
  123   ///
  124   /// Equivalent to [SemanticsFlag.isObscured].
  125   bool get isPassword => _flags['isPassword'] as bool;
  126 
  127   /// Whether the node is long clickable.
  128   ///
  129   /// Equivalent to having [SemanticsAction.longPress].
  130   bool get isLongClickable => _flags['isLongClickable'] as bool;
  131 
  132   /// Gets a [Rect] which defines the position and size of the semantics node.
  133   Rect getRect() {
  134     final Map<String, Object> rawRect = _values['rect'] as Map<String, Object>;
  135     final Map<String, int> rect = rawRect.cast<String, int>();
  136     return Rect.fromLTRB(
  137       rect['left'].toDouble(),
  138       rect['top'].toDouble(),
  139       rect['right'].toDouble(),
  140       rect['bottom'].toDouble(),
  141     );
  142   }
  143 
  144   /// Gets a [Size] which defines the size of the semantics node.
  145   Size getSize() {
  146     final Rect rect = getRect();
  147     return Size(rect.bottom - rect.top, rect.right - rect.left);
  148   }
  149 
  150   /// Gets a list of [AndroidSemanticsActions] which are defined for the node.
  151   List<AndroidSemanticsAction> getActions() => <AndroidSemanticsAction>[
  152     for (final int id in (_values['actions'] as List<dynamic>).cast<int>()) AndroidSemanticsAction.deserialize(id),
  153   ];
  154 
  155   @override
  156   String toString() {
  157     return _values.toString();
  158   }
  159 }
  160 
  161 
  162 /// A Dart VM implementation of a rectangle.
  163 ///
  164 /// Created to mirror the implementation of [ui.Rect].
  165 @immutable
  166 class Rect {
  167   /// Creates a new rectangle.
  168   ///
  169   /// All values are required.
  170   const Rect.fromLTRB(this.left, this.top, this.right, this.bottom);
  171 
  172   /// The top side of the rectangle.
  173   final double top;
  174 
  175   /// The left side of the rectangle.
  176   final double left;
  177 
  178   /// The right side of the rectangle.
  179   final double right;
  180 
  181   /// The bottom side of the rectangle.
  182   final double bottom;
  183 
  184   @override
  185   int get hashCode =>
  186       top.hashCode ^ left.hashCode ^ right.hashCode ^ bottom.hashCode;
  187 
  188   @override
  189   bool operator ==(Object other) {
  190     if (other.runtimeType != runtimeType)
  191       return false;
  192     return other is Rect
  193         && other.top == top
  194         && other.left == left
  195         && other.right == right
  196         && other.bottom == bottom;
  197   }
  198 
  199   @override
  200   String toString() => 'Rect.fromLTRB($left, $top, $right, $bottom)';
  201 }
  202 
  203 /// A Dart VM implementation of a Size.
  204 ///
  205 /// Created to mirror the implementation [ui.Size].
  206 @immutable
  207 class Size {
  208   /// Creates a new [Size] object.
  209   const Size(this.width, this.height);
  210 
  211   /// The width of some object.
  212   final double width;
  213 
  214   /// The height of some object.
  215   final double height;
  216 
  217   @override
  218   int get hashCode => width.hashCode ^ height.hashCode;
  219 
  220   @override
  221   bool operator ==(Object other) {
  222     if (other.runtimeType != runtimeType)
  223       return false;
  224     return other is Size
  225         && other.width == width
  226         && other.height == height;
  227   }
  228 
  229   @override
  230   String toString() => 'Size{$width, $height}';
  231 }