Skip to content

Commit

Permalink
Persist filters for the Network and CPU profiler pages (#8456)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenzieschmoll authored Oct 23, 2024
1 parent 1d92ca4 commit 681edca
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class CpuProfilerController extends DisposableController
static const _nativeFilterId = 'native';
static const _coreDartFilterId = 'core-dart';
static const _coreFlutterFilterId = 'core-flutter';
static const _uriQueryFilterId = 'cpu-profiler-uri-filter';

/// The toggle filters available for the CPU profiler.
@override
Expand All @@ -166,11 +167,9 @@ class CpuProfilerController extends DisposableController
),
];

static const uriFilterId = 'cpu-profiler-uri-filter';

@override
Map<String, QueryFilterArgument<CpuStackFrame>> createQueryFilterArgs() => {
uriFilterId: QueryFilterArgument<CpuStackFrame>(
_uriQueryFilterId: QueryFilterArgument<CpuStackFrame>(
keys: ['uri', 'u'],
exampleUsages: [
'uri:my_dart_package/some_lib.dart',
Expand All @@ -181,6 +180,10 @@ class CpuProfilerController extends DisposableController
),
};

@override
ValueNotifier<String>? get filterTagNotifier =>
preferences.cpuProfiler.filterTag;

int selectedProfilerTabIndex = 0;

ProfilerTab selectedProfilerTab = ProfilerTab.bottomUp;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

part of 'preferences.dart';

class CpuProfilerPreferencesController extends DisposableController
with AutoDisposeControllerMixin {
/// The active filter tag for the cpu profiler screen.
///
/// This value caches the most recent filter settings.
final filterTag = ValueNotifier<String>('');

@visibleForTesting
static const filterStorageId = 'cpuProfiler.filter';

Future<void> init() async {
filterTag.value = await storage.getValue(filterStorageId) ?? '';
addAutoDisposeListener(
filterTag,
() => storage.setValue(filterStorageId, filterTag.value),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import '../globals.dart';
import '../query_parameters.dart';
import '../utils.dart';

part '_cpu_profiler_preferences.dart';
part '_extension_preferences.dart';
part '_inspector_preferences.dart';
part '_memory_preferences.dart';
Expand Down Expand Up @@ -81,6 +82,9 @@ class PreferencesController extends DisposableController
final verboseLoggingEnabled =
ValueNotifier<bool>(Logger.root.level == verboseLoggingLevel);

CpuProfilerPreferencesController get cpuProfiler => _cpuProfiler;
final _cpuProfiler = CpuProfilerPreferencesController();

ExtensionsPreferencesController get devToolsExtensions => _extensions;
final _extensions = ExtensionsPreferencesController();

Expand Down Expand Up @@ -110,6 +114,7 @@ class PreferencesController extends DisposableController
}
await _initVerboseLogging();

await cpuProfiler.init();
await devToolsExtensions.init();
await inspector.init();
await logging.init();
Expand Down Expand Up @@ -230,6 +235,7 @@ class PreferencesController extends DisposableController

@override
void dispose() {
cpuProfiler.dispose();
devToolsExtensions.dispose();
inspector.dispose();
logging.dispose();
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ To learn more about DevTools, check out the
## General updates

* Persist filter settings across sessions. - [#8447](https://github.com/flutter/devtools/pull/8447),
[#8456](https://github.com/flutter/devtools/pull/8456)
[#8470](https://github.com/flutter/devtools/pull/8470)

## Inspector updates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void main() {
setUp(() async {
setCharacterWidthForTables();
setGlobal(ServiceConnectionManager, fakeServiceManager);
setGlobal(PreferencesController, PreferencesController());

final transformer = CpuProfileTransformer();
controller = CpuProfilerController();
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app/test/shared/flame_chart_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ void main() {
final mockServiceManager =
mockServiceConnection.serviceManager as MockServiceManager;
setGlobal(ServiceConnectionManager, mockServiceConnection);
setGlobal(PreferencesController, PreferencesController());

final connectedApp = MockConnectedApp();
mockConnectedApp(
Expand Down
45 changes: 45 additions & 0 deletions packages/devtools_app/test/shared/preferences_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,51 @@ void main() {
});
});

group('$CpuProfilerPreferencesController', () {
late CpuProfilerPreferencesController controller;
late FlutterTestStorage storage;

setUp(() async {
setGlobal(Storage, storage = FlutterTestStorage());
controller = CpuProfilerPreferencesController();
await controller.init();
});

test('has expected default values', () {
expect(controller.filterTag.value, '');
});

test('stores values and reads them on init', () async {
storage.values.clear();

// Remember original values.
final originalFilterTag = controller.filterTag.value;

// Change the values in controller.
const filterTag = 'foo|[{"level":2}]|regexp';
controller.filterTag.value = filterTag;

// Check the values are stored.
expect(storage.values, hasLength(1));

// Reload the values from storage.
await controller.init();

// Check they did not change back to the original values.
expect(controller.filterTag.value, filterTag);

// Change the values from storage.
storage.values[CpuProfilerPreferencesController.filterStorageId] =
originalFilterTag;

// Reload the values from storage.
await controller.init();

// Check they flipped values are loaded.
expect(controller.filterTag.value, originalFilterTag);
});
});

// TODO(https://github.com/flutter/devtools/issues/4342): Add more tests.
group('$InspectorPreferencesController', () {
late InspectorPreferencesController controller;
Expand Down

0 comments on commit 681edca

Please sign in to comment.