blob: 5ac8e3336f98c052fc98093eab2dc904e1661309 [file] [log] [blame]
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Prefix this import to avoid accidentally using IO stuff in cross-platform
// contexts.
import '../util/io.dart' as io;
import 'operating_system.dart';
import 'runtime.dart';
/// The platform on which a test suite is loaded.
class SuitePlatform {
/// The runtime that hosts the suite.
final Runtime runtime;
/// The operating system on which the suite is running.
///
/// This will always be [OperatingSystem.none] if `runtime.isBrowser` is
/// true.
final OperatingSystem os;
/// Whether we're running on Google-internal infrastructure.
final bool inGoogle;
/// Creates a new platform with the given [runtime] and [os], which defaults
/// to [OperatingSystem.none].
///
/// Throws an [ArgumentError] if [runtime] is a browser and [os] is not
/// `null` or [OperatingSystem.none].
SuitePlatform(this.runtime, {OperatingSystem os, this.inGoogle: false})
: os = os ?? OperatingSystem.none {
if (runtime.isBrowser && this.os != OperatingSystem.none) {
throw new ArgumentError('No OS should be passed for runtime "$runtime".');
}
}
/// Creates a new platform with the given [runtime] and [os] and [inGoogle]
/// determined using `dart:io`.
///
/// If [runtime] is a browser, this will set [os] to [OperatingSystem.none].
///
/// Throws an [UnsupportedError] if called in a context where `dart:io` is
/// unavailable.
SuitePlatform.current(this.runtime)
: os = runtime.isBrowser ? OperatingSystem.none : io.currentOS,
inGoogle = io.inGoogle;
/// Converts a JSON-safe representation generated by [serialize] back into a
/// [SuitePlatform].
factory SuitePlatform.deserialize(Object serialized) {
var map = serialized as Map;
return new SuitePlatform(new Runtime.deserialize(map['runtime']),
os: OperatingSystem.find(map['os']), inGoogle: map['inGoogle']);
}
/// Converts [this] into a JSON-safe object that can be converted back to a
/// [SuitePlatform] using [new SuitePlatform.deserialize].
Object serialize() => {
'runtime': runtime.serialize(),
'os': os.identifier,
'inGoogle': inGoogle
};
}