-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #887 from wordpress-mobile/feature/add-content-wat…
…cher-for-aztec-text Add content watcher for aztec text
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
aztec/src/main/kotlin/org/wordpress/aztec/AztecContentChangeWatcher.kt
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,33 @@ | ||
package org.wordpress.aztec | ||
|
||
import java.lang.ref.WeakReference | ||
|
||
class AztecContentChangeWatcher { | ||
private val observers = mutableListOf<WeakReference<AztecTextChangeObserver>>() | ||
fun registerObserver(observer: AztecTextChangeObserver) { | ||
if (observers.none { it.get() == observer }) { | ||
observers.add(WeakReference(observer)) | ||
} | ||
} | ||
|
||
fun unregisterObserver(observer: AztecTextChangeObserver) { | ||
observers.removeAll { it.get() == observer } | ||
} | ||
|
||
internal fun notifyContentChanged() { | ||
val iterator = observers.iterator() | ||
while (iterator.hasNext()) { | ||
val item = iterator.next() | ||
val foundObserver = item.get() | ||
if (foundObserver == null) { | ||
iterator.remove() | ||
} else { | ||
foundObserver.onContentChanged() | ||
} | ||
} | ||
} | ||
|
||
interface AztecTextChangeObserver { | ||
fun onContentChanged() | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
aztec/src/test/kotlin/org/wordpress/aztec/AztecContentChangeWatcherTest.kt
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,74 @@ | ||
package org.wordpress.aztec | ||
|
||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@RunWith(JUnit4::class) | ||
class AztecContentChangeWatcherTest { | ||
private lateinit var aztecContentChangeWatcher: AztecContentChangeWatcher | ||
@Before | ||
fun setUp() { | ||
aztecContentChangeWatcher = AztecContentChangeWatcher() | ||
} | ||
|
||
@Test | ||
fun `notifies registered observer`() { | ||
// Given | ||
var contentChanged = false | ||
setupRegisteredObserver { | ||
contentChanged = true | ||
} | ||
|
||
// When | ||
aztecContentChangeWatcher.notifyContentChanged() | ||
|
||
// Then | ||
assertTrue(contentChanged) | ||
} | ||
|
||
@Test | ||
fun `does not notify unregistered observer`() { | ||
// Given | ||
var contentChanged = false | ||
val observer = setupRegisteredObserver { | ||
contentChanged = true | ||
} | ||
|
||
// When | ||
aztecContentChangeWatcher.unregisterObserver(observer) | ||
aztecContentChangeWatcher.notifyContentChanged() | ||
|
||
// Then | ||
assertFalse(contentChanged) | ||
} | ||
|
||
@Test | ||
fun `observer is garbage collected and reference is lost`() { | ||
// Given | ||
var contentChanged = false | ||
setupRegisteredObserver { | ||
contentChanged = true | ||
} | ||
System.gc() | ||
|
||
// When | ||
aztecContentChangeWatcher.notifyContentChanged() | ||
|
||
// Then | ||
assertFalse(contentChanged) | ||
} | ||
|
||
private fun setupRegisteredObserver(onContentChanged: () -> Unit): AztecContentChangeWatcher.AztecTextChangeObserver { | ||
val observer = object : AztecContentChangeWatcher.AztecTextChangeObserver { | ||
override fun onContentChanged() { | ||
onContentChanged() | ||
} | ||
} | ||
aztecContentChangeWatcher.registerObserver(observer) | ||
return observer | ||
} | ||
} |