Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,14 @@ void ImageResponseObserverCoordinator::nativeImageResponseProgress(
int64_t loaded,
int64_t total) const {
mutex_.lock();
// Progress is reported on a different thread than completion, so it can
// arrive after the request has finished. It has nothing left to report then.
if (status_ != ImageResponse::Status::Loading &&
status_ != ImageResponse::Status::Cancelled) {
mutex_.unlock();
return;
}
auto observers = observers_;
react_native_assert(
status_ == ImageResponse::Status::Loading ||
status_ == ImageResponse::Status::Cancelled);
mutex_.unlock();

for (const auto& observer : observers) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <memory>

#include <gtest/gtest.h>

#include <react/renderer/imagemanager/ImageResponseObserver.h>
#include <react/renderer/imagemanager/ImageResponseObserverCoordinator.h>

using namespace facebook::react;

namespace {

class CountingObserver : public ImageResponseObserver {
public:
void didReceiveProgress(
float /*progress*/,
int64_t /*loaded*/,
int64_t /*total*/) const override {
++progressCount;
}
void didReceiveImage(const ImageResponse& /*imageResponse*/) const override {
++imageCount;
}
void didReceiveFailure(const ImageLoadError& /*error*/) const override {
++failureCount;
}

mutable int progressCount{0};
mutable int imageCount{0};
mutable int failureCount{0};
};

} // namespace

TEST(ImageResponseObserverCoordinatorTest, reportsProgressWhileLoading) {
const ImageResponseObserverCoordinator coordinator{{}, {}};
const auto observer = std::make_shared<CountingObserver>();
coordinator.addObserver(observer);

coordinator.nativeImageResponseProgress(0.5f, 50, 100);

EXPECT_EQ(observer->progressCount, 1);
}

TEST(ImageResponseObserverCoordinatorTest, ignoresProgressAfterCompletion) {
const ImageResponseObserverCoordinator coordinator{{}, {}};
const auto observer = std::make_shared<CountingObserver>();
coordinator.addObserver(observer);
coordinator.nativeImageResponseComplete(ImageResponse{nullptr, nullptr});

coordinator.nativeImageResponseProgress(1.0f, 100, 100);

EXPECT_EQ(observer->imageCount, 1);
EXPECT_EQ(observer->progressCount, 0);
}

TEST(ImageResponseObserverCoordinatorTest, ignoresProgressAfterFailure) {
const ImageResponseObserverCoordinator coordinator{{}, {}};
const auto observer = std::make_shared<CountingObserver>();
coordinator.addObserver(observer);
coordinator.nativeImageResponseFailed(ImageLoadError{nullptr});

coordinator.nativeImageResponseProgress(1.0f, 100, 100);

EXPECT_EQ(observer->failureCount, 1);
EXPECT_EQ(observer->progressCount, 0);
}
Loading