Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 23 additions & 27 deletions Runtime/Native/Windows/NativeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ internal static void CleanScopedAttributes()
// the reason behind this decision is to make sure user change in the configuration
// won't leave any useless data
var attributesJson = PlayerPrefs.GetString(ScopedAttributeListKey);
if (!HasScopedAttributesEmpty(attributesJson))
if (!HasScopedAttributes(attributesJson))
{
return;
}
Expand All @@ -390,34 +390,30 @@ internal static void CleanScopedAttributes()

internal static IDictionary<string, string> GetScopedAttributes()
{
var attributesJson = PlayerPrefs.GetString(ScopedAttributeListKey);
if (!HasScopedAttributesEmpty(attributesJson))
{
return new Dictionary<string, string>();
}

var result = new Dictionary<string, string>(StringComparer.Ordinal);
ScopedAttributesContainer attributes = null;

try
{
attributes = JsonUtility.FromJson<ScopedAttributesContainer>(attributesJson);
}
catch (Exception e)
var attributesJson = PlayerPrefs.GetString(ScopedAttributeListKey);
if (HasScopedAttributes(attributesJson))
{
Debug.LogWarning($"Backtrace Failed to parse scoped attributes at read: {e.Message}");
return result;
}
ScopedAttributesContainer attributes;

if (attributes?.Keys == null)
{
return result;
}
try
{
attributes = JsonUtility.FromJson<ScopedAttributesContainer>(attributesJson);
}
catch (Exception e)
{
Debug.LogWarning($"Backtrace Failed to parse scoped attributes at read: {e.Message}");
return result;
}

foreach (var attributeKey in attributes.Keys)
{
var value = PlayerPrefs.GetString(string.Format(ScopedAttributesPattern, attributeKey), string.Empty);
result[attributeKey] = value;
if (attributes?.Keys != null)
{
foreach (var attributeKey in attributes.Keys)
{
var value = PlayerPrefs.GetString(string.Format(ScopedAttributesPattern, attributeKey), string.Empty);
result[attributeKey] = value;
}
}
}

// extend scoped attributes with legacy attributes stored by Backtrace-Unity library in previous versions
Expand Down Expand Up @@ -459,7 +455,7 @@ private HashSet<string> LoadScopedKeys()
{
var keys = new HashSet<string>(StringComparer.Ordinal);
var attributesJson = PlayerPrefs.GetString(ScopedAttributeListKey);
if (HasScopedAttributesEmpty(attributesJson))
if (HasScopedAttributes(attributesJson))
{
try
{
Expand Down Expand Up @@ -586,7 +582,7 @@ private void AddScopedAttribute(string key, string value)
PlayerPrefs.Save();
}

private static bool HasScopedAttributesEmpty(string attributesJson)
private static bool HasScopedAttributes(string attributesJson)
{
return !(string.IsNullOrEmpty(attributesJson) || attributesJson == "{}");
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/Runtime/BacktraceStackTraceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ public IEnumerator TestStackTraceCreation_ShouldUseEnvStackTraceWhenExStackTrace
[Test]
public void TestStackTraceCreation_AndroidException_ValidStackTraceObject()
{
#if !UNITY_ANDROID && !UNITY_EDITOR
Assert.Ignore("Android stack trace parsing is only compiled for Android and the Editor.");
#endif
var stackTrace = ConvertStackTraceToString(_anrStackTrace);
var exception = new BacktraceUnhandledException(string.Empty, stackTrace);
var backtraceStackTrace = new BacktraceStackTrace(exception);
Expand All @@ -348,6 +351,9 @@ public void TestStackTraceCreation_AndroidException_ValidStackTraceObject()
[Test]
public void TestNativeStackTraceDetection_AndroidExceptionShouldSetFlag_NativeStackTraceIsSet()
{
#if !UNITY_ANDROID && !UNITY_EDITOR
Assert.Ignore("Android stack trace parsing is only compiled for Android and the Editor.");
#endif
var stackTrace = ConvertStackTraceToString(_anrStackTrace);
var exception = new BacktraceUnhandledException(string.Empty, stackTrace);
Assert.IsTrue(exception.NativeStackTrace);
Expand All @@ -366,6 +372,9 @@ public void TestNativeStackTraceDetection_UnityExceptionShouldNotSetFlag_NativeS
[Test]
public void TestStackTraceCreation_AndroidMixModeCallStack_ValidStackTraceObject()
{
#if !UNITY_ANDROID && !UNITY_EDITOR
Assert.Ignore("Android stack trace parsing is only compiled for Android and the Editor.");
#endif
var stackTrace = ConvertStackTraceToString(_mixModeCallStack);
var exception = new BacktraceUnhandledException(string.Empty, stackTrace);
var backtraceStackTrace = new BacktraceStackTrace(exception);
Expand Down
13 changes: 13 additions & 0 deletions Tests/Runtime/Client/BacktraceClientDisableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ public void Setup()
AfterSetup(false);
}

// Skip in a Player build: DisableInEditor only disables the client in the Editor
private static void SkipWhenNotInEditor()
{
if (!Application.isEditor)
{
Assert.Ignore("DisableInEditor only applies in the Editor.");
}
}

[Test]
public void TestEditorDisabling_ShouldntSendData_ShouldntSendExceptionViaSendAPI()
{
SkipWhenNotInEditor();
var clientConfiguration = GetValidClientConfiguration();
BacktraceClient.Configuration = clientConfiguration;
BacktraceClient.Configuration.DisableInEditor = true;
Expand All @@ -42,6 +52,7 @@ public void TestEditorDisabling_ShouldntSendData_ShouldntSendExceptionViaSendAPI
[Test]
public void TestEditorDisabling_ShouldntSendData_ShouldntSendReportViaSendAPI()
{
SkipWhenNotInEditor();
var clientConfiguration = GetValidClientConfiguration();
BacktraceClient.Configuration = clientConfiguration;
BacktraceClient.Configuration.DisableInEditor = true;
Expand All @@ -63,6 +74,7 @@ public void TestEditorDisabling_ShouldntSendData_ShouldntSendReportViaSendAPI()
[Test]
public void TestEditorDisabling_ShouldntSendData_ShouldntSendMessageViaSendAPI()
{
SkipWhenNotInEditor();
var clientConfiguration = GetValidClientConfiguration();
BacktraceClient.Configuration = clientConfiguration;
BacktraceClient.Configuration.DisableInEditor = true;
Expand All @@ -84,6 +96,7 @@ public void TestEditorDisabling_ShouldntSendData_ShouldntSendMessageViaSendAPI()
[Test]
public void TestEditorDisabling_ShouldntSendData_ShouldntSendUnhandledException()
{
SkipWhenNotInEditor();
var clientConfiguration = GetValidClientConfiguration();
BacktraceClient.Configuration = clientConfiguration;
BacktraceClient.Configuration.DisableInEditor = true;
Expand Down
14 changes: 13 additions & 1 deletion Tests/Runtime/Native/Windows/ScopedNativeAttributesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ public sealed class ScopedNativeAttributesTests
private const string ScopedKeyList = "backtrace-scoped-attributes";
private const string ScopedValuePattern = "bt-{0}";

[TearDown]
// PlayerPrefs persist between runs in a Player build, so clean before and after each test
[SetUp]
public void Setup()
{
CleanState();
}

[TearDown]
public void Cleanup()
{
CleanState();
}

private void CleanState()
{
CleanLegacyAttributes();
NativeClient.CleanScopedAttributes();
Expand Down
Loading