| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef AAPT_TEST_BUILDERS_H |
| 18 |
#define AAPT_TEST_BUILDERS_H |
| 19 |
|
| 20 |
#include <memory> |
| 21 |
|
| 22 |
#include "android-base/macros.h" |
| 23 |
#include "androidfw/ConfigDescription.h" |
| 24 |
|
| 25 |
#include "Resource.h" |
| 26 |
#include "ResourceTable.h" |
| 27 |
#include "ResourceValues.h" |
| 28 |
#include "configuration/ConfigurationParser.h" |
| 29 |
#include "configuration/ConfigurationParser.internal.h" |
| 30 |
#include "process/IResourceTableConsumer.h" |
| 31 |
#include "test/Common.h" |
| 32 |
#include "xml/XmlDom.h" |
| 33 |
|
| 34 |
namespace aapt { |
| 35 |
namespace test { |
| 36 |
|
| 37 |
class ResourceTableBuilder { |
| 38 |
public: |
| 39 |
ResourceTableBuilder() = default; |
| 40 |
|
| 41 |
ResourceTableBuilder& AddSimple(const android::StringPiece& name, const ResourceId& id = {}); |
| 42 |
ResourceTableBuilder& AddSimple(const android::StringPiece& name, |
| 43 |
const android::ConfigDescription& config, |
| 44 |
const ResourceId& id = {}); |
| 45 |
ResourceTableBuilder& AddReference(const android::StringPiece& name, |
| 46 |
const android::StringPiece& ref); |
| 47 |
ResourceTableBuilder& AddReference(const android::StringPiece& name, const ResourceId& id, |
| 48 |
const android::StringPiece& ref); |
| 49 |
ResourceTableBuilder& AddString(const android::StringPiece& name, |
| 50 |
const android::StringPiece& str); |
| 51 |
ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id, |
| 52 |
const android::StringPiece& str); |
| 53 |
ResourceTableBuilder& AddString(const android::StringPiece& name, const ResourceId& id, |
| 54 |
const android::ConfigDescription& config, |
| 55 |
const android::StringPiece& str); |
| 56 |
ResourceTableBuilder& AddFileReference(const android::StringPiece& name, |
| 57 |
const android::StringPiece& path, |
| 58 |
io::IFile* file = nullptr); |
| 59 |
ResourceTableBuilder& AddFileReference(const android::StringPiece& name, const ResourceId& id, |
| 60 |
const android::StringPiece& path, |
| 61 |
io::IFile* file = nullptr); |
| 62 |
ResourceTableBuilder& AddFileReference(const android::StringPiece& name, |
| 63 |
const android::StringPiece& path, |
| 64 |
const android::ConfigDescription& config, |
| 65 |
io::IFile* file = nullptr); |
| 66 |
ResourceTableBuilder& AddValue(const android::StringPiece& name, std::unique_ptr<Value> value); |
| 67 |
ResourceTableBuilder& AddValue(const android::StringPiece& name, const ResourceId& id, |
| 68 |
std::unique_ptr<Value> value); |
| 69 |
ResourceTableBuilder& AddValue(const android::StringPiece& name, |
| 70 |
const android::ConfigDescription& config, |
| 71 |
const ResourceId& id, std::unique_ptr<Value> value); |
| 72 |
ResourceTableBuilder& SetSymbolState(const android::StringPiece& name, const ResourceId& id, |
| 73 |
Visibility::Level level, bool allow_new = false); |
| 74 |
ResourceTableBuilder& SetOverlayable(const android::StringPiece& name, |
| 75 |
const OverlayableItem& overlayable); |
| 76 |
ResourceTableBuilder& Add(NewResource&& res); |
| 77 |
|
| 78 |
StringPool* string_pool(); |
| 79 |
std::unique_ptr<ResourceTable> Build(); |
| 80 |
|
| 81 |
private: |
| 82 |
DISALLOW_COPY_AND_ASSIGN(ResourceTableBuilder); |
| 83 |
|
| 84 |
std::unique_ptr<ResourceTable> table_ = util::make_unique<ResourceTable>(); |
| 85 |
}; |
| 86 |
|
| 87 |
std::unique_ptr<Reference> BuildReference(const android::StringPiece& ref, |
| 88 |
const std::optional<ResourceId>& id = {}); |
| 89 |
std::unique_ptr<BinaryPrimitive> BuildPrimitive(uint8_t type, uint32_t data); |
| 90 |
|
| 91 |
template <typename T> |
| 92 |
class ValueBuilder { |
| 93 |
public: |
| 94 |
template <typename... Args> |
| 95 |
explicit ValueBuilder(Args&&... args) : value_(new T{std::forward<Args>(args)...}) { |
| 96 |
} |
| 97 |
|
| 98 |
template <typename... Args> |
| 99 |
ValueBuilder& SetSource(Args&&... args) { |
| 100 |
value_->SetSource(Source{std::forward<Args>(args)...}); |
| 101 |
return *this; |
| 102 |
} |
| 103 |
|
| 104 |
ValueBuilder& SetComment(const android::StringPiece& str) { |
| 105 |
value_->SetComment(str); |
| 106 |
return *this; |
| 107 |
} |
| 108 |
|
| 109 |
std::unique_ptr<Value> Build() { |
| 110 |
return std::move(value_); |
| 111 |
} |
| 112 |
|
| 113 |
private: |
| 114 |
DISALLOW_COPY_AND_ASSIGN(ValueBuilder); |
| 115 |
|
| 116 |
std::unique_ptr<Value> value_; |
| 117 |
}; |
| 118 |
|
| 119 |
class AttributeBuilder { |
| 120 |
public: |
| 121 |
AttributeBuilder(); |
| 122 |
AttributeBuilder& SetTypeMask(uint32_t typeMask); |
| 123 |
AttributeBuilder& SetWeak(bool weak); |
| 124 |
AttributeBuilder& AddItem(const android::StringPiece& name, uint32_t value); |
| 125 |
std::unique_ptr<Attribute> Build(); |
| 126 |
|
| 127 |
private: |
| 128 |
DISALLOW_COPY_AND_ASSIGN(AttributeBuilder); |
| 129 |
|
| 130 |
std::unique_ptr<Attribute> attr_; |
| 131 |
}; |
| 132 |
|
| 133 |
class StyleBuilder { |
| 134 |
public: |
| 135 |
StyleBuilder() = default; |
| 136 |
StyleBuilder& SetParent(const android::StringPiece& str); |
| 137 |
StyleBuilder& AddItem(const android::StringPiece& str, std::unique_ptr<Item> value); |
| 138 |
StyleBuilder& AddItem(const android::StringPiece& str, const ResourceId& id, |
| 139 |
std::unique_ptr<Item> value); |
| 140 |
std::unique_ptr<Style> Build(); |
| 141 |
|
| 142 |
private: |
| 143 |
DISALLOW_COPY_AND_ASSIGN(StyleBuilder); |
| 144 |
|
| 145 |
std::unique_ptr<Style> style_ = util::make_unique<Style>(); |
| 146 |
}; |
| 147 |
|
| 148 |
class StyleableBuilder { |
| 149 |
public: |
| 150 |
StyleableBuilder() = default; |
| 151 |
StyleableBuilder& AddItem(const android::StringPiece& str, |
| 152 |
const std::optional<ResourceId>& id = {}); |
| 153 |
std::unique_ptr<Styleable> Build(); |
| 154 |
|
| 155 |
private: |
| 156 |
DISALLOW_COPY_AND_ASSIGN(StyleableBuilder); |
| 157 |
|
| 158 |
std::unique_ptr<Styleable> styleable_ = util::make_unique<Styleable>(); |
| 159 |
}; |
| 160 |
|
| 161 |
std::unique_ptr<xml::XmlResource> BuildXmlDom(const android::StringPiece& str); |
| 162 |
std::unique_ptr<xml::XmlResource> BuildXmlDomForPackageName(IAaptContext* context, |
| 163 |
const android::StringPiece& str); |
| 164 |
|
| 165 |
class ArtifactBuilder { |
| 166 |
public: |
| 167 |
ArtifactBuilder() = default; |
| 168 |
|
| 169 |
ArtifactBuilder& SetName(const std::string& name); |
| 170 |
ArtifactBuilder& SetVersion(int version); |
| 171 |
ArtifactBuilder& AddAbi(configuration::Abi abi); |
| 172 |
ArtifactBuilder& AddDensity(const android::ConfigDescription& density); |
| 173 |
ArtifactBuilder& AddLocale(const android::ConfigDescription& locale); |
| 174 |
ArtifactBuilder& SetAndroidSdk(int min_sdk); |
| 175 |
configuration::OutputArtifact Build(); |
| 176 |
|
| 177 |
private: |
| 178 |
DISALLOW_COPY_AND_ASSIGN(ArtifactBuilder); |
| 179 |
|
| 180 |
configuration::OutputArtifact artifact_; |
| 181 |
}; |
| 182 |
|
| 183 |
class PostProcessingConfigurationBuilder { |
| 184 |
public: |
| 185 |
PostProcessingConfigurationBuilder() = default; |
| 186 |
|
| 187 |
PostProcessingConfigurationBuilder& AddAbiGroup(const std::string& label, |
| 188 |
std::vector<configuration::Abi> abis = {}); |
| 189 |
PostProcessingConfigurationBuilder& AddDensityGroup(const std::string& label, |
| 190 |
std::vector<std::string> densities = {}); |
| 191 |
PostProcessingConfigurationBuilder& AddLocaleGroup(const std::string& label, |
| 192 |
std::vector<std::string> locales = {}); |
| 193 |
PostProcessingConfigurationBuilder& AddDeviceFeatureGroup(const std::string& label); |
| 194 |
PostProcessingConfigurationBuilder& AddGlTextureGroup(const std::string& label); |
| 195 |
PostProcessingConfigurationBuilder& AddAndroidSdk(std::string label, int min_sdk); |
| 196 |
PostProcessingConfigurationBuilder& AddArtifact(configuration::ConfiguredArtifact artrifact); |
| 197 |
|
| 198 |
configuration::PostProcessingConfiguration Build(); |
| 199 |
|
| 200 |
private: |
| 201 |
template <typename T> |
| 202 |
inline PostProcessingConfigurationBuilder& AddGroup(const std::string& label, |
| 203 |
configuration::Group<T>* group, |
| 204 |
std::vector<T> to_add = {}) { |
| 205 |
auto& values = GetOrCreateGroup(label, group); |
| 206 |
values.insert(std::begin(values), std::begin(to_add), std::end(to_add)); |
| 207 |
return *this; |
| 208 |
} |
| 209 |
|
| 210 |
configuration::PostProcessingConfiguration config_; |
| 211 |
}; |
| 212 |
|
| 213 |
class ConfigDescriptionBuilder { |
| 214 |
public: |
| 215 |
ConfigDescriptionBuilder() = default; |
| 216 |
|
| 217 |
ConfigDescriptionBuilder& setMcc(uint16_t mcc) { |
| 218 |
config_.mcc = mcc; |
| 219 |
return *this; |
| 220 |
} |
| 221 |
ConfigDescriptionBuilder& setMnc(uint16_t mnc) { |
| 222 |
config_.mnc = mnc; |
| 223 |
return *this; |
| 224 |
} |
| 225 |
ConfigDescriptionBuilder& setLanguage(uint16_t language) { |
| 226 |
config_.language[0] = language >> 8; |
| 227 |
config_.language[1] = language & 0xff; |
| 228 |
return *this; |
| 229 |
} |
| 230 |
ConfigDescriptionBuilder& setCountry(uint16_t country) { |
| 231 |
config_.country[0] = country >> 8; |
| 232 |
config_.country[1] = country & 0xff; |
| 233 |
return *this; |
| 234 |
} |
| 235 |
ConfigDescriptionBuilder& setOrientation(uint8_t orientation) { |
| 236 |
config_.orientation = orientation; |
| 237 |
return *this; |
| 238 |
} |
| 239 |
ConfigDescriptionBuilder& setTouchscreen(uint8_t touchscreen) { |
| 240 |
config_.touchscreen = touchscreen; |
| 241 |
return *this; |
| 242 |
} |
| 243 |
ConfigDescriptionBuilder& setDensity(uint16_t density) { |
| 244 |
config_.density = density; |
| 245 |
return *this; |
| 246 |
} |
| 247 |
ConfigDescriptionBuilder& setKeyboard(uint8_t keyboard) { |
| 248 |
config_.keyboard = keyboard; |
| 249 |
return *this; |
| 250 |
} |
| 251 |
ConfigDescriptionBuilder& setNavigation(uint8_t navigation) { |
| 252 |
config_.navigation = navigation; |
| 253 |
return *this; |
| 254 |
} |
| 255 |
ConfigDescriptionBuilder& setInputFlags(uint8_t inputFlags) { |
| 256 |
config_.inputFlags = inputFlags; |
| 257 |
return *this; |
| 258 |
} |
| 259 |
ConfigDescriptionBuilder& setInputPad0(uint8_t inputPad0) { |
| 260 |
config_.inputPad0 = inputPad0; |
| 261 |
return *this; |
| 262 |
} |
| 263 |
ConfigDescriptionBuilder& setScreenWidth(uint16_t screenWidth) { |
| 264 |
config_.screenWidth = screenWidth; |
| 265 |
return *this; |
| 266 |
} |
| 267 |
ConfigDescriptionBuilder& setScreenHeight(uint16_t screenHeight) { |
| 268 |
config_.screenHeight = screenHeight; |
| 269 |
return *this; |
| 270 |
} |
| 271 |
ConfigDescriptionBuilder& setSdkVersion(uint16_t sdkVersion) { |
| 272 |
config_.sdkVersion = sdkVersion; |
| 273 |
return *this; |
| 274 |
} |
| 275 |
ConfigDescriptionBuilder& setMinorVersion(uint16_t minorVersion) { |
| 276 |
config_.minorVersion = minorVersion; |
| 277 |
return *this; |
| 278 |
} |
| 279 |
ConfigDescriptionBuilder& setScreenLayout(uint8_t screenLayout) { |
| 280 |
config_.screenLayout = screenLayout; |
| 281 |
return *this; |
| 282 |
} |
| 283 |
ConfigDescriptionBuilder& setUiMode(uint8_t uiMode) { |
| 284 |
config_.uiMode = uiMode; |
| 285 |
return *this; |
| 286 |
} |
| 287 |
ConfigDescriptionBuilder& setSmallestScreenWidthDp(uint16_t smallestScreenWidthDp) { |
| 288 |
config_.smallestScreenWidthDp = smallestScreenWidthDp; |
| 289 |
return *this; |
| 290 |
} |
| 291 |
ConfigDescriptionBuilder& setScreenWidthDp(uint16_t screenWidthDp) { |
| 292 |
config_.screenWidthDp = screenWidthDp; |
| 293 |
return *this; |
| 294 |
} |
| 295 |
ConfigDescriptionBuilder& setScreenHeightDp(uint16_t screenHeightDp) { |
| 296 |
config_.screenHeightDp = screenHeightDp; |
| 297 |
return *this; |
| 298 |
} |
| 299 |
ConfigDescriptionBuilder& setScreenLayout2(uint8_t screenLayout2) { |
| 300 |
config_.screenLayout2 = screenLayout2; |
| 301 |
return *this; |
| 302 |
} |
| 303 |
ConfigDescriptionBuilder& setColorMode(uint8_t colorMode) { |
| 304 |
config_.colorMode = colorMode; |
| 305 |
return *this; |
| 306 |
} |
| 307 |
ConfigDescriptionBuilder& setScreenConfigPad2(uint16_t screenConfigPad2) { |
| 308 |
config_.screenConfigPad2 = screenConfigPad2; |
| 309 |
return *this; |
| 310 |
} |
| 311 |
android::ConfigDescription Build() { |
| 312 |
return config_; |
| 313 |
} |
| 314 |
|
| 315 |
private: |
| 316 |
android::ConfigDescription config_; |
| 317 |
}; |
| 318 |
|
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
#endif |