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 kOutputFilename = "temp_outputfile.tmp";
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.
class FileHandlerTest: public testing::Test {

View File

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

View File

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

View File

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

View File

@ -7,6 +7,9 @@
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <queue>
#include "gtest/gtest.h"
#include "packet_manipulator.h"
#include "typedefs.h"
@ -19,12 +22,11 @@ namespace test {
const double kNeverDropProbability = 0.0;
const double kAlwaysDropProbability = 1.0;
const int kBurstLength = 1;
class PacketManipulatorTest: public PacketRelatedTest {
protected:
PacketReader packet_reader_;
EncodedImage image_;
NetworkingConfig 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) {
PacketManipulatorImpl manipulator(&packet_reader_, no_drop_config_);
}
@ -107,27 +138,33 @@ TEST_F(PacketManipulatorTest, UniformDropAll) {
0, packet_data_, image_);
}
// Use our customized test class to make the second packet being lost
TEST_F(PacketManipulatorTest, UniformDropSinglePacket) {
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:
int nbr_packets_dropped = manipulator.ManipulatePackets(&image_);
// The deterministic behavior (since we've set srand) will
// make the packet manipulator to throw away the second packet.
// The third packet is lost because when we have lost one, the remains shall
// also be discarded.
// Since we setup the predictive packet manipulator, it will throw away the
// second packet. The third packet is also lost because when we have lost one,
// the remains shall also be discarded (in the current implementation).
VerifyPacketLoss(2, nbr_packets_dropped, kPacketSizeInBytes, packet1_,
image_);
}
// Use our customized test class to make the second packet being lost
TEST_F(PacketManipulatorTest, BurstDropNinePackets) {
// Create a longer packet data structure
const int kDataLength = kPacketSizeInBytes * 10;
// Create a longer packet data structure (10 packets)
const int kNbrPackets = 10;
const int kDataLength = kPacketSizeInBytes * kNbrPackets;
WebRtc_UWord8 data[kDataLength];
WebRtc_UWord8* data_pointer = data;
// 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);
}
// Overwrite the defaults from the test fixture:
@ -135,10 +172,16 @@ TEST_F(PacketManipulatorTest, BurstDropNinePackets) {
image_._length = 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_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:
int nbr_packets_dropped = manipulator.ManipulatePackets(&image_);

View File

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

View File

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

View File

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