This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[web] Tests for browser history implementation #15324
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
// Copyright 2013 The Flutter Authors. 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:async'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:ui/src/engine.dart'; | ||
|
||
import '../spy.dart'; | ||
|
||
TestLocationStrategy _strategy; | ||
TestLocationStrategy get strategy => _strategy; | ||
set strategy(TestLocationStrategy newStrategy) { | ||
window.locationStrategy = _strategy = newStrategy; | ||
} | ||
|
||
const Map<String, bool> originState = <String, bool>{'origin': true}; | ||
const Map<String, bool> flutterState = <String, bool>{'flutter': true}; | ||
|
||
const MethodCodec codec = JSONMethodCodec(); | ||
|
||
void emptyCallback(ByteData date) {} | ||
|
||
void main() { | ||
group('BrowserHistory', () { | ||
final PlatformMessagesSpy spy = PlatformMessagesSpy(); | ||
|
||
setUp(() { | ||
spy.setUp(); | ||
}); | ||
|
||
tearDown(() { | ||
spy.tearDown(); | ||
strategy = null; | ||
}); | ||
|
||
test('basic setup works', () { | ||
strategy = TestLocationStrategy.fromEntry( | ||
TestHistoryEntry('initial state', null, '/initial')); | ||
|
||
// There should be two entries: origin and flutter. | ||
expect(strategy.history, hasLength(2)); | ||
|
||
// The origin entry is setup but its path should remain unchanged. | ||
final TestHistoryEntry originEntry = strategy.history[0]; | ||
expect(originEntry.state, originState); | ||
expect(originEntry.url, '/initial'); | ||
|
||
// The flutter entry is pushed and its path should be derived from the | ||
// origin entry. | ||
final TestHistoryEntry flutterEntry = strategy.history[1]; | ||
expect(flutterEntry.state, flutterState); | ||
expect(flutterEntry.url, '/initial'); | ||
|
||
// The flutter entry is the current entry. | ||
expect(strategy.currentEntry, flutterEntry); | ||
}); | ||
|
||
test('browser back button pops routes correctly', () async { | ||
strategy = | ||
TestLocationStrategy.fromEntry(TestHistoryEntry(null, null, '/home')); | ||
|
||
// Initially, we should be on the flutter entry. | ||
expect(strategy.history, hasLength(2)); | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/home'); | ||
|
||
pushRoute('/page1'); | ||
// The number of entries shouldn't change. | ||
expect(strategy.history, hasLength(2)); | ||
expect(strategy.currentEntryIndex, 1); | ||
// But the url of the current entry (flutter entry) should be updated. | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/page1'); | ||
|
||
// No platform messages have been sent so far. | ||
expect(spy.messages, isEmpty); | ||
// Clicking back should take us to page1. | ||
await strategy.back(); | ||
// First, the framework should've received a `popRoute` platform message. | ||
expect(spy.messages, hasLength(1)); | ||
expect(spy.messages[0].channel, 'flutter/navigation'); | ||
expect(spy.messages[0].methodName, 'popRoute'); | ||
expect(spy.messages[0].methodArguments, isNull); | ||
// We still have 2 entries. | ||
expect(strategy.history, hasLength(2)); | ||
expect(strategy.currentEntryIndex, 1); | ||
// The url of the current entry (flutter entry) should go back to /home. | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/home'); | ||
}); | ||
|
||
test('multiple browser back clicks', () async { | ||
strategy = | ||
TestLocationStrategy.fromEntry(TestHistoryEntry(null, null, '/home')); | ||
|
||
pushRoute('/page1'); | ||
pushRoute('/page2'); | ||
|
||
// Make sure we are on page2. | ||
expect(strategy.history, hasLength(2)); | ||
expect(strategy.currentEntryIndex, 1); | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/page2'); | ||
|
||
// Back to page1. | ||
await strategy.back(); | ||
// 1. The engine sends a `popRoute` platform message. | ||
expect(spy.messages, hasLength(1)); | ||
expect(spy.messages[0].channel, 'flutter/navigation'); | ||
expect(spy.messages[0].methodName, 'popRoute'); | ||
expect(spy.messages[0].methodArguments, isNull); | ||
spy.messages.clear(); | ||
// 2. The framework sends a `routePopped` platform message. | ||
popRoute('/page1'); | ||
// 3. The history state should reflect that /page1 is currently active. | ||
expect(strategy.history, hasLength(2)); | ||
expect(strategy.currentEntryIndex, 1); | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/page1'); | ||
|
||
// Back to home. | ||
await strategy.back(); | ||
// 1. The engine sends a `popRoute` platform message. | ||
expect(spy.messages, hasLength(1)); | ||
expect(spy.messages[0].channel, 'flutter/navigation'); | ||
expect(spy.messages[0].methodName, 'popRoute'); | ||
expect(spy.messages[0].methodArguments, isNull); | ||
spy.messages.clear(); | ||
// 2. The framework sends a `routePopped` platform message. | ||
popRoute('/home'); | ||
// 3. The history state should reflect that /page1 is currently active. | ||
expect(strategy.history, hasLength(2)); | ||
expect(strategy.currentEntryIndex, 1); | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/home'); | ||
|
||
// The next browser back will exit the app. | ||
await strategy.back(); | ||
// 1. The engine sends a `popRoute` platform message. | ||
expect(spy.messages, hasLength(1)); | ||
expect(spy.messages[0].channel, 'flutter/navigation'); | ||
expect(spy.messages[0].methodName, 'popRoute'); | ||
expect(spy.messages[0].methodArguments, isNull); | ||
spy.messages.clear(); | ||
// 2. The framework sends a `SystemNavigator.pop` platform message | ||
// because there are no more routes to pop. | ||
await systemNavigatorPop(); | ||
// 3. The active entry doesn't belong to our history anymore because we | ||
// navigated past it. | ||
expect(strategy.currentEntryIndex, -1); | ||
}); | ||
|
||
test('handle user-provided url', () async { | ||
strategy = | ||
TestLocationStrategy.fromEntry(TestHistoryEntry(null, null, '/home')); | ||
|
||
await _strategy.simulateUserTypingUrl('/page3'); | ||
// This delay is necessary to wait for [BrowserHistory] because it | ||
// performs a `back` operation which results in a new event loop. | ||
await Future.delayed(Duration.zero); | ||
// 1. The engine sends a `pushRoute` platform message. | ||
expect(spy.messages, hasLength(1)); | ||
expect(spy.messages[0].channel, 'flutter/navigation'); | ||
expect(spy.messages[0].methodName, 'pushRoute'); | ||
expect(spy.messages[0].methodArguments, '/page3'); | ||
spy.messages.clear(); | ||
// 2. The framework sends a `routePushed` platform message. | ||
pushRoute('/page3'); | ||
// 3. The history state should reflect that /page3 is currently active. | ||
expect(strategy.history, hasLength(3)); | ||
expect(strategy.currentEntryIndex, 1); | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/page3'); | ||
|
||
// Back to home. | ||
await strategy.back(); | ||
// 1. The engine sends a `popRoute` platform message. | ||
expect(spy.messages, hasLength(1)); | ||
expect(spy.messages[0].channel, 'flutter/navigation'); | ||
expect(spy.messages[0].methodName, 'popRoute'); | ||
expect(spy.messages[0].methodArguments, isNull); | ||
spy.messages.clear(); | ||
// 2. The framework sends a `routePopped` platform message. | ||
popRoute('/home'); | ||
// 3. The history state should reflect that /page1 is currently active. | ||
expect(strategy.history, hasLength(2)); | ||
expect(strategy.currentEntryIndex, 1); | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/home'); | ||
}); | ||
|
||
test('user types unknown url', () async { | ||
strategy = | ||
TestLocationStrategy.fromEntry(TestHistoryEntry(null, null, '/home')); | ||
|
||
await _strategy.simulateUserTypingUrl('/unknown'); | ||
// This delay is necessary to wait for [BrowserHistory] because it | ||
// performs a `back` operation which results in a new event loop. | ||
await Future.delayed(Duration.zero); | ||
// 1. The engine sends a `pushRoute` platform message. | ||
expect(spy.messages, hasLength(1)); | ||
expect(spy.messages[0].channel, 'flutter/navigation'); | ||
expect(spy.messages[0].methodName, 'pushRoute'); | ||
expect(spy.messages[0].methodArguments, '/unknown'); | ||
spy.messages.clear(); | ||
// 2. The framework doesn't recognize the route name and ignores it. | ||
// 3. The history state should reflect that /home is currently active. | ||
expect(strategy.history, hasLength(3)); | ||
expect(strategy.currentEntryIndex, 1); | ||
expect(strategy.currentEntry.state, flutterState); | ||
expect(strategy.currentEntry.url, '/home'); | ||
}); | ||
}); | ||
} | ||
|
||
void pushRoute(String routeName) { | ||
window.sendPlatformMessage( | ||
'flutter/navigation', | ||
codec.encodeMethodCall(MethodCall( | ||
'routePushed', | ||
<String, dynamic>{'previousRouteName': '/foo', 'routeName': routeName}, | ||
)), | ||
emptyCallback, | ||
); | ||
} | ||
|
||
void replaceRoute(String routeName) { | ||
window.sendPlatformMessage( | ||
'flutter/navigation', | ||
codec.encodeMethodCall(MethodCall( | ||
'routeReplaced', | ||
<String, dynamic>{'previousRouteName': '/foo', 'routeName': routeName}, | ||
)), | ||
emptyCallback, | ||
); | ||
} | ||
|
||
void popRoute(String previousRouteName) { | ||
window.sendPlatformMessage( | ||
'flutter/navigation', | ||
codec.encodeMethodCall(MethodCall( | ||
'routePopped', | ||
<String, dynamic>{ | ||
'previousRouteName': previousRouteName, | ||
'routeName': '/foo' | ||
}, | ||
)), | ||
emptyCallback, | ||
); | ||
} | ||
|
||
Future<void> systemNavigatorPop() { | ||
final Completer<void> completer = Completer<void>(); | ||
window.sendPlatformMessage( | ||
'flutter/platform', | ||
codec.encodeMethodCall(MethodCall('SystemNavigator.pop')), | ||
(_) => completer.complete(), | ||
); | ||
return completer.future; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nturgut FYI, this test is good first candidate for a cross framework-engine integration test. Currently it has to emulate what the framework does, which could go out of sync with the framework. But an integration test would allow us to actually have the framework respond to and initiate history changes. That would make it a much more reliable test.