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 <cstdint>
17 #include <fstream>
18 #include <iostream>
19 #include <map>
20 
21 #include "options.h"
22 #include "simulator.h"
23 
24 constexpr int32_t PARAM_ONE = 1;
25 constexpr int32_t PARAM_TWO = 2;
26 constexpr int32_t PARAM_THREE = 3;
27 constexpr int32_t PARAM_FOUR = 4;
28 constexpr int32_t PARAM_FIVE = 5;
29 constexpr int32_t PARAM_SIX = 6;
30 constexpr int32_t PARAM_SEVEN = 7;
31 constexpr int32_t PARAM_EIGHT = 8;
32 constexpr int32_t PARAM_NINE = 9;
33 constexpr int32_t PARAM_TEN = 10;
34 constexpr int32_t PARAM_ELEVEN = 11;
35 constexpr int32_t PARAM_TWELVE = 12;
36 constexpr int32_t PARAM_THIRTEEN = 13;
37 constexpr int32_t PARAM_FOURTEEN = 14;
38 constexpr int32_t PARAM_FIFTEEN = 15;
39 constexpr int32_t PARAM_SIXTEEN = 16;
40 constexpr int32_t PARAM_SEVENTEEN = 17;
41 constexpr int32_t PARAM_EIGHTEEN = 18;
42 
main(int32_t argc,const char * argv[])43 int32_t main(int32_t argc, const char *argv[])
44 {
45     if (argc < PARAM_EIGHTEEN) {
46         std::cout << "Insufficient parameters." << std::endl;
47         return 1;
48     }
49 
50     OHOS::AbilityRuntime::Options options;
51     options.modulePath = argv[PARAM_ONE];
52     options.resourcePath = argv[PARAM_TWO];
53     options.debugPort = atoi(argv[PARAM_THREE]);
54     options.assetPath = argv[PARAM_FOUR];
55     options.systemResourcePath = argv[PARAM_FIVE];
56     options.appResourcePath = argv[PARAM_SIX];
57     options.containerSdkPath = argv[PARAM_SEVEN];
58     options.url = argv[PARAM_EIGHT];
59     options.language = argv[PARAM_NINE];
60     options.region = argv[PARAM_TEN];
61     options.script = argv[PARAM_ELEVEN];
62     options.themeId = atoi(argv[PARAM_TWELVE]);
63     options.deviceWidth = atoi(argv[PARAM_THIRTEEN]);
64     options.deviceHeight = atoi(argv[PARAM_FOURTEEN]);
65     options.isRound = atoi(argv[PARAM_FIFTEEN]);
66     options.previewPath = argv[PARAM_SIXTEEN];
67 
68     OHOS::AppExecFwk::Configuration config;
69     config.AddItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, "testlanguage");
70     config.AddItem(OHOS::AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, "light");
71     config.AddItem(OHOS::AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, "vertical");
72     auto configuration = std::make_shared<OHOS::AppExecFwk::Configuration>(config);
73     options.configuration = configuration;
74 
75     std::string moduleJsonPath = argv[PARAM_SEVENTEEN];
76     std::ifstream stream(moduleJsonPath, std::ios::ate | std::ios::binary);
77     if (!stream.is_open()) {
78         std::cout << "Failed to open: " << moduleJsonPath << std::endl;
79         return -1;
80     }
81 
82     size_t len = stream.tellg();
83     std::cout << "module json len: " << len << std::endl;
84     std::unique_ptr<uint8_t[]> buffer = std::make_unique<uint8_t[]>(len);
85     stream.seekg(0);
86     stream.read(reinterpret_cast<char*>(buffer.get()), len);
87     stream.close();
88     auto buf = buffer.release();
89     options.moduleJsonBuffer.assign(buf, buf + len);
90 
91     auto simulator = OHOS::AbilityRuntime::Simulator::Create(options);
92     if (!simulator) {
93         std::cout << "Create Simulator failed." << std::endl;
94         return 1;
95     }
96     std::map<std::string, std::string> mockList {};
97     simulator->SetMockList(mockList);
98     mockList.emplace("test1", "1");
99     mockList.emplace("test2", "2");
100     mockList.emplace("test3", "!@#$%^&*(){}[]:\";',./<>?\\|");
101     mockList.emplace("test4", "中文测试");
102     simulator->SetMockList(mockList);
103 
104     std::string abilitySrcPath {argv[PARAM_EIGHTEEN]};
105     int64_t id = simulator->StartAbility(abilitySrcPath, [](int64_t abilityId) {});
106     if (id < 0) {
107         std::cout << "Start Ability failed." << std::endl;
108         return 1;
109     }
110 
111     config.AddItem(OHOS::AppExecFwk::ConfigurationInner::APPLICATION_DIRECTION, "horizontal");
112     simulator->UpdateConfiguration(config);
113 
114     simulator->TerminateAbility(id);
115     return 0;
116 }
117