test: Break out engine mock objects into their own name-space

Engine mocks don't need to be in the `Geary` namespace, and including
them there makes it difficult to use them in client tests, so put them
all in their own name-space and corresponding directory.
This commit is contained in:
Michael Gratton 2020-08-10 16:13:57 +10:00 committed by Michael James Gratton
parent 3e156525ae
commit 2030b2dec7
24 changed files with 436 additions and 414 deletions

View file

@ -20,7 +20,7 @@ class Geary.AccountInformationTest : TestCase {
new AccountInformation(
"test",
ServiceProvider.OTHER,
new MockCredentialsMediator(),
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
).save_sent
);
@ -28,7 +28,7 @@ class Geary.AccountInformationTest : TestCase {
new AccountInformation(
"test",
ServiceProvider.GMAIL,
new MockCredentialsMediator(),
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
).save_sent
);
@ -36,7 +36,7 @@ class Geary.AccountInformationTest : TestCase {
new AccountInformation(
"test",
ServiceProvider.OUTLOOK,
new MockCredentialsMediator(),
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
).save_sent
);
@ -44,7 +44,7 @@ class Geary.AccountInformationTest : TestCase {
new AccountInformation(
"test",
ServiceProvider.YAHOO,
new MockCredentialsMediator(),
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
).save_sent
);
@ -54,7 +54,7 @@ class Geary.AccountInformationTest : TestCase {
AccountInformation test = new AccountInformation(
"test",
ServiceProvider.OTHER,
new MockCredentialsMediator(),
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
);
@ -124,7 +124,7 @@ class Geary.AccountInformationTest : TestCase {
return new AccountInformation(
"test",
provider,
new MockCredentialsMediator(),
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
);
}

View file

@ -1,294 +0,0 @@
/*
* Copyright 2017 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class Geary.MockAccount : Account,
ValaUnit.TestAssertions,
ValaUnit.MockObject {
public class MockSearchQuery : SearchQuery {
internal MockSearchQuery(Account owner,
string raw) {
base(owner, raw, SearchQuery.Strategy.EXACT);
}
}
public class MockClientService : ClientService {
public MockClientService(AccountInformation account,
ServiceInformation configuration,
Endpoint remote) {
base(account, configuration, remote);
}
public override async void start(GLib.Cancellable? cancellable = null)
throws GLib.Error {
throw new EngineError.UNSUPPORTED("Mock method");
}
public override async void stop(GLib.Cancellable? cancellable = null)
throws GLib.Error {
throw new EngineError.UNSUPPORTED("Mock method");
}
public override void became_reachable() {
}
public override void became_unreachable() {
}
}
protected Gee.Queue<ValaUnit.ExpectedCall> expected {
get; set; default = new Gee.LinkedList<ValaUnit.ExpectedCall>();
}
public MockAccount(AccountInformation config) {
base(config,
new MockClientService(
config,
config.incoming,
new Endpoint(
new GLib.NetworkAddress(
config.incoming.host, config.incoming.port
),
0, 0
)
),
new MockClientService(
config,
config.outgoing,
new Endpoint(
new GLib.NetworkAddress(
config.outgoing.host, config.outgoing.port
),
0, 0
)
)
);
}
public override async void open_async(Cancellable? cancellable = null) throws Error {
void_call("open_async", { cancellable });
}
public override async void close_async(Cancellable? cancellable = null) throws Error {
void_call("close_async", { cancellable });
}
public override bool is_open() {
try {
return boolean_call("is_open", {}, false);
} catch (Error err) {
return false;
}
}
public override async void rebuild_async(Cancellable? cancellable = null) throws Error {
void_call("rebuild_async", { cancellable });
}
public override Gee.Collection<Folder> list_matching_folders(FolderPath? parent) {
try {
return object_call<Gee.Collection<Folder>>(
"get_containing_folders_async", {parent}, Gee.List.empty<Folder>()
);
} catch (GLib.Error err) {
return Gee.Collection.empty<Folder>();
}
}
public override async Folder create_personal_folder(
string name,
Folder.SpecialUse use = NONE,
GLib.Cancellable? cancellable = null
) throws GLib.Error {
return object_call<Folder>(
"create_personal_folder",
{ box_arg(name), box_arg(use), cancellable },
new MockFolder(null, null, null, use, null)
);
}
public override EmailIdentifier to_email_identifier(GLib.Variant serialised)
throws EngineError.BAD_PARAMETERS {
try {
return object_or_throw_call<EmailIdentifier>(
"to_email_identifier",
{ box_arg(serialised) },
new EngineError.BAD_PARAMETERS("Mock error")
);
} catch (EngineError.BAD_PARAMETERS err) {
throw err;
} catch (GLib.Error err) {
return new MockEmailIdentifer(0);
}
}
public override FolderPath to_folder_path(GLib.Variant serialised)
throws EngineError.BAD_PARAMETERS {
try {
return object_or_throw_call<FolderPath>(
"to_folder_path",
{ box_arg(serialised) },
new EngineError.BAD_PARAMETERS("Mock error")
);
} catch (EngineError.BAD_PARAMETERS err) {
throw err;
} catch (GLib.Error err) {
return new FolderRoot("#mock", false);
}
}
public override Folder get_folder(FolderPath path)
throws EngineError.NOT_FOUND {
try {
return object_or_throw_call<Folder>(
"get_folder",
{ path },
new EngineError.NOT_FOUND("Mock error")
);
} catch (EngineError.NOT_FOUND err) {
throw err;
} catch (GLib.Error err) {
return new MockFolder(null, null, null, NONE, null);
}
}
public override Gee.Collection<Folder> list_folders() {
try {
return object_call<Gee.Collection<Folder>>(
"list_folders", {}, Gee.List.empty<Folder>()
);
} catch (GLib.Error err) {
return Gee.List.empty<Folder>();
}
}
public override Folder? get_special_folder(Folder.SpecialUse special) {
try {
return object_call<Folder?>(
"get_special_folder", {box_arg(special)}, null
);
} catch (GLib.Error err) {
return null;
}
}
public override async Folder get_required_special_folder_async(Folder.SpecialUse special,
Cancellable? cancellable = null)
throws Error {
return object_or_throw_call<Folder>(
"get_required_special_folder_async",
{box_arg(special), cancellable},
new EngineError.NOT_FOUND("Mock call")
);
}
public override async Gee.MultiMap<Email,FolderPath?>?
local_search_message_id_async(RFC822.MessageID message_id,
Email.Field requested_fields,
bool partial_ok,
Gee.Collection<FolderPath?>? folder_blacklist,
EmailFlags? flag_blacklist,
Cancellable? cancellable = null)
throws Error {
return object_call<Gee.MultiMap<Email,FolderPath?>?>(
"local_search_message_id_async",
{
message_id,
box_arg(requested_fields),
box_arg(partial_ok),
folder_blacklist,
flag_blacklist,
cancellable
},
null
);
}
public override async Gee.List<Email> list_local_email_async(
Gee.Collection<EmailIdentifier> ids,
Email.Field required_fields,
GLib.Cancellable? cancellable = null
) throws GLib.Error {
return object_or_throw_call<Gee.List<Email>>(
"list_local_email_async",
{ids, box_arg(required_fields), cancellable},
new EngineError.NOT_FOUND("Mock call")
);
}
public override async Email local_fetch_email_async(EmailIdentifier email_id,
Email.Field required_fields,
Cancellable? cancellable = null)
throws Error {
return object_or_throw_call<Email>(
"local_fetch_email_async",
{email_id, box_arg(required_fields), cancellable},
new EngineError.NOT_FOUND("Mock call")
);
}
public override async SearchQuery new_search_query(string raw,
SearchQuery.Strategy strategy,
GLib.Cancellable? cancellable)
throws GLib.Error {
return new MockSearchQuery(this, raw);
}
public override async Gee.Collection<EmailIdentifier>?
local_search_async(SearchQuery query,
int limit = 100,
int offset = 0,
Gee.Collection<FolderPath?>? folder_blacklist = null,
Gee.Collection<EmailIdentifier>? search_ids = null,
Cancellable? cancellable = null)
throws Error {
return object_call<Gee.Collection<EmailIdentifier>?>(
"local_search_async",
{
query,
box_arg(limit),
box_arg(offset),
folder_blacklist,
search_ids,
cancellable
},
null
);
}
public override async Gee.Set<string>?
get_search_matches_async(SearchQuery query,
Gee.Collection<EmailIdentifier> ids,
Cancellable? cancellable = null)
throws Error {
return object_call<Gee.Set<string>?>(
"get_search_matches_async", {query, ids, cancellable}, null
);
}
public override async Gee.MultiMap<EmailIdentifier, FolderPath>?
get_containing_folders_async(Gee.Collection<EmailIdentifier> ids,
Cancellable? cancellable) throws Error {
return object_call<Gee.MultiMap<EmailIdentifier, FolderPath>?>(
"get_containing_folders_async", {ids, cancellable}, null
);
}
public override async void cleanup_storage(GLib.Cancellable? cancellable) {
}
}

View file

@ -1,46 +0,0 @@
/*
* Copyright 2019 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
internal class Geary.ContactStoreMock : GLib.Object,
ContactStore, ValaUnit.TestAssertions, ValaUnit.MockObject {
protected Gee.Queue<ValaUnit.ExpectedCall> expected {
get; set; default = new Gee.LinkedList<ValaUnit.ExpectedCall>();
}
public async Contact? get_by_rfc822(Geary.RFC822.MailboxAddress address,
GLib.Cancellable? cancellable)
throws GLib.Error {
return object_call<Contact?>(
"get_by_rfc822", { address, cancellable }, null
);
}
public async Gee.Collection<Contact> search(string query,
uint min_importance,
uint limit,
GLib.Cancellable? cancellable)
throws GLib.Error {
return object_call<Gee.Collection<Contact>>(
"search",
{
box_arg(query),
uint_arg(min_importance),
uint_arg(limit),
cancellable
},
Gee.Collection.empty<Contact>()
);
}
public async void update_contacts(Gee.Collection<Contact> updated,
GLib.Cancellable? cancellable)
throws GLib.Error {
void_call("update_contacts", { updated, cancellable });
}
}

View file

@ -1,45 +0,0 @@
/*
* Copyright 2017 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class Geary.MockCredentialsMediator :
GLib.Object,
CredentialsMediator,
ValaUnit.TestAssertions,
ValaUnit.MockObject {
protected Gee.Queue<ValaUnit.ExpectedCall> expected {
get; set; default = new Gee.LinkedList<ValaUnit.ExpectedCall>();
}
public virtual async bool load_token(AccountInformation account,
ServiceInformation service,
GLib.Cancellable? cancellable)
throws GLib.Error {
return object_call<bool>("load_token", { service, cancellable }, false);
}
/**
* Prompt the user to enter passwords for the given services.
*
* Set the out parameters for the services to the values entered
* by the user (out parameters for services not being prompted for
* are ignored). Return false if the user tried to cancel the
* interaction, or true if they tried to proceed.
*/
public virtual async bool prompt_token(AccountInformation account,
ServiceInformation service,
GLib.Cancellable? cancellable)
throws GLib.Error {
return boolean_call(
"prompt_token",
{ account, service, cancellable },
false
);
}
}

View file

@ -1,46 +0,0 @@
/*
* Copyright 2017 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class Geary.MockEmailIdentifer : EmailIdentifier {
private int id;
public MockEmailIdentifer(int id) {
this.id = id;
}
public override uint hash() {
return GLib.int_hash(this.id);
}
public override bool equal_to(Geary.EmailIdentifier other) {
return (
this.get_type() == other.get_type() &&
this.id == ((MockEmailIdentifer) other).id
);
}
public override string to_string() {
return "%s(%d)".printf(
this.get_type().name(),
this.id
);
}
public override GLib.Variant to_variant() {
return new GLib.Variant.int32(id);
}
public override int natural_sort_comparator(Geary.EmailIdentifier other) {
MockEmailIdentifer? other_mock = other as MockEmailIdentifer;
return (other_mock == null) ? 1 : this.id - other_mock.id;
}
}

View file

@ -1,21 +0,0 @@
/*
* Copyright 2017 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class Geary.MockEmailProperties : EmailProperties {
public MockEmailProperties(GLib.DateTime received) {
base(received, 0);
}
public override string to_string() {
return "MockEmailProperties: %s/%lli".printf(
this.date_received.to_string(), this.total_bytes
);
}
}

View file

@ -21,7 +21,7 @@ class Geary.EmailTest: TestCase {
public void email_from_basic_message() throws GLib.Error {
var message = resource_to_message(BASIC_TEXT_PLAIN);
var email = new Email.from_message(new MockEmailIdentifer(0), message);
var email = new Email.from_message(new Mock.EmailIdentifer(0), message);
assert_non_null(email);
assert_non_null(email.subject);
@ -30,7 +30,7 @@ class Geary.EmailTest: TestCase {
public void email_from_multipart() throws GLib.Error {
var message = resource_to_message(BASIC_MULTIPART_ALTERNATIVE);
var email = new Email.from_message(new MockEmailIdentifer(0), message);
var email = new Email.from_message(new Mock.EmailIdentifer(0), message);
assert_non_null(email);
assert_non_null(email.subject);

View file

@ -49,7 +49,7 @@ class Geary.EngineTest : TestCase {
this.account = new AccountInformation(
"test",
ServiceProvider.OTHER,
new MockCredentialsMediator(),
new Mock.CredentialsMediator(),
new RFC822.MailboxAddress(null, "test1@example.com")
);
this.account.set_account_directories(this.tmp, this.tmp);

View file

@ -1,129 +0,0 @@
/*
* Copyright 2017 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class Geary.MockFolder : Folder,
ValaUnit.TestAssertions,
ValaUnit.MockObject {
public override Account account {
get { return this._account; }
}
public override FolderProperties properties {
get { return this._properties; }
}
public override FolderPath path {
get { return this._path; }
}
public override Folder.SpecialUse used_as {
get { return this._used_as; }
}
public override ProgressMonitor opening_monitor {
get { return this._opening_monitor; }
}
protected Gee.Queue<ValaUnit.ExpectedCall> expected {
get; set; default = new Gee.LinkedList<ValaUnit.ExpectedCall>();
}
private Account _account;
private FolderProperties _properties;
private FolderPath _path;
private Folder.SpecialUse _used_as;
private ProgressMonitor _opening_monitor;
public MockFolder(Account? account,
FolderProperties? properties,
FolderPath? path,
Folder.SpecialUse used_as,
ProgressMonitor? monitor) {
this._account = account;
this._properties = properties ?? new MockFolderPoperties();
this._path = path;
this._used_as = used_as;
this._opening_monitor = monitor;
}
public override Folder.OpenState get_open_state() {
return OpenState.CLOSED;
}
public override async bool open_async(Folder.OpenFlags open_flags,
Cancellable? cancellable = null)
throws GLib.Error {
return yield boolean_call_async(
"open_async",
{ int_arg(open_flags), cancellable },
false
);
}
public override async bool close_async(Cancellable? cancellable = null)
throws GLib.Error {
return yield boolean_call_async(
"close_async", { cancellable }, false
);
}
public override async void wait_for_close_async(Cancellable? cancellable = null)
throws GLib.Error {
throw new EngineError.UNSUPPORTED("Mock method");
}
public override async void synchronise_remote(GLib.Cancellable? cancellable)
throws GLib.Error {
void_call("synchronise_remote", { cancellable });
}
public override async Gee.List<Geary.Email>?
list_email_by_id_async(Geary.EmailIdentifier? initial_id,
int count,
Geary.Email.Field required_fields,
Folder.ListFlags flags,
Cancellable? cancellable = null)
throws GLib.Error {
return yield object_call_async<Gee.List<Email>?>(
"list_email_by_id_async",
{initial_id, int_arg(count), box_arg(required_fields), box_arg(flags), cancellable},
null
);
}
public override async Gee.List<Geary.Email>?
list_email_by_sparse_id_async(Gee.Collection<Geary.EmailIdentifier> ids,
Geary.Email.Field required_fields,
Folder.ListFlags flags,
Cancellable? cancellable = null)
throws GLib.Error {
return yield object_call_async<Gee.List<Email>?>(
"list_email_by_sparse_id_async",
{ids, box_arg(required_fields), box_arg(flags), cancellable},
null
);
}
public override async Geary.Email
fetch_email_async(Geary.EmailIdentifier email_id,
Geary.Email.Field required_fields,
Folder.ListFlags flags,
Cancellable? cancellable = null)
throws GLib.Error {
throw new EngineError.UNSUPPORTED("Mock method");
}
public override void set_used_as_custom(bool enabled)
throws EngineError.UNSUPPORTED {
throw new EngineError.UNSUPPORTED("Mock method");
}
}

View file

@ -1,24 +0,0 @@
/*
* Copyright 2019 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
public class Geary.MockFolderPoperties : FolderProperties {
public MockFolderPoperties() {
base(
0,
0,
Trillian.UNKNOWN,
Trillian.UNKNOWN,
Trillian.UNKNOWN,
false,
false,
false
);
}
}