Skip to content
Merged
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
17 changes: 9 additions & 8 deletions packages/core/lib/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class Context extends JSONExtendableImpl {
nativeContext.library as NativeContextLibrary),
locale = nativeContext.locale ?? "",
network = nativeContext.network == null
? ContextNetwork(false, false)
? ContextNetwork(false, false, false)
: ContextNetwork.fromNative(
nativeContext.network as NativeContextNetwork),
os = nativeContext.os == null
Expand All @@ -370,7 +370,7 @@ class Context extends JSONExtendableImpl {
"device",
"library",
"locale",
"networm",
"network",
"os",
"screen",
"timezone",
Expand Down Expand Up @@ -495,18 +495,20 @@ class ContextOS extends JSONExtendableImpl {
class ContextNetwork extends JSONExtendableImpl {
bool cellular;
bool wifi;
bool bluetooth;

ContextNetwork(this.cellular, this.wifi, {super.custom});
ContextNetwork(this.cellular, this.wifi, this.bluetooth, {super.custom});
ContextNetwork.fromNative(NativeContextNetwork nativeContextNetwork)
: cellular = nativeContextNetwork.cellular ?? false,
wifi = nativeContextNetwork.wifi ?? false;
wifi = nativeContextNetwork.wifi ?? false,
bluetooth = nativeContextNetwork.bluetooth ?? false;

factory ContextNetwork.fromJson(Map<String, dynamic> json) =>
JSONExtendable.fromJson(
json, _$ContextNetworkFromJson, ContextNetwork._builtInKeys);
Map<String, dynamic> toJson() => _toJson(_$ContextNetworkToJson(this));

static final Set<String> _builtInKeys = {"cellular", "wifi"};
static final Set<String> _builtInKeys = {"cellular", "wifi", "bluetooth"};
}

@JsonSerializable(explicitToJson: true, includeIfNull: false)
Expand All @@ -515,8 +517,7 @@ class ContextScreen extends JSONExtendableImpl {
int width;
double? density; // android only

ContextScreen(this.height, this.width,
{this.density, super.custom});
ContextScreen(this.height, this.width, {this.density, super.custom});
ContextScreen.fromNative(NativeContextScreen nativeContextScreen)
: height = nativeContextScreen.height ?? 0,
width = nativeContextScreen.width ?? 0,
Expand Down Expand Up @@ -667,5 +668,5 @@ ContextDevice mergeContextDevice(ContextDevice a, ContextDevice b) {
}

ContextScreen mergeContextScreen(ContextScreen a, ContextScreen b) {
return ContextScreen(a.height, b.width, density: a.density ?? b.density);
return ContextScreen(a.height, a.width, density: a.density ?? b.density);
}
2 changes: 2 additions & 0 deletions packages/core/lib/event.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/core/lib/state.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 17 additions & 25 deletions packages/core/test/events_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ class MockPlatform extends AnalyticsPlatform {
return Future.value(mockNativeContext);
}

final List<Object> contextObj = [
"build",
"name",
"namespace",
"version"
];
final List<Object> contextObj = ["build", "name", "namespace", "version"];

final List<Object> deviceObj = [
"id",
Expand All @@ -44,27 +39,13 @@ class MockPlatform extends AnalyticsPlatform {
"token"
];

final List<Object> libraryObj = [
"name",
"version"
];
final List<Object> libraryObj = ["name", "version"];

final List<Object> networkObj = [
true,
false,
false
];
final List<Object> networkObj = [true, false, false];

final List<Object> osObj = [
"name",
"version"
];
final List<Object> osObj = ["name", "version"];

final List<Object> screenObj = [
100,
100,
5.5
];
final List<Object> screenObj = [100, 100, 5.5];

Object buildObject() {
final List<Object> encondeObj = [
Expand Down Expand Up @@ -146,6 +127,18 @@ void main() {
reverseContext.traits.custom["custom"]);
});

test("NativeContext to/from json doesn't change values", () async {
var context = Context.fromNative(NativeContext(), UserTraits());
var tempJson = context.toJson();
for (var i = 0; i < 5; i++) {
final contextStr = jsonEncode(tempJson);
final reverseContextJson = jsonDecode(contextStr);
final reverseContext = Context.fromJson(reverseContextJson);
tempJson = reverseContext.toJson();
}
expect(context.toJson(), tempJson);
});

test("Test encode method on NativeContext", () async {
final context = await AnalyticsPlatform.instance.getContext();
final contextEncode = context.encode();
Expand Down Expand Up @@ -186,6 +179,5 @@ void main() {
final nativeContextApi = NativeContextApi();
nativeContextApi.getContext(true);
});

});
}