Skip to content

Commit f5a5159

Browse files
Fix Crash Edit Categories Fragment (commons-app#5510)
* fix crash * Tests Added * Tests and Null Checks Added
1 parent 5357923 commit f5a5159

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoriesFragment.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
102102
wikiText = bundle.getString("WikiText");
103103
nearbyPlaceCategory = bundle.getString(SELECTED_NEARBY_PLACE_CATEGORY);
104104
}
105-
if(callback!=null) {
106-
init();
107-
presenter.getCategories().observe(getViewLifecycleOwner(), this::setCategories);
108-
}
105+
init();
106+
presenter.getCategories().observe(getViewLifecycleOwner(), this::setCategories);
109107

110108
}
111109

112110
private void init() {
113111
if (media == null) {
114-
tvTitle.setText(getString(R.string.step_count, callback.getIndexInViewFlipper(this) + 1,
115-
callback.getTotalNumberOfSteps(), getString(R.string.categories_activity_title)));
112+
if (callback != null) {
113+
tvTitle.setText(getString(R.string.step_count, callback.getIndexInViewFlipper(this) + 1,
114+
callback.getTotalNumberOfSteps(), getString(R.string.categories_activity_title)));
115+
}
116116
} else {
117117
tvTitle.setText(R.string.edit_categories);
118118
tvSubTitle.setVisibility(View.GONE);
@@ -221,7 +221,9 @@ public void run() {
221221

222222
@Override
223223
public void goToNextScreen() {
224-
callback.onNextButtonClicked(callback.getIndexInViewFlipper(this));
224+
if (callback != null){
225+
callback.onNextButtonClicked(callback.getIndexInViewFlipper(this));
226+
}
225227
}
226228

227229
@Override
@@ -314,7 +316,9 @@ public void onPreviousButtonClicked() {
314316
mediaDetailFragment.onResume();
315317
goBackToPreviousScreen();
316318
} else {
317-
callback.onPreviousButtonClicked(callback.getIndexInViewFlipper(this));
319+
if (callback != null) {
320+
callback.onPreviousButtonClicked(callback.getIndexInViewFlipper(this));
321+
}
318322
}
319323
}
320324

app/src/test/kotlin/fr/free/nrw/commons/upload/categories/UploadCategoriesFragmentUnitTests.kt

+13-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.app.ProgressDialog
44
import android.content.Context
55
import android.os.Looper
66
import android.text.Editable
7+
import android.util.Log
78
import android.view.LayoutInflater
89
import android.view.View
910
import android.widget.Button
@@ -116,7 +117,6 @@ class UploadCategoriesFragmentUnitTests {
116117
Whitebox.setInternalState(fragment, "pbCategories", pbCategories)
117118
Whitebox.setInternalState(fragment, "tilContainerEtSearch", tilContainerEtSearch)
118119
Whitebox.setInternalState(fragment, "adapter", adapter)
119-
Whitebox.setInternalState(fragment, "callback", callback)
120120
Whitebox.setInternalState(fragment, "presenter", presenter)
121121
Whitebox.setInternalState(fragment, "etSearch", etSearch)
122122
Whitebox.setInternalState(fragment, "rvCategories", rvCategories)
@@ -365,4 +365,15 @@ class UploadCategoriesFragmentUnitTests {
365365
method.invoke(fragment)
366366
}
367367

368-
}
368+
@Test
369+
@Throws(Exception::class)
370+
fun `Test init when callback is null`() {
371+
Shadows.shadowOf(Looper.getMainLooper()).idle()
372+
val method: Method = UploadCategoriesFragment::class.java.getDeclaredMethod(
373+
"init"
374+
)
375+
method.isAccessible = true
376+
method.invoke(fragment)
377+
}
378+
379+
}

0 commit comments

Comments
 (0)