1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "tool_system_test.h"
17
18 #include <gtest/gtest.h>
19 #include <thread>
20 #include "ability_command.h"
21 #include "bundle_command.h"
22
23 using namespace OHOS::AAFwk;
24 using namespace OHOS::AppExecFwk;
25
ExecuteCommand(const std::string & command)26 std::string ToolSystemTest::ExecuteCommand(const std::string &command)
27 {
28 std::string result = "";
29 FILE *file = popen(command.c_str(), "r");
30
31 // wait for services
32 std::this_thread::sleep_for(std::chrono::seconds(TIME_DELAY_FOR_SERVICES));
33
34 if (file != nullptr) {
35 char commandResult[1024] = {0};
36
37 fgets(commandResult, sizeof(commandResult), file);
38
39 pclose(file);
40 file = nullptr;
41
42 result.append(commandResult);
43 }
44
45 return result;
46 }
47
InstallBundle(const std::string & bundlePath,const bool checkResult)48 void ToolSystemTest::InstallBundle(const std::string &bundlePath, const bool checkResult)
49 {
50 // install a bundle
51 std::string command = "bm install -p " + bundlePath;
52 std::string commandResult = ExecuteCommand(command);
53
54 if (checkResult) {
55 EXPECT_EQ(commandResult, STRING_INSTALL_BUNDLE_OK + "\n");
56 }
57 }
58
UninstallBundle(const std::string & bundleName,const bool checkResult)59 void ToolSystemTest::UninstallBundle(const std::string &bundleName, const bool checkResult)
60 {
61 // uninstall a bundle
62 std::string command = "bm uninstall -n " + bundleName;
63 std::string commandResult = ExecuteCommand(command);
64
65 if (checkResult) {
66 EXPECT_EQ(commandResult, STRING_UNINSTALL_BUNDLE_OK + "\n");
67 }
68 }
69
StartAbility(const std::string & device,const std::string & abilityName,const std::string & bundleName,const bool checkResult)70 void ToolSystemTest::StartAbility(
71 const std::string &device, const std::string &abilityName, const std::string &bundleName, const bool checkResult)
72 {
73 // start an ability
74 std::string command = "aa start -d " + device + " -a " + abilityName + " -b " + bundleName;
75 std::string commandResult = ExecuteCommand(command);
76
77 if (checkResult) {
78 EXPECT_EQ(commandResult, STRING_START_ABILITY_OK + "\n");
79 }
80 }
81