Line data Source code
1 : // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 : // for details. All rights reserved. Use of this source code is governed by a
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : /// An enum of all Dart runtimes supported by the test runner.
6 : class Runtime {
7 : // When adding new runtimes, be sure to update the baseline and derived
8 : // variable tests in test/backend/platform_selector/evaluate_test.
9 :
10 : /// The command-line Dart VM.
11 : static const Runtime vm = Runtime('VM', 'vm', isDartVM: true);
12 :
13 : /// Google Chrome.
14 : static const Runtime chrome =
15 : Runtime('Chrome', 'chrome', isBrowser: true, isJS: true, isBlink: true);
16 :
17 : /// Mozilla Firefox.
18 : static const Runtime firefox =
19 : Runtime('Firefox', 'firefox', isBrowser: true, isJS: true);
20 :
21 : /// Apple Safari.
22 : static const Runtime safari =
23 : Runtime('Safari', 'safari', isBrowser: true, isJS: true);
24 :
25 : /// Microsoft Internet Explorer.
26 : static const Runtime internetExplorer =
27 : Runtime('Internet Explorer', 'ie', isBrowser: true, isJS: true);
28 :
29 : /// The command-line Node.js VM.
30 : static const Runtime nodeJS = Runtime('Node.js', 'node', isJS: true);
31 :
32 : /// The platforms that are supported by the test runner by default.
33 : static const List<Runtime> builtIn = [
34 : Runtime.vm,
35 : Runtime.chrome,
36 : Runtime.firefox,
37 : Runtime.safari,
38 : Runtime.internetExplorer,
39 : Runtime.nodeJS
40 : ];
41 :
42 : /// The human-friendly name of the platform.
43 : final String name;
44 :
45 : /// The identifier used to look up the platform.
46 : final String identifier;
47 :
48 : /// The parent platform that this is based on, or `null` if there is no
49 : /// parent.
50 : final Runtime? parent;
51 :
52 : /// Returns whether this is a child of another platform.
53 0 : bool get isChild => parent != null;
54 :
55 : /// Whether this platform runs the Dart VM in any capacity.
56 : final bool isDartVM;
57 :
58 : /// Whether this platform is a browser.
59 : final bool isBrowser;
60 :
61 : /// Whether this platform runs Dart compiled to JavaScript.
62 : final bool isJS;
63 :
64 : /// Whether this platform uses the Blink rendering engine.
65 : final bool isBlink;
66 :
67 : /// Whether this platform has no visible window.
68 : final bool isHeadless;
69 :
70 : /// Returns the platform this is based on, or [this] if it's not based on
71 : /// anything.
72 : ///
73 : /// That is, returns [parent] if it's non-`null` or [this] if it's `null`.
74 0 : Runtime get root => parent ?? this;
75 :
76 11 : const Runtime(this.name, this.identifier,
77 : {this.isDartVM = false,
78 : this.isBrowser = false,
79 : this.isJS = false,
80 : this.isBlink = false,
81 : this.isHeadless = false})
82 : : parent = null;
83 :
84 0 : Runtime._child(this.name, this.identifier, Runtime this.parent)
85 0 : : isDartVM = parent.isDartVM,
86 0 : isBrowser = parent.isBrowser,
87 0 : isJS = parent.isJS,
88 0 : isBlink = parent.isBlink,
89 0 : isHeadless = parent.isHeadless;
90 :
91 : /// Converts a JSON-safe representation generated by [serialize] back into a
92 : /// [Runtime].
93 11 : factory Runtime.deserialize(Object serialized) {
94 11 : if (serialized is String) {
95 : return builtIn
96 44 : .firstWhere((platform) => platform.identifier == serialized);
97 : }
98 :
99 : var map = serialized as Map;
100 0 : var parent = map['parent'];
101 : if (parent != null) {
102 : // Note that the returned platform's [parent] won't necessarily be `==` to
103 : // a separately-deserialized parent platform. This should be fine, though,
104 : // since we only deserialize platforms in the remote execution context
105 : // where they're only used to evaluate platform selectors.
106 0 : return Runtime._child(map['name'] as String, map['identifier'] as String,
107 0 : Runtime.deserialize(parent as Object));
108 : }
109 :
110 0 : return Runtime(map['name'] as String, map['identifier'] as String,
111 0 : isDartVM: map['isDartVM'] as bool,
112 0 : isBrowser: map['isBrowser'] as bool,
113 0 : isJS: map['isJS'] as bool,
114 0 : isBlink: map['isBlink'] as bool,
115 0 : isHeadless: map['isHeadless'] as bool);
116 : }
117 :
118 : /// Converts [this] into a JSON-safe object that can be converted back to a
119 : /// [Runtime] using [Runtime.deserialize].
120 0 : Object serialize() {
121 0 : if (builtIn.contains(this)) return identifier;
122 :
123 0 : if (parent != null) {
124 0 : return {
125 0 : 'name': name,
126 0 : 'identifier': identifier,
127 0 : 'parent': parent!.serialize()
128 : };
129 : }
130 :
131 0 : return {
132 0 : 'name': name,
133 0 : 'identifier': identifier,
134 0 : 'isDartVM': isDartVM,
135 0 : 'isBrowser': isBrowser,
136 0 : 'isJS': isJS,
137 0 : 'isBlink': isBlink,
138 0 : 'isHeadless': isHeadless
139 : };
140 : }
141 :
142 : /// Returns a child of [this] that counts as both this platform's identifier
143 : /// and the new [identifier].
144 : ///
145 : /// This may not be called on a platform that's already a child.
146 0 : Runtime extend(String name, String identifier) {
147 0 : if (parent == null) return Runtime._child(name, identifier, this);
148 0 : throw StateError('A child platform may not be extended.');
149 : }
150 :
151 0 : @override
152 0 : String toString() => name;
153 : }
|