MockSharedPreferencesTest.kt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.migrations
  21. import org.junit.Before
  22. import org.junit.Test
  23. import org.junit.Assert.assertEquals
  24. import org.junit.Assert.assertFalse
  25. import org.junit.Assert.assertNotSame
  26. import org.junit.Assert.assertTrue
  27. @Suppress("MagicNumber")
  28. class MockSharedPreferencesTest {
  29. private lateinit var mock: MockSharedPreferences
  30. @Before
  31. fun setUp() {
  32. mock = MockSharedPreferences()
  33. }
  34. @Test
  35. fun getSetStringSet() {
  36. val value = setOf("alpha", "bravo", "charlie")
  37. mock.edit().putStringSet("key", value).apply()
  38. val copy = mock.getStringSet("key", mutableSetOf())
  39. assertNotSame(value, copy)
  40. assertEquals(value, copy)
  41. }
  42. @Test
  43. fun getSetInt() {
  44. val value = 42
  45. val editor = mock.edit()
  46. editor.putInt("key", value)
  47. assertEquals(100, mock.getInt("key", 100))
  48. editor.apply()
  49. assertEquals(42, mock.getInt("key", 100))
  50. }
  51. @Test
  52. fun getSetBoolean() {
  53. val value = true
  54. val editor = mock.edit()
  55. editor.putBoolean("key", value)
  56. assertFalse(mock.getBoolean("key", false))
  57. editor.apply()
  58. assertTrue(mock.getBoolean("key", false))
  59. }
  60. @Test
  61. fun getSetString() {
  62. val value = "a value"
  63. val editor = mock.edit()
  64. editor.putString("key", value)
  65. assertEquals("default", mock.getString("key", "default"))
  66. editor.apply()
  67. assertEquals("a value", mock.getString("key", "default"))
  68. }
  69. @Test
  70. fun getAll() {
  71. // GIVEN
  72. // few properties are stored in shared preferences
  73. mock.edit()
  74. .putInt("int", 1)
  75. .putBoolean("bool", true)
  76. .putString("string", "value")
  77. .putStringSet("stringSet", setOf("alpha", "bravo"))
  78. .apply()
  79. assertEquals(4, mock.store.size)
  80. // WHEN
  81. // all properties are retrieved
  82. val all = mock.all
  83. // THEN
  84. // returned map is a different instance
  85. // map is equal to internal storage
  86. assertNotSame(all, mock.store)
  87. assertEquals(all, mock.store)
  88. }
  89. }