Skip to content

Commit

Permalink
feature: add Scope[] operator
Browse files Browse the repository at this point in the history
  • Loading branch information
qii committed Nov 22, 2019
1 parent b7299ff commit 043fab3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
@@ -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 <reified T> Scope.get(key: Any): T {
return getService<T>(key) as T
}

operator fun Scope.set(key: Any, value: Any) {
register(key, value)
}
@@ -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"])
}
}

0 comments on commit 043fab3

Please sign in to comment.