Repository navigation
Expand file tree
/
Copy pathMainViewModelTest.java
More file actions
178 lines (149 loc) · 10.2 KB
/
Copy pathMainViewModelTest.java
File metadata and controls
178 lines (149 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.main;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import android.content.Context;
import androidx.arch.core.executor.testing.InstantTaskExecutorRule;
import androidx.test.core.app.ApplicationProvider;
import com.nextcloud.android.sso.exceptions.UnknownErrorException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
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)
@Config(sdk = 36)
public class MainViewModelTest {
@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
private Context context;
private Method fromCategoriesWithNotesCount;
@Before
public void setup() throws NoSuchMethodException {
context = ApplicationProvider.getApplicationContext();
fromCategoriesWithNotesCount = MainViewModel.class.getDeclaredMethod("fromCategoriesWithNotesCount", Context.class, String.class, List.class, Integer.TYPE, Integer.TYPE);
fromCategoriesWithNotesCount.setAccessible(true);
}
@Test
public void fromCategoriesWithNotesCount_nothing_expanded() throws InvocationTargetException, IllegalAccessException {
//noinspection unchecked
final var navigationItems = (List<NavigationItem>) fromCategoriesWithNotesCount.invoke(null, context, "", getSaneCategoriesWithNotesCount(), 56, 0);
assertNotNull(navigationItems);
assertEquals(5, navigationItems.size());
assertEquals(ENavigationCategoryType.RECENT, navigationItems.get(0).type);
assertEquals(ENavigationCategoryType.FAVORITES, navigationItems.get(1).type);
assertEquals("Foo", navigationItems.get(2).label);
assertEquals("Bar", navigationItems.get(3).label);
assertEquals("Baz", navigationItems.get(4).label);
}
@Test
public void fromCategoriesWithNotesCount_Bar_expanded() throws InvocationTargetException, IllegalAccessException {
//noinspection unchecked
final var navigationItems = (List<NavigationItem>) fromCategoriesWithNotesCount.invoke(null, context, "Bar", getSaneCategoriesWithNotesCount(), 56, 0);
assertNotNull(navigationItems);
assertEquals(9, navigationItems.size());
assertEquals(ENavigationCategoryType.RECENT, navigationItems.get(0).type);
assertEquals(ENavigationCategoryType.FAVORITES, navigationItems.get(1).type);
assertEquals("Foo", navigationItems.get(2).label);
assertEquals("Bar", navigationItems.get(3).label);
assertEquals("abc", navigationItems.get(4).label);
assertEquals("xyz", navigationItems.get(5).label);
assertEquals("aaa", navigationItems.get(6).label);
assertEquals("ddd", navigationItems.get(7).label);
assertEquals("Baz", navigationItems.get(8).label);
}
@Test
public void fromCategoriesWithNotesCount_invalid_expanded() throws InvocationTargetException, IllegalAccessException {
//noinspection unchecked
final var navigationItems = (List<NavigationItem>) fromCategoriesWithNotesCount.invoke(null, context, "ThisCategoryDoesNotExist", getSaneCategoriesWithNotesCount(), 56, 0);
assertNotNull(navigationItems);
assertEquals(5, navigationItems.size());
assertEquals(ENavigationCategoryType.RECENT, navigationItems.get(0).type);
assertEquals(ENavigationCategoryType.FAVORITES, navigationItems.get(1).type);
assertEquals("Foo", navigationItems.get(2).label);
assertEquals("Bar", navigationItems.get(3).label);
assertEquals("Baz", navigationItems.get(4).label);
}
/**
* Expanded sub categories are not supported and should therefore be treated like an unknown category
*/
@Test
public void fromCategoriesWithNotesCount_subcategory_expanded() throws InvocationTargetException, IllegalAccessException {
//noinspection unchecked
final var navigationItems = (List<NavigationItem>) fromCategoriesWithNotesCount.invoke(null, context, "Bar/abc", getSaneCategoriesWithNotesCount(), 56, 0);
assertNotNull(navigationItems);
assertEquals(5, navigationItems.size());
assertEquals(ENavigationCategoryType.RECENT, navigationItems.get(0).type);
assertEquals(ENavigationCategoryType.FAVORITES, navigationItems.get(1).type);
assertEquals("Foo", navigationItems.get(2).label);
assertEquals("Bar", navigationItems.get(3).label);
assertEquals("Baz", navigationItems.get(4).label);
}
@Test
public void fromCategoriesWithNotesCount_only_deep_category_without_favorites() throws InvocationTargetException, IllegalAccessException {
//noinspection unchecked
final var navigationItems = (List<NavigationItem>) fromCategoriesWithNotesCount.invoke(null, context, "Bar/abc", List.of(
new CategoryWithNotesCount(1, "Bar/abc/def", 5)
), 0, 0);
assertNotNull(navigationItems);
assertEquals(3, navigationItems.size());
assertEquals(ENavigationCategoryType.RECENT, navigationItems.get(0).type);
assertEquals(ENavigationCategoryType.FAVORITES, navigationItems.get(1).type);
assertEquals("Bar", navigationItems.get(2).label);
}
@Test
public void containsNonInfrastructureRelatedItems() {
//noinspection ConstantConditions
final var vm = new MainViewModel(ApplicationProvider.getApplicationContext(), null);
assertFalse(vm.containsNonInfrastructureRelatedItems(null));
assertFalse(vm.containsNonInfrastructureRelatedItems(Collections.emptyList()));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new UnknownErrorException("Software caused connection abort"))));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new UnknownErrorException("failed to connect to example.com (port 443) from /:: (port 39885) after 5000ms: connect failed: ENETUNREACH (Network is unreachable)"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new UnknownErrorException("Foo bar"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"), new UnknownErrorException("Software caused connection abort"))));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Software caused connection abort"))));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("failed to connect to example.com (port 443) from /:: (port 39885) after 5000ms: connect failed: ENETUNREACH (Network is unreachable)"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Foo bar"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"), new RuntimeException("Software caused connection abort"))));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException(new UnknownErrorException("Software caused connection abort")))));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException(new UnknownErrorException("failed to connect to example.com (port 443) from /:: (port 39885) after 5000ms: connect failed: ENETUNREACH (Network is unreachable)")))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException(new UnknownErrorException("Foo bar")))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"), new RuntimeException(new UnknownErrorException("Software caused connection abort")))));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Foo", new UnknownErrorException("Software caused connection abort")))));
assertFalse(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Foo", new UnknownErrorException("failed to connect to example.com (port 443) from /:: (port 39885) after 5000ms: connect failed: ENETUNREACH (Network is unreachable)")))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Foo", new UnknownErrorException("Foo bar")))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new Exception("Software caused connection abort"), new RuntimeException("Foo", new UnknownErrorException("Software caused connection abort")))));
assertTrue(vm.containsNonInfrastructureRelatedItems(List.of(new RuntimeException("Foo", new Exception("Software caused connection abort")))));
}
private static List<CategoryWithNotesCount> getSaneCategoriesWithNotesCount() {
return 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)
);
}
}