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  
22  using namespace OHOS::AAFwk;
23  using namespace OHOS::AppExecFwk;
24  
25  namespace {
26  const std::string STRING_INSTALL_BUNDLE_OK = "install bundle successfully.";
27  const std::string STRING_UNINSTALL_BUNDLE_OK = "uninstall bundle successfully.";
28  }  // namespace
29  
ExecuteCommand(const std::string & command)30  std::string ToolSystemTest::ExecuteCommand(const std::string& command)
31  {
32      std::string result = "";
33      FILE* file = popen(command.c_str(), "r");
34  
35      // wait for services
36      std::this_thread::sleep_for(std::chrono::seconds(TIME_DELAY_FOR_SERVICES));
37  
38      if (file != nullptr) {
39          char commandResult[1024] = { 0 };
40          while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
41              result.append(commandResult);
42          }
43          pclose(file);
44          file = nullptr;
45      }
46  
47      return result;
48  }
49  
InstallBundle(const std::string & bundlePath,const bool checkResult)50  void ToolSystemTest::InstallBundle(const std::string& bundlePath, const bool checkResult)
51  {
52      // install a bundle
53      std::string command = "bm install -p " + bundlePath;
54      std::string commandResult = ExecuteCommand(command);
55  
56      if (checkResult) {
57          EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_INSTALL_BUNDLE_OK + "\n");
58      }
59  }
60  
UninstallBundle(const std::string & bundleName,const bool checkResult)61  void ToolSystemTest::UninstallBundle(const std::string& bundleName, const bool checkResult)
62  {
63      // uninstall a bundle
64      std::string command = "bm uninstall -n " + bundleName;
65      std::string commandResult = ExecuteCommand(command);
66  
67      if (checkResult) {
68          EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_UNINSTALL_BUNDLE_OK + "\n");
69      }
70  }
71  
StartAbility(const std::string & device,const std::string & abilityName,const std::string & bundleName,const bool checkResult)72  void ToolSystemTest::StartAbility(
73      const std::string& device, const std::string& abilityName, const std::string& bundleName, const bool checkResult)
74  {
75      // start an ability
76      std::string command = "aa start -d " + device + " -a " + abilityName + " -b " + bundleName;
77      std::string commandResult = ExecuteCommand(command);
78  
79      if (checkResult) {
80          EXPECT_PRED2(ToolSystemTest::IsSubSequence, commandResult, STRING_START_ABILITY_OK + "\n");
81      }
82  }
83  
IsSubSequence(const std::string & str,const std::string & subStr)84  bool ToolSystemTest::IsSubSequence(const std::string& str, const std::string& subStr)
85  {
86      return str.find(subStr) != std::string::npos;
87  }
88