1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker #include "command.h"
22*288bf522SAndroid Build Coastguard Worker #include "get_test_data.h"
23*288bf522SAndroid Build Coastguard Worker #include "record_file.h"
24*288bf522SAndroid Build Coastguard Worker #include "test_util.h"
25*288bf522SAndroid Build Coastguard Worker #include "utils.h"
26*288bf522SAndroid Build Coastguard Worker
27*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
28*288bf522SAndroid Build Coastguard Worker
29*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
30*288bf522SAndroid Build Coastguard Worker
WaitUntilAppExit(const std::string & package_name)31*288bf522SAndroid Build Coastguard Worker static bool WaitUntilAppExit(const std::string& package_name) {
32*288bf522SAndroid Build Coastguard Worker while (true) {
33*288bf522SAndroid Build Coastguard Worker std::unique_ptr<FILE, decltype(&pclose)> fp(popen("ps -e", "re"), pclose);
34*288bf522SAndroid Build Coastguard Worker if (!fp) {
35*288bf522SAndroid Build Coastguard Worker return false;
36*288bf522SAndroid Build Coastguard Worker }
37*288bf522SAndroid Build Coastguard Worker std::string s;
38*288bf522SAndroid Build Coastguard Worker if (!android::base::ReadFdToString(fileno(fp.get()), &s)) {
39*288bf522SAndroid Build Coastguard Worker return false;
40*288bf522SAndroid Build Coastguard Worker }
41*288bf522SAndroid Build Coastguard Worker if (s.find(package_name) == std::string::npos) {
42*288bf522SAndroid Build Coastguard Worker break;
43*288bf522SAndroid Build Coastguard Worker }
44*288bf522SAndroid Build Coastguard Worker sleep(1);
45*288bf522SAndroid Build Coastguard Worker }
46*288bf522SAndroid Build Coastguard Worker return true;
47*288bf522SAndroid Build Coastguard Worker }
48*288bf522SAndroid Build Coastguard Worker
CheckPerfDataFile(const std::string & filename)49*288bf522SAndroid Build Coastguard Worker static void CheckPerfDataFile(const std::string& filename) {
50*288bf522SAndroid Build Coastguard Worker auto reader = RecordFileReader::CreateInstance(filename);
51*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(reader);
52*288bf522SAndroid Build Coastguard Worker bool has_sample = false;
53*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
54*288bf522SAndroid Build Coastguard Worker if (r->type() == PERF_RECORD_SAMPLE) {
55*288bf522SAndroid Build Coastguard Worker has_sample = true;
56*288bf522SAndroid Build Coastguard Worker }
57*288bf522SAndroid Build Coastguard Worker return true;
58*288bf522SAndroid Build Coastguard Worker }));
59*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(has_sample);
60*288bf522SAndroid Build Coastguard Worker }
61*288bf522SAndroid Build Coastguard Worker
RecordApp(const std::string & package_name,const std::string & apk_path)62*288bf522SAndroid Build Coastguard Worker static void RecordApp(const std::string& package_name, const std::string& apk_path) {
63*288bf522SAndroid Build Coastguard Worker // 1. Install apk.
64*288bf522SAndroid Build Coastguard Worker AppHelper app_helper;
65*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(app_helper.InstallApk(apk_path, package_name));
66*288bf522SAndroid Build Coastguard Worker
67*288bf522SAndroid Build Coastguard Worker // 2. Prepare recording.
68*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(CreateCommandInstance("api-prepare")->Run({"--app", package_name, "--days", "1"}));
69*288bf522SAndroid Build Coastguard Worker
70*288bf522SAndroid Build Coastguard Worker // 3. Start the app.
71*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(app_helper.StartApp("am start " + package_name + "/.MainActivity"));
72*288bf522SAndroid Build Coastguard Worker
73*288bf522SAndroid Build Coastguard Worker // 4. Wait until the app stops.
74*288bf522SAndroid Build Coastguard Worker sleep(3);
75*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(WaitUntilAppExit(package_name));
76*288bf522SAndroid Build Coastguard Worker
77*288bf522SAndroid Build Coastguard Worker // 5. Collect perf.data.
78*288bf522SAndroid Build Coastguard Worker SetRunInAppToolForTesting(true, true);
79*288bf522SAndroid Build Coastguard Worker TemporaryFile tmpfile;
80*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(
81*288bf522SAndroid Build Coastguard Worker CreateCommandInstance("api-collect")->Run({"--app", package_name, "-o", tmpfile.path}));
82*288bf522SAndroid Build Coastguard Worker
83*288bf522SAndroid Build Coastguard Worker // 6. Verify perf.data.
84*288bf522SAndroid Build Coastguard Worker TemporaryDir tmpdir;
85*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(Workload::RunCmd({"unzip", "-d", tmpdir.path, tmpfile.path}));
86*288bf522SAndroid Build Coastguard Worker for (const std::string& filename : GetEntriesInDir(tmpdir.path)) {
87*288bf522SAndroid Build Coastguard Worker CheckPerfDataFile(std::string(tmpdir.path) + "/" + filename);
88*288bf522SAndroid Build Coastguard Worker }
89*288bf522SAndroid Build Coastguard Worker }
90*288bf522SAndroid Build Coastguard Worker
91*288bf522SAndroid Build Coastguard Worker #endif // defined(__ANDROID__)
92*288bf522SAndroid Build Coastguard Worker
93*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_api,java_app)94*288bf522SAndroid Build Coastguard Worker TEST(cmd_api, java_app) {
95*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
96*288bf522SAndroid Build Coastguard Worker RecordApp("simpleperf.demo.java_api", GetTestData("java_api.apk"));
97*288bf522SAndroid Build Coastguard Worker #else
98*288bf522SAndroid Build Coastguard Worker GTEST_LOG_(INFO) << "This test tests recording apps on Android.";
99*288bf522SAndroid Build Coastguard Worker #endif
100*288bf522SAndroid Build Coastguard Worker }
101*288bf522SAndroid Build Coastguard Worker
102*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(cmd_api,native_app)103*288bf522SAndroid Build Coastguard Worker TEST(cmd_api, native_app) {
104*288bf522SAndroid Build Coastguard Worker #if defined(__ANDROID__)
105*288bf522SAndroid Build Coastguard Worker RecordApp("simpleperf.demo.cpp_api", GetTestData("cpp_api.apk"));
106*288bf522SAndroid Build Coastguard Worker #else
107*288bf522SAndroid Build Coastguard Worker GTEST_LOG_(INFO) << "This test tests recording apps on Android.";
108*288bf522SAndroid Build Coastguard Worker #endif
109*288bf522SAndroid Build Coastguard Worker }
110