Project import generated by Copybara.

GitOrigin-RevId: 33adfdf31f3a5cbf9edc07ee1ea583e95080bdc5
This commit is contained in:
MediaPipe Team
2021-06-24 17:55:26 -04:00
committed by chuoling
parent b544a314b3
commit 139237092f
151 changed files with 4023 additions and 1001 deletions
+27 -2
View File
@@ -176,6 +176,7 @@ cc_library(
deps = [
":resource_util_custom",
"@com_google_absl//absl/container:flat_hash_map",
"//mediapipe/framework/deps:file_path",
"//mediapipe/framework/port:ret_check",
"//mediapipe/framework/port:singleton",
"//mediapipe/framework/port:status",
@@ -184,7 +185,6 @@ cc_library(
"@com_google_absl//absl/strings",
] + select({
"//conditions:default": [
"//mediapipe/framework/deps:file_path",
"@com_google_absl//absl/flags:flag",
],
"//mediapipe:android": [
@@ -193,7 +193,6 @@ cc_library(
],
"//mediapipe:ios": [],
"//mediapipe:macos": [
"//mediapipe/framework/deps:file_path",
"@com_google_absl//absl/flags:flag",
],
}),
@@ -295,3 +294,29 @@ cc_test(
"@eigen_archive//:eigen3",
],
)
cc_library(
name = "packet_test_util",
testonly = 1,
hdrs = ["packet_test_util.h"],
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework:demangle",
"//mediapipe/framework:packet",
"//mediapipe/framework:timestamp",
"//mediapipe/framework/port:gtest_main",
],
)
cc_test(
name = "packet_test_util_test",
size = "small",
srcs = ["packet_test_util_test.cc"],
deps = [
":packet_test_util",
"//mediapipe/framework:packet",
"//mediapipe/framework:port",
"//mediapipe/framework:timestamp",
"//mediapipe/framework/port:gtest_main",
],
)
+123
View File
@@ -0,0 +1,123 @@
// Copyright 2021 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Utilities that help to make assertions about packet contents in tests.
#ifndef MEDIAPIPE_UTIL_PACKET_TEST_UTIL_H_
#define MEDIAPIPE_UTIL_PACKET_TEST_UTIL_H_
#include <ostream>
#include <string>
#include <typeinfo>
#include "mediapipe/framework/demangle.h"
#include "mediapipe/framework/packet.h"
#include "mediapipe/framework/port/gmock.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/timestamp.h"
namespace mediapipe {
namespace internal {
template <typename PayloadType>
class PacketMatcher : public ::testing::MatcherInterface<const Packet&> {
public:
template <typename InnerMatcher>
explicit PacketMatcher(InnerMatcher inner_matcher)
: inner_matcher_(
::testing::SafeMatcherCast<const PayloadType&>(inner_matcher)) {}
// Returns true iff the packet contains value of PayloadType satisfying
// the inner matcher.
bool MatchAndExplain(
const Packet& packet,
::testing::MatchResultListener* listener) const override {
if (!packet.ValidateAsType<PayloadType>().ok()) {
*listener << packet.DebugString() << " does not contain expected type "
<< ExpectedTypeName();
return false;
}
::testing::StringMatchResultListener match_listener;
const PayloadType& payload = packet.Get<PayloadType>();
const bool matches =
inner_matcher_.MatchAndExplain(payload, &match_listener);
const std::string explanation = match_listener.str();
*listener << packet.DebugString() << " containing value "
<< ::testing::PrintToString(payload);
if (!explanation.empty()) {
*listener << ", which " << explanation;
}
return matches;
}
void DescribeTo(std::ostream* os) const override {
*os << "packet contains value of type " << ExpectedTypeName() << " that ";
inner_matcher_.DescribeTo(os);
}
void DescribeNegationTo(std::ostream* os) const override {
*os << "packet does not contain value of type " << ExpectedTypeName()
<< " that ";
inner_matcher_.DescribeNegationTo(os);
}
private:
static std::string ExpectedTypeName() {
return ::mediapipe::Demangle(typeid(PayloadType).name());
}
const ::testing::Matcher<const PayloadType&> inner_matcher_;
};
} // namespace internal
// Creates matcher validating that the packet contains value of expected type
// and satisfying the provided inner matcher.
//
// PayloadType template parameter has to be specified explicitly, but matcher
// type can be inferred. Example:
//
// EXPECT_THAT(MakePacket<int>(42), PacketContains<int>(Eq(42)))
template <typename PayloadType, typename InnerMatcher>
inline ::testing::Matcher<const Packet&> PacketContains(
InnerMatcher inner_matcher) {
return ::testing::MakeMatcher(
new internal::PacketMatcher<PayloadType>(inner_matcher));
}
// Creates matcher validating the packet's timestamp satisfies the provided
// timestamp_matcher. It also checks that the packet contains value of expected
// type and satisfies the provided content matcher.
//
// PayloadType template parameter has to be specified explicitly, but matcher
// type can be inferred. Example:
//
// EXPECT_THAT(MakePacket<int>(42).At(Timestamp(20)),
// PacketContainsTimestampAndPayload<int>( //
// Eq(Timestamp(20)),
// Eq(42)))
template <typename PayloadType, typename TimestampMatcher,
typename ContentMatcher>
inline ::testing::Matcher<const Packet&> PacketContainsTimestampAndPayload(
TimestampMatcher timestamp_matcher, ContentMatcher content_matcher) {
return testing::AllOf(
testing::Property("Packet::Timestamp", &Packet::Timestamp,
timestamp_matcher),
PacketContains<PayloadType>(content_matcher));
}
} // namespace mediapipe
#endif // MEDIAPIPE_UTIL_PACKET_TEST_UTIL_H_
+108
View File
@@ -0,0 +1,108 @@
// Copyright 2021 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "mediapipe/util/packet_test_util.h"
#include <string>
#include "mediapipe/framework/packet.h"
#include "mediapipe/framework/port.h"
#include "mediapipe/framework/port/gmock.h"
#include "mediapipe/framework/port/gtest-spi.h"
#include "mediapipe/framework/timestamp.h"
namespace mediapipe {
namespace {
using ::testing::Eq;
TEST(PacketTestUtilTest, Matches) {
const Packet int_packet = MakePacket<int>(42);
EXPECT_THAT(int_packet, PacketContains<int>(Eq(42)));
}
TEST(PacketTestUtilTest, MatchesContentWithMatchingTimestamp) {
const Packet int_packet = MakePacket<int>(42).At(Timestamp::PostStream());
EXPECT_THAT(int_packet, PacketContainsTimestampAndPayload<int>(
Eq(Timestamp::PostStream()), Eq(42)));
}
TEST(PacketTestUtilTest, MatchesContentWithMismatchingTimestamp) {
const Packet int_packet = MakePacket<int>(42).At(Timestamp(0ll));
EXPECT_NONFATAL_FAILURE(
{
EXPECT_THAT(int_packet, PacketContainsTimestampAndPayload<int>(
Eq(Timestamp::PostStream()), Eq(42)));
},
"`Packet::Timestamp` is equal to Timestamp::PostStream()");
}
TEST(PacketTestUtilTest, DoesNotMatch) {
const Packet int_packet = MakePacket<int>(42);
EXPECT_NONFATAL_FAILURE(
{ EXPECT_THAT(int_packet, PacketContains<int>(Eq(47))); },
"containing value 42");
}
TEST(PacketTestUtilTest, DoesNotMatchContentWithMatchingTimestamp) {
const Packet int_packet = MakePacket<int>(42).At(Timestamp(0ll));
EXPECT_NONFATAL_FAILURE(
{
EXPECT_THAT(int_packet, PacketContainsTimestampAndPayload<int>(
Eq(Timestamp(0ll)), Eq(47)));
},
"type int that is equal to 47");
}
TEST(PacketTestUtilTest, DoesNotMatchContentWithMismatchingTimestamp) {
const Packet int_packet = MakePacket<int>(42).At(Timestamp(0ll));
EXPECT_NONFATAL_FAILURE(
{
EXPECT_THAT(int_packet, PacketContainsTimestampAndPayload<int>(
Eq(Timestamp(20ll)), Eq(47)));
},
"`Packet::Timestamp` is equal to 20) and (packet contains value of type "
"int that is equal to 47");
}
TEST(PacketTestUtilTest, TypeMismatch) {
const Packet string_packet = MakePacket<std::string>("42");
EXPECT_NONFATAL_FAILURE(
{ EXPECT_THAT(string_packet, PacketContains<int>(Eq(42))); },
"does not contain expected type int");
}
TEST(PacketTestUtilTest, TypeMismatchContentWithMatchingTimestamp) {
const Packet int_packet = MakePacket<std::string>("42").At(Timestamp(0ll));
EXPECT_NONFATAL_FAILURE(
{
EXPECT_THAT(int_packet, PacketContainsTimestampAndPayload<int>(
Eq(Timestamp(0ll)), Eq(47)));
},
"does not contain expected type int");
}
TEST(PacketTestUtilTest, TypeMismatchContentWithMismatchingTimestamp) {
const Packet int_packet = MakePacket<std::string>("42").At(Timestamp(0ll));
EXPECT_NONFATAL_FAILURE(
{
EXPECT_THAT(int_packet, PacketContainsTimestampAndPayload<int>(
Eq(Timestamp::PreStream()), Eq(47)));
},
"`Packet::Timestamp` is equal to Timestamp::PreStream()) and (packet "
"contains value of type int that is equal to 47");
}
} // namespace
} // namespace mediapipe
+1 -3
View File
@@ -29,11 +29,9 @@ absl::StatusOr<api2::Packet<TfLiteModelPtr>> TfLiteModelLoader::LoadFromPath(
// TODO: get rid of manual resolving with PathToResourceAsFile
// as soon as it's incorporated into GetResourceContents.
if (!status_or_content.ok()) {
LOG(WARNING)
<< "Trying to resolve path manually as GetResourceContents failed: "
<< status_or_content.message();
ASSIGN_OR_RETURN(auto resolved_path,
mediapipe::PathToResourceAsFile(model_path));
VLOG(2) << "Loading the model from " << resolved_path;
MP_RETURN_IF_ERROR(
mediapipe::GetResourceContents(resolved_path, &model_blob));
}