1 // Copyright (c) 2024 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gn/ohos_components.h"
6 #include "gn/ohos_components_impl.h"
7
8 #include <cstdint>
9 #include <iostream>
10 #include <memory>
11 #include <utility>
12
13 #include "gn/test_with_scope.h"
14 #include "util/test/test.h"
15
16 static const std::string COMPONENT_PATHS = "{"
17 "\"foo\": {"
18 "\"subsystem\": \"samples\","
19 "\"path\": \"components/foo\","
20 "\"innerapis\": {"
21 "\"label\": \"//components/foo/interfaces/innerapis/libfoo:libfoo\","
22 "\"name\": \"libfoo\""
23 "}"
24 "},"
25 "\"bar\": {"
26 "\"subsystem\": \"samples\","
27 "\"path\": \"components/bar\","
28 "\"innerapis\": {"
29 "\"label\": \"//components/bar/interfaces/innerapis/libbar:libbar\","
30 "\"name\": \"libbar\""
31 "}"
32 "},"
33 "\"baz\": {"
34 "\"subsystem\": \"samples\","
35 "\"path\": \"components/baz\","
36 "\"innerapis\": {"
37 "\"label\": \"//components/baz/interfaces/innerapis/libbaz:libbaz\","
38 "\"name\": \"libbaz\""
39 "}"
40 "}"
41 "}";
42
TEST(OhosComponent,ComponentInnerApi)43 TEST(OhosComponent, ComponentInnerApi) {
44 OhosComponent com("foo", "samples", "components/foo", {"components/foo"}, false);
45 EXPECT_EQ("foo", com.name());
46 EXPECT_EQ("samples", com.subsystem());
47 EXPECT_EQ("//components/foo", com.path());
48
49 // Add two innerapis
50 const std::string fooLabel = "//components/foo/interfaces/innerapis/libfoo:libfoo";
51 com.addInnerApi("libfoo", fooLabel.c_str());
52 const std::string barLabel = "//components/bar/interfaces/innerapis/libbar:libbar";
53 com.addInnerApi("libbar", barLabel.c_str());
54
55 // Get first innerapi
56 std::string result = com.getInnerApi("libfoo");
57 EXPECT_EQ(fooLabel, result);
58
59 // Get second innerapi api
60 result = com.getInnerApi("libbar");
61 EXPECT_EQ(barLabel, result);
62
63 // Get non exist innerapi api
64 result = com.getInnerApi("libnone");
65 EXPECT_EQ(0, result.size());
66
67 // Check valid innerapi label
68 bool ret = com.isInnerApi("//components/bar/interfaces/innerapis/libbar:libbar");
69 ASSERT_TRUE(ret);
70
71 // Check invalid innerapi label
72 ret = com.isInnerApi("//components/bar/interfaces/innerapis/libbar:libbar2");
73 ASSERT_FALSE(ret);
74 }
75
TEST(OhosComponentsImpl,LoadComponentSubsystemAndPaths)76 TEST(OhosComponentsImpl, LoadComponentSubsystemAndPaths) {
77 OhosComponentsImpl *mgr = new OhosComponentsImpl();
78 std::string errStr;
79 bool ret = mgr->LoadComponentInfo(COMPONENT_PATHS, false, errStr);
80 ASSERT_TRUE(ret);
81
82 const OhosComponent *component;
83 component = mgr->matchComponentByLabel("components/foo");
84 EXPECT_EQ("foo", component->name());
85 component = mgr->matchComponentByLabel("//components/foo");
86 EXPECT_EQ("foo", component->name());
87 component = mgr->matchComponentByLabel("//components/foo:libfoo");
88 EXPECT_EQ("foo", component->name());
89 component = mgr->matchComponentByLabel("//components/foo/test");
90 EXPECT_EQ("foo", component->name());
91 component = mgr->matchComponentByLabel("//components/foo/test:libfoo");
92 EXPECT_EQ("foo", component->name());
93
94 component = mgr->matchComponentByLabel("components/fo");
95 EXPECT_EQ(nullptr, component);
96 component = mgr->matchComponentByLabel("components/");
97 EXPECT_EQ(nullptr, component);
98 component = mgr->matchComponentByLabel("components");
99 EXPECT_EQ(nullptr, component);
100
101 Err err;
102 Value external_dep(nullptr, "foo:libfoo");
103 std::string label;
104 int whole_status = -1;
105 Label current_toolchain;
106 ret = mgr->GetExternalDepsLabel(external_dep, label, current_toolchain, whole_status, &err);
107 ASSERT_TRUE(ret);
108
109 delete mgr;
110 }