Skip to content

Commit ac1fae2

Browse files
Ian BirdIanDBird
Ian Bird
authored andcommitted
Add ability to query if identity is available
1 parent 8354a65 commit ac1fae2

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

sdk/src/main/java/com/uid2/UID2Manager.kt

+6
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ public class UID2Manager internal constructor(
122122
else -> null
123123
}
124124

125+
/**
126+
* Gets whether or not [UID2Manager] has a known [UID2Identity]. If not, a new identity should be generated and set
127+
* via [setIdentity].
128+
*/
129+
public fun hasIdentity(): Boolean = currentIdentity != null
130+
125131
/**
126132
* Gets the current Identity Status.
127133
*/

sdk/src/test/java/com/uid2/UID2ManagerTest.kt

+38
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,44 @@ class UID2ManagerTest {
121121
assertManagerState(manager, null, NO_IDENTITY)
122122
}
123123

124+
@Test
125+
fun `reports when has identity`() = runTest(testDispatcher) {
126+
// When the default identity is restored, verify that hasIdentity reflects this.
127+
assertNotNull(manager.currentIdentity)
128+
assertTrue(manager.hasIdentity())
129+
}
130+
131+
@Test
132+
fun `reports when no identity available`() = runTest(testDispatcher) {
133+
// Reset the Manager's identity
134+
manager.resetIdentity()
135+
testDispatcher.scheduler.advanceUntilIdle()
136+
137+
// Verify that no identity is reported.
138+
assertNull(manager.currentIdentity)
139+
assertFalse(manager.hasIdentity())
140+
}
141+
142+
@Test
143+
fun `reports when no identity available after opt-out`() = runTest(testDispatcher) {
144+
// Configure the client so that when asked to refresh, it actually reports that the user has now opted out.
145+
whenever(client.refreshIdentity(initialIdentity.refreshToken, initialIdentity.refreshResponseKey)).thenReturn(
146+
RefreshPackage(
147+
null,
148+
OPT_OUT,
149+
"User opt-ed out",
150+
),
151+
)
152+
153+
// Ask the manager to refresh, allowing the current TestDispatcher to process any jobs.
154+
manager.refreshIdentity()
155+
testDispatcher.scheduler.advanceUntilIdle()
156+
157+
// Verify that no identity is reported.
158+
assertNull(manager.currentIdentity)
159+
assertFalse(manager.hasIdentity())
160+
}
161+
124162
@Test
125163
fun `refresh no-op when no identity`() = runTest(testDispatcher) {
126164
// Create a mock StorageManager that doesn't have any previously saved Identity.

0 commit comments

Comments
 (0)