blob: 4ea9309e5e5b2994bc321084e06ae07b6605d72b [file] [log] [blame]
Florian Mayere8599d42019-10-14 14:58:15 +01001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "test/cts/utils.h"
18
19#include <stdlib.h>
20#include <sys/system_properties.h>
21
22#include "perfetto/base/logging.h"
23#include "test/gtest_and_gmock.h"
24
25namespace perfetto {
26namespace {
27
28// invokes |callback| once the target app is in the desired state
29void PollRunState(bool desired_run_state,
30 base::TestTaskRunner* task_runner,
31 const std::string& name,
32 std::function<void()> callback) {
33 bool app_running = IsAppRunning(name);
34 if (app_running == desired_run_state) {
35 callback();
36 return;
37 }
38 task_runner->PostDelayedTask(
39 [desired_run_state, task_runner, name, callback] {
40 PollRunState(desired_run_state, task_runner, name, std::move(callback));
41 },
42 /*delay_ms=*/5);
43}
44
45} // namespace
46
47bool IsDebuggableBuild() {
48 char buf[PROP_VALUE_MAX + 1] = {};
49 int ret = __system_property_get("ro.debuggable", buf);
50 PERFETTO_CHECK(ret >= 0);
51 std::string debuggable(buf);
52 if (debuggable == "1")
53 return true;
54 return false;
55}
56
57// note: cannot use gtest macros due to return type
58bool IsAppRunning(const std::string& name) {
59 std::string cmd = "pgrep -f " + name;
60 int retcode = system(cmd.c_str());
61 PERFETTO_CHECK(retcode >= 0);
62 int exit_status = WEXITSTATUS(retcode);
63 if (exit_status == 0)
64 return true;
65 if (exit_status == 1)
66 return false;
67 PERFETTO_FATAL("unexpected exit status from system(pgrep): %d", exit_status);
68}
69
Hector Dearmanec1de7d2020-02-07 16:17:40 +000070void WaitForProcess(const std::string& process,
71 const std::string& checkpoint_name,
72 base::TestTaskRunner* task_runner,
73 uint32_t delay_ms) {
Florian Mayere8599d42019-10-14 14:58:15 +010074 bool desired_run_state = true;
75 const auto checkpoint = task_runner->CreateCheckpoint(checkpoint_name);
76 task_runner->PostDelayedTask(
Hector Dearmanec1de7d2020-02-07 16:17:40 +000077 [desired_run_state, task_runner, process, checkpoint] {
78 PollRunState(desired_run_state, task_runner, process,
Florian Mayere8599d42019-10-14 14:58:15 +010079 std::move(checkpoint));
80 },
Hector Dearmanec1de7d2020-02-07 16:17:40 +000081 delay_ms);
82}
83
84void StartAppActivity(const std::string& app_name,
85 const std::string& activity_name,
86 const std::string& checkpoint_name,
87 base::TestTaskRunner* task_runner,
88 uint32_t delay_ms) {
89 std::string start_cmd = "am start " + app_name + "/." + activity_name;
90 int status = system(start_cmd.c_str());
91 ASSERT_TRUE(status >= 0 && WEXITSTATUS(status) == 0) << "status: " << status;
92 WaitForProcess(app_name, checkpoint_name, task_runner, delay_ms);
Florian Mayere8599d42019-10-14 14:58:15 +010093}
94
95void StopApp(const std::string& app_name,
96 const std::string& checkpoint_name,
97 base::TestTaskRunner* task_runner) {
98 std::string stop_cmd = "am force-stop " + app_name;
99 int status = system(stop_cmd.c_str());
100 ASSERT_TRUE(status >= 0 && WEXITSTATUS(status) == 0) << "status: " << status;
101
102 bool desired_run_state = false;
103 auto checkpoint = task_runner->CreateCheckpoint(checkpoint_name);
104 task_runner->PostTask([desired_run_state, task_runner, app_name, checkpoint] {
105 PollRunState(desired_run_state, task_runner, app_name,
106 std::move(checkpoint));
107 });
108}
109
110void StopApp(std::string app_name) {
111 std::string stop_cmd = "am force-stop " + app_name;
112 system(stop_cmd.c_str());
113}
114
115} // namespace perfetto