Unverified Commit c84528d9 authored by Dan Field's avatar Dan Field Committed by GitHub

Implement ==/hashCode for ViewConfiguration, avoid unnecessary layer creation/replacement (#81928)

parent 562b6f76
...@@ -38,6 +38,18 @@ class ViewConfiguration { ...@@ -38,6 +38,18 @@ class ViewConfiguration {
return Matrix4.diagonal3Values(devicePixelRatio, devicePixelRatio, 1.0); 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 @override
String toString() => '$size at ${debugFormatDouble(devicePixelRatio)}x'; String toString() => '$size at ${debugFormatDouble(devicePixelRatio)}x';
} }
......
...@@ -2,12 +2,23 @@ ...@@ -2,12 +2,23 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'rendering_tester.dart'; import 'rendering_tester.dart';
void main() { 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', () { group('RenderView', () {
test('accounts for device pixel ratio in paintBounds', () { test('accounts for device pixel ratio in paintBounds', () {
layout(RenderAspectRatio(aspectRatio: 1.0)); layout(RenderAspectRatio(aspectRatio: 1.0));
...@@ -17,5 +28,33 @@ void main() { ...@@ -17,5 +28,33 @@ void main() {
final Size physicalSize = logicalSize * devicePixelRatio; final Size physicalSize = logicalSize * devicePixelRatio;
expect(renderer.renderView.paintBounds, Offset.zero & physicalSize); 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);
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment