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