• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2011 The Android Open Source Project
3 //
4 #include <utils/Vector.h>
5 #include <utils/KeyedVector.h>
6 #include <iostream>
7 #include <cassert>
8 #include <utils/String8.h>
9 #include <utility>
10 
11 #include "DirectoryWalker.h"
12 #include "MockDirectoryWalker.h"
13 #include "FileFinder.h"
14 
15 using namespace android;
16 
17 using std::pair;
18 using std::cout;
19 using std::endl;
20 
21 
22 
main()23 int main()
24 {
25 
26     cout << "\n\n STARTING FILE FINDER TESTS" << endl;
27     String8 path("ApiDemos");
28 
29     // Storage to pass to findFiles()
30     KeyedVector<String8,time_t> testStorage;
31 
32     // Mock Directory Walker initialization. First data, then sdw
33     Vector< pair<String8,time_t> > data;
34     data.push( pair<String8,time_t>(String8("hello.png"),3) );
35     data.push( pair<String8,time_t>(String8("world.PNG"),3) );
36     data.push( pair<String8,time_t>(String8("foo.pNg"),3) );
37     // Neither of these should be found
38     data.push( pair<String8,time_t>(String8("hello.jpg"),3) );
39     data.push( pair<String8,time_t>(String8(".hidden.png"),3));
40 
41     DirectoryWalker* sdw = new StringDirectoryWalker(path,data);
42 
43     // Extensions to look for
44     Vector<String8> exts;
45     exts.push(String8(".png"));
46 
47     errno = 0;
48 
49     // Make sure we get a valid mock directory walker
50     // Make sure we finish without errors
51     cout << "Checking DirectoryWalker...";
52     assert(sdw != NULL);
53     cout << "PASSED" << endl;
54 
55     // Make sure we finish without errors
56     cout << "Running findFiles()...";
57     bool findStatus = FileFinder::findFiles(path,exts, testStorage, sdw);
58     assert(findStatus);
59     cout << "PASSED" << endl;
60 
61     const size_t SIZE_EXPECTED = 3;
62     // Check to make sure we have the right number of things in our storage
63     cout << "Running size comparison: Size is " << testStorage.size() << ", ";
64     cout << "Expected " << SIZE_EXPECTED << "...";
65     if(testStorage.size() == SIZE_EXPECTED)
66         cout << "PASSED" << endl;
67     else {
68         cout << "FAILED" << endl;
69         errno++;
70     }
71 
72     // Check to make sure that each of our found items has the right extension
73     cout << "Checking Returned Extensions...";
74     bool extsOkay = true;
75     String8 wrongExts;
76     for (size_t i = 0; i < SIZE_EXPECTED; ++i) {
77         String8 testExt(testStorage.keyAt(i).getPathExtension());
78         testExt.toLower();
79         if (testExt != ".png") {
80             wrongExts += testStorage.keyAt(i);
81             wrongExts += "\n";
82             extsOkay = false;
83         }
84     }
85     if (extsOkay)
86         cout << "PASSED" << endl;
87     else {
88         cout << "FAILED" << endl;
89         cout << "The following extensions didn't check out" << endl << wrongExts;
90     }
91 
92     // Clean up
93     delete sdw;
94 
95     if(errno == 0) {
96         cout << "ALL TESTS PASSED" << endl;
97     } else {
98         cout << errno << " TESTS FAILED" << endl;
99     }
100     return errno;
101 }