// Copyright (c) 2022, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Regression test for https://github.com/flutter/flutter/issues/88104.
//
// Ensures that the `TypeArguments` register is correctly preserved when
// regenerating the allocation stub for generic classes after enabling
// allocation tracing.
import 'dart:async';
import 'dart:developer';

import 'package:vm_service/vm_service.dart';

import 'common/service_test_common.dart';
import 'common/test_helper.dart';

// AUTOGENERATED START
//
// Update these constants by running:
//
// dart pkg/vm_service/test/update_line_numbers.dart pkg/vm_service/test/regress_88104_test.dart
//
const LINE_A = 30;
// AUTOGENERATED END

class Foo<T> {}

Future<void> testMain() async {
  debugger(); // LINE_A
  for (int i = 0; i < 10; ++i) {
    Foo<int>();
    await Future.delayed(const Duration(milliseconds: 10));
  }
}

final tests = <IsolateTest>[
  hasStoppedAtBreakpoint,
  stoppedAtLine(LINE_A),
  (VmService service, IsolateRef isolateRef) async {
    final isolateId = isolateRef.id!;
    final isolate = await service.getIsolate(isolateId);
    final rootLibId = isolate.rootLib!.id!;
    final rootLib = await service.getObject(isolateId, rootLibId) as Library;
    final fooCls = rootLib.classes!.first;
    await service.setTraceClassAllocation(isolateId, fooCls.id!, true);
  },
  resumeIsolate,
  hasStoppedAtExit,
];

void main([args = const <String>[]]) => runIsolateTests(
      args,
      tests,
      'regress_88104_test.dart',
      testeeConcurrent: testMain,
      pauseOnExit: true,
    );
