Skip to content
This repository was archived by the owner on Jan 14, 2018. It is now read-only.
/ android-app Public archive

Commit 9ecd918

Browse files
committed
Add tests for setup fragment
1 parent 0d27570 commit 9ecd918

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* This file is part of FreedomBox.
3+
*
4+
* FreedomBox is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* FreedomBox is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with FreedomBox. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.freedombox.freedombox.view.fragments
19+
20+
import android.preference.PreferenceManager
21+
import android.widget.EditText
22+
import android.widget.Switch
23+
import org.freedombox.freedombox.BuildConfig
24+
import org.freedombox.freedombox.R
25+
import org.freedombox.freedombox.views.activities.SetupActivity
26+
import org.junit.Assert
27+
import org.junit.Test
28+
import org.junit.runner.RunWith
29+
import org.robolectric.Robolectric
30+
import org.robolectric.RobolectricTestRunner
31+
import org.robolectric.RuntimeEnvironment
32+
import org.robolectric.Shadows
33+
import org.robolectric.annotation.Config
34+
35+
36+
@RunWith(RobolectricTestRunner::class)
37+
@Config(constants = BuildConfig::class)
38+
class SetupFragmentTest {
39+
40+
@Test
41+
fun shouldBeAbleToViewViewsInScreen() {
42+
43+
val activity = Robolectric.setupActivity(SetupActivity::class.java)
44+
val shadowActvity = Shadows.shadowOf(activity)
45+
46+
val boxName = shadowActvity.findViewById(R.id.boxName)
47+
Assert.assertNotNull(boxName)
48+
49+
val discoveredUrl = shadowActvity.findViewById(R.id.discoveredUrl)
50+
Assert.assertNotNull(discoveredUrl)
51+
52+
val username = shadowActvity.findViewById(R.id.username)
53+
Assert.assertNotNull(username)
54+
55+
val password = shadowActvity.findViewById(R.id.password)
56+
Assert.assertNotNull(password)
57+
58+
var default = shadowActvity.findViewById(R.id.setDefault)
59+
Assert.assertNotNull(default)
60+
61+
var saveConfig = shadowActvity.findViewById(R.id.saveConfig)
62+
Assert.assertNotNull(saveConfig)
63+
64+
}
65+
66+
@Test
67+
fun navigateToLauncherScreenOnButtonClick() {
68+
69+
val activity = Robolectric.setupActivity(SetupActivity::class.java)
70+
val shadowActvity = Shadows.shadowOf(activity)
71+
shadowActvity.findViewById(R.id.saveConfig).performClick()
72+
Assert.assertTrue(shadowActvity.isFinishing)
73+
}
74+
75+
@Test
76+
fun checkInformationStoredInSharedPreferenceOnButtonClick() {
77+
val applicationContext = RuntimeEnvironment.application.applicationContext
78+
val sharedPreferences = PreferenceManager
79+
.getDefaultSharedPreferences(applicationContext)
80+
81+
val boxName = "freedomBox"
82+
val domain = "domain"
83+
val username = "username"
84+
val password = "password"
85+
val default = false
86+
87+
val key = "default_box"
88+
val value = "[{\"boxName\":\"" + boxName + "\"," +
89+
"\"domain\":\"" + domain + "\"," +
90+
"\"username\":\"" + username + "\"," +
91+
"\"password\":\"" + password + "\"," +
92+
"\"default\":" + default + "}]"
93+
94+
95+
val activity = Robolectric.setupActivity(SetupActivity::class.java)
96+
val shadowActvity = Shadows.shadowOf(activity)
97+
(shadowActvity.findViewById(R.id.boxName) as EditText).setText(boxName)
98+
(shadowActvity.findViewById(R.id.discoveredUrl) as EditText).setText(domain)
99+
(shadowActvity.findViewById(R.id.username) as EditText).setText(username)
100+
(shadowActvity.findViewById(R.id.password) as EditText).setText(password)
101+
(shadowActvity.findViewById(R.id.setDefault) as Switch).setChecked(default)
102+
103+
shadowActvity.findViewById(R.id.saveConfig).performClick()
104+
Assert.assertEquals(value, sharedPreferences.getString(key, null))
105+
106+
}
107+
108+
}

0 commit comments

Comments
 (0)