-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSettingsLogsFragmentTest.kt
More file actions
209 lines (180 loc) 路 7.18 KB
/
Copy pathSettingsLogsFragmentTest.kt
File metadata and controls
209 lines (180 loc) 路 7.18 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* openCloud Android client application
*
* @author Juan Carlos Garrote Gasc贸n
*
* Copyright (C) 2021 ownCloud GmbH.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.opencloud.android.settings.logs
import android.content.Context
import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.preference.CheckBoxPreference
import androidx.preference.Preference
import androidx.preference.PreferenceManager
import androidx.preference.SwitchPreferenceCompat
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import eu.opencloud.android.R
import eu.opencloud.android.presentation.logging.LogsListActivity
import eu.opencloud.android.presentation.settings.logging.SettingsLogsFragment
import eu.opencloud.android.presentation.logging.LogListViewModel
import eu.opencloud.android.presentation.settings.logging.SettingsLogsViewModel
import eu.opencloud.android.utils.matchers.verifyPreference
import io.mockk.every
import io.mockk.mockk
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.dsl.module
class SettingsLogsFragmentTest {
private lateinit var fragmentScenario: FragmentScenario<SettingsLogsFragment>
private lateinit var prefEnableLogging: SwitchPreferenceCompat
private lateinit var prefHttpLogs: CheckBoxPreference
private lateinit var prefLogsListActivity: Preference
private lateinit var logsViewModel: SettingsLogsViewModel
private lateinit var logListViewModel: LogListViewModel
private lateinit var context: Context
@Before
fun setUp() {
context = InstrumentationRegistry.getInstrumentation().targetContext
logsViewModel = mockk(relaxed = true)
logListViewModel = mockk(relaxed = true)
stopKoin()
startKoin {
context
allowOverride(override = true)
modules(
module {
viewModel {
logsViewModel
}
viewModel {
logListViewModel
}
}
)
}
Intents.init()
}
@After
fun tearDown() {
Intents.release()
PreferenceManager.getDefaultSharedPreferences(context).edit().clear().commit()
unmockkAll()
}
private fun launchTest(enabledLogging: Boolean) {
every { logsViewModel.isLoggingEnabled() } returns enabledLogging
fragmentScenario = launchFragmentInContainer(themeResId = R.style.Theme_openCloud)
fragmentScenario.onFragment { fragment ->
prefEnableLogging = fragment.findPreference(SettingsLogsFragment.PREFERENCE_ENABLE_LOGGING)!!
prefHttpLogs = fragment.findPreference(SettingsLogsFragment.PREFERENCE_LOG_HTTP)!!
prefLogsListActivity = fragment.findPreference(SettingsLogsFragment.PREFERENCE_LOGS_LIST)!!
}
}
@Test
fun logsViewLoggingDisabled() {
launchTest(enabledLogging = false)
prefEnableLogging.verifyPreference(
keyPref = SettingsLogsFragment.PREFERENCE_ENABLE_LOGGING,
titlePref = context.getString(R.string.prefs_enable_logging),
summaryPref = context.getString(R.string.prefs_enable_logging_summary),
visible = true,
enabled = true
)
prefHttpLogs.verifyPreference(
keyPref = SettingsLogsFragment.PREFERENCE_LOG_HTTP,
titlePref = context.getString(R.string.prefs_http_logs),
visible = true,
enabled = false
)
prefLogsListActivity.verifyPreference(
keyPref = SettingsLogsFragment.PREFERENCE_LOGS_LIST,
titlePref = context.getString(R.string.prefs_log_open_logs_list_view),
visible = true,
enabled = true,
)
}
@Test
fun logsViewLoggingEnabled() {
launchTest(enabledLogging = true)
prefEnableLogging.verifyPreference(
keyPref = SettingsLogsFragment.PREFERENCE_ENABLE_LOGGING,
titlePref = context.getString(R.string.prefs_enable_logging),
summaryPref = context.getString(R.string.prefs_enable_logging_summary),
visible = true,
enabled = true
)
prefHttpLogs.verifyPreference(
keyPref = SettingsLogsFragment.PREFERENCE_LOG_HTTP,
titlePref = context.getString(R.string.prefs_http_logs),
visible = true,
enabled = true
)
prefLogsListActivity.verifyPreference(
keyPref = SettingsLogsFragment.PREFERENCE_LOGS_LIST,
titlePref = context.getString(R.string.prefs_log_open_logs_list_view),
visible = true,
enabled = true,
)
}
@Ignore
@Test
fun enableLoggingMakesSettingsEnable() {
launchTest(enabledLogging = false)
onView(withText(R.string.prefs_enable_logging)).perform(click())
assertTrue(prefHttpLogs.isEnabled)
}
@Test
fun disableLoggingMakesSettingsDisable() {
launchTest(enabledLogging = false)
onView(withText(R.string.prefs_enable_logging)).perform(click())
onView(withText(R.string.prefs_enable_logging)).perform(click())
assertFalse(prefHttpLogs.isEnabled)
assertTrue(prefLogsListActivity.isEnabled)
}
@Test
fun checkHttpLogs() {
launchTest(enabledLogging = true)
onView(withText(R.string.prefs_http_logs)).perform(click())
assertTrue(prefHttpLogs.isChecked)
}
@Test
fun disableLoggingMakesHttpLogsNotChecked() {
launchTest(enabledLogging = false)
onView(withText(R.string.prefs_enable_logging)).perform(click())
onView(withText(R.string.prefs_http_logs)).perform(click())
onView(withText(R.string.prefs_enable_logging)).perform(click())
assertFalse(prefHttpLogs.isChecked)
}
@Test
fun loggerOpen() {
launchTest(enabledLogging = true)
onView(withText(R.string.prefs_log_open_logs_list_view)).perform(click())
intended(hasComponent(LogsListActivity::class.java.name))
}
}