Skip to content

Commit 56e21ab

Browse files
authored
Removed butterknife and cleaned up the test (#5446)
1 parent 87d1255 commit 56e21ab

File tree

2 files changed

+44
-53
lines changed

2 files changed

+44
-53
lines changed

app/src/main/java/fr/free/nrw/commons/navtab/MoreBottomSheetLoggedOutFragment.java

+27-19
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
import androidx.annotation.NonNull;
1313
import androidx.annotation.Nullable;
1414
import androidx.appcompat.app.AlertDialog;
15-
import butterknife.ButterKnife;
16-
import butterknife.OnClick;
1715
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
1816
import fr.free.nrw.commons.AboutActivity;
1917
import fr.free.nrw.commons.CommonsApplication;
2018
import fr.free.nrw.commons.R;
2119
import fr.free.nrw.commons.auth.LoginActivity;
20+
import fr.free.nrw.commons.databinding.FragmentMoreBottomSheetLoggedOutBinding;
2221
import fr.free.nrw.commons.di.ApplicationlessInjection;
2322
import fr.free.nrw.commons.kvstore.JsonKvStore;
2423
import fr.free.nrw.commons.logging.CommonsLogSender;
@@ -29,6 +28,7 @@
2928

3029
public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment {
3130

31+
private FragmentMoreBottomSheetLoggedOutBinding binding;
3232
@Inject
3333
CommonsLogSender commonsLogSender;
3434
@Inject
@@ -39,30 +39,40 @@ public class MoreBottomSheetLoggedOutFragment extends BottomSheetDialogFragment
3939
@Override
4040
public View onCreateView(@NonNull final LayoutInflater inflater,
4141
@Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
42-
super.onCreateView(inflater, container, savedInstanceState);
43-
final View view = inflater.inflate(R.layout.fragment_more_bottom_sheet_logged_out, container, false);
44-
ButterKnife.bind(this, view);
45-
return view;
42+
binding = FragmentMoreBottomSheetLoggedOutBinding.inflate(inflater, container, false);
43+
return binding.getRoot();
44+
}
45+
46+
@Override
47+
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
48+
binding.moreLogin.setOnClickListener(v -> onLogoutClicked());
49+
binding.moreFeedback.setOnClickListener(v -> onFeedbackClicked());
50+
binding.moreAbout.setOnClickListener(v -> onAboutClicked());
51+
binding.moreSettings.setOnClickListener(v -> onSettingsClicked());
52+
}
53+
54+
@Override
55+
public void onDestroyView() {
56+
super.onDestroyView();
57+
binding = null;
4658
}
4759

4860
@Override
4961
public void onAttach(@NonNull final Context context) {
5062
super.onAttach(context);
5163
ApplicationlessInjection
52-
.getInstance(getActivity().getApplicationContext())
64+
.getInstance(requireActivity().getApplicationContext())
5365
.getCommonsApplicationComponent()
5466
.inject(this);
5567
}
5668

57-
@OnClick(R.id.more_login)
5869
public void onLogoutClicked() {
5970
applicationKvStore.putBoolean("login_skipped", false);
60-
Intent intent = new Intent(getContext(), LoginActivity.class);
61-
getActivity().finish(); //Kill the activity from which you will go to next activity
71+
final Intent intent = new Intent(getContext(), LoginActivity.class);
72+
requireActivity().finish(); //Kill the activity from which you will go to next activity
6273
startActivity(intent);
6374
}
6475

65-
@OnClick(R.id.more_feedback)
6676
public void onFeedbackClicked() {
6777
showAlertDialog();
6878
}
@@ -71,7 +81,7 @@ public void onFeedbackClicked() {
7181
* This method shows the alert dialog when a user wants to send feedback about the app.
7282
*/
7383
private void showAlertDialog() {
74-
new AlertDialog.Builder(getActivity())
84+
new AlertDialog.Builder(requireActivity())
7585
.setMessage(R.string.feedback_sharing_data_alert)
7686
.setCancelable(false)
7787
.setPositiveButton(R.string.ok, (dialog, which) -> {
@@ -81,8 +91,8 @@ private void showAlertDialog() {
8191
}
8292

8393
/**
84-
* This method collects the feedback message and starts and activity with implicit intent
85-
* to available email client.
94+
* This method collects the feedback message and starts and activity with implicit intent to
95+
* available email client.
8696
*/
8797
private void sendFeedback() {
8898
final String technicalInfo = commonsLogSender.getExtraInfo();
@@ -103,18 +113,16 @@ private void sendFeedback() {
103113
}
104114
}
105115

106-
@OnClick(R.id.more_about)
107116
public void onAboutClicked() {
108117
final Intent intent = new Intent(getActivity(), AboutActivity.class);
109118
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
110-
getActivity().startActivity(intent);
119+
requireActivity().startActivity(intent);
111120
}
112121

113-
@OnClick(R.id.more_settings)
114122
public void onSettingsClicked() {
115123
final Intent intent = new Intent(getActivity(), SettingsActivity.class);
116124
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
117-
getActivity().startActivity(intent);
125+
requireActivity().startActivity(intent);
118126
}
119127

120128
private class BaseLogoutListener implements CommonsApplication.LogoutListener {
@@ -127,7 +135,7 @@ public void onLogoutComplete() {
127135
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
128136
nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
129137
startActivity(nearbyIntent);
130-
getActivity().finish();
138+
requireActivity().finish();
131139
}
132140
}
133141
}
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,60 @@
11
package fr.free.nrw.commons.navtab
22

3-
import android.content.Context
43
import android.os.Looper
5-
import androidx.fragment.app.FragmentManager
6-
import androidx.fragment.app.FragmentTransaction
7-
import androidx.test.core.app.ApplicationProvider
4+
import androidx.fragment.app.testing.FragmentScenario
5+
import androidx.fragment.app.testing.launchFragmentInContainer
6+
import androidx.lifecycle.Lifecycle
7+
import androidx.test.ext.junit.runners.AndroidJUnit4
88
import fr.free.nrw.commons.TestCommonsApplication
9-
import fr.free.nrw.commons.profile.ProfileActivity
10-
import org.junit.Assert
119
import org.junit.Before
1210
import org.junit.Test
1311
import org.junit.runner.RunWith
14-
import org.robolectric.Robolectric
15-
import org.robolectric.RobolectricTestRunner
1612
import org.robolectric.Shadows
1713
import org.robolectric.annotation.Config
1814
import org.robolectric.annotation.LooperMode
15+
import fr.free.nrw.commons.R
1916

20-
@RunWith(RobolectricTestRunner::class)
17+
@RunWith(AndroidJUnit4::class)
2118
@Config(sdk = [21], application = TestCommonsApplication::class)
2219
@LooperMode(LooperMode.Mode.PAUSED)
2320
class MoreBottomSheetLoggedOutFragmentUnitTests {
24-
25-
private lateinit var fragment: MoreBottomSheetLoggedOutFragment
26-
27-
private lateinit var context: Context
21+
private lateinit var scenario: FragmentScenario<MoreBottomSheetLoggedOutFragment>
2822

2923
@Before
3024
fun setUp() {
31-
32-
context = ApplicationProvider.getApplicationContext()
33-
34-
val activity = Robolectric.buildActivity(ProfileActivity::class.java).create().get()
35-
fragment = MoreBottomSheetLoggedOutFragment()
36-
val fragmentManager: FragmentManager = activity.supportFragmentManager
37-
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
38-
fragmentTransaction.add(fragment, null)
39-
fragmentTransaction.commit()
40-
41-
}
42-
43-
@Test
44-
@Throws(Exception::class)
45-
fun checkFragmentNotNull() {
46-
Assert.assertNotNull(fragment)
25+
scenario = launchFragmentInContainer(
26+
initialState = Lifecycle.State.RESUMED,
27+
themeResId = R.style.LightAppTheme
28+
) {
29+
MoreBottomSheetLoggedOutFragment()
30+
}
4731
}
4832

4933
@Test
5034
@Throws(Exception::class)
5135
fun testOnSettingsClicked() {
5236
Shadows.shadowOf(Looper.getMainLooper()).idle()
53-
fragment.onSettingsClicked()
37+
scenario.onFragment { it.onSettingsClicked() }
5438
}
5539

5640
@Test
5741
@Throws(Exception::class)
5842
fun testOnAboutClicked() {
5943
Shadows.shadowOf(Looper.getMainLooper()).idle()
60-
fragment.onAboutClicked()
44+
scenario.onFragment { it.onAboutClicked() }
6145
}
6246

6347
@Test
6448
@Throws(Exception::class)
6549
fun testOnFeedbackClicked() {
6650
Shadows.shadowOf(Looper.getMainLooper()).idle()
67-
fragment.onFeedbackClicked()
51+
scenario.onFragment { it.onFeedbackClicked() }
6852
}
6953

7054
@Test
7155
@Throws(Exception::class)
7256
fun testOnLogoutClicked() {
7357
Shadows.shadowOf(Looper.getMainLooper()).idle()
74-
fragment.onLogoutClicked()
58+
scenario.onFragment { it.onLogoutClicked() }
7559
}
76-
7760
}

0 commit comments

Comments
 (0)