diff --git a/library/scene_ktx/src/main/java/com/bytedance/scene/ktx/ScopeExtensions.kt b/library/scene_ktx/src/main/java/com/bytedance/scene/ktx/ScopeExtensions.kt new file mode 100644 index 0000000..c788661 --- /dev/null +++ b/library/scene_ktx/src/main/java/com/bytedance/scene/ktx/ScopeExtensions.kt @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2019 ByteDance Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.bytedance.scene.ktx + +import com.bytedance.scene.Scope + +inline operator fun Scope.get(key: Any): T { + return getService(key) as T +} + +operator fun Scope.set(key: Any, value: Any) { + register(key, value) +} \ No newline at end of file diff --git a/library/scene_ktx/src/test/java/com/bytedance/scene/ktx/ScopeExtensionsTests.kt b/library/scene_ktx/src/test/java/com/bytedance/scene/ktx/ScopeExtensionsTests.kt new file mode 100644 index 0000000..a72882d --- /dev/null +++ b/library/scene_ktx/src/test/java/com/bytedance/scene/ktx/ScopeExtensionsTests.kt @@ -0,0 +1,54 @@ +package com.bytedance.scene.ktx + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import com.bytedance.scene.Scene +import com.bytedance.scene.State +import com.bytedance.scene.group.GroupScene +import com.bytedance.scene.utlity.ViewIdGenerator +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + + +@RunWith(RobolectricTestRunner::class) +@Config(manifest = Config.NONE) +class ScopeExtensionsTests { + class TestGroupScene : GroupScene() { + val mId: Int = ViewIdGenerator.generateViewId() + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): ViewGroup { + return FrameLayout(requireSceneContext()) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + view.id = mId + } + } + + class TestChildScene : Scene() { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View { + return View(requireSceneContext()) + } + } + + @Test + fun test() { + val groupScene = TestGroupScene() + createFromSceneLifecycleManager(groupScene) + + groupScene.scope["test"] = "Value" + val childScene = TestChildScene() + groupScene.add(groupScene.mId, childScene, "Test") + + val value: String = childScene.scope["test"] + Assert.assertEquals("Value", value) + Assert.assertNull(childScene.scope["test_not_exists"]) + } +} \ No newline at end of file