• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
17 
18 #include "gtest/gtest.h"
19 
20 #include "base/memory/ace_type.h"
21 #include "frameworks/bridge/common/manifest/manifest_router.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 namespace OHOS::Ace::Framework {
27 namespace {
28 
29 const std::string PAGES_LIST = "                              "
30                                "{                             "
31                                "  \"pages\": [                "
32                                "                \"index\",    "
33                                "                \"first\"     "
34                                "              ]               "
35                                "}";
36 
37 const std::string INDEX_JS = "index.js";
38 const std::string FIRST_JS = "first.js";
39 const std::string SECOND_JS = "second.js";
40 } // namespace
41 
42 class ManifestRouterTest : public testing::Test {
43 public:
44    static void SetUpTestCase();
45    static void TearDownTestCase();
46    void SetUp();
47    void TearDown();
48 };
49 
SetUpTestCase()50 void ManifestRouterTest::SetUpTestCase()
51 {
52     GTEST_LOG_(INFO) << "ManifestRouterTest SetUpTestCase";
53 }
54 
TearDownTestCase()55 void ManifestRouterTest::TearDownTestCase()
56 {
57     GTEST_LOG_(INFO) << "ManifestRouterTest TearDownTestCase";
58 }
59 
SetUp()60 void ManifestRouterTest::SetUp()
61 {
62     GTEST_LOG_(INFO) << "ManifestRouterTest SetUp";
63 }
64 
TearDown()65 void ManifestRouterTest::TearDown()
66 {
67     GTEST_LOG_(INFO) << "ManifestRouterTest TearDown";
68 }
69 
70 /**
71 * @tc.name: ManifestRouterTest001
72 * @tc.desc: ManifestRouter get path with empty uri or empty pages_
73 * @tc.type: FUNC
74 */
75 HWTEST_F(ManifestRouterTest, ManifestRouterTest001, TestSize.Level1)
76 {
77     std::string uri;
78     auto manifestRouter = AceType::MakeRefPtr<ManifestRouter>();
79     auto result = manifestRouter->GetPagePath(uri);
80     ASSERT_EQ(result, "");
81 
82     uri = "aaa";
83     auto pageList = manifestRouter->GetPageList();
84     pageList.clear();
85     result = manifestRouter->GetPagePath(uri);
86     ASSERT_EQ(result, "");
87 }
88 
89 /**
90 * @tc.name: ManifestRouterTest002
91 * @tc.desc: ManifestRouter get path with root uri or correct uri
92 * @tc.type: FUNC
93  */
94 HWTEST_F(ManifestRouterTest, ManifestRouterTest002, TestSize.Level1)
95 {
96     auto manifestRouter = AceType::MakeRefPtr<ManifestRouter>();
97     auto rootJson = JsonUtil::ParseJsonString(PAGES_LIST);
98     manifestRouter->RouterParse(rootJson);
99 
100     std::string uri = "/";
101     auto result = manifestRouter->GetPagePath(uri);
102     ASSERT_EQ(result, INDEX_JS);
103 
104     uri = "aaa";
105     result = manifestRouter->GetPagePath(uri);
106     ASSERT_EQ(result, "");
107 
108     uri = "first";
109     result = manifestRouter->GetPagePath(uri);
110     ASSERT_EQ(result, FIRST_JS);
111 }
112 
113 /**
114 * @tc.name: ManifestRouterTest003
115 * @tc.desc: ManifestRouter get path with empty uri or uri which contains correct suffix
116 * @tc.type: FUNC
117 */
118 HWTEST_F(ManifestRouterTest, ManifestRouterTest003, TestSize.Level1)
119 {
120     std::string uri;
121     auto manifestRouter = AceType::MakeRefPtr<ManifestRouter>();
122     auto result = manifestRouter->GetPagePath(uri);
123     auto entry = manifestRouter->GetEntry(".js");
124     ASSERT_EQ(result, "");
125     ASSERT_EQ(entry, "");
126 
127     auto rootJson = JsonUtil::ParseJsonString(PAGES_LIST);
128     manifestRouter->RouterParse(rootJson);
129     uri = "/";
130     result = manifestRouter->GetPagePath(uri, ".js");
131     entry = manifestRouter->GetEntry(".js");
132     ASSERT_EQ(result, INDEX_JS);
133     ASSERT_EQ(entry, INDEX_JS);
134 
135     uri = "/etc";
136     result = manifestRouter->GetPagePath(uri, ".js");
137     ASSERT_EQ(result, "");
138 
139     uri = "first";
140     result = manifestRouter->GetPagePath(uri, ".js");
141     ASSERT_EQ(result, FIRST_JS);
142 
143     uri = "second.js";
144     result = manifestRouter->GetPagePath(uri, ".js");
145     ASSERT_EQ(result, SECOND_JS);
146 }
147 } // namespace OHOS::Ace