Change QpackEncoderStreamSenderTests to use a mock delegate.

Since QpackEncoderStreamSender has been converted to use
QpackInstructionEncoder, it is guaranteed to call Delegate::Write() exactly once
per instruction sent.  Document this in the header file.  Also, this allows
tests to use a mock delegate, and have one expected call per instruction
encoded, which makes it easier to identify what fragment of the encoded data
belongs to each instruction.

This CL merges internal changes 222418576 and 225631270 by bnc.

Change-Id: Ibbb0d8c1c56711026e3e46eb2ecac503672f36ca
Reviewed-on: https://chromium-review.googlesource.com/c/1378577
Commit-Queue: Bence Béky <bnc@chromium.org>
Reviewed-by: Ryan Hamilton <rch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#616978}
diff --git a/net/third_party/quic/core/qpack/qpack_encoder_stream_sender.h b/net/third_party/quic/core/qpack/qpack_encoder_stream_sender.h
index d8675c9..351676b 100644
--- a/net/third_party/quic/core/qpack/qpack_encoder_stream_sender.h
+++ b/net/third_party/quic/core/qpack/qpack_encoder_stream_sender.h
@@ -22,7 +22,8 @@
     virtual ~Delegate() = default;
 
     // Encoded |data| is ready to be written on the encoder stream.
-    // |data| is guaranteed to be not empty.
+    // Write() is called exactly once for each instruction, |data| contains the
+    // entire encoded instruction and it is guaranteed to be not empty.
     virtual void Write(QuicStringPiece data) = 0;
   };
 
diff --git a/net/third_party/quic/core/qpack/qpack_encoder_stream_sender_test.cc b/net/third_party/quic/core/qpack/qpack_encoder_stream_sender_test.cc
index de30a4b..284a2ca 100644
--- a/net/third_party/quic/core/qpack/qpack_encoder_stream_sender_test.cc
+++ b/net/third_party/quic/core/qpack/qpack_encoder_stream_sender_test.cc
@@ -9,101 +9,99 @@
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using ::testing::Eq;
+using ::testing::StrictMock;
+
 namespace quic {
 namespace test {
 namespace {
 
-class TestSendingDelegate : public QpackEncoderStreamSender::Delegate {
+class MockSenderDelegate : public QpackEncoderStreamSender::Delegate {
  public:
-  ~TestSendingDelegate() override = default;
+  ~MockSenderDelegate() override = default;
 
-  void Write(QuicStringPiece data) override {
-    EXPECT_FALSE(data.empty());
-    buffer_.append(data.data(), data.size());
-  }
-
-  const QuicString& buffer() { return buffer_; }
-
- private:
-  QuicString buffer_;
+  MOCK_METHOD1(Write, void(QuicStringPiece));
 };
 
 class QpackEncoderStreamSenderTest : public QuicTest {
  protected:
   QpackEncoderStreamSenderTest() : stream_(&delegate_) {}
 
-  QpackEncoderStreamSender* stream() { return &stream_; }
-  const QuicString& buffer() { return delegate_.buffer(); }
-
- private:
-  TestSendingDelegate delegate_;
+  StrictMock<MockSenderDelegate> delegate_;
   QpackEncoderStreamSender stream_;
 };
 
 TEST_F(QpackEncoderStreamSenderTest, InsertWithNameReference) {
   // Static, index fits in prefix, empty value.
-  stream()->SendInsertWithNameReference(true, 5, "");
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("c500"))));
+  stream_.SendInsertWithNameReference(true, 5, "");
+
   // Static, index fits in prefix, Huffman encoded value.
-  stream()->SendInsertWithNameReference(true, 2, "foo");
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("c28294e7"))));
+  stream_.SendInsertWithNameReference(true, 2, "foo");
+
   // Not static, index does not fit in prefix, not Huffman encoded value.
-  stream()->SendInsertWithNameReference(false, 137, "bar");
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("bf4a03626172"))));
+  stream_.SendInsertWithNameReference(false, 137, "bar");
+
   // Value length does not fit in prefix.
   // 'Z' would be Huffman encoded to 8 bits, so no Huffman encoding is used.
-  stream()->SendInsertWithNameReference(false, 42, QuicString(127, 'Z'));
-
-  EXPECT_EQ(
-      QuicTextUtils::HexDecode(
-          "c500"
-          "c28294e7"
-          "bf4a03626172"
+  EXPECT_CALL(
+      delegate_,
+      Write(Eq(QuicTextUtils::HexDecode(
           "aa7f005a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"
           "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"
           "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"
-          "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"),
-      buffer());
+          "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"))));
+  stream_.SendInsertWithNameReference(false, 42, QuicString(127, 'Z'));
 }
 
 TEST_F(QpackEncoderStreamSenderTest, InsertWithoutNameReference) {
   // Empty name and value.
-  stream()->SendInsertWithoutNameReference("", "");
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("4000"))));
+  stream_.SendInsertWithoutNameReference("", "");
+
   // Huffman encoded short strings.
-  stream()->SendInsertWithoutNameReference("bar", "bar");
+  EXPECT_CALL(delegate_,
+              Write(Eq(QuicTextUtils::HexDecode("4362617203626172"))));
+  stream_.SendInsertWithoutNameReference("bar", "bar");
+
   // Not Huffman encoded short strings.
-  stream()->SendInsertWithoutNameReference("foo", "foo");
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("6294e78294e7"))));
+  stream_.SendInsertWithoutNameReference("foo", "foo");
+
   // Not Huffman encoded long strings; length does not fit on prefix.
   // 'Z' would be Huffman encoded to 8 bits, so no Huffman encoding is used.
-  stream()->SendInsertWithoutNameReference(QuicString(31, 'Z'),
-                                           QuicString(127, 'Z'));
-
-  EXPECT_EQ(
-      QuicTextUtils::HexDecode(
-          "4000"
-          "4362617203626172"
-          "6294e78294e7"
+  EXPECT_CALL(
+      delegate_,
+      Write(Eq(QuicTextUtils::HexDecode(
           "5f005a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a7f"
           "005a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"
           "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"
           "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"
-          "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"),
-      buffer());
+          "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a"))));
+  stream_.SendInsertWithoutNameReference(QuicString(31, 'Z'),
+                                         QuicString(127, 'Z'));
 }
 
 TEST_F(QpackEncoderStreamSenderTest, Duplicate) {
   // Small index fits in prefix.
-  stream()->SendDuplicate(17);
-  // Large index requires two extension bytes.
-  stream()->SendDuplicate(500);
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("11"))));
+  stream_.SendDuplicate(17);
 
-  EXPECT_EQ(QuicTextUtils::HexDecode("111fd503"), buffer());
+  // Large index requires two extension bytes.
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("1fd503"))));
+  stream_.SendDuplicate(500);
 }
 
 TEST_F(QpackEncoderStreamSenderTest, DynamicTableSizeUpdate) {
   // Small max size fits in prefix.
-  stream()->SendDynamicTableSizeUpdate(17);
-  // Large max size requires two extension bytes.
-  stream()->SendDynamicTableSizeUpdate(500);
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("31"))));
+  stream_.SendDynamicTableSizeUpdate(17);
 
-  EXPECT_EQ(QuicTextUtils::HexDecode("313fd503"), buffer());
+  // Large max size requires two extension bytes.
+  EXPECT_CALL(delegate_, Write(Eq(QuicTextUtils::HexDecode("3fd503"))));
+  stream_.SendDynamicTableSizeUpdate(500);
 }
 
 }  // namespace