features_test.dart 14.6 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
      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
    testWithoutContext('Flutter web help string', () {
85 86
      expect(flutterWebFeature.generateHelpMessage(),
      'Enable or disable Flutter for web. '
87
      'This setting will take effect on the master, beta, and stable channels.');
88 89
    });

90
    testWithoutContext('Flutter macOS desktop help string', () {
91
      expect(flutterMacOSDesktopFeature.generateHelpMessage(),
92
      'Enable or disable support for desktop on macOS. '
93
      'This setting will take effect on the master, beta, and stable channels.');
94 95
    });

96
    testWithoutContext('Flutter Linux desktop help string', () {
97
      expect(flutterLinuxDesktopFeature.generateHelpMessage(),
98
      'Enable or disable support for desktop on Linux. '
99
      'This setting will take effect on the master, beta, and stable channels.');
100 101
    });

102
    testWithoutContext('Flutter Windows desktop help string', () {
103
      expect(flutterWindowsDesktopFeature.generateHelpMessage(),
104
      'Enable or disable support for desktop on Windows. '
105
      'This setting will take effect on the master, beta, and stable channels.');
106 107
    });

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

117
      expect(testWithoutContextFeature.generateHelpMessage(), 'Enable or disable example. '
118
          'This setting will take effect on the master, beta, and stable channels.');
119 120 121 122
    });

    /// Flutter Web

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    /// Flutter macOS desktop.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    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);
      });
    }

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
    // 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);
      });
    }

402 403
  });
}