1 /*
2  * Copyright (c) 2022-2024 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 <iostream>
19 #include <thread>
20 #include <unistd.h>
21 
22 #include "hilog_tag_wrapper.h"
23 #include "system_time.h"
24 
25 #define private public
26 #include "test_observer.h"
27 #undef protected
28 #define private public
29 #include "ability_manager_client.h"
30 #undef private
31 #include "ability_manager_interface.h"
32 
33 using namespace testing::ext;
34 using namespace OHOS;
35 using namespace OHOS::AAFwk;
36 
37 namespace {
38 const std::string MSG = "observer msg";
39 const int RESULT_CODE = 20;
40 const int64_t TIMEOUT = 50;
41 const std::string CMD = "ls -l";
42 }  // namespace
43 
44 class TestObserverTest : public ::testing::Test {
45 public:
46     static void SetUpTestCase();
47     static void TearDownTestCase();
48     void SetUp() override;
49     void TearDown() override;
50     void MakeMockObjects() const;
51     static void thfunc(TestObserver& observer);
52 };
53 
SetUpTestCase()54 void TestObserverTest::SetUpTestCase()
55 {}
56 
TearDownTestCase()57 void TestObserverTest::TearDownTestCase()
58 {}
59 
SetUp()60 void TestObserverTest::SetUp()
61 {}
62 
TearDown()63 void TestObserverTest::TearDown()
64 {}
65 
thfunc(TestObserver & observer)66 void TestObserverTest::thfunc(TestObserver& observer)
67 {
68     TAG_LOGI(AAFwkTag::TEST, "thfunc");
69     sleep(1);
70     TAG_LOGI(AAFwkTag::TEST, "after sleep 1s thfunc");
71     observer.isFinished_ = true;
72 }
73 
74 /**
75  * @tc.number: Test_Observer_Test_0100
76  * @tc.name: TestFinished
77  * @tc.desc: Verify the TestFinished.
78  */
79 HWTEST_F(TestObserverTest, Test_Observer_Test_0100, Function | MediumTest | Level1)
80 {
81     TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Test_0100 is called");
82     TestObserver observer;
83     observer.TestFinished(MSG.c_str(), RESULT_CODE);
84     EXPECT_TRUE(observer.isFinished_);
85 }
86 
87 /**
88  * @tc.number: Test_Observer_Test_0200
89  * @tc.name: ExecuteShellCommand
90  * @tc.desc: Verify the ExecuteShellCommand.
91  */
92 HWTEST_F(TestObserverTest, Test_Observer_Test_0200, Function | MediumTest | Level1)
93 {
94     TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Test_0200 is called");
95     TestObserver observer;
96     EXPECT_EQ(observer.ExecuteShellCommand(CMD.c_str(), TIMEOUT).stdResult.size(), 0);
97 }
98 
99 /**
100  * @tc.number: Test_Observer_Test_0300
101  * @tc.name: WaitForFinish
102  * @tc.desc: Verify the WaitForFinish.
103  */
104 HWTEST_F(TestObserverTest, Test_Observer_Test_0300, Function | MediumTest | Level1)
105 {
106     TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Test_0300 is called");
107     TestObserver observer;
108     std::thread th(&TestObserverTest::thfunc, std::ref(observer));
109     bool ret = observer.WaitForFinish(5000);
110     th.join();
111     EXPECT_TRUE(ret);
112 }
113 
114 /**
115  * @tc.number: Test_Observer_Test_0400
116  * @tc.name: WaitForFinish
117  * @tc.desc: Verify the WaitForFinish return false.
118  */
119 HWTEST_F(TestObserverTest, Test_Observer_Test_0400, Function | MediumTest | Level1)
120 {
121     TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Test_0400 is called");
122     TestObserver observer;
123     observer.isFinished_ = false;
124     bool ret = observer.WaitForFinish(1);
125     EXPECT_FALSE(ret);
126 }
127 
128 /**
129  * @tc.number: Test_Observer_Test_0500
130  * @tc.name: TestStatus
131  * @tc.desc: Verify the TestStatus.
132  */
133 HWTEST_F(TestObserverTest, Test_Observer_Test_0500, Function | MediumTest | Level1)
134 {
135     TAG_LOGI(AAFwkTag::TEST, "Test_Observer_Test_0500 is called");
136     std::shared_ptr<TestObserver> observer = std::make_shared<TestObserver>();
137     ASSERT_NE(observer, nullptr);
138     observer->TestStatus(MSG.c_str(), RESULT_CODE);
139 }
140