diff options
Diffstat (limited to 'tools/aapt/tests')
-rw-r--r-- | tools/aapt/tests/CrunchCache_test.cpp | 97 | ||||
-rw-r--r-- | tools/aapt/tests/FileFinder_test.cpp | 101 | ||||
-rw-r--r-- | tools/aapt/tests/MockCacheUpdater.h | 40 | ||||
-rw-r--r-- | tools/aapt/tests/MockDirectoryWalker.h | 85 | ||||
-rw-r--r-- | tools/aapt/tests/MockFileFinder.h | 55 | ||||
-rw-r--r-- | tools/aapt/tests/plurals/AndroidManifest.xml | 5 | ||||
-rw-r--r-- | tools/aapt/tests/plurals/res/values/strings.xml | 7 | ||||
-rwxr-xr-x | tools/aapt/tests/plurals/run.sh | 16 |
8 files changed, 406 insertions, 0 deletions
diff --git a/tools/aapt/tests/CrunchCache_test.cpp b/tools/aapt/tests/CrunchCache_test.cpp new file mode 100644 index 0000000..20b5022 --- /dev/null +++ b/tools/aapt/tests/CrunchCache_test.cpp @@ -0,0 +1,97 @@ +// +// Copyright 2011 The Android Open Source Project +// +#include <utils/String8.h> +#include <iostream> +#include <errno.h> + +#include "CrunchCache.h" +#include "FileFinder.h" +#include "MockFileFinder.h" +#include "CacheUpdater.h" +#include "MockCacheUpdater.h" + +using namespace android; +using std::cout; +using std::endl; + +void expectEqual(int got, int expected, const char* desc) { + cout << "Checking " << desc << ": "; + cout << "Got " << got << ", expected " << expected << "..."; + cout << ( (got == expected) ? "PASSED" : "FAILED") << endl; + errno += ((got == expected) ? 0 : 1); +} + +int main() { + + errno = 0; + + String8 source("res"); + String8 dest("res2"); + + // Create data for MockFileFinder to feed to the cache + KeyedVector<String8, time_t> sourceData; + // This shouldn't be updated + sourceData.add(String8("res/drawable/hello.png"),3); + // This should be updated + sourceData.add(String8("res/drawable/world.png"),5); + // This should cause make directory to be called + sourceData.add(String8("res/drawable-cool/hello.png"),3); + + KeyedVector<String8, time_t> destData; + destData.add(String8("res2/drawable/hello.png"),3); + destData.add(String8("res2/drawable/world.png"),3); + // this should call delete + destData.add(String8("res2/drawable/dead.png"),3); + + // Package up data and create mock file finder + KeyedVector<String8, KeyedVector<String8,time_t> > data; + data.add(source,sourceData); + data.add(dest,destData); + FileFinder* ff = new MockFileFinder(data); + CrunchCache cc(source,dest,ff); + + MockCacheUpdater* mcu = new MockCacheUpdater(); + CacheUpdater* cu(mcu); + + cout << "Running Crunch..."; + int result = cc.crunch(cu); + cout << ((result > 0) ? "PASSED" : "FAILED") << endl; + errno += ((result > 0) ? 0 : 1); + + const int EXPECTED_RESULT = 2; + expectEqual(result, EXPECTED_RESULT, "number of files touched"); + + cout << "Checking calls to deleteFile and processImage:" << endl; + const int EXPECTED_DELETES = 1; + const int EXPECTED_PROCESSED = 2; + // Deletes + expectEqual(mcu->deleteCount, EXPECTED_DELETES, "deleteFile"); + // processImage + expectEqual(mcu->processCount, EXPECTED_PROCESSED, "processImage"); + + const int EXPECTED_OVERWRITES = 3; + result = cc.crunch(cu, true); + expectEqual(result, EXPECTED_OVERWRITES, "number of files touched with overwrite"); + \ + + if (errno == 0) + cout << "ALL TESTS PASSED!" << endl; + else + cout << errno << " TESTS FAILED" << endl; + + delete ff; + delete cu; + + // TESTS BELOW WILL GO AWAY SOON + + String8 source2("ApiDemos/res"); + String8 dest2("ApiDemos/res2"); + + FileFinder* sff = new SystemFileFinder(); + CacheUpdater* scu = new SystemCacheUpdater(); + + CrunchCache scc(source2,dest2,sff); + + scc.crunch(scu); +}
\ No newline at end of file diff --git a/tools/aapt/tests/FileFinder_test.cpp b/tools/aapt/tests/FileFinder_test.cpp new file mode 100644 index 0000000..07bd665 --- /dev/null +++ b/tools/aapt/tests/FileFinder_test.cpp @@ -0,0 +1,101 @@ +// +// Copyright 2011 The Android Open Source Project +// +#include <utils/Vector.h> +#include <utils/KeyedVector.h> +#include <iostream> +#include <cassert> +#include <utils/String8.h> +#include <utility> + +#include "DirectoryWalker.h" +#include "MockDirectoryWalker.h" +#include "FileFinder.h" + +using namespace android; + +using std::pair; +using std::cout; +using std::endl; + + + +int main() +{ + + cout << "\n\n STARTING FILE FINDER TESTS" << endl; + String8 path("ApiDemos"); + + // Storage to pass to findFiles() + KeyedVector<String8,time_t> testStorage; + + // Mock Directory Walker initialization. First data, then sdw + Vector< pair<String8,time_t> > data; + data.push( pair<String8,time_t>(String8("hello.png"),3) ); + data.push( pair<String8,time_t>(String8("world.PNG"),3) ); + data.push( pair<String8,time_t>(String8("foo.pNg"),3) ); + // Neither of these should be found + data.push( pair<String8,time_t>(String8("hello.jpg"),3) ); + data.push( pair<String8,time_t>(String8(".hidden.png"),3)); + + DirectoryWalker* sdw = new StringDirectoryWalker(path,data); + + // Extensions to look for + Vector<String8> exts; + exts.push(String8(".png")); + + errno = 0; + + // Make sure we get a valid mock directory walker + // Make sure we finish without errors + cout << "Checking DirectoryWalker..."; + assert(sdw != NULL); + cout << "PASSED" << endl; + + // Make sure we finish without errors + cout << "Running findFiles()..."; + bool findStatus = FileFinder::findFiles(path,exts, testStorage, sdw); + assert(findStatus); + cout << "PASSED" << endl; + + const size_t SIZE_EXPECTED = 3; + // Check to make sure we have the right number of things in our storage + cout << "Running size comparison: Size is " << testStorage.size() << ", "; + cout << "Expected " << SIZE_EXPECTED << "..."; + if(testStorage.size() == SIZE_EXPECTED) + cout << "PASSED" << endl; + else { + cout << "FAILED" << endl; + errno++; + } + + // Check to make sure that each of our found items has the right extension + cout << "Checking Returned Extensions..."; + bool extsOkay = true; + String8 wrongExts; + for (size_t i = 0; i < SIZE_EXPECTED; ++i) { + String8 testExt(testStorage.keyAt(i).getPathExtension()); + testExt.toLower(); + if (testExt != ".png") { + wrongExts += testStorage.keyAt(i); + wrongExts += "\n"; + extsOkay = false; + } + } + if (extsOkay) + cout << "PASSED" << endl; + else { + cout << "FAILED" << endl; + cout << "The following extensions didn't check out" << endl << wrongExts; + } + + // Clean up + delete sdw; + + if(errno == 0) { + cout << "ALL TESTS PASSED" << endl; + } else { + cout << errno << " TESTS FAILED" << endl; + } + return errno; +}
\ No newline at end of file diff --git a/tools/aapt/tests/MockCacheUpdater.h b/tools/aapt/tests/MockCacheUpdater.h new file mode 100644 index 0000000..c7f4bd7 --- /dev/null +++ b/tools/aapt/tests/MockCacheUpdater.h @@ -0,0 +1,40 @@ +// +// Copyright 2011 The Android Open Source Project +// +#ifndef MOCKCACHEUPDATER_H +#define MOCKCACHEUPDATER_H + +#include <utils/String8.h> +#include "CacheUpdater.h" + +using namespace android; + +class MockCacheUpdater : public CacheUpdater { +public: + + MockCacheUpdater() + : deleteCount(0), processCount(0) { }; + + // Make sure all the directories along this path exist + virtual void ensureDirectoriesExist(String8 path) + { + // Nothing to do + }; + + // Delete a file + virtual void deleteFile(String8 path) { + deleteCount++; + }; + + // Process an image from source out to dest + virtual void processImage(String8 source, String8 dest) { + processCount++; + }; + + // DATA MEMBERS + int deleteCount; + int processCount; +private: +}; + +#endif // MOCKCACHEUPDATER_H
\ No newline at end of file diff --git a/tools/aapt/tests/MockDirectoryWalker.h b/tools/aapt/tests/MockDirectoryWalker.h new file mode 100644 index 0000000..5900cf3 --- /dev/null +++ b/tools/aapt/tests/MockDirectoryWalker.h @@ -0,0 +1,85 @@ +// +// Copyright 2011 The Android Open Source Project +// +#ifndef MOCKDIRECTORYWALKER_H +#define MOCKDIRECTORYWALKER_H + +#include <utils/Vector.h> +#include <utils/String8.h> +#include <utility> +#include "DirectoryWalker.h" + +using namespace android; +using std::pair; + +// String8 Directory Walker +// This is an implementation of the Directory Walker abstraction that is built +// for testing. +// Instead of system calls it queries a private data structure for the directory +// entries. It takes a path and a map of filenames and their modification times. +// functions are inlined since they are short and simple + +class StringDirectoryWalker : public DirectoryWalker { +public: + StringDirectoryWalker(String8& path, Vector< pair<String8,time_t> >& data) + : mPos(0), mBasePath(path), mData(data) { + //fprintf(stdout,"StringDW built to mimic %s with %d files\n", + // mBasePath.string()); + }; + // Default copy constructor, and destructor are fine + + virtual bool openDir(String8 path) { + // If the user is trying to query the "directory" that this + // walker was initialized with, then return success. Else fail. + return path == mBasePath; + }; + virtual bool openDir(const char* path) { + String8 p(path); + openDir(p); + return true; + }; + // Advance to next entry in the Vector + virtual struct dirent* nextEntry() { + // Advance position and check to see if we're done + if (mPos >= mData.size()) + return NULL; + + // Place data in the entry descriptor. This class only returns files. + mEntry.d_type = DT_REG; + mEntry.d_ino = mPos; + // Copy chars from the string name to the entry name + size_t i = 0; + for (i; i < mData[mPos].first.size(); ++i) + mEntry.d_name[i] = mData[mPos].first[i]; + mEntry.d_name[i] = '\0'; + + // Place data in stats + mStats.st_ino = mPos; + mStats.st_mtime = mData[mPos].second; + + // Get ready to move to the next entry + mPos++; + + return &mEntry; + }; + // Get the stats for the current entry + virtual struct stat* entryStats() { + return &mStats; + }; + // Nothing to do in clean up + virtual void closeDir() { + // Nothing to do + }; + virtual DirectoryWalker* clone() { + return new StringDirectoryWalker(*this); + }; +private: + // Current position in the Vector + size_t mPos; + // Base path + String8 mBasePath; + // Data to simulate a directory full of files. + Vector< pair<String8,time_t> > mData; +}; + +#endif // MOCKDIRECTORYWALKER_H
\ No newline at end of file diff --git a/tools/aapt/tests/MockFileFinder.h b/tools/aapt/tests/MockFileFinder.h new file mode 100644 index 0000000..da5ea4f --- /dev/null +++ b/tools/aapt/tests/MockFileFinder.h @@ -0,0 +1,55 @@ +// +// Copyright 2011 The Android Open Source Project +// + +#ifndef MOCKFILEFINDER_H +#define MOCKFILEFINDER_H + +#include <utils/Vector.h> +#include <utils/KeyedVector.h> +#include <utils/String8.h> + +#include "DirectoryWalker.h" + +using namespace android; + +class MockFileFinder : public FileFinder { +public: + MockFileFinder (KeyedVector<String8, KeyedVector<String8,time_t> >& files) + : mFiles(files) + { + // Nothing left to do + }; + + /** + * findFiles implementation for the abstraction. + * PRECONDITIONS: + * No checking is done, so there MUST be an entry in mFiles with + * path matching basePath. + * + * POSTCONDITIONS: + * fileStore is filled with a copy of the data in mFiles corresponding + * to the basePath. + */ + + virtual bool findFiles(String8 basePath, Vector<String8>& extensions, + KeyedVector<String8,time_t>& fileStore, + DirectoryWalker* dw) + { + const KeyedVector<String8,time_t>* payload(&mFiles.valueFor(basePath)); + // Since KeyedVector doesn't implement swap + // (who doesn't use swap??) we loop and add one at a time. + for (size_t i = 0; i < payload->size(); ++i) { + fileStore.add(payload->keyAt(i),payload->valueAt(i)); + } + return true; + } + +private: + // Virtual mapping between "directories" and the "files" contained + // in them + KeyedVector<String8, KeyedVector<String8,time_t> > mFiles; +}; + + +#endif // MOCKFILEFINDER_H
\ No newline at end of file diff --git a/tools/aapt/tests/plurals/AndroidManifest.xml b/tools/aapt/tests/plurals/AndroidManifest.xml new file mode 100644 index 0000000..c721dee --- /dev/null +++ b/tools/aapt/tests/plurals/AndroidManifest.xml @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.aapt.test.plurals"> + +</manifest> diff --git a/tools/aapt/tests/plurals/res/values/strings.xml b/tools/aapt/tests/plurals/res/values/strings.xml new file mode 100644 index 0000000..1c1fc19 --- /dev/null +++ b/tools/aapt/tests/plurals/res/values/strings.xml @@ -0,0 +1,7 @@ +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + <string name="ok">OK</string> + <plurals name="a_plural"> + <item quantity="one">A dog</item> + <item quantity="other">Some dogs</item> + </plurals> +</resources> diff --git a/tools/aapt/tests/plurals/run.sh b/tools/aapt/tests/plurals/run.sh new file mode 100755 index 0000000..4d39e10 --- /dev/null +++ b/tools/aapt/tests/plurals/run.sh @@ -0,0 +1,16 @@ +TEST_DIR=tools/aapt/tests/plurals +TEST_OUT_DIR=out/plurals_test + +rm -rf $TEST_OUT_DIR +mkdir -p $TEST_OUT_DIR +mkdir -p $TEST_OUT_DIR/java + +#gdb --args \ +aapt package -v -x -m -z -J $TEST_OUT_DIR/java -M $TEST_DIR/AndroidManifest.xml \ + -I out/target/common/obj/APPS/framework-res_intermediates/package-export.apk \ + -P $TEST_OUT_DIR/public_resources.xml \ + -S $TEST_DIR/res + +echo +echo "==================== FILES CREATED ==================== " +find $TEST_OUT_DIR -type f |