diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-10-08 13:15:15 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-10-08 13:15:15 +0300 |
commit | e7837b7c350fb27a0d211a363b56d69b95064c0e (patch) | |
tree | 5d83adf6672626ecbff1a45aadc7e6d4302e45b4 | |
parent | e03866f4d306192a4d4247868104da4219a993c3 (diff) |
Add unit test
Signed-off-by: Stefan Niedermann <info@niedermann.it>
-rw-r--r-- | app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryWithNotesCount.java | 10 | ||||
-rw-r--r-- | app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java | 86 |
2 files changed, 96 insertions, 0 deletions
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryWithNotesCount.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryWithNotesCount.java index ecdeae58..08be7454 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryWithNotesCount.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/CategoryWithNotesCount.java @@ -6,6 +6,16 @@ public class CategoryWithNotesCount { private String category; private Integer totalNotes; + public CategoryWithNotesCount() { + // Default constructor for Room + } + + public CategoryWithNotesCount(long accountId, String category, Integer totalNotes) { + this.accountId = accountId; + this.category = category; + this.totalNotes = totalNotes; + } + public Integer getTotalNotes() { return totalNotes; } diff --git a/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java b/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java new file mode 100644 index 00000000..d7100db3 --- /dev/null +++ b/app/src/test/java/it/niedermann/owncloud/notes/main/MainViewModelTest.java @@ -0,0 +1,86 @@ +package it.niedermann.owncloud.notes.main; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; + +import android.content.Context; +import android.util.Log; + +import androidx.arch.core.executor.testing.InstantTaskExecutorRule; +import androidx.lifecycle.SavedStateHandle; +import androidx.test.core.app.ApplicationProvider; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.lang.reflect.InvocationTargetException; +import java.util.List; +import java.util.Optional; + +import it.niedermann.owncloud.notes.main.navigation.NavigationItem; +import it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount; +import it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType; + +@RunWith(RobolectricTestRunner.class) +public class MainViewModelTest { + + @Rule + public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule(); + + private MainViewModel viewModel; + private Context context; + + @Before + public void setup() { + context = ApplicationProvider.getApplicationContext(); + viewModel = new MainViewModel(ApplicationProvider.getApplicationContext(), mock(SavedStateHandle.class)); + } + + @Test + public void fromCategoriesWithNotesCount() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + final var fromCategoriesWithNotesCount = MainViewModel.class.getDeclaredMethod("fromCategoriesWithNotesCount", Context.class, String.class, List.class, Integer.TYPE, Integer.TYPE); + fromCategoriesWithNotesCount.setAccessible(true); + + final var categoriesWithNotesCount = List.of( + new CategoryWithNotesCount(1, "Foo", 13), + new CategoryWithNotesCount(1, "Bar", 30), + new CategoryWithNotesCount(1, "Bar/abc", 10), + new CategoryWithNotesCount(1, "Bar/abc/def", 5), + new CategoryWithNotesCount(1, "Bar/xyz/zyx", 10), + new CategoryWithNotesCount(1, "Bar/aaa/bbb", 8), + new CategoryWithNotesCount(1, "Bar/ddd", 2), + new CategoryWithNotesCount(1, "Baz", 13) + ); + + //noinspection unchecked + final var allCollapsed = (List<NavigationItem>) fromCategoriesWithNotesCount.invoke(null, context, "", categoriesWithNotesCount, 56, 0); + + assertNotNull(allCollapsed); + assertEquals(5, allCollapsed.size()); + assertEquals(ENavigationCategoryType.RECENT, allCollapsed.get(0).type); + assertEquals(ENavigationCategoryType.FAVORITES, allCollapsed.get(1).type); + assertEquals("Foo", allCollapsed.get(2).label); + assertEquals("Bar", allCollapsed.get(3).label); + assertEquals("Baz", allCollapsed.get(4).label); + + //noinspection unchecked + final var barExpanded = (List<NavigationItem>) fromCategoriesWithNotesCount.invoke(null, context, "Bar", categoriesWithNotesCount, 56, 0); + + assertNotNull(barExpanded); + assertEquals(10, barExpanded.size()); + assertEquals(ENavigationCategoryType.RECENT, barExpanded.get(0).type); + assertEquals(ENavigationCategoryType.FAVORITES, barExpanded.get(1).type); + assertEquals("Foo", barExpanded.get(2).label); + assertEquals("Bar", barExpanded.get(3).label); + assertEquals("abc", barExpanded.get(4).label); + assertEquals("abc/de", barExpanded.get(5).label); + assertEquals("xyz/zyx", barExpanded.get(6).label); + assertEquals("aaa/bb", barExpanded.get(7).label); + assertEquals("dd", barExpanded.get(8).label); + assertEquals("Baz", barExpanded.get(9).label); + } +}
\ No newline at end of file |