Skip to content

Commit

Permalink
fix background span not sent because not checking visibility tracker …
Browse files Browse the repository at this point in the history
…current screen
  • Loading branch information
rezastallone committed Oct 11, 2023
1 parent ae948e6 commit 57122a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ Stream<File> listFilesRecursively(File directory) {
return Stream.empty();
}
File[] files = directory.listFiles();
if (files == null){
return Stream.empty();
}
Stream<File> directories = Stream.of(files).filter(File::isDirectory);
Stream<File> plainFiles = Stream.of(files).filter(File::isFile);
return Stream.concat(plainFiles, directories.flatMap(this::listFilesRecursively));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ public Stream<File> getAllSpanFiles() {

@Override
public Stream<File> getPendingFiles() {
if (visibleScreenTracker.getPreviouslyVisibleScreen() != null) {
if (isAppForeground()) {
moveBackgroundSpanToPendingSpan();
}
return fileUtils.listSpanFiles(spanDir);
}

private boolean isAppForeground() {
return (visibleScreenTracker.getCurrentlyVisibleScreen() != null && !visibleScreenTracker.getCurrentlyVisibleScreen().equals("unknown")) || visibleScreenTracker.getPreviouslyVisibleScreen() != null;
}

private void moveBackgroundSpanToPendingSpan() {
fileUtils
.listSpanFiles(getCurrentSessionBackgroundFile())
Expand Down Expand Up @@ -90,9 +94,15 @@ public File provideSpanFile() {
}

private File getSpanFile() {
if (visibleScreenTracker.getPreviouslyVisibleScreen() == null) {
if (!isAppForeground()) {
Log.d(
LOG_TAG,
"Creating background span " + uniqueId);
return getCurrentSessionBackgroundFile();
}
Log.d(
LOG_TAG,
"Creating foreground span " + uniqueId);
return spanDir;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ void setup() {
@Test
void getPendingFiles_givenInBackground_shouldReturnForegoundOnlySpan() {
when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn(null);
when(visibleScreenTracker.getCurrentlyVisibleScreen()).thenReturn("unknown");
List<File> spans = fileProvider.getPendingFiles().collect(Collectors.toList());
assertEquals(0, spans.size());
}

@Test
void
getPendingFiles_givenPrevouslyInBackground_shouldMoveBackgroundSpanToForegroundSpanForSending() {
void getPendingFiles_givenPrevouslyInBackground_shouldMoveBackgroundSpanToForegroundSpanForSending() {
when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn("MainActivity");
when(visibleScreenTracker.getCurrentlyVisibleScreen()).thenReturn("MainActivity");

List<File> backgroundFiles = new ArrayList<>();
File fileToMove = mock();
Expand Down Expand Up @@ -86,6 +87,7 @@ void getPendingFiles_givenInBackground_shouldReturnForegoundOnlySpan() {
@Test
void getSpanPath_givenInBackground_shouldReturnBackgroundSpanPath() {
when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn(null);
when(visibleScreenTracker.getCurrentlyVisibleScreen()).thenReturn("unknown");

File path = fileProvider.provideSpanFile();

Expand All @@ -95,6 +97,7 @@ void getSpanPath_givenInBackground_shouldReturnBackgroundSpanPath() {
@Test
void getSpanPath_givenInForeground_shouldReturnForegroundSpanPath() {
when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn("MainActivity");
when(visibleScreenTracker.getCurrentlyVisibleScreen()).thenReturn("MainActivity");

File path = fileProvider.provideSpanFile();

Expand Down

0 comments on commit 57122a5

Please sign in to comment.