• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define protected public
24 #include "core/components/theme/theme_manager_impl.h"
25 #include "frameworks/bridge/card_frontend/card_frontend.h"
26 
27 namespace OHOS::Ace::Framework {
28 
29 CardTestFactory::CardTestFactory() = default;
30 CardTestFactory::~CardTestFactory() = default;
31 
ParseCardDsl(const RefPtr<JsAcePage> & page,const std::string & content)32 void CardTestFactory::ParseCardDsl(const RefPtr<JsAcePage>& page, const std::string& content)
33 {
34     ParseCardDslWithParams(page, content, "");
35 }
36 
FlushCommands(const RefPtr<Framework::JsAcePage> & page)37 void CardTestFactory::FlushCommands(const RefPtr<Framework::JsAcePage>& page)
38 {
39     auto jsCommands = std::make_shared<std::vector<RefPtr<Framework::JsCommand>>>();
40     page->PopAllCommands(*jsCommands);
41 
42     for (const auto& command : *jsCommands) {
43         command->Execute(page);
44     }
45 }
46 
ParseCardDslWithParams(const RefPtr<JsAcePage> & page,const std::string & content,const std::string & params)47 void CardTestFactory::ParseCardDslWithParams(
48     const RefPtr<JsAcePage>& page, const std::string& content, const std::string& params)
49 {
50     page->SetFlushCallback(
51         [](const RefPtr<Framework::JsAcePage>& page) { CardTestFactory::GetInstance().FlushCommands(page); });
52     CardFrontend cardFrontend;
53     if (!context_) {
54         auto frontend = AceType::MakeRefPtr<MockFrontend>();
55         auto platformWindow = std::make_unique<MockPlatformWindow>();
56         auto window = std::make_unique<Window>(std::move(platformWindow));
57         context_ = AceType::MakeRefPtr<PipelineContext>(std::move(window), nullptr, nullptr, nullptr, frontend, 0);
58         context_->SetThemeManager(AceType::MakeRefPtr<ThemeManagerImpl>());
59     }
60     cardFrontend.Initialize(FrontendType::JS_CARD, nullptr);
61     cardFrontend.ParsePage(context_, content, params, page);
62     nodeCnt_ = cardFrontend.parseJsCard_->GetNumberOfNodes();
63 }
64 
65 } // namespace OHOS::Ace::Framework
66