mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Don't broadcast an event unless settings have changed.
This commit is contained in:
@@ -8,7 +8,6 @@ import org.briarproject.R;
|
||||
import org.briarproject.android.BriarActivity;
|
||||
import org.briarproject.api.android.ReferenceManager;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
@@ -54,7 +53,6 @@ implements InvitationListener {
|
||||
private String contactName = null;
|
||||
|
||||
// Fields that are accessed from background threads must be volatile
|
||||
@Inject private volatile DatabaseComponent db;
|
||||
@Inject private volatile IdentityManager identityManager;
|
||||
|
||||
@Override
|
||||
|
||||
@@ -930,11 +930,19 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
|
||||
public void mergeSettings(Settings s, String namespace) throws DbException {
|
||||
boolean changed = false;
|
||||
lock.writeLock().lock();
|
||||
try {
|
||||
T txn = db.startTransaction();
|
||||
try {
|
||||
db.mergeSettings(txn, s, namespace);
|
||||
Settings old = db.getSettings(txn, namespace);
|
||||
Settings merged = new Settings();
|
||||
merged.putAll(old);
|
||||
merged.putAll(s);
|
||||
if (!merged.equals(old)) {
|
||||
db.mergeSettings(txn, s, namespace);
|
||||
changed = true;
|
||||
}
|
||||
db.commitTransaction(txn);
|
||||
} catch (DbException e) {
|
||||
db.abortTransaction(txn);
|
||||
@@ -943,7 +951,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
} finally {
|
||||
lock.writeLock().unlock();
|
||||
}
|
||||
eventBus.broadcast(new SettingsUpdatedEvent(namespace));
|
||||
if (changed) eventBus.broadcast(new SettingsUpdatedEvent(namespace));
|
||||
}
|
||||
|
||||
public void receiveAck(ContactId c, Ack a) throws DbException {
|
||||
|
||||
@@ -24,12 +24,14 @@ import org.briarproject.api.event.MessageToRequestEvent;
|
||||
import org.briarproject.api.event.MessageValidatedEvent;
|
||||
import org.briarproject.api.event.MessagesAckedEvent;
|
||||
import org.briarproject.api.event.MessagesSentEvent;
|
||||
import org.briarproject.api.event.SettingsUpdatedEvent;
|
||||
import org.briarproject.api.event.SubscriptionAddedEvent;
|
||||
import org.briarproject.api.event.SubscriptionRemovedEvent;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.lifecycle.ShutdownManager;
|
||||
import org.briarproject.api.settings.Settings;
|
||||
import org.briarproject.api.sync.Ack;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
import org.briarproject.api.sync.Group;
|
||||
@@ -1319,4 +1321,47 @@ public class DatabaseComponentImplTest extends BriarTestCase {
|
||||
2, 456);
|
||||
return new TransportKeys(transportId, inPrev, inCurr, inNext, outCurr);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeSettings() throws Exception {
|
||||
final Settings before = new Settings();
|
||||
before.put("foo", "bar");
|
||||
before.put("baz", "bam");
|
||||
final Settings update = new Settings();
|
||||
update.put("baz", "qux");
|
||||
final Settings merged = new Settings();
|
||||
merged.put("foo", "bar");
|
||||
merged.put("baz", "qux");
|
||||
Mockery context = new Mockery();
|
||||
@SuppressWarnings("unchecked")
|
||||
final Database<Object> database = context.mock(Database.class);
|
||||
final ShutdownManager shutdown = context.mock(ShutdownManager.class);
|
||||
final EventBus eventBus = context.mock(EventBus.class);
|
||||
context.checking(new Expectations() {{
|
||||
// mergeSettings()
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).getSettings(txn, "namespace");
|
||||
will(returnValue(before));
|
||||
oneOf(database).mergeSettings(txn, update, "namespace");
|
||||
oneOf(database).commitTransaction(txn);
|
||||
oneOf(eventBus).broadcast(with(any(SettingsUpdatedEvent.class)));
|
||||
// mergeSettings() again
|
||||
oneOf(database).startTransaction();
|
||||
will(returnValue(txn));
|
||||
oneOf(database).getSettings(txn, "namespace");
|
||||
will(returnValue(merged));
|
||||
oneOf(database).commitTransaction(txn);
|
||||
}});
|
||||
|
||||
DatabaseComponent db = createDatabaseComponent(database, eventBus,
|
||||
shutdown);
|
||||
|
||||
// First merge should broadcast an event
|
||||
db.mergeSettings(update, "namespace");
|
||||
// Second merge should not broadcast an event
|
||||
db.mergeSettings(update, "namespace");
|
||||
|
||||
context.assertIsSatisfied();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user