1 /* 2 * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 "mock_environment.h" 18 19 #include "core/common/environment/environment.h" 20 #include "core/common/environment/environment_interface.h" 21 #include "core/common/environment/environment_proxy.h" 22 23 using namespace testing; 24 using namespace testing::ext; 25 26 namespace OHOS::Ace { 27 // namespace 28 class EnvironmentTest : public testing::Test { 29 public: SetUpTestCase()30 static void SetUpTestCase() {} TearDownTestCase()31 static void TearDownTestCase() {} SetUp()32 void SetUp() {} TearDown()33 void TearDown() {} 34 }; 35 36 /** 37 * @tc.name: CastToEnvironmentTest001 38 * @tc.desc: Test cast to environment. 39 * @tc.type: FUNC 40 */ 41 HWTEST_F(EnvironmentTest, CastToEnvironmentTest001, TestSize.Level1) 42 { 43 /** 44 * @tc.steps: step1. Call GetInstance first. 45 * @tc.expected: step1. The return value is not null. 46 */ 47 EnvironmentProxy* firstResult = EnvironmentProxy::GetInstance(); 48 EXPECT_NE(firstResult, nullptr); 49 /** 50 * @tc.steps: step2. Call GetInstance second. 51 * @tc.expected: step2. The return value is same with first. 52 */ 53 EnvironmentProxy* secondResult = EnvironmentProxy::GetInstance(); 54 EXPECT_EQ(secondResult, firstResult); 55 } 56 57 /** 58 * @tc.name: CastToEnvironmentTest002 59 * @tc.desc: Test cast to environment. 60 * @tc.type: FUNC 61 */ 62 HWTEST_F(EnvironmentTest, CastToEnvironmentTest002, TestSize.Level1) 63 { 64 /** 65 * @tc.steps: step1. Input delegate is null. 66 * @tc.expected: step1. The return environment is null. 67 */ 68 EnvironmentProxy::GetInstance()->SetDelegate(nullptr); 69 RefPtr<TaskExecutor> taskExecutor; 70 RefPtr<Environment> environment = EnvironmentProxy::GetInstance()->GetEnvironment(taskExecutor); 71 72 EXPECT_EQ(environment, nullptr); 73 } 74 75 /** 76 * @tc.name: CastToEnvironmentTest003 77 * @tc.desc: Test cast to environment. 78 * @tc.type: FUNC 79 */ 80 HWTEST_F(EnvironmentTest, CastToEnvironmentTest003, TestSize.Level1) 81 { 82 /** 83 * @tc.steps: step1. Input delegate is not null. 84 * @tc.expected: step1. Return expected results. 85 */ 86 EnvironmentProxy::GetInstance()->SetDelegate(std::make_unique<MockEnvironmentProxyImpl>()); 87 RefPtr<TaskExecutor> taskExecutor; 88 RefPtr<Environment> environment = EnvironmentProxy::GetInstance()->GetEnvironment(taskExecutor); 89 EXPECT_NE(environment, nullptr); 90 91 std::string value = environment->GetAccessibilityEnabled(); 92 EXPECT_EQ(value, RET_TEST); 93 } 94 } // namespace OHOS::Ace 95