Bug 1329568 - gtest for MediaMIMEType and family - r?jya draft
authorGerald Squelart <gsquelart@mozilla.com>
Fri, 30 Dec 2016 17:41:17 +1100
changeset 460526 67e62969a1c332cb6aa48e1af6fc20f501573de8
parent 460525 58b6eb38da30fac3d139f447ce93bb61c0ad24e4
child 542062 fdbbb1e1e49b97d497ac743be31ee26f051d05ae
push id41406
push usergsquelart@mozilla.com
push dateFri, 13 Jan 2017 06:23:59 +0000
reviewersjya
bugs1329568
milestone53.0a1
Bug 1329568 - gtest for MediaMIMEType and family - r?jya MozReview-Commit-ID: BhQR490Tn7U
dom/media/gtest/TestMediaMIMETypes.cpp
dom/media/gtest/TestVideoUtils.cpp
dom/media/gtest/moz.build
new file mode 100644
--- /dev/null
+++ b/dom/media/gtest/TestMediaMIMETypes.cpp
@@ -0,0 +1,237 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "gtest/gtest.h"
+#include "MediaContentType.h"
+
+using namespace mozilla;
+
+TEST(MediaMIMETypes, DependentMIMEType)
+{
+  static const struct
+  {
+    const char* mString;
+    DependentMediaMIMEType mDependentMediaMIMEType;
+  }
+  tests[] =
+  {
+    { "audio/mp4",         MEDIAMIMETYPE("audio/mp4")         },
+    { "video/mp4",         MEDIAMIMETYPE("video/mp4")         },
+    { "application/x-mp4", MEDIAMIMETYPE("application/x-mp4") }
+  };
+  for (const auto& test : tests) {
+    EXPECT_TRUE(test.mDependentMediaMIMEType.AsDependentString().EqualsASCII(test.mString));
+    MediaMIMEType mimetype(test.mDependentMediaMIMEType);
+    EXPECT_TRUE(mimetype.AsString().Equals(test.mDependentMediaMIMEType.AsDependentString()));
+    EXPECT_EQ(mimetype, test.mDependentMediaMIMEType);
+    EXPECT_EQ(mimetype, MediaMIMEType(test.mDependentMediaMIMEType));
+  }
+}
+
+TEST(MediaMIMETypes, MakeMediaMIMEType_bad)
+{
+  static const char* tests[] =
+  {
+    "",
+    " ",
+    "/",
+    "audio",
+    "audio/",
+    "mp4",
+    "/mp4",
+    "a/b"
+  };
+
+  for (const auto& test : tests) {
+    Maybe<MediaMIMEType> type = MakeMediaMIMEType(test);
+    EXPECT_TRUE(type.isNothing())
+      << "MakeMediaMIMEType(\"" << test << "\").isNothing()";
+  }
+}
+
+TEST(MediaMIMETypes, MediaMIMEType)
+{
+  static const struct
+  {
+    const char* mTypeString;
+    const char* mAsString;
+    bool mApplication;
+    bool mAudio;
+    bool mVideo;
+    bool mEqualsLiteralVideoSlashMp4; // tests `== "video/mp4"`
+  } tests[] =
+  { // in                    AsString         app    audio  video  ==v/mp4
+    { "video/mp4",           "video/mp4",     false, false, true,  true  },
+    { "video/mp4; codecs=0", "video/mp4",     false, false, true,  true  },
+    { "VIDEO/MP4",           "video/mp4",     false, false, true,  true  },
+    { "audio/mp4",           "audio/mp4",     false, true,  false, false },
+    { "application/x",       "application/x", true, false,  false, false }
+  };
+
+  for (const auto& test : tests) {
+    Maybe<MediaMIMEType> type = MakeMediaMIMEType(test.mTypeString);
+    EXPECT_TRUE(type.isSome())
+      << "MakeMediaMIMEType(\"" << test.mTypeString << "\").isSome()";
+    EXPECT_TRUE(type->AsString().EqualsASCII(test.mAsString))
+      << "MakeMediaMIMEType(\"" << test.mTypeString << "\")->AsString() == \"" << test.mAsString << "\"";
+    EXPECT_EQ(test.mApplication, type->HasApplicationMajorType())
+      << "MakeMediaMIMEType(\"" << test.mTypeString << "\")->HasApplicationMajorType() == " << (test.mApplication ? "true" : "false");
+    EXPECT_EQ(test.mAudio, type->HasAudioMajorType())
+      << "MakeMediaMIMEType(\"" << test.mTypeString << "\")->HasAudioMajorType() == " << (test.mAudio ? "true" : "false");
+    EXPECT_EQ(test.mVideo, type->HasVideoMajorType())
+      << "MakeMediaMIMEType(\"" << test.mTypeString << "\")->HasVideoMajorType() == " << (test.mVideo ? "true" : "false");
+    EXPECT_EQ(test.mEqualsLiteralVideoSlashMp4, *type == MEDIAMIMETYPE("video/mp4"))
+      << "*MakeMediaMIMEType(\"" << test.mTypeString << "\") == MEDIAMIMETYPE(\"video/mp4\")";
+  }
+}
+
+TEST(MediaMIMETypes, MediaCodecs)
+{
+  MediaCodecs empty("");
+  EXPECT_TRUE(empty.IsEmpty());
+  EXPECT_TRUE(empty.AsString().EqualsLiteral(""));
+  EXPECT_FALSE(empty.Contains(NS_LITERAL_STRING("")));
+  EXPECT_FALSE(empty.Contains(NS_LITERAL_STRING("c1")));
+  int iterations = 0;
+  for (const auto& codec : empty.Range()) {
+    ++iterations;
+    Unused << codec;
+  }
+  EXPECT_EQ(0, iterations);
+
+  MediaCodecs space(" ");
+  EXPECT_FALSE(space.IsEmpty());
+  EXPECT_TRUE(space.AsString().EqualsLiteral(" "));
+  EXPECT_TRUE(space.Contains(NS_LITERAL_STRING("")));
+  EXPECT_FALSE(space.Contains(NS_LITERAL_STRING("c1")));
+  iterations = 0;
+  for (const auto& codec : space.Range()) {
+    ++iterations;
+    EXPECT_TRUE(codec.IsEmpty());
+  }
+  EXPECT_EQ(1, iterations);
+
+  MediaCodecs one(" c1 ");
+  EXPECT_FALSE(one.IsEmpty());
+  EXPECT_TRUE(one.AsString().EqualsLiteral(" c1 "));
+  EXPECT_FALSE(one.Contains(NS_LITERAL_STRING("")));
+  EXPECT_TRUE(one.Contains(NS_LITERAL_STRING("c1")));
+  iterations = 0;
+  for (const auto& codec : one.Range()) {
+    ++iterations;
+    EXPECT_TRUE(codec.EqualsLiteral("c1"));
+  }
+  EXPECT_EQ(1, iterations);
+
+  MediaCodecs two(" c1 , c2 ");
+  EXPECT_FALSE(two.IsEmpty());
+  EXPECT_TRUE(two.AsString().EqualsLiteral(" c1 , c2 "));
+  EXPECT_FALSE(two.Contains(NS_LITERAL_STRING("")));
+  EXPECT_TRUE(two.Contains(NS_LITERAL_STRING("c1")));
+  EXPECT_TRUE(two.Contains(NS_LITERAL_STRING("c2")));
+  iterations = 0;
+  for (const auto& codec : two.Range()) {
+    ++iterations;
+    char buffer[] = "c0";
+    buffer[1] += iterations;
+    EXPECT_TRUE(codec.EqualsASCII(buffer));
+  }
+  EXPECT_EQ(2, iterations);
+
+  EXPECT_TRUE(two.ContainsAll(two));
+  EXPECT_TRUE(two.ContainsAll(one));
+  EXPECT_FALSE(one.ContainsAll(two));
+}
+
+TEST(MediaMIMETypes, MakeMediaExtendedMIMEType_bad)
+{
+  static const char* tests[] =
+  {
+    "",
+    " ",
+    "/",
+    "audio",
+    "audio/",
+    "mp4",
+    "/mp4",
+    "a/b"
+  };
+
+  for (const auto& test : tests) {
+    Maybe<MediaExtendedMIMEType> type = MakeMediaExtendedMIMEType(test);
+    EXPECT_TRUE(type.isNothing())
+      << "MakeMediaExtendedMIMEType(\"" << test << "\").isNothing()";
+  }
+}
+
+TEST(MediaMIMETypes, MediaExtendedMIMEType)
+{
+  // Some generic tests first.
+  static const struct
+  {
+    const char* mTypeString;
+    const char* mTypeAsString;
+    bool mApplication;
+    bool mAudio;
+    bool mVideo;
+    bool mEqualsLiteralVideoSlashMp4; // tests `== "video/mp4"`
+    bool mHaveCodecs;
+  } tests[] =
+  { // in                    Type().AsString  app    audio  video ==v/mp4 codecs
+    { "video/mp4",           "video/mp4",     false, false, true,  true,  false },
+    { "video/mp4; codecs=0", "video/mp4",     false, false, true,  true,  true  },
+    { "VIDEO/MP4",           "video/mp4",     false, false, true,  true,  false },
+    { "audio/mp4",           "audio/mp4",     false, true,  false, false, false },
+    { "application/x",       "application/x", true, false,  false, false, false }
+  };
+
+  for (const auto& test : tests) {
+    Maybe<MediaExtendedMIMEType> type = MakeMediaExtendedMIMEType(test.mTypeString);
+    EXPECT_TRUE(type.isSome())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\").isSome()";
+    EXPECT_TRUE(type->OriginalString().EqualsASCII(test.mTypeString))
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->AsString() == \"" << test.mTypeAsString << "\"";
+    EXPECT_TRUE(type->Type().AsString().EqualsASCII(test.mTypeAsString))
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->AsString() == \"" << test.mTypeAsString << "\"";
+    EXPECT_EQ(test.mApplication, type->Type().HasApplicationMajorType())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->Type().HasApplicationMajorType() == " << (test.mApplication ? "true" : "false");
+    EXPECT_EQ(test.mAudio, type->Type().HasAudioMajorType())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->Type().HasAudioMajorType() == " << (test.mAudio ? "true" : "false");
+    EXPECT_EQ(test.mVideo, type->Type().HasVideoMajorType())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->Type().HasVideoMajorType() == " << (test.mVideo ? "true" : "false");
+    EXPECT_EQ(test.mEqualsLiteralVideoSlashMp4, type->Type() == MEDIAMIMETYPE("video/mp4"))
+      << "*MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->Type() == MEDIAMIMETYPE(\"video/mp4\")";
+    EXPECT_EQ(test.mHaveCodecs, type->HaveCodecs())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->HaveCodecs() == " << (test.mHaveCodecs ? "true" : "false");
+    EXPECT_NE(test.mHaveCodecs, type->Codecs().IsEmpty())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->Codecs.IsEmpty() != " << (test.mHaveCodecs ? "true" : "false");
+    EXPECT_FALSE(type->GetWidth())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->GetWidth()";
+    EXPECT_FALSE(type->GetHeight())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->GetHeight()";
+    EXPECT_FALSE(type->GetFramerate())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->GetFramerate()";
+    EXPECT_FALSE(type->GetBitrate())
+      << "MakeMediaExtendedMIMEType(\"" << test.mTypeString << "\")->GetBitrate()";
+  }
+
+  // Test all extra parameters.
+  Maybe<MediaExtendedMIMEType> type =
+    MakeMediaExtendedMIMEType(
+      "video/mp4; codecs=\"a,b\"; width=1024; Height=768; FrameRate=60; BITRATE=100000");
+  EXPECT_TRUE(type->HaveCodecs());
+  EXPECT_FALSE(type->Codecs().IsEmpty());
+  EXPECT_TRUE(type->Codecs().AsString().EqualsASCII("a,b"));
+  EXPECT_TRUE(type->Codecs().Contains(NS_LITERAL_STRING("a")));
+  EXPECT_TRUE(type->Codecs().Contains(NS_LITERAL_STRING("b")));
+  EXPECT_TRUE(!!type->GetWidth());
+  EXPECT_EQ(1024, *type->GetWidth());
+  EXPECT_TRUE(!!type->GetHeight());
+  EXPECT_EQ(768, *type->GetHeight());
+  EXPECT_TRUE(!!type->GetFramerate());
+  EXPECT_EQ(60, *type->GetFramerate());
+  EXPECT_TRUE(!!type->GetBitrate());
+  EXPECT_EQ(100000, *type->GetBitrate());
+}
--- a/dom/media/gtest/TestVideoUtils.cpp
+++ b/dom/media/gtest/TestVideoUtils.cpp
@@ -4,16 +4,32 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "gtest/gtest.h"
 #include "nsString.h"
 #include "VideoUtils.h"
 
 using namespace mozilla;
 
+TEST(MediaMIMETypes, IsMediaMIMEType)
+{
+  EXPECT_TRUE(IsMediaMIMEType("audio/mp4"));
+  EXPECT_TRUE(IsMediaMIMEType("video/mp4"));
+  EXPECT_TRUE(IsMediaMIMEType("application/x-mp4"));
+
+  EXPECT_TRUE(IsMediaMIMEType("audio/m"));
+  EXPECT_FALSE(IsMediaMIMEType("audio/"));
+
+  EXPECT_FALSE(IsMediaMIMEType("vide/mp4"));
+  EXPECT_FALSE(IsMediaMIMEType("videos/mp4"));
+
+  // Expect lowercase only.
+  EXPECT_FALSE(IsMediaMIMEType("Video/mp4"));
+}
+
 TEST(StringListRange, MakeStringListRange)
 {
   static const struct
   {
     const char* mList;
     const char* mExpectedSkipEmpties;
     const char* mExpectedProcessAll;
     const char* mExpectedProcessEmpties;
--- a/dom/media/gtest/moz.build
+++ b/dom/media/gtest/moz.build
@@ -12,16 +12,17 @@ UNIFIED_SOURCES += [
     'TestAudioPacketizer.cpp',
     'TestAudioSegment.cpp',
     'TestGMPCrossOrigin.cpp',
     'TestGMPRemoveAndDelete.cpp',
     'TestGMPUtils.cpp',
     'TestIntervalSet.cpp',
     'TestMediaDataDecoder.cpp',
     'TestMediaEventSource.cpp',
+    'TestMediaMIMETypes.cpp',
     'TestMozPromise.cpp',
     'TestMP3Demuxer.cpp',
     'TestMP4Demuxer.cpp',
     # 'TestMP4Reader.cpp', disabled so we can turn check tests back on (bug 1175752)
     'TestTrackEncoder.cpp',
     'TestVideoSegment.cpp',
     'TestVideoUtils.cpp',
     'TestVPXDecoding.cpp',