features_test.dart 14.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_tools/src/base/config.dart';
6
import 'package:flutter_tools/src/base/platform.dart';
7
import 'package:flutter_tools/src/features.dart';
8
import 'package:flutter_tools/src/flutter_features.dart';
9 10

import '../src/common.dart';
11
import '../src/fakes.dart';
12 13 14

void main() {
  group('Features', () {
15 16 17
    late Config testConfig;
    late FakePlatform platform;
    late FlutterFeatureFlags featureFlags;
18 19

    setUp(() {
20
      testConfig = Config.test();
21
      platform = FakePlatform(environment: <String, String>{});
22

23
      for (final Feature feature in allConfigurableFeatures) {
24
        testConfig.setValue(feature.configSetting!, false);
25
      }
26 27

      featureFlags = FlutterFeatureFlags(
28
        flutterVersion: FakeFlutterVersion(),
29
        config: testConfig,
30
        platform: platform,
31
      );
32 33
    });

34 35
    FeatureFlags createFlags(String channel) {
      return FlutterFeatureFlags(
36
        flutterVersion: FakeFlutterVersion(branch: channel),
37 38 39 40 41
        config: testConfig,
        platform: platform,
      );
    }

42
    testWithoutContext('setting has safe defaults', () {
43 44 45 46 47 48
      const FeatureChannelSetting featureSetting = FeatureChannelSetting();

      expect(featureSetting.available, false);
      expect(featureSetting.enabledByDefault, false);
    });

49
    testWithoutContext('has safe defaults', () {
50 51 52 53 54 55 56
      const Feature feature = Feature(name: 'example');

      expect(feature.name, 'example');
      expect(feature.environmentOverride, null);
      expect(feature.configSetting, null);
    });

57
    testWithoutContext('retrieves the correct setting for each branch', () {
58 59 60 61
      const FeatureChannelSetting masterSetting = FeatureChannelSetting(available: true);
      const FeatureChannelSetting betaSetting = FeatureChannelSetting(available: true);
      const FeatureChannelSetting stableSetting = FeatureChannelSetting(available: true);
      const Feature feature = Feature(
62 63 64 65 66 67 68 69 70 71 72 73
        name: 'example',
        master: masterSetting,
        beta: betaSetting,
        stable: stableSetting,
      );

      expect(feature.getSettingForChannel('master'), masterSetting);
      expect(feature.getSettingForChannel('beta'), betaSetting);
      expect(feature.getSettingForChannel('stable'), stableSetting);
      expect(feature.getSettingForChannel('unknown'), masterSetting);
    });

74
    testWithoutContext('env variables are only enabled with "true" string', () {
75
      platform.environment = <String, String>{'FLUTTER_WEB': 'hello'};
76 77 78

      expect(featureFlags.isWebEnabled, false);

79
      platform.environment = <String, String>{'FLUTTER_WEB': 'true'};
80 81

      expect(featureFlags.isWebEnabled, true);
82
    });
83

84 85 86 87 88 89
    testWithoutContext('Flutter web wasm only enable on master', () {
      expect(flutterWebWasm.getSettingForChannel('master').enabledByDefault, isTrue);
      expect(flutterWebWasm.getSettingForChannel('beta').enabledByDefault, isFalse);
      expect(flutterWebWasm.getSettingForChannel('stable').enabledByDefault, isFalse);
    });

90
    testWithoutContext('Flutter web help string', () {
91
      expect(flutterWebFeature.generateHelpMessage(),
92
      'Enable or disable Flutter for web.');
93 94
    });

95
    testWithoutContext('Flutter macOS desktop help string', () {
96
      expect(flutterMacOSDesktopFeature.generateHelpMessage(),
97
      'Enable or disable support for desktop on macOS.');
98 99
    });

100
    testWithoutContext('Flutter Linux desktop help string', () {
101
      expect(flutterLinuxDesktopFeature.generateHelpMessage(),
102
      'Enable or disable support for desktop on Linux.');
103 104
    });

105
    testWithoutContext('Flutter Windows desktop help string', () {
106
      expect(flutterWindowsDesktopFeature.generateHelpMessage(),
107
      'Enable or disable support for desktop on Windows.');
108 109
    });

110 111
    testWithoutContext('help string on multiple channels', () {
      const Feature testWithoutContextFeature = Feature(
112 113 114 115 116 117 118
        name: 'example',
        master: FeatureChannelSetting(available: true),
        beta: FeatureChannelSetting(available: true),
        stable: FeatureChannelSetting(available: true),
        configSetting: 'foo',
      );

119
      expect(testWithoutContextFeature.generateHelpMessage(), 'Enable or disable example.');
120 121 122 123
    });

    /// Flutter Web

124
    testWithoutContext('Flutter web off by default on master', () {
125
      final FeatureFlags featureFlags = createFlags('master');
126 127

      expect(featureFlags.isWebEnabled, false);
128
    });
129

130
    testWithoutContext('Flutter web enabled with config on master', () {
131
      final FeatureFlags featureFlags = createFlags('master');
132
      testConfig.setValue('enable-web', true);
133 134

      expect(featureFlags.isWebEnabled, true);
135
    });
136

137
    testWithoutContext('Flutter web enabled with environment variable on master', () {
138 139
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_WEB': 'true'};
140 141

      expect(featureFlags.isWebEnabled, true);
142
    });
143

144
    testWithoutContext('Flutter web off by default on beta', () {
145
      final FeatureFlags featureFlags = createFlags('beta');
146 147

      expect(featureFlags.isWebEnabled, false);
148
    });
149

150
    testWithoutContext('Flutter web enabled with config on beta', () {
151
      final FeatureFlags featureFlags = createFlags('beta');
152
      testConfig.setValue('enable-web', true);
153

154
      expect(featureFlags.isWebEnabled, true);
155
    });
156

157
    testWithoutContext('Flutter web not enabled with environment variable on beta', () {
158 159
     final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_WEB': 'true'};
160

161
      expect(featureFlags.isWebEnabled, true);
162
    });
163

164
    testWithoutContext('Flutter web on by default on stable', () {
165
      final FeatureFlags featureFlags = createFlags('stable');
166
      testConfig.removeValue('enable-web');
167

168
      expect(featureFlags.isWebEnabled, true);
169
    });
170

171
    testWithoutContext('Flutter web enabled with config on stable', () {
172
      final FeatureFlags featureFlags = createFlags('stable');
173
      testConfig.setValue('enable-web', true);
174

175
      expect(featureFlags.isWebEnabled, true);
176
    });
177

178
    testWithoutContext('Flutter web not enabled with environment variable on stable', () {
179 180
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_WEB': 'enabled'};
181 182

      expect(featureFlags.isWebEnabled, false);
183
    });
184 185 186

    /// Flutter macOS desktop.

187
    testWithoutContext('Flutter macos desktop off by default on master', () {
188
      final FeatureFlags featureFlags = createFlags('master');
189 190

      expect(featureFlags.isMacOSEnabled, false);
191
    });
192

193
    testWithoutContext('Flutter macos desktop enabled with config on master', () {
194
      final FeatureFlags featureFlags = createFlags('master');
195
      testConfig.setValue('enable-macos-desktop', true);
196 197

      expect(featureFlags.isMacOSEnabled, true);
198
    });
199

200
    testWithoutContext('Flutter macos desktop enabled with environment variable on master', () {
201 202
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_MACOS': 'true'};
203 204

      expect(featureFlags.isMacOSEnabled, true);
205
    });
206

207
    testWithoutContext('Flutter macos desktop off by default on beta', () {
208
      final FeatureFlags featureFlags = createFlags('beta');
209 210

      expect(featureFlags.isMacOSEnabled, false);
211
    });
212

213
    testWithoutContext('Flutter macos desktop enabled with config on beta', () {
214
      final FeatureFlags featureFlags = createFlags('beta');
215
      testConfig.setValue('enable-macos-desktop', true);
216

217
      expect(featureFlags.isMacOSEnabled, true);
218
    });
219

220
    testWithoutContext('Flutter macos desktop enabled with environment variable on beta', () {
221 222
      final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_MACOS': 'true'};
223

224
      expect(featureFlags.isMacOSEnabled, true);
225
    });
226

227
    testWithoutContext('Flutter macos desktop off by default on stable', () {
228
      final FeatureFlags featureFlags = createFlags('stable');
229 230

      expect(featureFlags.isMacOSEnabled, false);
231
    });
232

233
    testWithoutContext('Flutter macos desktop enabled with config on stable', () {
234
      final FeatureFlags featureFlags = createFlags('stable');
235
      testConfig.setValue('enable-macos-desktop', true);
236

237
      expect(featureFlags.isMacOSEnabled, true);
238
    });
239

240
    testWithoutContext('Flutter macos desktop enabled with environment variable on stable', () {
241 242
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_MACOS': 'true'};
243

244
      expect(featureFlags.isMacOSEnabled, true);
245
    });
246 247

    /// Flutter Linux Desktop
248
    testWithoutContext('Flutter linux desktop off by default on master', () {
249
      final FeatureFlags featureFlags = createFlags('stable');
250 251

      expect(featureFlags.isLinuxEnabled, false);
252
    });
253

254
    testWithoutContext('Flutter linux desktop enabled with config on master', () {
255
      final FeatureFlags featureFlags = createFlags('master');
256
      testConfig.setValue('enable-linux-desktop', true);
257 258

      expect(featureFlags.isLinuxEnabled, true);
259
    });
260

261
    testWithoutContext('Flutter linux desktop enabled with environment variable on master', () {
262 263
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_LINUX': 'true'};
264 265

      expect(featureFlags.isLinuxEnabled, true);
266
    });
267

268
    testWithoutContext('Flutter linux desktop off by default on beta', () {
269
      final FeatureFlags featureFlags = createFlags('beta');
270 271

      expect(featureFlags.isLinuxEnabled, false);
272
    });
273

274
    testWithoutContext('Flutter linux desktop enabled with config on beta', () {
275
      final FeatureFlags featureFlags = createFlags('beta');
276
      testConfig.setValue('enable-linux-desktop', true);
277

278
      expect(featureFlags.isLinuxEnabled, true);
279
    });
280

281
    testWithoutContext('Flutter linux desktop enabled with environment variable on beta', () {
282 283
      final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_LINUX': 'true'};
284

285
      expect(featureFlags.isLinuxEnabled, true);
286
    });
287

288
    testWithoutContext('Flutter linux desktop off by default on stable', () {
289
      final FeatureFlags featureFlags = createFlags('stable');
290 291

      expect(featureFlags.isLinuxEnabled, false);
292
    });
293

294
    testWithoutContext('Flutter linux desktop enabled with config on stable', () {
295
      final FeatureFlags featureFlags = createFlags('stable');
296
      testConfig.setValue('enable-linux-desktop', true);
297

298
      expect(featureFlags.isLinuxEnabled, true);
299
    });
300

301
    testWithoutContext('Flutter linux desktop enabled with environment variable on stable', () {
302 303
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_LINUX': 'true'};
304

305
      expect(featureFlags.isLinuxEnabled, true);
306
    });
307 308

    /// Flutter Windows desktop.
309
    testWithoutContext('Flutter Windows desktop off by default on master', () {
310
      final FeatureFlags featureFlags = createFlags('master');
311 312

      expect(featureFlags.isWindowsEnabled, false);
313
    });
314

315
    testWithoutContext('Flutter Windows desktop enabled with config on master', () {
316
      final FeatureFlags featureFlags = createFlags('master');
317
      testConfig.setValue('enable-windows-desktop', true);
318 319

      expect(featureFlags.isWindowsEnabled, true);
320
    });
321

322
    testWithoutContext('Flutter Windows desktop enabled with environment variable on master', () {
323 324
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_WINDOWS': 'true'};
325 326

      expect(featureFlags.isWindowsEnabled, true);
327
    });
328

329
    testWithoutContext('Flutter Windows desktop off by default on beta', () {
330
      final FeatureFlags featureFlags = createFlags('beta');
331 332

      expect(featureFlags.isWindowsEnabled, false);
333
    });
334

335
    testWithoutContext('Flutter Windows desktop enabled with config on beta', () {
336
      final FeatureFlags featureFlags = createFlags('beta');
337
      testConfig.setValue('enable-windows-desktop', true);
338

339
      expect(featureFlags.isWindowsEnabled, true);
340
    });
341

342
    testWithoutContext('Flutter Windows desktop enabled with environment variable on beta', () {
343 344
      final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_WINDOWS': 'true'};
345

346
      expect(featureFlags.isWindowsEnabled, true);
347
    });
348

349
    testWithoutContext('Flutter Windows desktop off by default on stable', () {
350
      final FeatureFlags featureFlags = createFlags('stable');
351 352

      expect(featureFlags.isWindowsEnabled, false);
353
    });
354

355
    testWithoutContext('Flutter Windows desktop enabled with config on stable', () {
356
      final FeatureFlags featureFlags = createFlags('stable');
357
      testConfig.setValue('enable-windows-desktop', true);
358

359
      expect(featureFlags.isWindowsEnabled, true);
360
    });
361

362
    testWithoutContext('Flutter Windows desktop enabled with environment variable on stable', () {
363 364
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_WINDOWS': 'true'};
365

366
      expect(featureFlags.isWindowsEnabled, true);
367
    });
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

    for (final Feature feature in <Feature>[
      flutterWindowsDesktopFeature,
      flutterMacOSDesktopFeature,
      flutterLinuxDesktopFeature,
    ]) {
      test('${feature.name} available and enabled by default on master', () {
        expect(feature.master.enabledByDefault, true);
        expect(feature.master.available, true);
      });
      test('${feature.name} available and enabled by default on beta', () {
        expect(feature.beta.enabledByDefault, true);
        expect(feature.beta.available, true);
      });
      test('${feature.name} available and enabled by default on stable', () {
        expect(feature.stable.enabledByDefault, true);
        expect(feature.stable.available, true);
      });
    }

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    // Custom devices on all channels
    for (final String channel in <String>['master', 'beta', 'stable']) {
      testWithoutContext('Custom devices are enabled with flag on $channel', () {
        final FeatureFlags featureFlags = createFlags(channel);
        testConfig.setValue('enable-custom-devices', true);
        expect(featureFlags.areCustomDevicesEnabled, true);
      });

      testWithoutContext('Custom devices are enabled with environment variable on $channel', () {
        final FeatureFlags featureFlags = createFlags(channel);
        platform.environment = <String, String>{'FLUTTER_CUSTOM_DEVICES': 'true'};
        expect(featureFlags.areCustomDevicesEnabled, true);
      });
    }

403 404
  });
}