Files
sdk/tests/lib_2/html/localstorage_test.dart
T
Nicholas Shahan 2a13b1fe26 Reland "[dartdevc] Break dart:_debugger dependency on dart:html"
Original change was broken by the fact that DDC expects all types marked as
native have all their superclasses marked as native when the super classes are
compiled.

In this case `MapMixin` from dart:collection was not being marked as native and
the only reason it was working before was that the library dependency enforced
the necessary order.

* Now `MapMixin` is explicitly marked as native.
* Added a test case to use some of the API on `window.localStorage` that is
  inherited from the MapMixin class.

Change-Id: I1584bfb86179016ee12c2acc5cfbbe81d086841e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126906
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Mark Zhou <markzipan@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
2019-12-04 00:51:18 +00:00

114 lines
3.3 KiB
Dart

// Copyright (c) 2012, 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.
import 'dart:html';
import 'package:expect/minitest.dart';
main() {
setUp(() {
window.localStorage['key1'] = 'val1';
window.localStorage['key2'] = 'val2';
window.localStorage['key3'] = 'val3';
});
tearDown(() {
window.localStorage.clear();
});
test('containsValue', () {
expect(window.localStorage.containsValue('does not exist'), isFalse);
expect(window.localStorage.containsValue('key1'), isFalse);
expect(window.localStorage.containsValue('val1'), isTrue);
expect(window.localStorage.containsValue('val3'), isTrue);
});
test('containsKey', () {
expect(window.localStorage.containsKey('does not exist'), isFalse);
expect(window.localStorage.containsKey('val1'), isFalse);
expect(window.localStorage.containsKey('key1'), isTrue);
expect(window.localStorage.containsKey('key3'), isTrue);
});
test('[]', () {
expect(window.localStorage['does not exist'], isNull);
expect(window.localStorage['key1'], 'val1');
expect(window.localStorage['key3'], 'val3');
});
test('[]=', () {
expect(window.localStorage['key4'], isNull);
window.localStorage['key4'] = 'val4';
expect(window.localStorage['key4'], 'val4');
expect(window.localStorage['key3'], 'val3');
window.localStorage['key3'] = 'val3-new';
expect(window.localStorage['key3'], 'val3-new');
});
test('putIfAbsent', () {
expect(window.localStorage['key4'], isNull);
expect(window.localStorage.putIfAbsent('key4', () => 'val4'), 'val4');
expect(window.localStorage['key4'], 'val4');
expect(window.localStorage['key3'], 'val3');
expect(
window.localStorage.putIfAbsent('key3', () {
fail('should not be called');
return 'unused';
}),
'val3');
expect(window.localStorage['key3'], 'val3');
});
test('remove', () {
expect(window.localStorage.remove('does not exist'), isNull);
expect(window.localStorage.remove('key3'), 'val3');
expect(window.localStorage, equals({'key1': 'val1', 'key2': 'val2'}));
});
test('clear', () {
window.localStorage.clear();
expect(window.localStorage, equals({}));
});
test('forEach', () {
var results = <String, String>{};
window.localStorage.forEach((k, v) {
results[k] = v;
});
expect(results, equals({'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}));
});
test('getKeys', () {
expect(window.localStorage.keys.toList(),
unorderedEquals(['key1', 'key2', 'key3']));
});
test('getVals', () {
expect(window.localStorage.values.toList(),
unorderedEquals(['val1', 'val2', 'val3']));
});
test('getEntries', () {
var results = <String, String>{};
window.localStorage.entries.forEach((e) {
results[e.key] = e.value;
});
expect(results, equals({'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}));
});
test('length', () {
expect(window.localStorage.length, 3);
window.localStorage.clear();
expect(window.localStorage.length, 0);
});
test('isEmpty', () {
expect(window.localStorage.isEmpty, isFalse);
window.localStorage.clear();
expect(window.localStorage.isEmpty, isTrue);
});
}