-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
LoginTest/app/src/main/java/piotrek/logintest/CredentialsStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package piotrek.logintest; | ||
|
||
import android.content.SharedPreferences; | ||
|
||
/** | ||
* Created by Admin on 2016-04-27. | ||
*/ | ||
public class CredentialsStorage implements ICredentialsStorage { | ||
private final SharedPreferences preferences; | ||
|
||
public CredentialsStorage(SharedPreferences preferences) { | ||
|
||
this.preferences = preferences; | ||
} | ||
|
||
@Override | ||
public void storeUsername(String username) { | ||
preferences | ||
.edit() | ||
.putString("user", username) | ||
.apply(); | ||
|
||
// apply - asynchroniczny | ||
// commit - synchroniczny | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
LoginTest/app/src/test/java/piotrek/logintest/ICredentialsStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package piotrek.logintest; | ||
|
||
import android.content.SharedPreferences; | ||
import android.preference.PreferenceManager; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricGradleTestRunner; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.shadows.ShadowPreferenceManager; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by Admin on 2016-04-27. | ||
*/ | ||
@RunWith(RobolectricGradleTestRunner.class) | ||
@Config(constants = BuildConfig.class, sdk = 21) | ||
public class ICredentialsStorageTest { | ||
|
||
@Test | ||
public void should_save_in_shared_preferences() throws Exception { | ||
// prepare | ||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application); | ||
|
||
ICredentialsStorage credentialsStorage = new CredentialsStorage(preferences); | ||
|
||
// run | ||
credentialsStorage.storeUsername("username"); | ||
// assert | ||
|
||
String current = preferences.getString("user", null); | ||
assertEquals("username", current); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters