From 92ad558e257d5c4ebf531d83686e00028a061c9c Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Tue, 28 Nov 2023 11:59:19 -0600 Subject: [PATCH] Fix turbulence seed for all tests with ink sparkles (#138757) Fixes https://github.com/flutter/flutter/issues/138753 This makes sure that InkSparkles are always fixed for testing rather than random. --- packages/flutter/lib/src/material/ink_sparkle.dart | 11 ++++++++++- packages/flutter/test/material/ink_sparkle_test.dart | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/material/ink_sparkle.dart b/packages/flutter/lib/src/material/ink_sparkle.dart index d4d107735b..798604985a 100644 --- a/packages/flutter/lib/src/material/ink_sparkle.dart +++ b/packages/flutter/lib/src/material/ink_sparkle.dart @@ -201,6 +201,13 @@ class InkSparkle extends InteractiveInkFeature { // Creates an element of randomness so that ink emanating from the same // pixel have slightly different rings and sparkles. + assert((){ + // In tests, randomness can cause flakes. So if a seed has not + // already been specified (i.e. for the purpose of the test), set it to + // the constant turbulence seed. + turbulenceSeed ??= _InkSparkleFactory.constantSeed; + return true; + }()); _turbulenceSeed = turbulenceSeed ?? math.Random().nextDouble() * 1000.0; } @@ -429,7 +436,9 @@ class InkSparkle extends InteractiveInkFeature { class _InkSparkleFactory extends InteractiveInkFeatureFactory { const _InkSparkleFactory() : turbulenceSeed = null; - const _InkSparkleFactory.constantTurbulenceSeed() : turbulenceSeed = 1337.0; + const _InkSparkleFactory.constantTurbulenceSeed() : turbulenceSeed = _InkSparkleFactory.constantSeed; + + static const double constantSeed = 1337.0; static void initializeShader() { if (!_initCalled) { diff --git a/packages/flutter/test/material/ink_sparkle_test.dart b/packages/flutter/test/material/ink_sparkle_test.dart index 40b47d7625..ca7c454d86 100644 --- a/packages/flutter/test/material/ink_sparkle_test.dart +++ b/packages/flutter/test/material/ink_sparkle_test.dart @@ -179,7 +179,7 @@ Future _runM3Test(WidgetTester tester, String positionName, double distanc key: repaintKey, child: ElevatedButton( key: buttonKey, - style: ElevatedButton.styleFrom(splashFactory: InkSparkle.constantTurbulenceSeedSplashFactory), + style: ElevatedButton.styleFrom(), child: const Text('Sparkle!'), onPressed: () { }, ),