features_test.dart 17.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 allFeatures) {
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 36 37 38 39 40 41
    FeatureFlags createFlags(String channel) {
      return FlutterFeatureFlags(
        flutterVersion: FakeFlutterVersion(channel: channel),
        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 62
      const FeatureChannelSetting masterSetting = FeatureChannelSetting(available: true);
      const FeatureChannelSetting devSetting = FeatureChannelSetting(available: true);
      const FeatureChannelSetting betaSetting = FeatureChannelSetting(available: true);
      const FeatureChannelSetting stableSetting = FeatureChannelSetting(available: true);
      const Feature feature = Feature(
63 64 65 66 67 68 69 70 71 72 73 74 75 76
        name: 'example',
        master: masterSetting,
        dev: devSetting,
        beta: betaSetting,
        stable: stableSetting,
      );

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

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

      expect(featureFlags.isWebEnabled, false);

82
      platform.environment = <String, String>{'FLUTTER_WEB': 'true'};
83 84

      expect(featureFlags.isWebEnabled, true);
85
    });
86

87
    testWithoutContext('Flutter web help string', () {
88 89
      expect(flutterWebFeature.generateHelpMessage(),
      'Enable or disable Flutter for web. '
90
      'This setting will take effect on the master, dev, beta, and stable channels.');
91 92
    });

93
    testWithoutContext('Flutter macOS desktop help string', () {
94
      expect(flutterMacOSDesktopFeature.generateHelpMessage(),
95
      'Enable or disable beta-quality support for desktop on macOS. '
96 97
      'This setting will take effect on the master, dev, beta, and stable channels. '
      'Newer beta versions are available on the beta channel.');
98 99
    });

100
    testWithoutContext('Flutter Linux desktop help string', () {
101
      expect(flutterLinuxDesktopFeature.generateHelpMessage(),
102
      'Enable or disable beta-quality support for desktop on Linux. '
103 104
      'This setting will take effect on the master, dev, beta, and stable channels. '
      'Newer beta versions are available on the beta channel.');
105 106
    });

107
    testWithoutContext('Flutter Windows desktop help string', () {
108
      expect(flutterWindowsDesktopFeature.generateHelpMessage(),
109
      'Enable or disable beta-quality support for desktop on Windows. '
110 111
      'This setting will take effect on the master, dev, beta, and stable channels. '
      'Newer beta versions are available on the beta channel.');
112 113
    });

114 115
    testWithoutContext('help string on multiple channels', () {
      const Feature testWithoutContextFeature = Feature(
116 117 118 119 120 121 122 123
        name: 'example',
        master: FeatureChannelSetting(available: true),
        dev: FeatureChannelSetting(available: true),
        beta: FeatureChannelSetting(available: true),
        stable: FeatureChannelSetting(available: true),
        configSetting: 'foo',
      );

124
      expect(testWithoutContextFeature.generateHelpMessage(), 'Enable or disable example. '
125
          'This setting will take effect on the master, dev, beta, and stable channels.');
126 127 128 129
    });

    /// Flutter Web

130
    testWithoutContext('Flutter web off by default on master', () {
131
      final FeatureFlags featureFlags = createFlags('master');
132 133

      expect(featureFlags.isWebEnabled, false);
134
    });
135

136
    testWithoutContext('Flutter web enabled with config on master', () {
137
      final FeatureFlags featureFlags = createFlags('master');
138
      testConfig.setValue('enable-web', true);
139 140

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

143
    testWithoutContext('Flutter web enabled with environment variable on master', () {
144 145
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_WEB': 'true'};
146 147

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

150
    testWithoutContext('Flutter web off by default on dev', () {
151
      final FeatureFlags featureFlags = createFlags('dev');
152 153

      expect(featureFlags.isWebEnabled, false);
154
    });
155

156
    testWithoutContext('Flutter web enabled with config on dev', () {
157
      final FeatureFlags featureFlags = createFlags('dev');
158
      testConfig.setValue('enable-web', true);
159 160

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

163
    testWithoutContext('Flutter web enabled with environment variable on dev', () {
164 165
      final FeatureFlags featureFlags = createFlags('dev');
      platform.environment = <String, String>{'FLUTTER_WEB': 'true'};
166 167

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

170
    testWithoutContext('Flutter web off by default on beta', () {
171
      final FeatureFlags featureFlags = createFlags('beta');
172 173

      expect(featureFlags.isWebEnabled, false);
174
    });
175

176
    testWithoutContext('Flutter web enabled with config on beta', () {
177
      final FeatureFlags featureFlags = createFlags('beta');
178
      testConfig.setValue('enable-web', true);
179

180
      expect(featureFlags.isWebEnabled, true);
181
    });
182

183
    testWithoutContext('Flutter web not enabled with environment variable on beta', () {
184 185
     final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_WEB': 'true'};
186

187
      expect(featureFlags.isWebEnabled, true);
188
    });
189

190
    testWithoutContext('Flutter web on by default on stable', () {
191
      final FeatureFlags featureFlags = createFlags('stable');
192
      testConfig.removeValue('enable-web');
193

194
      expect(featureFlags.isWebEnabled, true);
195
    });
196

197
    testWithoutContext('Flutter web enabled with config on stable', () {
198
      final FeatureFlags featureFlags = createFlags('stable');
199
      testConfig.setValue('enable-web', true);
200

201
      expect(featureFlags.isWebEnabled, true);
202
    });
203

204
    testWithoutContext('Flutter web not enabled with environment variable on stable', () {
205 206
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_WEB': 'enabled'};
207 208

      expect(featureFlags.isWebEnabled, false);
209
    });
210 211 212

    /// Flutter macOS desktop.

213
    testWithoutContext('Flutter macos desktop off by default on master', () {
214
      final FeatureFlags featureFlags = createFlags('master');
215 216

      expect(featureFlags.isMacOSEnabled, false);
217
    });
218

219
    testWithoutContext('Flutter macos desktop enabled with config on master', () {
220
      final FeatureFlags featureFlags = createFlags('master');
221
      testConfig.setValue('enable-macos-desktop', true);
222 223

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

226
    testWithoutContext('Flutter macos desktop enabled with environment variable on master', () {
227 228
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_MACOS': 'true'};
229 230

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

233
    testWithoutContext('Flutter macos desktop off by default on dev', () {
234
      final FeatureFlags featureFlags = createFlags('dev');
235 236

      expect(featureFlags.isMacOSEnabled, false);
237
    });
238

239
    testWithoutContext('Flutter macos desktop enabled with config on dev', () {
240
      final FeatureFlags featureFlags = createFlags('dev');
241
      testConfig.setValue('enable-macos-desktop', true);
242

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

246
    testWithoutContext('Flutter macos desktop enabled with environment variable on dev', () {
247 248
      final FeatureFlags featureFlags = createFlags('dev');
      platform.environment = <String, String>{'FLUTTER_MACOS': 'true'};
249

250
      expect(featureFlags.isMacOSEnabled, true);
251
    });
252

253
    testWithoutContext('Flutter macos desktop off by default on beta', () {
254
      final FeatureFlags featureFlags = createFlags('beta');
255 256

      expect(featureFlags.isMacOSEnabled, false);
257
    });
258

259
    testWithoutContext('Flutter macos desktop enabled with config on beta', () {
260
      final FeatureFlags featureFlags = createFlags('beta');
261
      testConfig.setValue('enable-macos-desktop', true);
262

263
      expect(featureFlags.isMacOSEnabled, true);
264
    });
265

266
    testWithoutContext('Flutter macos desktop enabled with environment variable on beta', () {
267 268
      final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_MACOS': 'true'};
269

270
      expect(featureFlags.isMacOSEnabled, true);
271
    });
272

273
    testWithoutContext('Flutter macos desktop off by default on stable', () {
274
      final FeatureFlags featureFlags = createFlags('stable');
275 276

      expect(featureFlags.isMacOSEnabled, false);
277
    });
278

279
    testWithoutContext('Flutter macos desktop enabled with config on stable', () {
280
      final FeatureFlags featureFlags = createFlags('stable');
281
      testConfig.setValue('enable-macos-desktop', true);
282

283
      expect(featureFlags.isMacOSEnabled, true);
284
    });
285

286
    testWithoutContext('Flutter macos desktop enabled with environment variable on stable', () {
287 288
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_MACOS': 'true'};
289

290
      expect(featureFlags.isMacOSEnabled, true);
291
    });
292 293

    /// Flutter Linux Desktop
294
    testWithoutContext('Flutter linux desktop off by default on master', () {
295
      final FeatureFlags featureFlags = createFlags('stable');
296 297

      expect(featureFlags.isLinuxEnabled, false);
298
    });
299

300
    testWithoutContext('Flutter linux desktop enabled with config on master', () {
301
      final FeatureFlags featureFlags = createFlags('master');
302
      testConfig.setValue('enable-linux-desktop', true);
303 304

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

307
    testWithoutContext('Flutter linux desktop enabled with environment variable on master', () {
308 309
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_LINUX': 'true'};
310 311

      expect(featureFlags.isLinuxEnabled, true);
312
    });
313

314
    testWithoutContext('Flutter linux desktop off by default on dev', () {
315
      final FeatureFlags featureFlags = createFlags('dev');
316 317

      expect(featureFlags.isLinuxEnabled, false);
318
    });
319

320
    testWithoutContext('Flutter linux desktop enabled with config on dev', () {
321
      final FeatureFlags featureFlags = createFlags('dev');
322
      testConfig.setValue('enable-linux-desktop', true);
323

324
      expect(featureFlags.isLinuxEnabled, true);
325
    });
326

327
    testWithoutContext('Flutter linux desktop enabled with environment variable on dev', () {
328 329
      final FeatureFlags featureFlags = createFlags('dev');
      platform.environment = <String, String>{'FLUTTER_LINUX': 'true'};
330

331
      expect(featureFlags.isLinuxEnabled, true);
332
    });
333

334
    testWithoutContext('Flutter linux desktop off by default on beta', () {
335
      final FeatureFlags featureFlags = createFlags('beta');
336 337

      expect(featureFlags.isLinuxEnabled, false);
338
    });
339

340
    testWithoutContext('Flutter linux desktop enabled with config on beta', () {
341
      final FeatureFlags featureFlags = createFlags('beta');
342
      testConfig.setValue('enable-linux-desktop', true);
343

344
      expect(featureFlags.isLinuxEnabled, true);
345
    });
346

347
    testWithoutContext('Flutter linux desktop enabled with environment variable on beta', () {
348 349
      final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_LINUX': 'true'};
350

351
      expect(featureFlags.isLinuxEnabled, true);
352
    });
353

354
    testWithoutContext('Flutter linux desktop off by default on stable', () {
355
      final FeatureFlags featureFlags = createFlags('stable');
356 357

      expect(featureFlags.isLinuxEnabled, false);
358
    });
359

360
    testWithoutContext('Flutter linux desktop enabled with config on stable', () {
361
      final FeatureFlags featureFlags = createFlags('stable');
362
      testConfig.setValue('enable-linux-desktop', true);
363

364
      expect(featureFlags.isLinuxEnabled, true);
365
    });
366

367
    testWithoutContext('Flutter linux desktop enabled with environment variable on stable', () {
368 369
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_LINUX': 'true'};
370

371
      expect(featureFlags.isLinuxEnabled, true);
372
    });
373 374

    /// Flutter Windows desktop.
375
    testWithoutContext('Flutter Windows desktop off by default on master', () {
376
      final FeatureFlags featureFlags = createFlags('master');
377 378

      expect(featureFlags.isWindowsEnabled, false);
379
    });
380

381
    testWithoutContext('Flutter Windows desktop enabled with config on master', () {
382
      final FeatureFlags featureFlags = createFlags('master');
383
      testConfig.setValue('enable-windows-desktop', true);
384 385

      expect(featureFlags.isWindowsEnabled, true);
386
    });
387

388
    testWithoutContext('Flutter Windows desktop enabled with environment variable on master', () {
389 390
      final FeatureFlags featureFlags = createFlags('master');
      platform.environment = <String, String>{'FLUTTER_WINDOWS': 'true'};
391 392

      expect(featureFlags.isWindowsEnabled, true);
393
    });
394

395
    testWithoutContext('Flutter Windows desktop off by default on dev', () {
396
      final FeatureFlags featureFlags = createFlags('dev');
397 398

      expect(featureFlags.isWindowsEnabled, false);
399
    });
400

401
    testWithoutContext('Flutter Windows desktop enabled with config on dev', () {
402
      final FeatureFlags featureFlags = createFlags('dev');
403
      testConfig.setValue('enable-windows-desktop', true);
404

405
      expect(featureFlags.isWindowsEnabled, true);
406
    });
407

408
    testWithoutContext('Flutter Windows desktop not enabled with environment variable on dev', () {
409 410
      final FeatureFlags featureFlags = createFlags('dev');
      platform.environment = <String, String>{'FLUTTER_WINDOWS': 'true'};
411

412
      expect(featureFlags.isWindowsEnabled, true);
413
    });
414

415
    testWithoutContext('Flutter Windows desktop off by default on beta', () {
416
      final FeatureFlags featureFlags = createFlags('beta');
417 418

      expect(featureFlags.isWindowsEnabled, false);
419
    });
420

421
    testWithoutContext('Flutter Windows desktop enabled with config on beta', () {
422
      final FeatureFlags featureFlags = createFlags('beta');
423
      testConfig.setValue('enable-windows-desktop', true);
424

425
      expect(featureFlags.isWindowsEnabled, true);
426
    });
427

428
    testWithoutContext('Flutter Windows desktop enabled with environment variable on beta', () {
429 430
      final FeatureFlags featureFlags = createFlags('beta');
      platform.environment = <String, String>{'FLUTTER_WINDOWS': 'true'};
431

432
      expect(featureFlags.isWindowsEnabled, true);
433
    });
434

435
    testWithoutContext('Flutter Windows desktop off by default on stable', () {
436
      final FeatureFlags featureFlags = createFlags('stable');
437 438

      expect(featureFlags.isWindowsEnabled, false);
439
    });
440

441
    testWithoutContext('Flutter Windows desktop enabled with config on stable', () {
442
      final FeatureFlags featureFlags = createFlags('stable');
443
      testConfig.setValue('enable-windows-desktop', true);
444

445
      expect(featureFlags.isWindowsEnabled, true);
446
    });
447

448
    testWithoutContext('Flutter Windows desktop enabled with environment variable on stable', () {
449 450
      final FeatureFlags featureFlags = createFlags('stable');
      platform.environment = <String, String>{'FLUTTER_WINDOWS': 'true'};
451

452
      expect(featureFlags.isWindowsEnabled, true);
453
    });
454 455 456 457

    // Windows UWP desktop

    testWithoutContext('Flutter Windows UWP desktop off by default on master', () {
458
      final FeatureFlags featureFlags = createFlags('master');
459 460 461 462 463

      expect(featureFlags.isWindowsUwpEnabled, false);
    });

    testWithoutContext('Flutter Windows UWP desktop enabled with config on master', () {
464
      final FeatureFlags featureFlags = createFlags('master');
465 466 467 468 469 470
      testConfig.setValue('enable-windows-uwp-desktop', true);

      expect(featureFlags.isWindowsUwpEnabled, true);
    });

    testWithoutContext('Flutter Windows UWP desktop off by default on stable', () {
471
      final FeatureFlags featureFlags = createFlags('stable');
472 473 474 475 476

      expect(featureFlags.isWindowsUwpEnabled, false);
    });

    testWithoutContext('Flutter Windows UWP desktop not enabled with config on stable', () {
477
      final FeatureFlags featureFlags = createFlags('stable');
478 479 480 481
      testConfig.setValue('enable-windows-uwp-desktop', true);

      expect(featureFlags.isWindowsUwpEnabled, false);
    });
482 483
  });
}