1 /*
2 * Copyright (c) 2021 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 "frameworks/bridge/test/unittest/cardfrontend/card_test_factory.h"
17
18 #include <map>
19
20 #include "frameworks/bridge/test/unittest/jsfrontend/dom_mock.h"
21
22 #define private public
23 #include "frameworks/bridge/card_frontend/card_frontend.h"
24
25 namespace OHOS::Ace::Framework {
26
27 CardTestFactory::CardTestFactory() = default;
28 CardTestFactory::~CardTestFactory() = default;
29
ParseCardDsl(const RefPtr<JsAcePage> & page,const std::string & content)30 void CardTestFactory::ParseCardDsl(const RefPtr<JsAcePage>& page, const std::string& content)
31 {
32 ParseCardDslWithParams(page, content, "");
33 }
34
FlushCommands(const RefPtr<Framework::JsAcePage> & page)35 void CardTestFactory::FlushCommands(const RefPtr<Framework::JsAcePage>& page)
36 {
37 auto jsCommands = std::make_shared<std::vector<RefPtr<Framework::JsCommand>>>();
38 page->PopAllCommands(*jsCommands);
39
40 for (const auto& command : *jsCommands) {
41 command->Execute(page);
42 }
43 }
44
ParseCardDslWithParams(const RefPtr<JsAcePage> & page,const std::string & content,const std::string & params)45 void CardTestFactory::ParseCardDslWithParams(
46 const RefPtr<JsAcePage>& page, const std::string& content, const std::string& params)
47 {
48 page->SetFlushCallback(
49 [](const RefPtr<Framework::JsAcePage>& page) { CardTestFactory::GetInstance().FlushCommands(page); });
50 CardFrontend cardFrontend;
51 if (!context_) {
52 auto frontend = AceType::MakeRefPtr<MockFrontend>();
53 auto platformWindow = std::make_unique<MockPlatformWindow>();
54 auto window = std::make_unique<Window>(std::move(platformWindow));
55 context_ = AceType::MakeRefPtr<PipelineContext>(std::move(window), nullptr, nullptr, nullptr, frontend, 0);
56 context_->SetThemeManager(AceType::MakeRefPtr<ThemeManager>());
57 }
58 cardFrontend.Initialize(FrontendType::JS_CARD, nullptr);
59 cardFrontend.ParsePage(context_, content, params, page);
60 nodeCnt_ = cardFrontend.parseJsCard_->GetNumberOfNodes();
61 }
62
63 } // namespace OHOS::Ace::Framework
64