1 /*
2 * Copyright (c) 2022 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 "base/memory/ace_type.h"
19 #include "bridge/common/manifest/manifest_parser.h"
20 #include "core/common/ace_engine.h"
21 #include "core/common/test/mock/mock_container.h"
22 #include "core/components/test/mock/mock_resource_adapter.h"
23 #include "core/components_ng/test/mock/theme/mock_theme_manager.h"
24 #include "core/pipeline_ng/test/mock/mock_interface.h"
25 #include "core/pipeline_ng/test/mock/mock_pipeline_base.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS::Ace::Framework {
31 namespace {
32 constexpr int32_t MANIFEST_WINDOW_WIDTH = 800;
33 constexpr int32_t DEFAULT_DESIGN_WIDTH = 720;
34 constexpr int32_t MANIFEST_WIDGETS_SIZE = 1;
35 constexpr int32_t MANIFEST_VERSION_CODE = 1000000;
36 constexpr int32_t MANIFEST_MINIPLATFORM_VERSION = 1;
37 const std::string MANIFEST_APP_NAME = "test";
38 const std::string MANIFEST_APP_NAMES = "$string:color";
39 const std::string MANIFEST_VERSION_NAME = "1.0";
40 const std::string MANIFEST_LOG_LEVEL = "level0";
41 const std::string MANIFEST_ICON = "icon";
42 const std::string MANIFEST_APP_ID = "dmstest";
43 const std::string MANIFEST_INDEX_JS = "index.js";
44 const std::string MANIFEST_DEFAULT_JS = ".js";
45 const std::string MANIFEST_WIDGETS_NAME = "timer";
46 const std::string MANIFEST_WIDGETS_PATH = "manifest";
47 const std::string MANIFEST_CUSTOM = " "
48 "{ "
49 " \"appName\": \"$string:color\", "
50 " \"versionName\": \"1.0\", "
51 " \"versionCode\": 1000000, "
52 " \"logLevel\": \"level0\", "
53 " \"icon\": \"icon\", "
54 " \"appID\": \"dmstest\", "
55 " \"minPlatformVersion\": 1, "
56 " \"webFeature\": true, "
57 " \"deviceType\": [ "
58 " \"liteWearable\" "
59 " ], "
60 " \"pages\": [ "
61 " \"index\", "
62 " \"first\" "
63 " ], "
64 " \"widgets\": [{ "
65 " \"name\" : \"photo\" "
66 " },{ "
67 " \"path\" : \"manifest\" "
68 " },{ "
69 " \"name\" : \"timer\", "
70 " \"path\" : \"manifest\" "
71 " }], "
72 " \"window\": { "
73 " \"designWidth\" : 800, "
74 " \"autoDesignWidth\" : true "
75 " } "
76 "}";
77
78 const std::string MANIFEST_DEVICE_TYPE = " "
79 "{ "
80 " \"appName\": \"$string:color\", "
81 " \"webFeature\": true, "
82 " \"deviceType\": [ "
83 " \"notLiteWearable\" "
84 " ], "
85 " \"pages\": [ "
86 " 100 "
87 " ], "
88 " \"window\": { "
89 " \"designWidth\" : -800, "
90 " \"autoDesignWidth\" : true "
91 " } "
92 "}";
93
94 const std::string MANIFEST_BAD_FORMAT = " "
95 "{ "
96 " \"appName\": \"test\", "
97 " \"webFeature\": true, "
98 " \"deviceType\": \"liteWearable\", "
99 " \"pages\": \"index\", "
100 " \"widgets\": \"name\" "
101 "}";
102 } // namespace
103
104 class ManifestParserTest : public testing::Test {
105 public:
106 static void SetUpTestCase();
107 static void TearDownTestCase();
108 void SetUp();
109 void TearDown();
110 };
111
SetUpTestCase()112 void ManifestParserTest::SetUpTestCase() {}
TearDownTestCase()113 void ManifestParserTest::TearDownTestCase() {}
SetUp()114 void ManifestParserTest::SetUp() {}
TearDown()115 void ManifestParserTest::TearDown() {}
116
117 /**
118 * @tc.name: ManifestParserTest001
119 * @tc.desc: Parse the manifest with wrong format
120 * @tc.type: FUNC
121 */
122 HWTEST_F(ManifestParserTest, ManifestParserTest001, TestSize.Level1)
123 {
124 /**
125 * @tc.steps: step1. Parse the empty manifest.
126 * @tc.expected: step1. Get the default information as expected.
127 */
128 ManifestParser manifestParser;
129 manifestParser.Parse("");
130 EXPECT_EQ(manifestParser.IsUseLiteStyle(), false);
131 EXPECT_EQ(manifestParser.GetRouter()->GetPagePath("/"), MANIFEST_DEFAULT_JS);
132 EXPECT_EQ(manifestParser.GetAppInfo()->GetAppName(), "");
133 EXPECT_EQ(manifestParser.GetWidget()->GetWidgetNum(), 0);
134 EXPECT_EQ(manifestParser.GetWindowConfig().designWidth, DEFAULT_DESIGN_WIDTH);
135
136 /**
137 * @tc.steps: step2. Parse the manifest with wrong format.
138 * @tc.expected: step2. Get the default information as expected.
139 */
140 manifestParser.Parse(MANIFEST_BAD_FORMAT);
141 manifestParser.GetAppInfo()->ParseI18nJsonInfo();
142 EXPECT_EQ(manifestParser.IsUseLiteStyle(), false);
143 EXPECT_EQ(manifestParser.IsWebFeature(), true);
144 EXPECT_EQ(manifestParser.GetRouter()->GetPagePath("/"), MANIFEST_DEFAULT_JS);
145 EXPECT_EQ(manifestParser.GetAppInfo()->GetAppName(), MANIFEST_APP_NAME);
146 EXPECT_EQ(manifestParser.GetWidget()->GetWidgetNum(), 0);
147 EXPECT_EQ(manifestParser.GetWindowConfig().designWidth, DEFAULT_DESIGN_WIDTH);
148
149 /**
150 * @tc.steps: step3. Parse the manifest that lacks some information.
151 * @tc.expected: step3. Get the information as expected.
152 */
153 manifestParser.Parse(MANIFEST_DEVICE_TYPE);
154 manifestParser.GetAppInfo()->ParseI18nJsonInfo();
155 EXPECT_EQ(manifestParser.IsUseLiteStyle(), false);
156 EXPECT_EQ(manifestParser.IsWebFeature(), true);
157 EXPECT_EQ(manifestParser.GetRouter()->GetPagePath("/"), MANIFEST_DEFAULT_JS);
158 EXPECT_EQ(manifestParser.GetAppInfo()->GetAppName(), MANIFEST_APP_NAMES);
159 EXPECT_EQ(manifestParser.GetWidget()->GetWidgetNum(), 0);
160 EXPECT_EQ(manifestParser.GetWindowConfig().designWidth, DEFAULT_DESIGN_WIDTH);
161 EXPECT_EQ(manifestParser.GetWindowConfig().autoDesignWidth, true);
162 }
163
164 /**
165 * @tc.name: ManifestParserTest002
166 * @tc.desc: Parse the manifest with correct format
167 * @tc.type: FUNC
168 */
169 HWTEST_F(ManifestParserTest, ManifestParserTest002, TestSize.Level1)
170 {
171 /**
172 * @tc.steps: step1. Parse the manifest with correct format .
173 * @tc.expected: step1. Get the information as expected.
174 */
175 uint32_t resId = 0;
176 std::string resourceName = "color";
177 std::string str = "string";
178 auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
179 auto pipelineContext = AceType::MakeRefPtr<MockPipelineBase>();
180 auto container = AceType::MakeRefPtr<MockContainer>(pipelineContext);
181 pipelineContext->SetThemeManager(themeManager);
182 AceEngine::Get().AddContainer(-1, container);
183
184 auto resourceAdapter = AceType::MakeRefPtr<MockResourceAdapter>();
185 auto themeConstant = AceType::MakeRefPtr<ThemeConstants>(resourceAdapter);
186 EXPECT_CALL(*themeManager, GetThemeConstants("", "")).WillOnce(Return(themeConstant));
187 EXPECT_CALL(*resourceAdapter, GetIdByName(resourceName, str, resId)).WillOnce(Return(true));
188
189 ManifestParser manifestParser;
190 manifestParser.Parse(MANIFEST_CUSTOM);
191 manifestParser.GetAppInfo()->ParseI18nJsonInfo();
192 auto manifestWidget = manifestParser.GetWidget()->GetWidgetList().find(MANIFEST_WIDGETS_NAME);
193 EXPECT_EQ(manifestParser.GetAppInfo()->GetAppName(), "");
194 EXPECT_EQ(manifestParser.GetAppInfo()->GetVersionName(), MANIFEST_VERSION_NAME);
195 EXPECT_EQ(manifestParser.GetAppInfo()->GetVersionCode(), MANIFEST_VERSION_CODE);
196 EXPECT_EQ(manifestParser.GetAppInfo()->GetLogLevel(), MANIFEST_LOG_LEVEL);
197 EXPECT_EQ(manifestParser.GetAppInfo()->GetIcon(), MANIFEST_ICON);
198 EXPECT_EQ(manifestParser.GetAppInfo()->GetAppID(), MANIFEST_APP_ID);
199 EXPECT_EQ(manifestParser.GetAppInfo()->GetMinPlatformVersion(), MANIFEST_MINIPLATFORM_VERSION);
200 EXPECT_EQ(manifestParser.GetRouter()->GetPagePath("/"), MANIFEST_INDEX_JS);
201 EXPECT_EQ(manifestParser.GetRouter()->GetEntry(".js"), MANIFEST_INDEX_JS);
202 EXPECT_EQ(manifestParser.GetWidget()->GetWidgetNum(), MANIFEST_WIDGETS_SIZE);
203 EXPECT_EQ(manifestWidget->second->GetWidgetName(), MANIFEST_WIDGETS_NAME);
204 EXPECT_EQ(manifestWidget->second->GetWidgetPath(), MANIFEST_WIDGETS_PATH);
205 EXPECT_EQ(manifestParser.GetWindowConfig().designWidth, MANIFEST_WINDOW_WIDTH);
206 EXPECT_EQ(manifestParser.GetWindowConfig().autoDesignWidth, true);
207 EXPECT_EQ(manifestParser.IsWebFeature(), true);
208 EXPECT_EQ(manifestParser.IsUseLiteStyle(), true);
209 }
210 } // namespace OHOS::Ace::Framework