[geary/wip/714104-refine-account-dialog: 147/180] Fix not being able to re-add account previously removed from the engine.



commit 948de1f436ddc8d022d4ccb05f7be53f5e3db07a
Author: Michael James Gratton <mike vee net>
Date:   Sun Jun 17 17:56:06 2018 +1000

    Fix not being able to re-add account previously removed from the engine.

 src/engine/api/geary-engine.vala       | 13 +++++----
 test/engine/api/geary-engine-test.vala | 52 +++++++++++++++++++++++++++++++++-
 2 files changed, 59 insertions(+), 6 deletions(-)
---
diff --git a/src/engine/api/geary-engine.vala b/src/engine/api/geary-engine.vala
index 41cce4b9..3fce1cfa 100644
--- a/src/engine/api/geary-engine.vala
+++ b/src/engine/api/geary-engine.vala
@@ -418,12 +418,14 @@ public class Geary.Engine : BaseObject {
         check_opened();
 
         // Ensure account is closed.
-        if (account_instances.has_key(account.id) && account_instances.get(account.id).is_open()) {
-            throw new EngineError.CLOSE_REQUIRED("Account %s must be closed before removal",
-                account.id);
+        if (this.account_instances.has_key(account.id) &&
+            this.account_instances.get(account.id).is_open()) {
+            throw new EngineError.CLOSE_REQUIRED(
+                "Account %s must be closed before removal", account.id
+            );
         }
 
-        if (accounts.has_key(account.id)) {
+        if (this.accounts.has_key(account.id)) {
             account.untrusted_host.disconnect(on_untrusted_host);
             account.disconnect_endpoints();
 
@@ -433,7 +435,8 @@ public class Geary.Engine : BaseObject {
             account_unavailable(account);
 
             // 2. Remove the account data from the engine.
-            account_instances.unset(account.id);
+            this.accounts.unset(account.id);
+            this.account_instances.unset(account.id);
         }
     }
 
diff --git a/test/engine/api/geary-engine-test.vala b/test/engine/api/geary-engine-test.vala
index bd7f169b..ae2c9c35 100644
--- a/test/engine/api/geary-engine-test.vala
+++ b/test/engine/api/geary-engine-test.vala
@@ -15,8 +15,11 @@ class Geary.EngineTest : TestCase {
 
     public EngineTest() {
         base("Geary.EngineTest");
-        add_test("create_orphan_account", create_orphan_account);
+        add_test("add_account", add_account);
+        add_test("remove_account", remove_account);
+        add_test("re_add_account", re_add_account);
         add_test("create_orphan_account_with_legacy", create_orphan_account_with_legacy);
+        add_test("create_orphan_account", create_orphan_account);
     }
 
     ~EngineTest() {
@@ -61,6 +64,53 @@ class Geary.EngineTest : TestCase {
         }
        }
 
+    public void add_account() throws GLib.Error {
+        AccountInformation info = this.engine.create_orphan_account(
+            new MockServiceInformation(),
+            new MockServiceInformation()
+        );
+        assert_false(this.engine.has_account(info.id));
+
+        this.engine.add_account(info);
+        assert_true(this.engine.has_account(info.id), "Account not added");
+
+        try {
+            this.engine.add_account(info);
+            assert_not_reached();
+        } catch (GLib.Error err) {
+            // expected
+        }
+    }
+
+    public void remove_account() throws GLib.Error {
+        AccountInformation info = this.engine.create_orphan_account(
+            new MockServiceInformation(),
+            new MockServiceInformation()
+        );
+        this.engine.add_account(info);
+        assert_true(this.engine.has_account(info.id));
+
+        this.engine.remove_account(info);
+        assert_false(this.engine.has_account(info.id), "Account not rmoeved");
+
+        // Should not throw an error
+        this.engine.remove_account(info);
+    }
+
+    public void re_add_account() throws GLib.Error {
+        AccountInformation info = this.engine.create_orphan_account(
+            new MockServiceInformation(),
+            new MockServiceInformation()
+        );
+        assert_false(this.engine.has_account(info.id));
+
+        this.engine.add_account(info);
+        this.engine.remove_account(info);
+        this.engine.add_account(info);
+
+        assert_true(this.engine.has_account(info.id));
+    }
+
     public void create_orphan_account() throws Error {
         try {
             AccountInformation info = this.engine.create_orphan_account(


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]