-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SuperEditor] Add test to demonstrate that changing layer builders does not re-layout #2388
Open
angelosilvestre
wants to merge
1
commit into
main
Choose a base branch
from
2042_theme-switch-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
103 changes: 103 additions & 0 deletions
103
super_editor/test/super_editor/supereditor_theme_switching_test.dart
This file contains 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,103 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:flutter_test_runners/flutter_test_runners.dart'; | ||
import 'package:super_editor/super_editor.dart'; | ||
import 'package:super_editor/super_editor_test.dart'; | ||
|
||
import 'test_documents.dart'; | ||
|
||
void main() { | ||
group('SuperEditor > theme switching', () { | ||
testWidgetsOnArbitraryDesktop('switches caret color', (tester) async { | ||
final brightnessNotifier = ValueNotifier<Brightness>(Brightness.light); | ||
|
||
await _pumpThemeSwitchingTestApp(tester, brightnessNotifier: brightnessNotifier); | ||
|
||
// Place the caret at the beginning of the paragraph. | ||
await tester.placeCaretInParagraph('1', 0); | ||
|
||
// Ensure the caret is green, because the theme is light. | ||
expect(_findDesktopCaretColor(tester), Colors.green.shade500); | ||
|
||
// Switch the theme to dark. | ||
brightnessNotifier.value = Brightness.dark; | ||
await tester.pumpAndSettle(); | ||
|
||
// Ensure the caret is red, because the theme is dark. | ||
expect(_findDesktopCaretColor(tester), Colors.red.shade500); | ||
}); | ||
|
||
testWidgetsOnArbitraryDesktop('switches caret color after typing', (tester) async { | ||
final brightnessNotifier = ValueNotifier<Brightness>(Brightness.light); | ||
|
||
await _pumpThemeSwitchingTestApp(tester, brightnessNotifier: brightnessNotifier); | ||
|
||
// Place the caret at the beginning of the paragraph. | ||
await tester.placeCaretInParagraph('1', 0); | ||
|
||
// Ensure the caret is green, because the theme is light. | ||
expect(_findDesktopCaretColor(tester), Colors.green.shade500); | ||
|
||
// Switch the theme to dark. | ||
brightnessNotifier.value = Brightness.dark; | ||
await tester.pumpAndSettle(); | ||
|
||
// Type a character to trigger a re-layout. | ||
await tester.typeImeText('a'); | ||
|
||
// Ensure the caret is red, because the theme is dark. | ||
expect(_findDesktopCaretColor(tester), Colors.red.shade500); | ||
}); | ||
}); | ||
} | ||
|
||
/// Pumps a widget tree that rebuilds when the [brightnessNotifier] changes. | ||
/// | ||
/// The widget tree contains a [SuperEditor] with a custom caret overlay that | ||
/// changes color based on the brightness of the theme. | ||
Future<void> _pumpThemeSwitchingTestApp( | ||
WidgetTester tester, { | ||
required ValueNotifier<Brightness> brightnessNotifier, | ||
}) async { | ||
final composer = MutableDocumentComposer(); | ||
final editor = createDefaultDocumentEditor( | ||
document: singleParagraphDoc(), | ||
composer: composer, | ||
); | ||
|
||
await tester.pumpWidget( | ||
MaterialApp( | ||
home: Scaffold( | ||
body: ValueListenableBuilder( | ||
valueListenable: brightnessNotifier, | ||
builder: (context, brightness, child) { | ||
return Theme( | ||
data: ThemeData( | ||
brightness: brightness, | ||
), | ||
child: SuperEditor( | ||
editor: editor, | ||
documentOverlayBuilders: [ | ||
// Copy all default overlay builders except the caret overlay builder. | ||
...defaultSuperEditorDocumentOverlayBuilders.where( | ||
(builder) => builder is! DefaultCaretOverlayBuilder, | ||
), | ||
DefaultCaretOverlayBuilder( | ||
caretStyle: CaretStyle( | ||
color: brightness == Brightness.light ? Colors.green : Colors.red, | ||
), | ||
) | ||
], | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
Color _findDesktopCaretColor(WidgetTester tester) { | ||
final caret = tester.widget<Container>(find.byKey(DocumentKeys.caret)); | ||
return (caret.decoration as BoxDecoration).color!; | ||
} |
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.
Looking at this widget tree, I'm a little confused about the build behavior.
Your description of the problem is that the overlay builders don't rebuild when the brightness changes.
I'm reading through this tree. I see the ValueListenableBuilder, which rebuilds whenever the brightness changes. When the ValueListenableBuilder rebuilds, it should construct a new SuperEditor instance, which then creates a new DefaultCaretOverlayBuilder. If we're rebuilding the ValueListenableBuilder, and rebuilding the SuperEditor, why isn't the DefaultCaretOverlayBuilder being built?
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.
Because none of the document nodes changed its content. Thus, we are not running another layout phase. We are only repainting the document components, without running a layout phase
ContentLayers
won't build the overlayers.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.
Can you phrase that statement in terms of the chain of events that I described? What did I get wrong in that series of events? It's not clear what "none of the document nodes changed" has to do with my comment. If we're rebuilding SuperEditor then why isn't SuperEditor running build on its internal ContentLayers? Where are those two builds getting disconnected, such that SuperEditor runs widget build, but ContentLayers doesn't?
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.
This is the sequence of methods being called:
The critical thing for us is that
RenderContentLayers.performLayout
is not called. It seems the subtree isn't considered dirty, so the layout does not run again.