Skip to content

Commit dc322d9

Browse files
andrewdacenkofacebook-github-bot
authored andcommitted
Add ImageSource debug convertions (#53042)
Summary: Pull Request resolved: #53042 Changelog: [Internal] Add debug convertions for ImageSource. Reviewed By: rubennorte Differential Revision: D79561513 fbshipit-source-id: f48d640d78b34a6e72d120f41f1f32ab963d1069
1 parent e9fdb23 commit dc322d9

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

packages/react-native/Libraries/Image/__tests__/Image-itest.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ describe('<Image>', () => {
4040
layoutMetrics-overflowInset="{top:0,right:0,bottom:0,left:0}"
4141
layoutMetrics-pointScaleFactor="3"
4242
overflow="hidden"
43+
source-scale="1"
44+
source-type="remote"
4345
/>,
4446
);
4547
});

packages/react-native/ReactCommon/react/renderer/components/image/ImageProps.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,25 @@ folly::dynamic ImageProps::getDiffProps(const Props* prevProps) const {
285285

286286
#if RN_DEBUG_STRING_CONVERTIBLE
287287
SharedDebugStringConvertibleList ImageProps::getDebugProps() const {
288-
const auto& defaultImageProps = ImageProps();
288+
const auto& imageProps = ImageProps();
289+
290+
auto sourcesList = SharedDebugStringConvertibleList{};
291+
if (sources.size() == 1) {
292+
sourcesList = sources[0].getDebugProps("source");
293+
} else if (sources.size() > 1) {
294+
for (const auto& source : sources) {
295+
std::string sourceName = "source@" + react::toString(source.scale) + "x";
296+
auto debugProps = source.getDebugProps(sourceName);
297+
sourcesList.insert(
298+
sourcesList.end(), debugProps.begin(), debugProps.end());
299+
}
300+
}
289301

290302
return ViewProps::getDebugProps() +
303+
defaultSource.getDebugProps("defaultSource") + sourcesList +
291304
SharedDebugStringConvertibleList{
292305
debugStringConvertibleItem(
293-
"blurRadius", blurRadius, defaultImageProps.blurRadius),
306+
"blurRadius", blurRadius, imageProps.blurRadius),
294307
};
295308
}
296309
#endif

packages/react-native/ReactCommon/react/renderer/imagemanager/primitives.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include <string>
1111
#include <vector>
1212

13+
#include <react/renderer/core/graphicsConversions.h>
1314
#include <react/renderer/core/propsConversions.h>
15+
#include <react/renderer/debug/debugStringConvertibleUtils.h>
1416
#include <react/renderer/graphics/Float.h>
1517
#include <react/renderer/graphics/Size.h>
1618

@@ -90,6 +92,66 @@ class ImageSource {
9092
}
9193

9294
#endif
95+
96+
#if RN_DEBUG_STRING_CONVERTIBLE
97+
SharedDebugStringConvertibleList getDebugProps(
98+
const std::string prefix) const {
99+
ImageSource imageSource{};
100+
101+
SharedDebugStringConvertibleList headersList;
102+
for (const auto& header : headers) {
103+
headersList.push_back(debugStringConvertibleItem(
104+
prefix + "-header-" + header.first, header.second));
105+
}
106+
107+
return headersList +
108+
SharedDebugStringConvertibleList{
109+
debugStringConvertibleItem(
110+
prefix + "-type", toString(type), toString(imageSource.type)),
111+
debugStringConvertibleItem(prefix + "-uri", uri, imageSource.uri),
112+
debugStringConvertibleItem(
113+
prefix + "-bundle", bundle, imageSource.bundle),
114+
debugStringConvertibleItem(
115+
prefix + "-scale", scale, imageSource.scale),
116+
debugStringConvertibleItem(
117+
prefix + "-size",
118+
react::toString(size),
119+
react::toString(imageSource.size)),
120+
debugStringConvertibleItem(
121+
prefix + "-body", body, imageSource.body),
122+
debugStringConvertibleItem(
123+
prefix + "-method", method, imageSource.method),
124+
debugStringConvertibleItem(
125+
prefix + "-cache",
126+
toString(cache),
127+
toString(imageSource.cache)),
128+
};
129+
}
130+
131+
std::string toString(const Type& typeValue) const {
132+
switch (typeValue) {
133+
case ImageSource::Type::Invalid:
134+
return "invalid";
135+
case ImageSource::Type::Remote:
136+
return "remote";
137+
case ImageSource::Type::Local:
138+
return "local";
139+
}
140+
}
141+
142+
std::string toString(const CacheStategy& cacheValue) const {
143+
switch (cacheValue) {
144+
case ImageSource::CacheStategy::Default:
145+
return "default";
146+
case ImageSource::CacheStategy::Reload:
147+
return "reload";
148+
case ImageSource::CacheStategy::ForceCache:
149+
return "force-cache";
150+
case ImageSource::CacheStategy::OnlyIfCached:
151+
return "only-if-cached";
152+
}
153+
}
154+
#endif
93155
};
94156

95157
#ifdef RN_SERIALIZABLE_STATE

0 commit comments

Comments
 (0)