1 /*
2 * Copyright (c) 2022-2023 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 "cJSON.h"
19 #include "clip/clip_plugin.h"
20 #include "serializable/serializable.h"
21
22 namespace OHOS::MiscServices {
23 using namespace testing::ext;
24 class ClipPluginTest : public testing::Test {
25 public:
26 static void SetUpTestCase(void);
27 static void TearDownTestCase(void);
28 void SetUp();
29 void TearDown();
30 };
31
SetUpTestCase(void)32 void ClipPluginTest::SetUpTestCase(void) { }
33
TearDownTestCase(void)34 void ClipPluginTest::TearDownTestCase(void) { }
35
SetUp(void)36 void ClipPluginTest::SetUp(void) { }
37
TearDown(void)38 void ClipPluginTest::TearDown(void) { }
39
40 class CustomClipPlugin : public ClipPlugin {
41 public:
SetPasteData(const GlobalEvent & event,const std::vector<uint8_t> & data)42 int32_t SetPasteData(const GlobalEvent &event, const std::vector<uint8_t> &data) override
43 {
44 (void)event;
45 (void)data;
46 return 0;
47 }
48
GetPasteData(const GlobalEvent & event,std::vector<uint8_t> & data)49 std::pair<int32_t, int32_t> GetPasteData(const GlobalEvent &event, std::vector<uint8_t> &data) override
50 {
51 (void)event;
52 (void)data;
53 return std::make_pair(0, 0);
54 }
55 };
56
57 /**
58 * @tc.name: MarshalTest001
59 * @tc.desc: Marshal.
60 * @tc.type: FUNC
61 * @tc.require:
62 * @tc.author:
63 */
64 HWTEST_F(ClipPluginTest, MarshalTest001, TestSize.Level0)
65 {
66 std::string PLUGIN_NAME_VAL = "distributed_clip";
__anonda38237e0102(ClipPlugin *plugin) 67 auto release = [&PLUGIN_NAME_VAL, this](ClipPlugin *plugin) {
68 ClipPlugin::DestroyPlugin(PLUGIN_NAME_VAL, plugin);
69 };
70 std::shared_ptr<ClipPlugin> clipPlugin;
71 clipPlugin = std::shared_ptr<ClipPlugin>(ClipPlugin::CreatePlugin(PLUGIN_NAME_VAL), release);
72 DistributedData::Serializable::json node;
73 ClipPlugin::GlobalEvent globalEvent;
74 std::string networkId = "networkId";
75 int32_t result = clipPlugin->PublishServiceState(networkId, ClipPlugin::ServiceStatus::CONNECT_SUCC);
76 ASSERT_TRUE(globalEvent.Marshal(node));
77 }
78
79 /**
80 * @tc.name: UnmarshalTest001
81 * @tc.desc: Unmarshal.
82 * @tc.type: FUNC
83 * @tc.require:
84 * @tc.author:
85 */
86 HWTEST_F(ClipPluginTest, UnmarshalTest001, TestSize.Level0)
87 {
88 std::string str = R"({
89 {"version", 1},
90 {"frameNum", 100},
91 {"user", "testUser"},
92 {"seqId", 12345},
93 {"expiration", 1000},
94 {"status", "active"},
95 {"deviceId", "123456"},
96 {"account", "testAcount"},
97 {"dataType", "image"},
98 {"syncTime", 2000},
99 })";
100 DistributedData::Serializable::json node = cJSON_Parse(str.c_str());
101 ClipPlugin::GlobalEvent globalEvent;
102 ASSERT_FALSE(globalEvent.Unmarshal(node));
103 cJSON_Delete(node);
104 }
105
106 /**
107 * @tc.name: PublishServiceStateTest
108 * @tc.desc: PublishServiceState.
109 * @tc.type: FUNC
110 * @tc.require:
111 * @tc.author:
112 */
113 HWTEST_F(ClipPluginTest, PublishServiceStateTest, TestSize.Level0)
114 {
115 CustomClipPlugin clipPlugin;
116 std::string networkId = "testNetworkId";
117 int32_t result = clipPlugin.PublishServiceState(networkId, ClipPlugin::ServiceStatus::CONNECT_SUCC);
118 ASSERT_EQ(0, result);
119 }
120
121 /**
122 * @tc.name: RegCreatorTest001
123 * @tc.desc: RegCreator.
124 * @tc.type: FUNC
125 * @tc.require:
126 * @tc.author:
127 */
128 HWTEST_F(ClipPluginTest, RegCreatorTestTest001, TestSize.Level0)
129 {
130 std::string name = "testFactory";
131 ClipPlugin::Factory *factory = nullptr;
132 bool result = ClipPlugin::RegCreator(name, factory);
133 EXPECT_FALSE(result);
134 }
135
136 /**
137 * @tc.name: DestroyPluginTest001
138 * @tc.desc: DestroyPlugin.
139 * @tc.type: FUNC
140 * @tc.require:
141 * @tc.author:
142 */
143 HWTEST_F(ClipPluginTest, DestroyPluginTest001, TestSize.Level0)
144 {
145 std::string name = "testPlugin";
146 ClipPlugin *plugin = nullptr;
147 EXPECT_FALSE(ClipPlugin::DestroyPlugin(name, plugin));
148 }
149 } // namespace OHOS::MiscServices
150