Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline support for Network page #8332

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fe11d5b
added initial implementation
hrajwade96 Sep 21, 2024
6c140b4
adding constants file
hrajwade96 Sep 29, 2024
0c1f414
updated NEXT_RELEASE_NOTES.md
hrajwade96 Sep 30, 2024
e7eeecb
Revert "updated NEXT_RELEASE_NOTES.md"
hrajwade96 Sep 30, 2024
c5ec6f6
used constants, added util for header size calc, added params
hrajwade96 Oct 2, 2024
131cde6
Merge branch 'master' into network_screen_offline_support
hrajwade96 Oct 2, 2024
2a9c4cb
param renamed, used _log
hrajwade96 Oct 2, 2024
4f473be
Merge remote-tracking branch 'origin/network_screen_offline_support' …
hrajwade96 Oct 2, 2024
ae58738
added copyright header
hrajwade96 Oct 2, 2024
2bfbe1c
setup changes implemented
hrajwade96 Oct 3, 2024
857cfbe
put class name in square brackets
hrajwade96 Oct 3, 2024
7150436
added isNullOrEmpty check
hrajwade96 Oct 3, 2024
da11a67
added exit offline cta, hiding other controls, removed json string
hrajwade96 Oct 4, 2024
941ec88
fix for request selection
hrajwade96 Oct 4, 2024
852b144
comments resolved
hrajwade96 Oct 8, 2024
433af38
removing getFullRequestData section
hrajwade96 Oct 8, 2024
4644b00
using updateOrAddAll for setting data
hrajwade96 Oct 8, 2024
6fd98d0
Wrapped with OfflineAwareControls, removed if-else.
hrajwade96 Oct 8, 2024
665ea0d
removed addAutoDisposeListener as we are using OfflineAwareControls
hrajwade96 Oct 8, 2024
b6df572
removed try-catch
hrajwade96 Oct 8, 2024
be63db4
code refactoring
hrajwade96 Oct 9, 2024
91505b5
added enum
hrajwade96 Oct 9, 2024
0715379
code refactoring, reverting changes on network service
hrajwade96 Oct 10, 2024
ade8b5f
using values from enum
hrajwade96 Oct 10, 2024
f21a4f6
delegating toJson using extension methods, code refactoring
hrajwade96 Oct 23, 2024
454ca92
socket data de-serialisation changes
hrajwade96 Oct 27, 2024
b7b9679
Merge branch 'master' into network_screen_offline_support
hrajwade96 Oct 27, 2024
ac726e8
used factory constructor, made enum private
hrajwade96 Nov 4, 2024
2ea3044
reorder params, used isEmpty
hrajwade96 Nov 4, 2024
e01bc3c
added tests, minor fixes
hrajwade96 Nov 17, 2024
e7b5c90
removed repetitive expect statements
hrajwade96 Nov 21, 2024
6ab0884
Merge remote-tracking branch 'upstream/master' into network_screen_of…
hrajwade96 Nov 22, 2024
4f31b92
minor fix
hrajwade96 Nov 22, 2024
04c8b41
added timelineMicrosOffset to offline data
hrajwade96 Nov 24, 2024
ab8c519
added timelineMicrosOffset to tests
hrajwade96 Nov 24, 2024
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 @@ -21,7 +21,7 @@ class OfflineNetworkData with Serializable {
/// Creates an instance of [OfflineNetworkData] from a JSON map.
factory OfflineNetworkData.fromJson(Map<String, Object?> json) {
final httpRequestJsonList =
json[_OfflineDataKeys.httpRequestData.name] as List<Object>?;
json[_OfflineDataKeys.httpRequestData.name] as List<Object?>?;

// Deserialize httpRequestData
final httpRequestData = httpRequestJsonList
Expand All @@ -41,7 +41,7 @@ class OfflineNetworkData with Serializable {

// Deserialize socketData
final socketJsonList =
json[_OfflineDataKeys.socketData.name] as List<Object>?;
json[_OfflineDataKeys.socketData.name] as List<Object?>?;
final socketData = socketJsonList
?.map((e) {
if (e is Map<String, Object?>) {
Expand Down
181 changes: 181 additions & 0 deletions packages/devtools_app/test/network/offline_data_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// 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.

import 'dart:convert';
import 'dart:io';

import 'package:devtools_app/src/screens/network/network_model.dart';
import 'package:devtools_app/src/screens/network/offline_network_data.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
late Map<String, Object?> jsonData;
late OfflineNetworkData offlineData;
late Socket firstSocket;
late Socket secondSocket;

setUpAll(() {
final file = File('test/network/sample_network_offline_data.json');
final fileContent = file.readAsStringSync();
jsonData = jsonDecode(fileContent) as Map<String, Object?>;

// Create OfflineNetworkData
offlineData = OfflineNetworkData.fromJson(jsonData);

// Extracting sockets for reuse in tests
firstSocket = offlineData.socketData.first;
secondSocket = offlineData.socketData.last;
});

group('Socket Tests', () {
test('Socket should deserialize from JSON correctly', () {
// Validate first socket
expect(firstSocket.id, '105553123901536');
expect(firstSocket.socketType, 'tcp');
expect(firstSocket.port, 443);
expect(firstSocket.readBytes, 4367);
expect(firstSocket.writeBytes, 18237);

// Validate timestamps
const timelineMicrosBase = 1731482170837171;
expect(
firstSocket.startTimestamp,
DateTime.fromMicrosecondsSinceEpoch(timelineMicrosBase + 171830570040),
);
expect(
firstSocket.endTimestamp,
DateTime.fromMicrosecondsSinceEpoch(timelineMicrosBase + 171830929647),
);
expect(
firstSocket.lastReadTimestamp,
DateTime.fromMicrosecondsSinceEpoch(timelineMicrosBase + 171830928421),
);
expect(
firstSocket.lastWriteTimestamp,
DateTime.fromMicrosecondsSinceEpoch(timelineMicrosBase + 171830669180),
);
});

test('Socket should serialize to JSON correctly', () {
final serializedJson = firstSocket.toJson();

// Validate serialized JSON
expect(serializedJson['timelineMicrosBase'], 1731482170837171);
expect((serializedJson['socket'] as Map)['id'], '105553123901536');
expect((serializedJson['socket'] as Map)['startTime'], 171830570040);
expect((serializedJson['socket'] as Map)['endTime'], 171830929647);
expect((serializedJson['socket'] as Map)['readBytes'], 4367);
expect((serializedJson['socket'] as Map)['writeBytes'], 18237);
});

test('Socket duration should be calculated correctly', () {
final expectedDuration = Duration(
microseconds: firstSocket.endTimestamp!.microsecondsSinceEpoch -
firstSocket.startTimestamp.microsecondsSinceEpoch,
);

expect(firstSocket.duration, expectedDuration);
});

test('Socket status should indicate "Open" or "Closed" based on endTime',
() {
expect(
firstSocket.status,
'Closed',
); // The provided socket has an endTime

// Modify socket to simulate "Open" status
final openSocketJson = {
...firstSocket.toJson(),
'socket': {
...(firstSocket.toJson()['socket'] as Map<String, Object?>),
'endTime': null,
},
};
final openSocket = Socket.fromJson(openSocketJson);

expect(openSocket.status, 'Open'); // No endTime indicates "Open"
});

test('Socket equality and hash code should work correctly', () {
expect(firstSocket == secondSocket, isFalse);
expect(firstSocket.hashCode != secondSocket.hashCode, isTrue);

final duplicateSocket = Socket.fromJson(firstSocket.toJson());
expect(firstSocket, equals(duplicateSocket));
expect(firstSocket.hashCode, equals(duplicateSocket.hashCode));
});
});

group('OfflineNetworkData Tests', () {
test('OfflineNetworkData should deserialize correctly', () {
// Validate httpRequestData
expect(offlineData.httpRequestData.length, 2);
expect(offlineData.httpRequestData.first.id, '975585676925010898');
expect(offlineData.httpRequestData.first.method, 'GET');
expect(
offlineData.httpRequestData.first.uri,
'https://jsonplaceholder.typicode.com/albums/1',
);

// Validate socketData
expect(offlineData.socketData.length, 2);
expect(offlineData.socketData.first.id, '105553123901536');
expect(offlineData.socketData.first.socketType, 'tcp');
expect(offlineData.socketData.first.port, 443);
kenzieschmoll marked this conversation as resolved.
Show resolved Hide resolved

// Validate selectedRequestId
expect(offlineData.selectedRequestId, isNull);
});

test('OfflineNetworkData should serialize correctly', () {
final serializedJson = offlineData.toJson();

// Validate serialized JSON
final httpRequestData = serializedJson['httpRequestData'] as List;
final firstRequest = httpRequestData.first as Map<String, Object?>;
final requestDetails = firstRequest['request'] as Map<String, Object?>;

expect(requestDetails['id'], '975585676925010898');

final socketData = serializedJson['socketData'] as List;
final firstSocket = socketData.first as Map<String, Object?>;
final socketDetails = firstSocket['socket'] as Map<String, Object?>;

expect(socketDetails['id'], '105553123901536');
kenzieschmoll marked this conversation as resolved.
Show resolved Hide resolved
});

test(
'isEmpty should return true when both httpRequestData and socketData are empty',
() {
final emptyOfflineData = OfflineNetworkData(
httpRequestData: [],
socketData: [],
);

expect(emptyOfflineData.isEmpty, isTrue);
});

test('isEmpty should return false when httpRequestData is populated', () {
final populatedHttpData = OfflineNetworkData(
httpRequestData: offlineData.httpRequestData,
socketData: [],
);

expect(populatedHttpData.isEmpty, isFalse);
});

test('toJson and fromJson should preserve data integrity', () {
final serializedJson = offlineData.toJson();
final restoredData = OfflineNetworkData.fromJson(serializedJson);

expect(
restoredData.httpRequestData.length,
offlineData.httpRequestData.length,
);
expect(restoredData.socketData.length, offlineData.socketData.length);
expect(restoredData.selectedRequestId, offlineData.selectedRequestId);
});
});
}
Loading