• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 #include "mock_clip_board.h"
18 
19 #include "base/utils/utils.h"
20 #include "core/common/clipboard/clipboard.h"
21 #include "core/common/clipboard/clipboard_interface.h"
22 #include "core/common/clipboard/clipboard_proxy.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Ace {
28 class ClipboardTest : public testing::Test {};
29 
30 /**
31  * @tc.name: CastToClipboardTestTest001
32  * @tc.desc: Test cast to clipboard
33  * @tc.type: FUNC
34  */
35 HWTEST_F(ClipboardTest, CastToClipboardTestTest001, TestSize.Level1)
36 {
37     /**
38      * @tc.steps: step1. call GetInstance first.
39      * @tc.expected: step1. The return value is not null.
40      */
41     ClipboardProxy* firstTestGetInstance = ClipboardProxy::GetInstance();
42     ASSERT_NE(firstTestGetInstance, nullptr);
43 
44     /**
45      * @tc.steps: step2. call GetInstance second.
46      * @tc.expected: step2. The return value is same with firstTestGetInstance.
47      */
48     ClipboardProxy* secondTestGetInstance = ClipboardProxy::GetInstance();
49     EXPECT_EQ(firstTestGetInstance, secondTestGetInstance);
50 }
51 
52 /**
53  * @tc.name: CastToClipboardTestTest001
54  * @tc.desc: Test cast to clipboard
55  * @tc.type: FUNC
56  */
57 HWTEST_F(ClipboardTest, CastToClipboardTestTest002, TestSize.Level1)
58 {
59     /**
60      * @tc.steps: step1. set delegate to nullptr.
61      * @tc.expected: step1. The return clipboard is null.
62      */
63     ClipboardProxy::GetInstance()->SetDelegate(nullptr);
64 
65     RefPtr<TaskExecutor> taskExecutor;
66     RefPtr<Clipboard> clipboard = ClipboardProxy::GetInstance()->GetClipboard(taskExecutor);
67     EXPECT_NE(clipboard, nullptr);
68 }
69 
70 std::string g_data;
ClipboardcallBack(const std::string & data)71 void ClipboardcallBack(const std::string& data)
72 {
73     g_data = data;
74 }
75 
76 /**
77  * @tc.name: CastToClipboardTestTest001
78  * @tc.desc: Test cast to clipboard
79  * @tc.type: FUNC
80  */
81 HWTEST_F(ClipboardTest, CastToClipboardTestTest003, TestSize.Level1)
82 {
83     /**
84      * @tc.steps: step1. set delegate to mock object.
85      * @tc.expected: step1. The return clipboard is ok and the GetData fuction is ok.
86      */
87     ClipboardProxy::GetInstance()->SetDelegate(std::make_unique<MockClipboardProxyImpl>());
88     RefPtr<TaskExecutor> taskExecutor;
89     RefPtr<Clipboard> clipboard = ClipboardProxy::GetInstance()->GetClipboard(taskExecutor);
90     ASSERT_NE(clipboard, nullptr);
91 
92     clipboard->GetData(ClipboardcallBack);
93     EXPECT_EQ(g_data, "");
94 }
95 } // namespace OHOS::Ace
96