Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PolygonGradientPainter extends CustomPainter {
void paint(Canvas canvas, Size size) {
final rowUnit = size.width / columnCount;
final columnUnit = size.height / rowCount;
final rand = Random(userId.length);
final rand = Random(userId.hashCode);

final squares = <Offset4>[];
final points = <Offset>{};
Expand Down Expand Up @@ -159,7 +159,7 @@ class PolygonGradientPainter extends CustomPainter {
List<Offset> transformPoints(Set<Offset> points, Size size) {
final transformedList = <Offset>[];
final orgList = points.toList();
final rand = Random(userId.length);
final rand = Random(userId.hashCode);

for (var i = 0; i < points.length; i++) {
final orgDx = orgList[i].dx;
Expand Down Expand Up @@ -235,9 +235,9 @@ class Offset4 {
final paint = Paint()
..color = Color.fromARGB(
255,
Random().nextInt(255),
Random().nextInt(255),
Random().nextInt(255),
Random(row * colSize + column).nextInt(255),
Random(row * colSize + column + 1).nextInt(255),
Random(row * colSize + column + 2).nextInt(255),
)
..shader = ui.Gradient.linear(
points[p1],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:alchemist/alchemist.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -112,4 +114,58 @@ void main() {
),
),
);

group('Gradient Avatar deterministic color tests', () {
test('users with same length IDs should have different gradient colors', () {
// Create a few user IDs of the same length but different values
final userIdsWithSameLength = ['12133', '12134', '12135', '98765', '54321'];
final gradientIndices = <int>[];

for (final userId in userIdsWithSameLength) {
// Create a mock random generator using userId.hashCode (the fix)
final rand = Random(userId.hashCode);
final gradientIndex = rand.nextInt(colorGradients.length);
gradientIndices.add(gradientIndex);
}

// Verify that not all gradient indices are the same
// (With the old behavior using userId.length, they would all be identical)
final uniqueIndices = gradientIndices.toSet();
expect(uniqueIndices.length, greaterThan(1),
reason: 'Users with same-length IDs should get different gradient colors');
});

test('same user ID should always get the same gradient color', () {
const userId = '12345';
final gradientIndices = <int>[];

// Generate gradient index multiple times for the same user
for (int i = 0; i < 5; i++) {
final rand = Random(userId.hashCode);
final gradientIndex = rand.nextInt(colorGradients.length);
gradientIndices.add(gradientIndex);
}

// All indices should be the same for the same user
final uniqueIndices = gradientIndices.toSet();
expect(uniqueIndices.length, equals(1),
reason: 'Same user ID should always get the same gradient color');
});

test('different user IDs should have potential for different colors', () {
final userIds = ['user1', 'user2', 'user3', 'user4', 'user5'];
final gradientIndices = <int>[];

for (final userId in userIds) {
final rand = Random(userId.hashCode);
final gradientIndex = rand.nextInt(colorGradients.length);
gradientIndices.add(gradientIndex);
}

// While not guaranteed, different users should very likely get different colors
final uniqueIndices = gradientIndices.toSet();
expect(uniqueIndices.length, greaterThan(1),
reason: 'Different users should have potential for different gradient colors');
});
});
}
Loading