Implement ==/hashCode for ViewConfiguration, avoid unnecessary layer creation/replacement (#81928)
This commit is contained in:
@@ -38,6 +38,18 @@ class ViewConfiguration {
|
||||
return Matrix4.diagonal3Values(devicePixelRatio, devicePixelRatio, 1.0);
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
if (other.runtimeType != runtimeType)
|
||||
return false;
|
||||
return other is ViewConfiguration
|
||||
&& other.size == size
|
||||
&& other.devicePixelRatio == devicePixelRatio;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => hashValues(size, devicePixelRatio);
|
||||
|
||||
@override
|
||||
String toString() => '$size at ${debugFormatDouble(devicePixelRatio)}x';
|
||||
}
|
||||
|
||||
@@ -2,12 +2,23 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'rendering_tester.dart';
|
||||
|
||||
void main() {
|
||||
// Create non-const instances, otherwise tests pass even if the
|
||||
// operator override is incorrect.
|
||||
ViewConfiguration createViewConfiguration({
|
||||
Size size = const Size(20, 20),
|
||||
double devicePixelRatio = 2.0,
|
||||
}) {
|
||||
return ViewConfiguration(size: size, devicePixelRatio: devicePixelRatio);
|
||||
}
|
||||
|
||||
group('RenderView', () {
|
||||
test('accounts for device pixel ratio in paintBounds', () {
|
||||
layout(RenderAspectRatio(aspectRatio: 1.0));
|
||||
@@ -17,5 +28,33 @@ void main() {
|
||||
final Size physicalSize = logicalSize * devicePixelRatio;
|
||||
expect(renderer.renderView.paintBounds, Offset.zero & physicalSize);
|
||||
});
|
||||
|
||||
test('does not replace the root layer unnecessarily', () {
|
||||
final ui.FlutterView window = TestWindow(window: ui.window);
|
||||
final RenderView view = RenderView(
|
||||
configuration: createViewConfiguration(),
|
||||
window: window,
|
||||
);
|
||||
final PipelineOwner owner = PipelineOwner();
|
||||
view.attach(owner);
|
||||
view.prepareInitialFrame();
|
||||
final ContainerLayer firstLayer = view.debugLayer!;
|
||||
view.configuration = createViewConfiguration();
|
||||
expect(identical(view.debugLayer, firstLayer), true);
|
||||
|
||||
view.configuration = createViewConfiguration(devicePixelRatio: 5.0);
|
||||
expect(identical(view.debugLayer, firstLayer), false);
|
||||
});
|
||||
});
|
||||
|
||||
test('ViewConfiguration == and hashCode', () {
|
||||
final ViewConfiguration viewConfigurationA = createViewConfiguration();
|
||||
final ViewConfiguration viewConfigurationB = createViewConfiguration();
|
||||
final ViewConfiguration viewConfigurationC = createViewConfiguration(devicePixelRatio: 3.0);
|
||||
|
||||
expect(viewConfigurationA == viewConfigurationB, true);
|
||||
expect(viewConfigurationA != viewConfigurationC, true);
|
||||
expect(viewConfigurationA.hashCode, viewConfigurationB.hashCode);
|
||||
expect(viewConfigurationA.hashCode != viewConfigurationC.hashCode, true);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user