geary/test/engine/mime-content-type-test.vala
Michael James Gratton a2a95686b4 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.
2018-05-10 13:53:24 +10:00

63 lines
2.1 KiB
Vala

/*
* Copyright 2016 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.
*/
class Geary.Mime.ContentTypeTest : TestCase {
public ContentTypeTest() {
base("Geary.Mime.ContentTypeTest");
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 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 {
assert(new ContentType("image", "jpeg", null).get_file_name_extension() == ".jpeg");
assert(new ContentType("test", "unknown", null).get_file_name_extension() == null);
}
public void guess_type_from_name() throws Error {
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 {
Memory.ByteBuffer png = new Memory.ByteBuffer(
{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}, 8 // PNG magic
);
Memory.ByteBuffer empty = new Memory.ByteBuffer({0x0}, 1);
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"
);
}
}