1 /*
2 * Copyright (c) 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 #define LOG_TAG "CustomUtdStoreTest"
16 #include <gtest/gtest.h>
17
18 #include <unistd.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 #include "logger.h"
23 #include "custom_utd_store.h"
24 #include "utd_common.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::UDMF;
28 using namespace OHOS;
29 namespace OHOS::Test {
30 constexpr const char* TEST_CFG_FILE = "/data/100-test/utd-adt.json";
31 constexpr const char* TEST_CFG_DIR = "/data/100-test/";
32
33 constexpr const char* TEST_DATA2 = "{\
34 \"CustomUTDs\": [{\
35 \"typeId\": \"com.example.utdtest.document\",\
36 \"belongingToTypes\": [\"com.example.utdtest2.document\"],\
37 \"FilenameExtensions\": [\".mydocument\", \".mydoc\"],\
38 \"mimeTypes\": [\"application/my-document\", \"application/my-doc\"],\
39 \"description\": \"My document.\",\
40 \"referenceURL\": \"http://www.mycompany.com/my-document.html\",\
41 \"iconFile\": \"resources/my-document.png\",\
42 \"installerBundles\":[\"com.example.utdtest\"],\
43 \"ownerBundle\":\"com.example.utdtest\"\
44 }, {\
45 \"typeId\": \"com.example.utdtest2.document\",\
46 \"belongingToTypes\": [\"general.object\"],\
47 \"FilenameExtensions\": [\".mydocument2\", \".mydoc2\"],\
48 \"mimeTypes\": [\"application/my-document2\", \"application/my-doc2\"],\
49 \"description\": \"My document 2.\",\
50 \"referenceURL\": \"hhttp://www.mycompany.com/my-document2.html\",\
51 \"iconFile\": \"resources/my-document2.png\",\
52 \"installerBundles\":[\"com.example.utdtest2\", \"com.example.utdtest\"],\
53 \"ownerBundle\":\"com.example.utdtest2\"\
54 }]}";
55
56 class CustomUtdStoreTest : public testing::Test {
57 public:
58 static void SetUpTestCase();
59 static void TearDownTestCase();
60 void SetUp() override;
61 void TearDown() override;
62 };
63
SetUpTestCase()64 void CustomUtdStoreTest::SetUpTestCase()
65 {
66 }
67
TearDownTestCase()68 void CustomUtdStoreTest::TearDownTestCase()
69 {
70 }
71
SetUp()72 void CustomUtdStoreTest::SetUp()
73 {
74 }
75
TearDown()76 void CustomUtdStoreTest::TearDown()
77 {
78 if (remove(TEST_CFG_FILE) == 0) {
79 rmdir(TEST_CFG_DIR);
80 LOG_INFO(UDMF_TEST, "Removed file success, %{public}s.", TEST_CFG_FILE);
81 } else {
82 LOG_INFO(UDMF_TEST, "Failed to remove the file., %{public}s.", TEST_CFG_FILE);
83 }
84 }
85
86 /**
87 * @tc.name: SaveTypeCfgs001
88 * @tc.desc: SaveTypeCfgs
89 * @tc.type: FUNC
90 */
91 HWTEST_F(CustomUtdStoreTest, SaveTypeCfgs001, TestSize.Level1)
92 {
93 LOG_INFO(UDMF_TEST, "SaveTypeCfgs001 begin.");
94 std::vector<TypeDescriptorCfg> typesCfg;
95 CustomUtdJsonParser parser;
96 parser.ParseStoredCustomUtdJson(TEST_DATA2, typesCfg);
97 auto status = CustomUtdStore::GetInstance().SaveTypeCfgs(typesCfg, TEST_CFG_FILE);
98 EXPECT_EQ(status, E_OK);
99
100 typesCfg.clear();
101 typesCfg = CustomUtdStore::GetInstance().GetTypeCfgs(TEST_CFG_FILE);
102 TypeDescriptorCfg type1 = *(typesCfg.begin());
103 EXPECT_EQ(type1.typeId, "com.example.utdtest.document");
104 EXPECT_EQ(*(type1.belongingToTypes.begin()), "com.example.utdtest2.document");
105 EXPECT_EQ(*(type1.filenameExtensions.begin()), ".mydocument");
106 EXPECT_EQ(*(type1.mimeTypes.begin()), "application/my-document");
107 EXPECT_EQ(type1.description, "My document.");
108 EXPECT_EQ(type1.referenceURL, "http://www.mycompany.com/my-document.html");
109 EXPECT_EQ(type1.iconFile, "resources/my-document.png");
110 EXPECT_EQ(*(type1.installerBundles.begin()), "com.example.utdtest");
111 EXPECT_EQ(type1.ownerBundle, "com.example.utdtest");
112
113 LOG_INFO(UDMF_TEST, "SaveTypeCfgs001 end.");
114 }
115 } // OHOS::Test