1/*
2 * Copyright 2018 Michael Gratton <mike@vee.net>
3 *
4 * This software is licensed under the GNU Lesser General Public License
5 * (version 2.1 or later). See the COPYING file in this distribution.
6 */
7
8class Geary.AccountInformationTest : TestCase {
9
10
11    public AccountInformationTest() {
12        base("Geary.AccountInformationTest");
13        add_test("test_save_sent_defaults", test_save_sent_defaults);
14        add_test("test_sender_mailboxes", test_sender_mailboxes);
15        add_test("test_service_label", test_service_label);
16        add_test("folder_steps_accessors", folder_steps_accessors);
17    }
18
19    public void test_save_sent_defaults() throws GLib.Error {
20        assert_true(
21            new AccountInformation(
22                "test",
23                ServiceProvider.OTHER,
24                new Mock.CredentialsMediator(),
25                new RFC822.MailboxAddress(null, "test1@example.com")
26            ).save_sent
27        );
28        assert_false(
29            new AccountInformation(
30                "test",
31                ServiceProvider.GMAIL,
32                new Mock.CredentialsMediator(),
33                new RFC822.MailboxAddress(null, "test1@example.com")
34            ).save_sent
35        );
36        assert_false(
37            new AccountInformation(
38                "test",
39                ServiceProvider.OUTLOOK,
40                new Mock.CredentialsMediator(),
41                new RFC822.MailboxAddress(null, "test1@example.com")
42            ).save_sent
43        );
44        assert_true(
45            new AccountInformation(
46                "test",
47                ServiceProvider.YAHOO,
48                new Mock.CredentialsMediator(),
49                new RFC822.MailboxAddress(null, "test1@example.com")
50            ).save_sent
51        );
52    }
53
54    public void test_sender_mailboxes() throws GLib.Error {
55        AccountInformation test = new AccountInformation(
56            "test",
57            ServiceProvider.OTHER,
58            new Mock.CredentialsMediator(),
59            new RFC822.MailboxAddress(null, "test1@example.com")
60        );
61
62        assert_true(test.primary_mailbox.equal_to(
63                        new RFC822.MailboxAddress(null, "test1@example.com")));
64        assert_false(test.has_sender_aliases);
65
66        test.append_sender(new RFC822.MailboxAddress(null, "test2@example.com"));
67        assert_true(test.has_sender_aliases);
68
69        test.append_sender(new RFC822.MailboxAddress(null, "test3@example.com"));
70        assert_true(test.has_sender_aliases);
71
72        assert_true(
73            test.has_sender_mailbox(new RFC822.MailboxAddress(null, "test1@example.com")),
74            "Primary address not found"
75        );
76        assert_true(
77            test.has_sender_mailbox(new RFC822.MailboxAddress(null, "test2@example.com")),
78            "First alt address not found"
79        );
80        assert_true(
81            test.has_sender_mailbox(new RFC822.MailboxAddress(null, "test3@example.com")),
82            "Second alt address not found"
83        );
84        assert_false(
85            test.has_sender_mailbox(new RFC822.MailboxAddress(null, "unknowne@example.com")),
86            "Unknown address found"
87        );
88    }
89
90    public void test_service_label() throws GLib.Error {
91        AccountInformation test = new_information();
92        assert_equal(test.service_label, "");
93
94        test = new_information();
95        test.incoming.host = "example.com";
96        assert_equal(test.service_label, "example.com");
97
98        test = new_information();
99        test.incoming.host = "test.example.com";
100        assert_equal(test.service_label, "example.com");
101
102        test = new_information();
103        test.incoming.host = "other.com";
104        test.outgoing.host = "other.com";
105        assert_equal(test.service_label, "other.com");
106
107        test = new_information();
108        test.incoming.host = "mail.other.com";
109        test.outgoing.host = "mail.other.com";
110        assert_equal(test.service_label, "other.com");
111
112        test = new_information();
113        test.incoming.host = "imap.other.com";
114        test.outgoing.host = "smtp.other.com";
115        assert_equal(test.service_label, "other.com");
116
117        test = new_information();
118        test.incoming.host = "not-mail.other.com";
119        test.outgoing.host = "not-mail.other.com";
120        assert_equal(test.service_label, "other.com");
121    }
122
123    public void folder_steps_accessors() throws GLib.Error {
124        AccountInformation test = new_information();
125
126        assert_collection(test.get_folder_steps_for_use(NONE)).is_empty();
127        assert_collection(test.get_folder_steps_for_use(ARCHIVE)).is_empty();
128        assert_collection(test.get_folder_steps_for_use(JUNK)).is_empty();
129
130        var archive = new Gee.ArrayList<string>.wrap({"Archive"});
131        test.set_folder_steps_for_use(ARCHIVE, archive);
132        assert_collection(test.get_folder_steps_for_use(ARCHIVE))
133        .is_non_empty()
134        .contains("Archive");
135
136        var junk = new Gee.ArrayList<string>.wrap({"Junk"});
137        test.set_folder_steps_for_use(JUNK, junk);
138        assert_collection(test.get_folder_steps_for_use(ARCHIVE))
139        .is_non_empty()
140        .contains("Archive");
141        assert_collection(test.get_folder_steps_for_use(JUNK))
142        .is_non_empty()
143        .contains("Junk");
144    }
145
146    private AccountInformation new_information(ServiceProvider provider =
147                                               ServiceProvider.OTHER) {
148        return new AccountInformation(
149            "test",
150            provider,
151            new Mock.CredentialsMediator(),
152            new RFC822.MailboxAddress(null, "test1@example.com")
153        );
154    }
155
156}
157