1 /*
2  * Copyright (c) 2023-2024 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 <gtest/gtest.h>
17 #include <gtest/hwext/gtest-multithread.h>
18 
19 #include "ability_handler.h"
20 #include "ability_local_record.h"
21 #include "auto_fill_extension_context.h"
22 #include "configuration_convertor.h"
23 #define private public
24 #define protected public
25 #include "configuration_utils.h"
26 #undef private
27 #undef protected
28 #include "hilog_tag_wrapper.h"
29 #include "iremote_object.h"
30 #define private public
31 #define protected public
32 #include "js_auto_fill_extension.h"
33 #include "js_auto_fill_extension_context.h"
34 #undef private
35 #undef protected
36 #include "js_auto_fill_extension_util.h"
37 #include "js_runtime.h"
38 #include "mock_ability_token.h"
39 #include "ohos_application.h"
40 #include "runtime.h"
41 #include "string_wrapper.h"
42 #include "want.h"
43 #ifdef SUPPORT_GRAPHICS
44 #include "locale_config.h"
45 #include "window_scene.h"
46 #endif
47 
48 using namespace testing;
49 using namespace testing::ext;
50 using namespace testing::mt;
51 
52 namespace OHOS {
53 namespace AbilityRuntime {
54 namespace {
55 constexpr char JS_AUTO_FILL_EXTENSION_TASK_RUNNER[] = "JsAutoFillExtension";
56 constexpr char DEFAULT_LANGUAGE[] = "zh_CN";
57 } // namespace
58 
59 class JsAutoFillExtensionTest : public testing::Test {
60 public:
61     static void SetUpTestCase();
62     static void TearDownTestCase();
63     void SetUp() override;
64     void TearDown() override;
65 
66     std::shared_ptr<JsAutoFillExtension> jsAutoFillExtension_ = nullptr;
67     std::shared_ptr<ApplicationContext> applicationContext_ = nullptr;
68     std::unique_ptr<Runtime> jsRuntime_ = nullptr;
69 
70 private:
71     void CreateJsAutoFillExtension();
72 };
73 
SetUpTestCase()74 void JsAutoFillExtensionTest::SetUpTestCase()
75 {}
76 
TearDownTestCase()77 void JsAutoFillExtensionTest::TearDownTestCase()
78 {}
79 
SetUp()80 void JsAutoFillExtensionTest::SetUp()
81 {
82     CreateJsAutoFillExtension();
83 }
84 
TearDown()85 void JsAutoFillExtensionTest::TearDown()
86 {}
87 
CreateJsAutoFillExtension()88 void JsAutoFillExtensionTest::CreateJsAutoFillExtension()
89 {
90     std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
91     sptr<IRemoteObject> token(new (std::nothrow) MockAbilityToken());
92     std::shared_ptr<AbilityLocalRecord> record = std::make_shared<AbilityLocalRecord>(abilityInfo, token);
93 
94     std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
95     std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
96 
97     Configuration config;
98     config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE, DEFAULT_LANGUAGE);
99     config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE, ConfigurationInner::COLOR_MODE_LIGHT);
100     contextImpl->SetConfiguration(std::make_shared<Configuration>(config));
101 
102     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
103     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
104 #ifdef SUPPORT_GRAPHICS
105     UErrorCode status = U_ZERO_ERROR;
106     icu::Locale locale = icu::Locale::forLanguageTag("zh", status);
107     TAG_LOGI(AAFwkTag::TEST, "language: %{public}s, script: %{public}s, region: %{public}s", locale.getLanguage(),
108              locale.getScript(), locale.getCountry());
109     resConfig->SetLocaleInfo(locale);
110 #endif
111     Global::Resource::RState updateRet = resourceManager->UpdateResConfig(*resConfig);
112     if (updateRet != Global::Resource::RState::SUCCESS) {
113         TAG_LOGE(AAFwkTag::TEST, "Init locale failed.");
114     }
115     contextImpl->SetResourceManager(resourceManager);
116 
117     std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
118         AbilityRuntime::ApplicationContext::GetInstance();
119     applicationContext->AttachContextImpl(contextImpl);
120     application->SetApplicationContext(applicationContext);
121     applicationContext_ = applicationContext;
122 
123     std::shared_ptr<AbilityHandler> handler = std::make_shared<AbilityHandler>(nullptr);
124 
125     auto eventRunner = AppExecFwk::EventRunner::Create(JS_AUTO_FILL_EXTENSION_TASK_RUNNER);
126     Runtime::Options options;
127     options.preload = true;
128     options.eventRunner = eventRunner;
129     jsRuntime_ = JsRuntime::Create(options);
130     ASSERT_NE(jsRuntime_, nullptr);
131 
132     JsAutoFillExtension *extension = JsAutoFillExtension::Create(jsRuntime_);
133     ASSERT_NE(extension, nullptr);
134     jsAutoFillExtension_.reset(extension);
135 
136     jsAutoFillExtension_->Init(record, application, handler, token);
137 }
138 
139 /**
140  * @tc.name: Configuration_0100
141  * @tc.desc: Js auto fill extension init.
142  * @tc.type: FUNC
143  */
144 HWTEST_F(JsAutoFillExtensionTest, Init_0100, TestSize.Level1)
145 {
146     ASSERT_NE(jsAutoFillExtension_, nullptr);
147     ASSERT_NE(applicationContext_, nullptr);
148 
149     auto context = jsAutoFillExtension_->GetContext();
150     ASSERT_NE(context, nullptr);
151     auto configuration = context->GetConfiguration();
152     EXPECT_NE(configuration, nullptr);
153     auto appConfig = applicationContext_->GetConfiguration();
154     EXPECT_NE(appConfig, nullptr);
155 }
156 
157 /**
158  * @tc.name: OnStart_0100
159  * @tc.desc: Js auto fill extension OnStart.
160  * @tc.type: FUNC
161  */
162 HWTEST_F(JsAutoFillExtensionTest, OnStart_0100, TestSize.Level1)
163 {
164     Want want;
165     ASSERT_NE(jsAutoFillExtension_, nullptr);
166     jsAutoFillExtension_->OnStart(want);
167 
168     auto context = jsAutoFillExtension_->GetContext();
169     ASSERT_NE(context, nullptr);
170     auto configuration = context->GetConfiguration();
171     EXPECT_NE(configuration, nullptr);
172     auto resourceManager = context->GetResourceManager();
173     EXPECT_NE(resourceManager, nullptr);
174     auto appConfig = applicationContext_->GetConfiguration();
175     EXPECT_NE(appConfig, nullptr);
176 }
177 
178 /**
179  * @tc.name: OnReloadInModal_0100
180  * @tc.desc: Js auto fill extension OnReloadInModal.
181  * @tc.type: FUNC
182  */
183 HWTEST_F(JsAutoFillExtensionTest, OnReloadInModal_0100, TestSize.Level1)
184 {
185     CustomData customData;
186     ASSERT_NE(jsAutoFillExtension_, nullptr);
187     jsAutoFillExtension_->isPopup_ = true;
188     auto ret = jsAutoFillExtension_->OnReloadInModal(nullptr, customData);
189     EXPECT_EQ(ret, ERR_NULL_OBJECT);
190 
191     sptr<AAFwk::SessionInfo> sessionInfo = new AAFwk::SessionInfo();
192     ret = jsAutoFillExtension_->OnReloadInModal(sessionInfo, customData);
193     EXPECT_EQ(ret, ERR_NULL_OBJECT);
194 }
195 } // namespace AbilityRuntime
196 } // namespace OHOS
197