|
| 1 | +// Copyright (c) 2024, 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 | +import 'dart:convert'; |
| 6 | + |
| 7 | +import 'package:dartpad_ui/gists.dart'; |
| 8 | +import 'package:dartpad_ui/local_storage.dart'; |
| 9 | +import 'package:dartpad_ui/model.dart'; |
| 10 | +import 'package:dartpad_ui/samples.g.dart'; |
| 11 | +import 'package:dartpad_ui/utils.dart'; |
| 12 | +import 'package:test/test.dart'; |
| 13 | + |
| 14 | +import 'gists_test.dart'; |
| 15 | + |
| 16 | +String getFallback() => |
| 17 | + LocalStorage.instance.getUserCode() ?? Samples.defaultSnippet(); |
| 18 | + |
| 19 | +void main() { |
| 20 | + const channel = Channel.stable; |
| 21 | + group('Autosave:', () { |
| 22 | + test('empty content is treated as null', () { |
| 23 | + expect(''.nullIfEmpty, isNull); |
| 24 | + |
| 25 | + LocalStorage.instance.saveUserCode('non-empty'); |
| 26 | + expect(LocalStorage.instance.getUserCode(), isNotNull); |
| 27 | + |
| 28 | + LocalStorage.instance.saveUserCode(''); |
| 29 | + expect(LocalStorage.instance.getUserCode(), isNull); |
| 30 | + }); |
| 31 | + |
| 32 | + test('null content means sample snippet is shown', () async { |
| 33 | + final model = AppModel(); |
| 34 | + final services = AppServices(model, channel); |
| 35 | + LocalStorage.instance.saveUserCode(''); |
| 36 | + expect(LocalStorage.instance.getUserCode(), isNull); |
| 37 | + |
| 38 | + await services.performInitialLoad( |
| 39 | + fallbackSnippet: getFallback(), |
| 40 | + ); |
| 41 | + expect(model.sourceCodeController.text, equals(Samples.defaultSnippet())); |
| 42 | + }); |
| 43 | + |
| 44 | + test('non-null content is shown', () async { |
| 45 | + const sample = 'Hello, World!'; |
| 46 | + final model = AppModel(); |
| 47 | + final services = AppServices(model, channel); |
| 48 | + LocalStorage.instance.saveUserCode(sample); |
| 49 | + expect(LocalStorage.instance.getUserCode(), equals(sample)); |
| 50 | + |
| 51 | + await services.performInitialLoad( |
| 52 | + fallbackSnippet: getFallback(), |
| 53 | + ); |
| 54 | + expect(model.sourceCodeController.text, equals(sample)); |
| 55 | + }); |
| 56 | + |
| 57 | + group('content is not shown with', () { |
| 58 | + const sample = 'Hello, World!'; |
| 59 | + LocalStorage.instance.saveUserCode(sample); |
| 60 | + // Not testing flutterSampleId to avoid breaking when the Flutter docs change |
| 61 | + |
| 62 | + test('Gist', () async { |
| 63 | + final model = AppModel(); |
| 64 | + final services = AppServices(model, channel); |
| 65 | + expect(LocalStorage.instance.getUserCode(), equals(sample)); |
| 66 | + |
| 67 | + // From gists_tests.dart |
| 68 | + const gistId = 'd3bd83918d21b6d5f778bdc69c3d36d6'; |
| 69 | + await services.performInitialLoad( |
| 70 | + fallbackSnippet: getFallback(), |
| 71 | + gistId: gistId, |
| 72 | + ); |
| 73 | + expect(model.sourceCodeController.text, isNot(equals(sample))); |
| 74 | + }); |
| 75 | + |
| 76 | + test('sample', () async { |
| 77 | + final model = AppModel(); |
| 78 | + final services = AppServices(model, channel); |
| 79 | + expect(LocalStorage.instance.getUserCode(), equals(sample)); |
| 80 | + |
| 81 | + // From samples_test.dart |
| 82 | + const sampleId = 'dart'; |
| 83 | + await services.performInitialLoad( |
| 84 | + fallbackSnippet: getFallback(), |
| 85 | + sampleId: sampleId, |
| 86 | + ); |
| 87 | + expect(model.sourceCodeController.text, isNot(equals(sample))); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | +} |
0 commit comments