NamedUser extends AirshipComponent { @Override protected void init() { super.init(); tagGroupRegistrar.setId(getId(), false); attributeRegistrar.setId(getId(), false); airshipChannel.addChannelListener(new AirshipChannelListener() { @Override public void onChannelCreated(@NonNull String channelId) { dispatchNamedUserUpdateJob(); } @Override public void onChannelUpdated(@NonNull String channelId) { } }); airshipChannel.addChannelRegistrationPayloadExtender(new AirshipChannel.ChannelRegistrationPayloadExtender() { @NonNull @Override public ChannelRegistrationPayload.Builder extend(@NonNull ChannelRegistrationPayload.Builder builder) { return builder.setNamedUserId(getId()); } }); if (airshipChannel.getId() != null && (!isIdUpToDate() || getId() != null)) { dispatchNamedUserUpdateJob(); } } NamedUser(@NonNull Context context, @NonNull PreferenceDataStore preferenceDataStore,
                     @NonNull AirshipRuntimeConfig runtimeConfig, @NonNull AirshipChannel airshipChannel); @VisibleForTesting  NamedUser(@NonNull Context context, @NonNull PreferenceDataStore preferenceDataStore,
              @NonNull AirshipChannel airshipChannel, @NonNull JobDispatcher dispatcher,
              @NonNull Clock clock, @NonNull NamedUserApiClient namedUserApiClient,
              @NonNull AttributeRegistrar attributeRegistrar, @NonNull TagGroupRegistrar tagGroupRegistrar); @Override @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @AirshipComponentGroups.Group int getComponentGroup(); @Override @WorkerThread @JobInfo.JobResult @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) int onPerformJob(@NonNull UAirship airship, @NonNull JobInfo jobInfo); @NonNull @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) List<TagGroupsMutation> getPendingTagUpdates(); @NonNull @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) List<AttributeMutation> getPendingAttributeUpdates(); @Nullable String getId(); void forceUpdate(); void setId(@Nullable @Size(max = MAX_NAMED_USER_ID_LENGTH) String namedUserId); @NonNull TagGroupsEditor editTagGroups(); @NonNull AttributeEditor editAttributes(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void addTagGroupListener(@NonNull TagGroupListener listener); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void addAttributeListener(@NonNull AttributeListener listener);  }
@Test public void testChannelCreateUpdatesNamedUser() { ArgumentCaptor<AirshipChannelListener> argument = ArgumentCaptor.forClass(AirshipChannelListener.class); namedUser.init(); verify(mockChannel).addChannelListener(argument.capture()); AirshipChannelListener listener = argument.getValue(); assertNotNull(listener); clearInvocations(mockDispatcher); listener.onChannelCreated("some-channel"); verify(mockDispatcher).dispatch(Mockito.argThat(new ArgumentMatcher<JobInfo>() { @Override public boolean matches(JobInfo jobInfo) { return jobInfo.getAction().equals(NamedUser.ACTION_UPDATE_NAMED_USER); } })); }
@Test public void testInit() { namedUser.setId("test"); shadowOf(application).clearStartedServices(); namedUser.init(); verify(mockDispatcher, atLeastOnce()).dispatch(Mockito.argThat(new ArgumentMatcher<JobInfo>() { @Override public boolean matches(JobInfo jobInfo) { return jobInfo.getAction().equals(NamedUser.ACTION_UPDATE_NAMED_USER); } })); }