"""Offline Android UI verifier for Ch 30: RatingBar."""
import json
import xml.etree.ElementTree as ET
ANDROID_NS = "{http://schemas.android.com/apk/res/android}"
LAYER_LIST_XML = """<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@mipmap/ic_rating_off1" />
<item android:id="@android:id/secondaryProgress" android:drawable="@mipmap/ic_rating_off1" />
<item android:id="@android:id/progress" android:drawable="@mipmap/ic_rating_on1" />
</layer-list>
"""
STYLE_XML = """<resources>
<style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingbar_full</item>
<item name="android:minHeight">24dip</item>
<item name="android:maxHeight">24dip</item>
</style>
</resources>
"""
class RatingBarModel:
def __init__(self, num_stars: int = 5, step_size: float = 0.5, rating: float = 0.0, is_indicator: bool = False):
self.num_stars = num_stars
self.step_size = step_size
self.is_indicator = is_indicator
self.rating = 0.0
self.events = []
self.set_rating(rating, from_user=False)
@property
def internal_max(self) -> int:
return int(round(self.num_stars / self.step_size))
def set_rating(self, value: float, from_user: bool = True) -> float:
if from_user and self.is_indicator:
return self.rating
clamped = max(0.0, min(float(self.num_stars), value))
snapped = round(round(clamped / self.step_size) * self.step_size, 4)
self.rating = snapped
self.events.append({"rating": snapped, "fromUser": from_user})
return self.rating
def main() -> None:
rb_half = RatingBarModel(num_stars=5, step_size=0.5, rating=2.3)
rb_tenth = RatingBarModel(num_stars=5, step_size=0.1, rating=4.74)
rb_half.set_rating(6.2, from_user=True)
rb_half.set_rating(-1.0, from_user=True)
c1_actual = {
"half_step_internal_max": rb_half.internal_max,
"half_step_trace": [e["rating"] for e in rb_half.events],
"tenth_step_internal_max": rb_tenth.internal_max,
"tenth_step_initial": rb_tenth.rating,
}
c1_expected = {
"half_step_internal_max": 10,
"half_step_trace": [2.5, 5.0, 0.0],
"tenth_step_internal_max": 50,
"tenth_step_initial": 4.7,
}
# Case 2: isIndicator mode blocks user touch changes while permitting programmatic setRating
rb_ind = RatingBarModel(num_stars=5, step_size=0.5, rating=3.5, is_indicator=True)
after_user = rb_ind.set_rating(5.0, from_user=True)
after_code = rb_ind.set_rating(4.5, from_user=False)
c2_actual = {
"after_user_touch": after_user,
"after_programmatic": after_code,
"event_count": len(rb_ind.events),
}
c2_expected = {
"after_user_touch": 3.5,
"after_programmatic": 4.5,
"event_count": 2,
}
# Case 3: Custom layer-list & style XML parsing
ll = ET.fromstring(LAYER_LIST_XML)
layer_map = {it.attrib[f"{ANDROID_NS}id"]: it.attrib[f"{ANDROID_NS}drawable"] for it in ll.findall("item")}
st = ET.fromstring(STYLE_XML).find("style")
st_items = {it.attrib["name"]: it.text for it in st.findall("item")}
c3_actual = {
"layers": layer_map,
"style_parent": st.attrib["parent"],
"style_items": st_items,
}
c3_expected = {
"layers": {
"@android:id/background": "@mipmap/ic_rating_off1",
"@android:id/secondaryProgress": "@mipmap/ic_rating_off1",
"@android:id/progress": "@mipmap/ic_rating_on1",
},
"style_parent": "@android:style/Widget.RatingBar",
"style_items": {
"android:progressDrawable": "@drawable/ratingbar_full",
"android:minHeight": "24dip",
"android:maxHeight": "24dip",
},
}
# Case 4: Built-in styles semantics
builtin_styles = {
"?android:attr/ratingBarStyleSmall": {"isIndicator_default": True, "interactive": False},
"?android:attr/ratingBarStyleIndicator": {"isIndicator_default": True, "interactive": False},
"@android:style/Widget.RatingBar": {"isIndicator_default": False, "interactive": True},
}
c4_expected = builtin_styles
cases = [
{
"name": "ratingbar_step_quantization_and_bounds",
"summary": "验证 RatingBar 的 numStars/stepSize 步进量化、区间钳位及底层 ProgressBar max 换算",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "indicator_mode_and_listener",
"summary": "验证 isIndicator=true 阻断用户触摸改动但允许代码 setRating 更新的行为",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "layer_list_and_custom_style_xml",
"summary": "解析自定义图标评分条的 layer-list 三层资源与 roomRatingBar 样式节点",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "ratingbar_builtin_styles_check",
"summary": "核对系统内置小号与指示器评分条样式默认仅展示不可交互的属性语义",
"expected": c4_expected,
"actual": builtin_styles,
"passed": builtin_styles == c4_expected,
},
]
print(json.dumps({
"version": "CPython 3.12.14 offline Android UI/XML/Adapter verifier (Android SDK/emulator absent)",
"cases": cases,
}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-ratingbar-lab/explore-tutorial-ratingbar.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-ratingbar.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1931.zip