1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 #include <getopt.h>
17 #include <stdio.h>
18 
19 #include <utils/String8.h>
20 
21 #include <UniquePtr.h>
22 
23 #include "GenericFactory.h"
24 #include "Log.h"
25 #include "Report.h"
26 #include "Settings.h"
27 #include "task/TaskGeneric.h"
28 #include "task/ModelBuilder.h"
29 
30 // For flushing report and log before exiting
31 class CleanupStatics {
32 public:
33 
CleanupStatics()34     CleanupStatics() {
35 
36     }
~CleanupStatics()37     ~CleanupStatics() {
38         Log::Finalize();
39         Report::Finalize();
40         // create zip file after log and report files are closed.
41         android::String8 reportDirPath =
42                 Settings::Instance()->getSetting(Settings::EREPORT_FILE).getPathDir();
43         android::String8 zipFilename = reportDirPath.getPathLeaf();
44         android::String8 command = android::String8::format("cd %s;zip -r ../%s.zip *",
45                 reportDirPath.string(), zipFilename.string());
46         fprintf(stderr, "\n\nexecuting %s\n", command.string());
47         if (system(command.string()) == -1) {
48             fprintf(stderr, "cannot create zip file with command %s\n", command.string());
49         }
50         Settings::Finalize();
51     }
52 };
53 
usage(char * bin)54 void usage(char* bin)
55 {
56     fprintf(stderr, "%s [-l log_level][-s serial] test_xml\n", bin);
57 }
main(int argc,char * argv[])58 int main(int argc, char *argv[])
59 {
60     if (argc < 2) {
61         fprintf(stderr, "%s [-l log_level][-s serial] test_xml\n", argv[0]);
62         return 1;
63     }
64     int logLevel = Log::ELogW;
65     char* serial = NULL;
66     int opt;
67     while ((opt = getopt(argc, argv, "l:s:")) != -1) {
68         switch (opt) {
69         case 'l':
70             logLevel = atoi(optarg);
71             break;
72         case 's':
73             serial = optarg;
74             break;
75         default:
76             usage(argv[0]);
77             return 1;
78         }
79     }
80     if (optind >= argc) {
81         usage(argv[0]);
82         return 1;
83     }
84 
85     android::String8 xmlFile(argv[optind]);
86 
87     android::String8 dirName;
88     if (!FileUtil::prepare(dirName)) {
89         fprintf(stderr, "cannot prepare report dir");
90         return 1;
91     }
92 
93     UniquePtr<CleanupStatics> staticStuffs(new CleanupStatics());
94     if (Settings::Instance() == NULL) {
95         fprintf(stderr, "caanot create Settings");
96         return 1;
97     }
98     if (serial != NULL) {
99         android::String8 strSerial(serial);
100         Settings::Instance()->addSetting(Settings::EADB, strSerial);
101     }
102     if (Log::Instance(dirName.string()) == NULL) {
103         fprintf(stderr, "cannot create Log");
104         return 1;
105     }
106     Log::Instance()->setLogLevel((Log::LogLevel)logLevel);
107     // Log can be used from here
108     if (Report::Instance(dirName.string()) == NULL) {
109 
110         LOGE("cannot create log");
111         return 1;
112     }
113 
114     GenericFactory factory;
115     ClientInterface* client = factory.createClientInterface();
116     if (client == NULL) {
117         fprintf(stderr, "cannot create ClientInterface");
118         return 1;
119     }
120     if (!client->init(Settings::Instance()->getSetting(Settings::EADB))) {
121         fprintf(stderr, "cannot init ClientInterface");
122         return 1;
123     }
124     android::String8 deviceInfo;
125     if (!client->getAudio()->getDeviceInfo(deviceInfo)) {
126         fprintf(stderr, "cannot get device info");
127         return 1;
128     }
129     delete client; // release so that it can be used in tests
130     Settings::Instance()->addSetting(Settings::EDEVICE_INFO, deviceInfo);
131 
132     ModelBuilder modelBuilder;
133     UniquePtr<TaskGeneric> topTask(modelBuilder.parseTestDescriptionXml(xmlFile));
134     if (topTask.get() == NULL) {
135         LOGE("Parsing of %x failed", xmlFile.string());
136         return 1;
137     }
138     Settings::Instance()->addSetting(Settings::ETEST_XML, xmlFile);
139     topTask->run();
140 
141     return 0;
142 }
143 
144