CrunchCache_test.cpp revision 8a39da80b33691b0c82458c3b7727e13ff71277e
1//
2// Copyright 2011 The Android Open Source Project
3//
4#include <utils/String8.h>
5#include <iostream>
6#include <errno.h>
7
8#include "CrunchCache.h"
9#include "FileFinder.h"
10#include "MockFileFinder.h"
11#include "CacheUpdater.h"
12#include "MockCacheUpdater.h"
13
14using namespace android;
15using std::cout;
16using std::endl;
17
18void expectEqual(int got, int expected, const char* desc) {
19    cout << "Checking " << desc << ": ";
20    cout << "Got " << got << ", expected " << expected << "...";
21    cout << ( (got == expected) ? "PASSED" : "FAILED") << endl;
22    errno += ((got == expected) ? 0 : 1);
23}
24
25int main() {
26
27    errno = 0;
28
29    String8 source("res");
30    String8 dest("res2");
31
32    // Create data for MockFileFinder to feed to the cache
33    KeyedVector<String8, time_t> sourceData;
34    // This shouldn't be updated
35    sourceData.add(String8("res/drawable/hello.png"),3);
36    // This should be updated
37    sourceData.add(String8("res/drawable/world.png"),5);
38    // This should cause make directory to be called
39    sourceData.add(String8("res/drawable-cool/hello.png"),3);
40
41    KeyedVector<String8, time_t> destData;
42    destData.add(String8("res2/drawable/hello.png"),3);
43    destData.add(String8("res2/drawable/world.png"),3);
44    // this should call delete
45    destData.add(String8("res2/drawable/dead.png"),3);
46
47    // Package up data and create mock file finder
48    KeyedVector<String8, KeyedVector<String8,time_t> > data;
49    data.add(source,sourceData);
50    data.add(dest,destData);
51    FileFinder* ff = new MockFileFinder(data);
52    CrunchCache cc(source,dest,ff);
53
54    MockCacheUpdater* mcu = new MockCacheUpdater();
55    CacheUpdater* cu(mcu);
56
57    cout << "Running Crunch...";
58    int result = cc.crunch(cu);
59    cout << ((result > 0) ? "PASSED" : "FAILED") << endl;
60    errno += ((result > 0) ? 0 : 1);
61
62    const int EXPECTED_RESULT = 2;
63    expectEqual(result, EXPECTED_RESULT, "number of files touched");
64
65    cout << "Checking calls to deleteFile and processImage:" << endl;
66    const int EXPECTED_DELETES = 1;
67    const int EXPECTED_PROCESSED = 2;
68    // Deletes
69    expectEqual(mcu->deleteCount, EXPECTED_DELETES, "deleteFile");
70    // processImage
71    expectEqual(mcu->processCount, EXPECTED_PROCESSED, "processImage");
72
73    const int EXPECTED_OVERWRITES = 3;
74    result = cc.crunch(cu, true);
75    expectEqual(result, EXPECTED_OVERWRITES, "number of files touched with overwrite");
76    \
77
78    if (errno == 0)
79        cout << "ALL TESTS PASSED!" << endl;
80    else
81        cout << errno << " TESTS FAILED" << endl;
82
83    delete ff;
84    delete cu;
85
86    // TESTS BELOW WILL GO AWAY SOON
87
88    String8 source2("ApiDemos/res");
89    String8 dest2("ApiDemos/res2");
90
91    FileFinder* sff = new SystemFileFinder();
92    CacheUpdater* scu = new SystemCacheUpdater();
93
94    CrunchCache scc(source2,dest2,sff);
95
96    scc.crunch(scu);
97}