Support default content types for both displayed and attached entities.

This replaces the const ContentType.DEFAULT_CONTENT_TYPE string and
related code with static defaults for both displayed and attached MIME
entities.

* src/engine/mime/mime-content-type.vala (ContentType): Replace
  DEFAULT_CONTENT_TYPE const with static DISPLAY_DEFAULT and
  ATTACHMENT_DEFAULT static instances. Remove is_default() since that no
  longer makes sense. Update call sites and unit tests.

* src/engine/mime/mime-content-parameters.vala (ContentParameters): Make
  the class immutable so it is safe to be used as a component of the
  static ContentType DISPLAY_DEFAULT and ATTACHMENT_DEFAULT members.
This commit is contained in:
Michael James Gratton 2018-05-09 17:29:30 +10:00
parent 037af00740
commit a2a95686b4
7 changed files with 81 additions and 73 deletions

View file

@ -62,7 +62,7 @@ class Geary.AttachmentTest : TestCase {
public override void set_up() {
try {
this.content_type = Mime.ContentType.deserialize(CONTENT_TYPE);
this.default_type = Mime.ContentType.deserialize(Mime.ContentType.DEFAULT_CONTENT_TYPE);
this.default_type = Mime.ContentType.ATTACHMENT_DEFAULT;
this.content_disposition = new Mime.ContentDisposition("attachment", null);
File source = File.new_for_path(_SOURCE_ROOT_DIR);

View file

@ -24,7 +24,7 @@ class Geary.ImapDB.AttachmentTest : TestCase {
Attachment test = new Attachment.from_part(1, part);
assert_string(
Geary.Mime.ContentType.DEFAULT_CONTENT_TYPE,
Geary.Mime.ContentType.ATTACHMENT_DEFAULT.to_string(),
test.content_type.to_string()
);
assert_null_string(test.content_id, "content_id");

View file

@ -9,14 +9,21 @@ class Geary.Mime.ContentTypeTest : TestCase {
public ContentTypeTest() {
base("Geary.Mime.ContentTypeTest");
add_test("is_default", is_default);
add_test("static_defaults", static_defaults);
add_test("get_file_name_extension", get_file_name_extension);
add_test("guess_type_from_name", guess_type_from_name);
add_test("guess_type_from_buf", guess_type_from_buf);
}
public void is_default() throws Error {
assert(new ContentType("application", "octet-stream", null).is_default());
public void static_defaults() throws Error {
assert_string(
"text/plain; charset=us-ascii",
ContentType.DISPLAY_DEFAULT.to_string()
);
assert_string(
"application/octet-stream",
ContentType.ATTACHMENT_DEFAULT.to_string()
);
}
public void get_file_name_extension() throws Error {
@ -25,17 +32,15 @@ class Geary.Mime.ContentTypeTest : TestCase {
}
public void guess_type_from_name() throws Error {
try {
assert(ContentType.guess_type("test.png", null).is_type("image", "png"));
} catch (Error err) {
assert_not_reached();
}
try {
assert(ContentType.guess_type("foo.test", null).get_mime_type() == ContentType.DEFAULT_CONTENT_TYPE);
} catch (Error err) {
assert_not_reached();
}
assert_true(
ContentType.guess_type("test.png", null).is_type("image", "png"),
"Expected image/png"
);
assert_true(
ContentType.guess_type("foo.test", null)
.is_same(ContentType.ATTACHMENT_DEFAULT),
"Expected ContentType.ATTACHMENT_DEFAULT"
);
}
public void guess_type_from_buf() throws Error {
@ -44,17 +49,15 @@ class Geary.Mime.ContentTypeTest : TestCase {
);
Memory.ByteBuffer empty = new Memory.ByteBuffer({0x0}, 1);
try {
assert(ContentType.guess_type(null, png).is_type("image", "png"));
} catch (Error err) {
assert_not_reached();
}
try {
assert(ContentType.guess_type(null, empty).get_mime_type() == ContentType.DEFAULT_CONTENT_TYPE);
} catch (Error err) {
assert_not_reached();
}
assert_true(
ContentType.guess_type(null, png).is_type("image", "png"),
"Expected image/png"
);
assert_true(
ContentType.guess_type(null, empty)
.is_same(ContentType.ATTACHMENT_DEFAULT),
"Expected ContentType.ATTACHMENT_DEFAULT"
);
}
}