Unit tests now compile and run at all platforms.

Cosmetic changes to mocks.h.

Review URL: http://webrtc-codereview.appspot.com/253003

git-svn-id: http://webrtc.googlecode.com/svn/trunk@871 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
kjellander@webrtc.org
2011-11-02 16:34:52 +00:00
parent 87c50f02c6
commit d292b9c9da
8 changed files with 107 additions and 72 deletions

View File

@@ -17,7 +17,9 @@ namespace test {
const std::string kInputFilename = "temp_inputfile.tmp"; const std::string kInputFilename = "temp_inputfile.tmp";
const std::string kOutputFilename = "temp_outputfile.tmp"; const std::string kOutputFilename = "temp_outputfile.tmp";
const std::string kInputFileContents = "baz"; const std::string kInputFileContents = "baz";
const int kFrameLength = 1e5; // 100 kB // Setting the kFrameLength value to a value much larger than the
// file to test causes the ReadFrame test to fail on Windows.
const int kFrameLength = 1000;
// Boilerplate code for proper unit tests for FileHandler. // Boilerplate code for proper unit tests for FileHandler.
class FileHandlerTest: public testing::Test { class FileHandlerTest: public testing::Test {

View File

@@ -24,29 +24,27 @@ namespace webrtc {
class MockVideoEncoder : public VideoEncoder { class MockVideoEncoder : public VideoEncoder {
public: public:
MOCK_CONST_METHOD2(Version, MOCK_CONST_METHOD2(Version,
WebRtc_Word32(WebRtc_Word8 *version, WebRtc_Word32 length)); WebRtc_Word32(WebRtc_Word8 *version,
WebRtc_Word32 length));
MOCK_METHOD3(InitEncode, MOCK_METHOD3(InitEncode,
WebRtc_Word32(const VideoCodec* codecSettings, WebRtc_Word32(const VideoCodec* codecSettings,
WebRtc_Word32 numberOfCores, WebRtc_Word32 numberOfCores,
WebRtc_UWord32 maxPayloadSize)); WebRtc_UWord32 maxPayloadSize));
MOCK_METHOD3(Encode, MOCK_METHOD3(Encode,
WebRtc_Word32(const RawImage& inputImage, WebRtc_Word32(const RawImage& inputImage,
const CodecSpecificInfo* codecSpecificInfo, const CodecSpecificInfo* codecSpecificInfo,
const VideoFrameType* frameType)); const VideoFrameType* frameType));
MOCK_METHOD1(RegisterEncodeCompleteCallback, MOCK_METHOD1(RegisterEncodeCompleteCallback,
WebRtc_Word32(EncodedImageCallback* callback)); WebRtc_Word32(EncodedImageCallback* callback));
MOCK_METHOD0(Release, MOCK_METHOD0(Release, WebRtc_Word32());
WebRtc_Word32()); MOCK_METHOD0(Reset, WebRtc_Word32());
MOCK_METHOD0(Reset, MOCK_METHOD1(SetPacketLoss, WebRtc_Word32(WebRtc_UWord32 packetLoss));
WebRtc_Word32());
MOCK_METHOD1(SetPacketLoss,
WebRtc_Word32(WebRtc_UWord32 packetLoss));
MOCK_METHOD2(SetRates, MOCK_METHOD2(SetRates,
WebRtc_Word32(WebRtc_UWord32 newBitRate, WebRtc_UWord32 frameRate)); WebRtc_Word32(WebRtc_UWord32 newBitRate,
MOCK_METHOD1(SetPeriodicKeyFrames, WebRtc_UWord32 frameRate));
WebRtc_Word32(bool enable)); MOCK_METHOD1(SetPeriodicKeyFrames, WebRtc_Word32(bool enable));
MOCK_METHOD2(CodecConfigParameters, MOCK_METHOD2(CodecConfigParameters,
WebRtc_Word32(WebRtc_UWord8* /*buffer*/, WebRtc_Word32)); WebRtc_Word32(WebRtc_UWord8* /*buffer*/, WebRtc_Word32));
}; };
class MockVideoDecoder : public VideoDecoder { class MockVideoDecoder : public VideoDecoder {
@@ -55,47 +53,36 @@ class MockVideoDecoder : public VideoDecoder {
WebRtc_Word32(const VideoCodec* codecSettings, WebRtc_Word32(const VideoCodec* codecSettings,
WebRtc_Word32 numberOfCores)); WebRtc_Word32 numberOfCores));
MOCK_METHOD5(Decode, MOCK_METHOD5(Decode,
WebRtc_Word32(const EncodedImage& inputImage, WebRtc_Word32(const EncodedImage& inputImage,
bool missingFrames, bool missingFrames,
const RTPFragmentationHeader* fragmentation, const RTPFragmentationHeader* fragmentation,
const CodecSpecificInfo* codecSpecificInfo, const CodecSpecificInfo* codecSpecificInfo,
WebRtc_Word64 renderTimeMs)); WebRtc_Word64 renderTimeMs));
MOCK_METHOD1(RegisterDecodeCompleteCallback, MOCK_METHOD1(RegisterDecodeCompleteCallback,
WebRtc_Word32(DecodedImageCallback* callback)); WebRtc_Word32(DecodedImageCallback* callback));
MOCK_METHOD0(Release, MOCK_METHOD0(Release, WebRtc_Word32());
WebRtc_Word32()); MOCK_METHOD0(Reset, WebRtc_Word32());
MOCK_METHOD0(Reset,
WebRtc_Word32());
MOCK_METHOD2(SetCodecConfigParameters, MOCK_METHOD2(SetCodecConfigParameters,
WebRtc_Word32(const WebRtc_UWord8* /*buffer*/, WebRtc_Word32)); WebRtc_Word32(const WebRtc_UWord8* /*buffer*/, WebRtc_Word32));
MOCK_METHOD0(Copy, MOCK_METHOD0(Copy, VideoDecoder*());
VideoDecoder*());
}; };
namespace test { namespace test {
class MockFileHandler : public FileHandler { class MockFileHandler : public FileHandler {
public: public:
MOCK_METHOD0(Init, MOCK_METHOD0(Init, bool());
bool()); MOCK_METHOD1(ReadFrame, bool(WebRtc_UWord8* source_buffer));
MOCK_METHOD1(ReadFrame, MOCK_METHOD1(WriteFrame, bool(WebRtc_UWord8* frame_buffer));
bool(WebRtc_UWord8* source_buffer)); MOCK_METHOD0(Close, void());
MOCK_METHOD1(WriteFrame, MOCK_METHOD1(GetFileSize, WebRtc_UWord64(std::string filename));
bool(WebRtc_UWord8* frame_buffer)); MOCK_METHOD0(GetFrameLength, int());
MOCK_METHOD0(Close, MOCK_METHOD0(GetNumberOfFrames, int());
void());
MOCK_METHOD1(GetFileSize,
WebRtc_UWord64(std::string filename));
MOCK_METHOD0(GetFrameLength,
int());
MOCK_METHOD0(GetNumberOfFrames,
int());
}; };
class MockPacketManipulator : public PacketManipulator { class MockPacketManipulator : public PacketManipulator {
public: public:
MOCK_METHOD1(ManipulatePackets, MOCK_METHOD1(ManipulatePackets, int(webrtc::EncodedImage* encoded_image));
int(webrtc::EncodedImage* encoded_image));
}; };
} // namespace test } // namespace test

View File

@@ -73,6 +73,10 @@ int PacketManipulatorImpl::ManipulatePackets(
return nbr_packets_dropped; return nbr_packets_dropped;
} }
inline double PacketManipulatorImpl::RandomUniform() {
return (std::rand() + 1.0)/(RAND_MAX + 1.0);
}
const char* PacketLossModeToStr(PacketLossMode e) { const char* PacketLossModeToStr(PacketLossMode e) {
switch (e) { switch (e) {
case kUniform: case kUniform:
@@ -81,6 +85,7 @@ const char* PacketLossModeToStr(PacketLossMode e) {
return "Burst"; return "Burst";
default: default:
assert(false); assert(false);
return "Unknown";
} }
} }

View File

@@ -90,12 +90,10 @@ class PacketManipulatorImpl : public PacketManipulator {
const NetworkingConfig& config); const NetworkingConfig& config);
virtual ~PacketManipulatorImpl(); virtual ~PacketManipulatorImpl();
virtual int ManipulatePackets(webrtc::EncodedImage* encoded_image); virtual int ManipulatePackets(webrtc::EncodedImage* encoded_image);
protected:
private:
// Returns a uniformly distributed random value between 0.0 and 1.0 // Returns a uniformly distributed random value between 0.0 and 1.0
inline double RandomUniform() { virtual double RandomUniform();
return (std::rand() + 1.0)/(RAND_MAX + 1.0); private:
}
PacketReader* packet_reader_; PacketReader* packet_reader_;
const NetworkingConfig& config_; const NetworkingConfig& config_;
// Used to simulate a burst over several frames. // Used to simulate a burst over several frames.

View File

@@ -7,6 +7,9 @@
* in the file PATENTS. All contributing project authors may * in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree. * be found in the AUTHORS file in the root of the source tree.
*/ */
#include <queue>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "packet_manipulator.h" #include "packet_manipulator.h"
#include "typedefs.h" #include "typedefs.h"
@@ -19,12 +22,11 @@ namespace test {
const double kNeverDropProbability = 0.0; const double kNeverDropProbability = 0.0;
const double kAlwaysDropProbability = 1.0; const double kAlwaysDropProbability = 1.0;
const int kBurstLength = 1; const int kBurstLength = 1;
class PacketManipulatorTest: public PacketRelatedTest { class PacketManipulatorTest: public PacketRelatedTest {
protected: protected:
PacketReader packet_reader_; PacketReader packet_reader_;
EncodedImage image_; EncodedImage image_;
NetworkingConfig drop_config_; NetworkingConfig drop_config_;
NetworkingConfig no_drop_config_; NetworkingConfig no_drop_config_;
@@ -70,6 +72,35 @@ class PacketManipulatorTest: public PacketRelatedTest {
} }
}; };
// Predictive packet manipulator that allows for setup of the result of
// the random invocations.
class PredictivePacketManipulatorImpl : public PacketManipulatorImpl {
public:
PredictivePacketManipulatorImpl(PacketReader* packet_reader,
const NetworkingConfig& config)
: PacketManipulatorImpl(packet_reader, config) {
}
// Adds a result. You must add at least the same number of results as the
// expected calls to the RandomUniform method. The results are added to a
// FIFO queue so they will be returned in the same order they were added.
void AddRandomResult(double result) {
ASSERT_TRUE(result >= 0.0 || result <= 1.0)
<< "Cannot add results outside the range 0.0 - 1.0, was:" << result;
random_results_.push(result);
}
protected:
double RandomUniform() {
EXPECT_GT(random_results_.size(), 0u) << "No more stored results, please "
"make sure AddRandomResult() is called same amount of times you're "
"going to invoke the RandomUniform() function, i.e. once per packet.";
double result = random_results_.front();
random_results_.pop();
return result;
}
private:
std::queue<double> random_results_;
};
TEST_F(PacketManipulatorTest, Constructor) { TEST_F(PacketManipulatorTest, Constructor) {
PacketManipulatorImpl manipulator(&packet_reader_, no_drop_config_); PacketManipulatorImpl manipulator(&packet_reader_, no_drop_config_);
} }
@@ -107,27 +138,33 @@ TEST_F(PacketManipulatorTest, UniformDropAll) {
0, packet_data_, image_); 0, packet_data_, image_);
} }
// Use our customized test class to make the second packet being lost
TEST_F(PacketManipulatorTest, UniformDropSinglePacket) { TEST_F(PacketManipulatorTest, UniformDropSinglePacket) {
drop_config_.packet_loss_probability = 0.5; drop_config_.packet_loss_probability = 0.5;
PacketManipulatorImpl manipulator(&packet_reader_, drop_config_); PredictivePacketManipulatorImpl manipulator(&packet_reader_, drop_config_);
manipulator.AddRandomResult(1.0);
manipulator.AddRandomResult(0.3); // less than 0.5 will cause packet loss
manipulator.AddRandomResult(1.0);
// Execute the test target method: // Execute the test target method:
int nbr_packets_dropped = manipulator.ManipulatePackets(&image_); int nbr_packets_dropped = manipulator.ManipulatePackets(&image_);
// The deterministic behavior (since we've set srand) will // Since we setup the predictive packet manipulator, it will throw away the
// make the packet manipulator to throw away the second packet. // second packet. The third packet is also lost because when we have lost one,
// The third packet is lost because when we have lost one, the remains shall // the remains shall also be discarded (in the current implementation).
// also be discarded.
VerifyPacketLoss(2, nbr_packets_dropped, kPacketSizeInBytes, packet1_, VerifyPacketLoss(2, nbr_packets_dropped, kPacketSizeInBytes, packet1_,
image_); image_);
} }
// Use our customized test class to make the second packet being lost
TEST_F(PacketManipulatorTest, BurstDropNinePackets) { TEST_F(PacketManipulatorTest, BurstDropNinePackets) {
// Create a longer packet data structure // Create a longer packet data structure (10 packets)
const int kDataLength = kPacketSizeInBytes * 10; const int kNbrPackets = 10;
const int kDataLength = kPacketSizeInBytes * kNbrPackets;
WebRtc_UWord8 data[kDataLength]; WebRtc_UWord8 data[kDataLength];
WebRtc_UWord8* data_pointer = data; WebRtc_UWord8* data_pointer = data;
// Fill with 0s, 1s and so on to be able to easily verify which were dropped: // Fill with 0s, 1s and so on to be able to easily verify which were dropped:
for (int i = 0; i < 10; ++i) { for (int i = 0; i < kNbrPackets; ++i) {
memset(data_pointer + i * kPacketSizeInBytes, i, kPacketSizeInBytes); memset(data_pointer + i * kPacketSizeInBytes, i, kPacketSizeInBytes);
} }
// Overwrite the defaults from the test fixture: // Overwrite the defaults from the test fixture:
@@ -135,10 +172,16 @@ TEST_F(PacketManipulatorTest, BurstDropNinePackets) {
image_._length = kDataLength; image_._length = kDataLength;
image_._size = kDataLength; image_._size = kDataLength;
drop_config_.packet_loss_probability = 0.4; drop_config_.packet_loss_probability = 0.5;
drop_config_.packet_loss_burst_length = 5; drop_config_.packet_loss_burst_length = 5;
drop_config_.packet_loss_mode = kBurst; drop_config_.packet_loss_mode = kBurst;
PacketManipulatorImpl manipulator(&packet_reader_, drop_config_); PredictivePacketManipulatorImpl manipulator(&packet_reader_, drop_config_);
manipulator.AddRandomResult(1.0);
manipulator.AddRandomResult(0.3); // less than 0.5 will cause packet loss
for (int i = 0; i < kNbrPackets - 2; ++i) {
manipulator.AddRandomResult(1.0);
}
// Execute the test target method: // Execute the test target method:
int nbr_packets_dropped = manipulator.ManipulatePackets(&image_); int nbr_packets_dropped = manipulator.ManipulatePackets(&image_);

View File

@@ -134,14 +134,12 @@ TEST_F(PacketReaderTest, NormalLargeData) {
// Test with empty data. // Test with empty data.
TEST_F(PacketReaderTest, EmptyData) { TEST_F(PacketReaderTest, EmptyData) {
const int kDataLengthInBytes = 0; const int kDataLengthInBytes = 0;
WebRtc_UWord8 data[0]; WebRtc_UWord8* data = new WebRtc_UWord8[kDataLengthInBytes];
WebRtc_UWord8* data_pointer = data;
reader_->InitializeReading(data, kDataLengthInBytes, kPacketSizeInBytes); reader_->InitializeReading(data, kDataLengthInBytes, kPacketSizeInBytes);
EXPECT_EQ(kDataLengthInBytes, reader_->NextPacket(&data_pointer)); EXPECT_EQ(kDataLengthInBytes, reader_->NextPacket(&data));
EXPECT_EQ(*data, *data_pointer);
// Do it again to make sure nothing changes // Do it again to make sure nothing changes
EXPECT_EQ(kDataLengthInBytes, reader_->NextPacket(&data_pointer)); EXPECT_EQ(kDataLengthInBytes, reader_->NextPacket(&data));
EXPECT_EQ(*data, *data_pointer); delete[] data;
} }
} // namespace test } // namespace test

View File

@@ -44,7 +44,7 @@ class PacketRelatedTest: public testing::Test {
} }
void SetUp() { void SetUp() {
// Initialize the random generator with 0 to get determenistic behaviour // Initialize the random generator with 0 to get deterministic behavior
srand(0); srand(0);
} }

View File

@@ -239,6 +239,7 @@ const char* ExcludeFrameTypesToStr(ExcludeFrameTypes e) {
return "ExcludeAllKeyFrames"; return "ExcludeAllKeyFrames";
default: default:
assert(false); assert(false);
return "Unknown";
} }
} }
@@ -262,6 +263,7 @@ const char* VideoCodecTypeToStr(webrtc::VideoCodecType e) {
return "Unknown"; return "Unknown";
default: default:
assert(false); assert(false);
return "Unknown";
} }
} }