Unit test for invalid resolution changes

BUG=aomedia:3349

Change-Id: I72d482f320774e05be8e88e1e9ff98d4f6904c5e
(cherry picked from commit 9984e702d29ec95ea0db486ddefae249fc8f23fa)
diff --git a/test/frame_size_tests.cc b/test/frame_size_tests.cc
index 32c415e..3b7cd1f 100644
--- a/test/frame_size_tests.cc
+++ b/test/frame_size_tests.cc
@@ -160,6 +160,75 @@
   EXPECT_EQ(frame_count, kNumFramesPerResolution * kFrameSizes.size());
 }
 
+TEST_P(AV1ResolutionChange, InvalidInputSize) {
+  struct FrameSize {
+    unsigned int width;
+    unsigned int height;
+  };
+  static constexpr std::array<FrameSize, 3> kFrameSizes = { {
+      { 1768, 0 },
+      { 0, 200 },
+      { 850, 200 },
+  } };
+
+  aom_codec_iface_t *iface = aom_codec_av1_cx();
+  aom_codec_enc_cfg_t cfg;
+  ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, usage_), AOM_CODEC_OK);
+
+  // Resolution changes are only permitted with one pass encoding with no lag.
+  cfg.g_pass = AOM_RC_ONE_PASS;
+  cfg.g_lag_in_frames = 0;
+  cfg.rc_end_usage = rc_mode_;
+
+  aom_codec_ctx_t ctx;
+  EXPECT_EQ(aom_codec_enc_init(&ctx, iface, &cfg, 0), AOM_CODEC_OK);
+  std::unique_ptr<aom_codec_ctx_t, decltype(&aom_codec_destroy)> enc(
+      &ctx, &aom_codec_destroy);
+  EXPECT_EQ(aom_codec_control(enc.get(), AOME_SET_CPUUSED, cpu_used_),
+            AOM_CODEC_OK);
+
+  int frame_count = 0;
+  ::libaom_test::RandomVideoSource video;
+  video.Begin();
+  constexpr int kNumFramesPerResolution = 2;
+  for (const auto &frame_size : kFrameSizes) {
+    cfg.g_w = frame_size.width;
+    cfg.g_h = frame_size.height;
+    if (cfg.g_w < 1 || cfg.g_w > 65536 || cfg.g_h < 1 || cfg.g_h > 65536) {
+      EXPECT_EQ(aom_codec_enc_config_set(enc.get(), &cfg),
+                AOM_CODEC_INVALID_PARAM);
+      continue;
+    }
+
+    EXPECT_EQ(aom_codec_enc_config_set(enc.get(), &cfg), AOM_CODEC_OK);
+    video.SetSize(cfg.g_w, cfg.g_h);
+
+    aom_codec_iter_t iter;
+    const aom_codec_cx_pkt_t *pkt;
+
+    for (int i = 0; i < kNumFramesPerResolution; ++i) {
+      video.Next();  // SetSize() does not call FillFrame().
+      EXPECT_EQ(aom_codec_encode(enc.get(), video.img(), video.pts(),
+                                 video.duration(), /*flags=*/0),
+                AOM_CODEC_OK);
+
+      iter = nullptr;
+      while ((pkt = aom_codec_get_cx_data(enc.get(), &iter)) != nullptr) {
+        ASSERT_EQ(pkt->kind, AOM_CODEC_CX_FRAME_PKT);
+        // The frame following a resolution change should be a keyframe as the
+        // change is too extreme to allow previous references to be used.
+        if (i == 0 || usage_ == AOM_USAGE_ALL_INTRA) {
+          EXPECT_NE(pkt->data.frame.flags & AOM_FRAME_IS_KEY, 0u)
+              << "frame " << frame_count;
+        }
+        frame_count++;
+      }
+    }
+  }
+
+  EXPECT_EQ(frame_count, 2);
+}
+
 INSTANTIATE_TEST_SUITE_P(
     Realtime, AV1ResolutionChange,
     ::testing::Combine(::testing::Values(AOM_USAGE_REALTIME),