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 while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
37 result.append(commandResult);
38 }
39 pclose(file);
40 file = nullptr;
41 }
42
43 return result;
44 }
45
InstallBundle(const std::string & bundlePath,const bool checkResult)46 void ToolSystemTest::InstallBundle(const std::string &bundlePath, const bool checkResult)
47 {
48 // install a bundle
49 std::string command = "bm install -p " + bundlePath;
50 std::string commandResult = ExecuteCommand(command);
51
52 if (checkResult) {
53 EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_INSTALL_BUNDLE_OK + "\n");
54 }
55 }
56
UninstallBundle(const std::string & bundleName,const bool checkResult)57 void ToolSystemTest::UninstallBundle(const std::string &bundleName, const bool checkResult)
58 {
59 // uninstall a bundle
60 std::string command = "bm uninstall -n " + bundleName;
61 std::string commandResult = ExecuteCommand(command);
62
63 if (checkResult) {
64 EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_UNINSTALL_BUNDLE_OK + "\n");
65 }
66 }
67
StartAbility(const std::string & device,const std::string & abilityName,const std::string & bundleName,const bool checkResult)68 void ToolSystemTest::StartAbility(
69 const std::string &device, const std::string &abilityName, const std::string &bundleName, const bool checkResult)
70 {
71 // start an ability
72 std::string command = "aa start -d " + device + " -a " + abilityName + " -b " + bundleName;
73 std::string commandResult = ExecuteCommand(command);
74
75 if (checkResult) {
76 EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_START_ABILITY_OK + "\n");
77 }
78 }
79
IsSubSequence(std::string str,std::string subStr)80 bool ToolSystemTest::IsSubSequence(std::string str, std::string subStr)
81 {
82 return str.find(subStr) != std::string::npos;
83 }