diff options
author | Yifan Hong | 2018-04-06 17:41:05 -0500 |
---|---|---|
committer | Yifan Hong | 2018-04-10 14:20:17 -0500 |
commit | 10d862283d70ab48941b2dbd1226f139480ed27c (patch) | |
tree | 57c914e500bad374a316f2bac054828968b01774 /test | |
parent | 00611d5483de8ddee95c1b251f126c704f5270f7 (diff) | |
download | platform-system-libvintf-10d862283d70ab48941b2dbd1226f139480ed27c.tar.gz platform-system-libvintf-10d862283d70ab48941b2dbd1226f139480ed27c.tar.xz platform-system-libvintf-10d862283d70ab48941b2dbd1226f139480ed27c.zip |
Cleanup global FileFetcherandroid-o-mr1-iot-preview-8
This global instance is modifiable everywhere and
is hard to maintain. It also contains references as
out-variables, which is not the standard.
Clean it up by refactoring it into a public (i.e.
not in details namespace) class, FileSystem.
Introduce VintfObject::InitFileSystem that allows
the behavior to be changed before any files are read.
Bug: 37999212
Test: host and target libvintf_test, vintf_object_test
Change-Id: I1c64b31fd37119450be89dfd1d2bfe76a71ccf3d
Merged-In: I1c64b31fd37119450be89dfd1d2bfe76a71ccf3d
Diffstat (limited to 'test')
-rw-r--r-- | test/utils-fake.cpp | 2 | ||||
-rw-r--r-- | test/utils-fake.h | 26 | ||||
-rw-r--r-- | test/vintf_object_tests.cpp | 54 |
3 files changed, 37 insertions, 45 deletions
diff --git a/test/utils-fake.cpp b/test/utils-fake.cpp index 38f6ab0..a04ba79 100644 --- a/test/utils-fake.cpp +++ b/test/utils-fake.cpp | |||
@@ -22,8 +22,6 @@ namespace details { | |||
22 | 22 | ||
23 | // Do not create the mock objects here as InitGoogleMock must be called | 23 | // Do not create the mock objects here as InitGoogleMock must be called |
24 | // first. | 24 | // first. |
25 | FileFetcher* gFetcher = nullptr; | ||
26 | |||
27 | PartitionMounter* gPartitionMounter = nullptr; | 25 | PartitionMounter* gPartitionMounter = nullptr; |
28 | 26 | ||
29 | ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory = nullptr; | 27 | ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory = nullptr; |
diff --git a/test/utils-fake.h b/test/utils-fake.h index 0253bca..f07ae48 100644 --- a/test/utils-fake.h +++ b/test/utils-fake.h | |||
@@ -27,28 +27,20 @@ namespace android { | |||
27 | namespace vintf { | 27 | namespace vintf { |
28 | namespace details { | 28 | namespace details { |
29 | 29 | ||
30 | class MockFileFetcher : public FileFetcher { | 30 | class MockFileSystem : public FileSystem { |
31 | public: | 31 | public: |
32 | MockFileFetcher() { | 32 | MockFileSystem() {} |
33 | // By default call through to the original. | ||
34 | ON_CALL(*this, fetch(_, _)).WillByDefault(Invoke([this](const auto& path, auto& fetched) { | ||
35 | return real_.fetchInternal(path, fetched, nullptr); | ||
36 | })); | ||
37 | ON_CALL(*this, listFiles(_, _, _)) | ||
38 | .WillByDefault( | ||
39 | Invoke([this](const std::string& path, std::vector<std::string>* out, | ||
40 | std::string* error) { return real_.listFiles(path, out, error); })); | ||
41 | } | ||
42 | 33 | ||
43 | MOCK_METHOD2(fetch, status_t(const std::string& path, std::string& fetched)); | 34 | MOCK_CONST_METHOD2(fetch, status_t(const std::string& path, std::string& fetched)); |
44 | MOCK_METHOD3(listFiles, status_t(const std::string&, std::vector<std::string>*, std::string*)); | 35 | MOCK_CONST_METHOD3(listFiles, |
36 | status_t(const std::string&, std::vector<std::string>*, std::string*)); | ||
45 | 37 | ||
46 | status_t fetch(const std::string& path, std::string& fetched, std::string*) override final { | 38 | status_t fetch(const std::string& path, std::string* fetched, std::string*) const override { |
47 | return fetch(path, fetched); | 39 | // Call the mocked function |
40 | return fetch(path, *fetched); | ||
48 | } | 41 | } |
49 | |||
50 | private: | 42 | private: |
51 | FileFetcher real_; | 43 | FileSystemImpl mImpl; |
52 | }; | 44 | }; |
53 | 45 | ||
54 | class MockPartitionMounter : public PartitionMounter { | 46 | class MockPartitionMounter : public PartitionMounter { |
diff --git a/test/vintf_object_tests.cpp b/test/vintf_object_tests.cpp index 2baeee3..77d62cb 100644 --- a/test/vintf_object_tests.cpp +++ b/test/vintf_object_tests.cpp | |||
@@ -275,67 +275,67 @@ const static std::vector<std::string> systemMatrixRegexXmls = { | |||
275 | " </hal>\n" | 275 | " </hal>\n" |
276 | "</compatibility-matrix>\n"}; | 276 | "</compatibility-matrix>\n"}; |
277 | 277 | ||
278 | // Setup the MockFileFetcher used by the fetchAllInformation template | 278 | static MockPartitionMounter& mounter() { |
279 | return *static_cast<MockPartitionMounter*>(gPartitionMounter); | ||
280 | } | ||
281 | static MockFileSystem& fetcher() { | ||
282 | return static_cast<MockFileSystem&>(details::getFileSystem()); | ||
283 | } | ||
284 | |||
285 | // Setup the MockFileSystem used by the fetchAllInformation template | ||
279 | // so it returns the given metadata info instead of fetching from device. | 286 | // so it returns the given metadata info instead of fetching from device. |
280 | void setupMockFetcher(const std::string& vendorManifestXml, const std::string& systemMatrixXml, | 287 | void setupMockFetcher(const std::string& vendorManifestXml, const std::string& systemMatrixXml, |
281 | const std::string& systemManifestXml, const std::string& vendorMatrixXml, | 288 | const std::string& systemManifestXml, const std::string& vendorMatrixXml, |
282 | const std::string& productModel) { | 289 | const std::string& productModel) { |
283 | MockFileFetcher* fetcher = static_cast<MockFileFetcher*>(gFetcher); | 290 | ON_CALL(fetcher(), listFiles(StrEq(kVendorManifestFragmentDir), _, _)) |
284 | |||
285 | ON_CALL(*fetcher, listFiles(StrEq(kVendorManifestFragmentDir), _, _)) | ||
286 | .WillByDefault(Return(::android::OK)); | 291 | .WillByDefault(Return(::android::OK)); |
287 | ON_CALL(*fetcher, listFiles(StrEq(kSystemManifestFragmentDir), _, _)) | 292 | ON_CALL(fetcher(), listFiles(StrEq(kSystemManifestFragmentDir), _, _)) |
288 | .WillByDefault(Return(::android::OK)); | 293 | .WillByDefault(Return(::android::OK)); |
289 | ON_CALL(*fetcher, listFiles(StrEq(kOdmManifestFragmentDir), _, _)) | 294 | ON_CALL(fetcher(), listFiles(StrEq(kOdmManifestFragmentDir), _, _)) |
290 | .WillByDefault(Return(::android::OK)); | 295 | .WillByDefault(Return(::android::OK)); |
291 | 296 | ||
292 | if (!productModel.empty()) { | 297 | if (!productModel.empty()) { |
293 | ON_CALL(*fetcher, fetch(StrEq(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml"), _)) | 298 | ON_CALL(fetcher(), |
299 | fetch(StrEq(kOdmLegacyVintfDir + "manifest_" + productModel + ".xml"), _)) | ||
294 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); | 300 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); |
295 | ON_CALL(*fetcher, fetch(StrEq(kOdmVintfDir + "manifest_" + productModel + ".xml"), _)) | 301 | ON_CALL(fetcher(), fetch(StrEq(kOdmVintfDir + "manifest_" + productModel + ".xml"), _)) |
296 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); | 302 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); |
297 | } | 303 | } |
298 | ON_CALL(*fetcher, fetch(StrEq(kOdmLegacyManifest), _)) | 304 | ON_CALL(fetcher(), fetch(StrEq(kOdmLegacyManifest), _)) |
299 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); | 305 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); |
300 | ON_CALL(*fetcher, fetch(StrEq(kOdmManifest), _)) | 306 | ON_CALL(fetcher(), fetch(StrEq(kOdmManifest), _)) |
301 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); | 307 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); |
302 | ON_CALL(*fetcher, fetch(StrEq(kVendorManifest), _)) | 308 | ON_CALL(fetcher(), fetch(StrEq(kVendorManifest), _)) |
303 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); | 309 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); |
304 | ON_CALL(*fetcher, fetch(StrEq(kVendorLegacyManifest), _)) | 310 | ON_CALL(fetcher(), fetch(StrEq(kVendorLegacyManifest), _)) |
305 | .WillByDefault(Invoke([vendorManifestXml](const std::string& path, std::string& fetched) { | 311 | .WillByDefault(Invoke([vendorManifestXml](const std::string& path, std::string& fetched) { |
306 | (void)path; | 312 | (void)path; |
307 | fetched = vendorManifestXml; | 313 | fetched = vendorManifestXml; |
308 | return 0; | 314 | return 0; |
309 | })); | 315 | })); |
310 | ON_CALL(*fetcher, fetch(StrEq(kSystemManifest), _)) | 316 | ON_CALL(fetcher(), fetch(StrEq(kSystemManifest), _)) |
311 | .WillByDefault(Invoke([systemManifestXml](const std::string& path, std::string& fetched) { | 317 | .WillByDefault(Invoke([systemManifestXml](const std::string& path, std::string& fetched) { |
312 | (void)path; | 318 | (void)path; |
313 | fetched = systemManifestXml; | 319 | fetched = systemManifestXml; |
314 | return 0; | 320 | return 0; |
315 | })); | 321 | })); |
316 | ON_CALL(*fetcher, fetch(StrEq(kVendorMatrix), _)) | 322 | ON_CALL(fetcher(), fetch(StrEq(kVendorMatrix), _)) |
317 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); | 323 | .WillByDefault(Return(::android::NAME_NOT_FOUND)); |
318 | ON_CALL(*fetcher, fetch(StrEq(kVendorLegacyMatrix), _)) | 324 | ON_CALL(fetcher(), fetch(StrEq(kVendorLegacyMatrix), _)) |
319 | .WillByDefault(Invoke([vendorMatrixXml](const std::string& path, std::string& fetched) { | 325 | .WillByDefault(Invoke([vendorMatrixXml](const std::string& path, std::string& fetched) { |
320 | (void)path; | 326 | (void)path; |
321 | fetched = vendorMatrixXml; | 327 | fetched = vendorMatrixXml; |
322 | return 0; | 328 | return 0; |
323 | })); | 329 | })); |
324 | ON_CALL(*fetcher, fetch(StrEq(kSystemLegacyMatrix), _)) | 330 | ON_CALL(fetcher(), fetch(StrEq(kSystemLegacyMatrix), _)) |
325 | .WillByDefault(Invoke([systemMatrixXml](const std::string& path, std::string& fetched) { | 331 | .WillByDefault(Invoke([systemMatrixXml](const std::string& path, std::string& fetched) { |
326 | (void)path; | 332 | (void)path; |
327 | fetched = systemMatrixXml; | 333 | fetched = systemMatrixXml; |
328 | return 0; | 334 | return 0; |
329 | })); | 335 | })); |
330 | // Don't list /system/etc/vintf unless otherwise specified. | 336 | // Don't list /system/etc/vintf unless otherwise specified. |
331 | ON_CALL(*fetcher, listFiles(StrEq(kSystemVintfDir), _, _)).WillByDefault(Return(::android::OK)); | 337 | ON_CALL(fetcher(), listFiles(StrEq(kSystemVintfDir), _, _)) |
332 | } | 338 | .WillByDefault(Return(::android::OK)); |
333 | |||
334 | static MockPartitionMounter &mounter() { | ||
335 | return *static_cast<MockPartitionMounter *>(gPartitionMounter); | ||
336 | } | ||
337 | static MockFileFetcher &fetcher() { | ||
338 | return *static_cast<MockFileFetcher*>(gFetcher); | ||
339 | } | 339 | } |
340 | 340 | ||
341 | class VintfObjectTestBase : public testing::Test { | 341 | class VintfObjectTestBase : public testing::Test { |
@@ -1230,8 +1230,10 @@ TEST_F(RegexTest, DeprecateLevel3) { | |||
1230 | int main(int argc, char** argv) { | 1230 | int main(int argc, char** argv) { |
1231 | ::testing::InitGoogleMock(&argc, argv); | 1231 | ::testing::InitGoogleMock(&argc, argv); |
1232 | 1232 | ||
1233 | NiceMock<MockFileFetcher> fetcher; | 1233 | if (!VintfObject::InitFileSystem(std::make_unique<NiceMock<MockFileSystem>>())) { |
1234 | gFetcher = &fetcher; | 1234 | LOG(FATAL) << "Cannot set mock file system because it is already initialized."; |
1235 | return 1; | ||
1236 | } | ||
1235 | 1237 | ||
1236 | NiceMock<MockPartitionMounter> mounter; | 1238 | NiceMock<MockPartitionMounter> mounter; |
1237 | gPartitionMounter = &mounter; | 1239 | gPartitionMounter = &mounter; |