1 /* 2 * Copyright (c) 2025 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 <gtest/gtest.h> 17 18 #include "ability_command.h" 19 #include "ability_manager_client.h" 20 #include "ability_manager_interface.h" 21 #include "hilog_tag_wrapper.h" 22 #include "mock_ability_manager_stub.h" 23 24 using namespace testing::ext; 25 using namespace OHOS; 26 using namespace OHOS::AAFwk; 27 28 namespace { 29 const std::string STRING_CLASS_NAME = "ohos.acts.aafwk.ability.test.ConstructorTest#testDataAbilityOtherFunction0010"; 30 const std::string STRING_USER_TEST_RUNNER = "JSUserTestRunner"; 31 const std::string STRING_PACKAGE_NAME = "com.example.myapplication"; 32 const std::string STRING_PACKAGE_NAME1 = "com.example.myapplication1"; 33 const std::string STRING_BUNDLE_NAME = "com.example.myapplication"; 34 const std::string STRING_MODULE_NAME = "com.example.myapplication.MyApplication"; 35 const std::string CLASS = "class"; 36 const std::string UNITTEST = "unittest"; 37 const std::string UNITTEST1 = "unittest1"; 38 const std::string TIME = "20"; 39 const std::string BLACK_ACTION_SELECT_DATA = "ohos.want.action.select"; 40 } // namespace 41 42 class AbilityCommandThirdTest : public ::testing::Test { 43 public: 44 static void SetUpTestCase(); 45 static void TearDownTestCase(); 46 void SetUp() override; 47 void TearDown() override; 48 49 void MakeMockObjects() const; 50 51 std::string cmd_ = "test"; 52 }; 53 SetUpTestCase()54 void AbilityCommandThirdTest::SetUpTestCase() 55 {} 56 TearDownTestCase()57 void AbilityCommandThirdTest::TearDownTestCase() 58 {} 59 SetUp()60 void AbilityCommandThirdTest::SetUp() 61 { 62 // reset optind to 0 63 optind = 0; 64 65 // make mock objects 66 MakeMockObjects(); 67 } 68 TearDown()69 void AbilityCommandThirdTest::TearDown() 70 {} 71 MakeMockObjects() const72 void AbilityCommandThirdTest::MakeMockObjects() const 73 { 74 // mock a stub 75 auto managerStubPtr = sptr<IAbilityManager>(new MockAbilityManagerStub()); 76 77 // set the mock stub 78 auto managerClientPtr = AbilityManagerClient::GetInstance(); 79 managerClientPtr->proxy_ = managerStubPtr; 80 } 81 82 /** 83 * @tc.number: Ability_Command_Second_Test_0100 84 * @tc.name: ExecCommand 85 * @tc.desc: Verify the "aa test -W" command. 86 */ 87 HWTEST_F(AbilityCommandThirdTest, Ability_Command_Third_Test_0100, Function | MediumTest | Level1) 88 { 89 TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Third_Test_0100 is called"); 90 char* argv[] = { 91 (char*)TOOL_NAME.c_str(), 92 (char*)"-U", 93 (char*)" ", 94 95 }; 96 int argc = sizeof(argv) / sizeof(argv[0]) - 1; 97 AbilityManagerShellCommand cmd(argc, argv); 98 Want want; 99 std::string windowMode; 100 EXPECT_EQ(cmd.MakeWantFromCmd(want, windowMode), OHOS::ERR_INVALID_VALUE); 101 want.SetElementName("com.example.test", "MainAbility"); 102 EXPECT_EQ(cmd.IsImplicitStartAction(want), false); 103 want.SetElementName("com.example.test", ""); 104 want.SetParam(AAFwk::SCREEN_MODE_KEY, ScreenMode::JUMP_SCREEN_MODE); 105 EXPECT_EQ(cmd.IsImplicitStartAction(want), false); 106 want.SetParam(AAFwk::SCREEN_MODE_KEY, ScreenMode::IDLE_SCREEN_MODE); 107 want.SetAction(STRING_PACKAGE_NAME1); 108 EXPECT_EQ(cmd.IsImplicitStartAction(want), true); 109 want.SetParam(AAFwk::SCREEN_MODE_KEY, ScreenMode::IDLE_SCREEN_MODE); 110 want.SetAction(BLACK_ACTION_SELECT_DATA); 111 EXPECT_EQ(cmd.IsImplicitStartAction(want), false); 112 } 113 114 /** 115 * @tc.number: Ability_Command_Third_Test_0200 116 * @tc.name: ExecCommand 117 * @tc.desc: Verify the "aa test -W" command. 118 */ 119 HWTEST_F(AbilityCommandThirdTest, Ability_Command_Third_Test_0200, Function | MediumTest | Level1) 120 { 121 TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Third_Test_0200 is called"); 122 char* argv[] = { 123 (char*)TOOL_NAME.c_str(), 124 (char*)"-W", 125 (char*)" ", 126 127 }; 128 int argc = sizeof(argv) / sizeof(argv[0]) - 1; 129 AbilityManagerShellCommand cmd(argc, argv); 130 Want want; 131 std::string windowMode; 132 EXPECT_EQ(cmd.MakeWantFromCmd(want, windowMode), OHOS::ERR_OK); 133 want.SetElementName("com.example.test", ""); 134 want.SetParam(AAFwk::SCREEN_MODE_KEY, ScreenMode::IDLE_SCREEN_MODE); 135 want.SetAction(STRING_PACKAGE_NAME1); 136 EXPECT_EQ(cmd.StartAbilityWithWait(want), OHOS::ERR_OK); 137 want.SetElementName("com.example.test", "MainAbility"); 138 EXPECT_EQ(cmd.IsImplicitStartAction(want), false); 139 EXPECT_EQ(cmd.StartAbilityWithWait(want), OHOS::ERR_OK); 140 } 141 142 /** 143 * @tc.number: Ability_Command_Third_Test_0300 144 * @tc.name: ExecCommand 145 * @tc.desc: Verify the "aa test -W" command. 146 */ 147 HWTEST_F(AbilityCommandThirdTest, Ability_Command_Third_Test_0300, Function | MediumTest | Level1) 148 { 149 TAG_LOGI(AAFwkTag::TEST, "Ability_Command_Third_Test_0300 is called"); 150 char* argv[] = { 151 (char*)TOOL_NAME.c_str(), 152 (char*)"-W", 153 (char*)" ", 154 155 }; 156 int argc = sizeof(argv) / sizeof(argv[0]) - 1; 157 AbilityManagerShellCommand cmd(argc, argv); 158 Want want; 159 std::string windowMode; 160 EXPECT_EQ(cmd.MakeWantFromCmd(want, windowMode), OHOS::ERR_OK); 161 AbilityStartWithWaitObserverData data; 162 data.reason = 0; 163 cmd.FormatOutputForWithWait(want, data); 164 data.reason = 1; 165 cmd.FormatOutputForWithWait(want, data); 166 data.reason = 2; 167 cmd.FormatOutputForWithWait(want, data); 168 EXPECT_EQ(cmd.startAbilityWithWaitFlag_, true); 169 }