Skip to content

Commit 416b23f

Browse files
Addressed @sharadb-amazon PR comments. Also restructured the code to prevent circular import issues.
1 parent 8ebfb19 commit 416b23f

File tree

3 files changed

+355
-168
lines changed

3 files changed

+355
-168
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env -S python3 -B
2+
3+
# Copyright (c) 2024 Project CHIP Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from enum import Enum
18+
from typing import List, Optional
19+
20+
"""
21+
This file defines the utility classes for creating and managing test sequences to validate the casting experience between
22+
the Linux tv-casting-app and the Linux tv-app. It includes an enumeration for the supported applications (App), a class to
23+
represent individual steps in a test sequence (Step), and a class to represent a complete test sequence (Sequence).
24+
Additionally, it provides helper functions to retrieve specific test sequences or all defined test sequences.
25+
"""
26+
27+
class App(Enum):
28+
"""An enumeration of the supported applications."""
29+
30+
TV_APP = 'tv-app'
31+
TV_CASTING_APP = 'tv-casting-app'
32+
33+
34+
class Step:
35+
"""A class to represent a step in a test sequence for validation.
36+
37+
A `Step` object contains attributes relevant to a test step where each object contains:
38+
- `app` subprocess to parse for `output_msg` or send `input_cmd`
39+
- `timeout_sec` specified the timeout duration for parsing the `output_msg` (optional, defaults to DEFAULT_TIMEOUT_SEC)
40+
- `output_msg` or `input_cmd` (mutually exclusive)
41+
42+
For output message blocks, define the start line, relevant lines, and the last line. If the last line contains trivial closing
43+
characters (e.g., closing brackets, braces, or commas), include the line before it with actual content. For example:
44+
`Step(subprocess_='tv-casting-app', output_msg=['InvokeResponseMessage =', 'exampleData', 'InteractionModelRevision =', '},'])`
45+
46+
For input commands, define the command string with placeholders for variables that need to be updated. For example:
47+
`Step(subprocess_='tv-casting-app', input_cmd='cast request 0\n')`
48+
"""
49+
50+
# The maximum default time to wait while parsing for output string(s).
51+
DEFAULT_TIMEOUT_SEC = 10
52+
53+
def __init__(
54+
self,
55+
app: App,
56+
timeout_sec: Optional[int] = DEFAULT_TIMEOUT_SEC,
57+
output_msg: Optional[List[str]] = None,
58+
input_cmd: Optional[str] = None,
59+
):
60+
# Validate that either `output_msg` or `input_cmd` is provided, but not both.
61+
if output_msg is not None and input_cmd is not None:
62+
raise ValueError('Step cannot contain both `output_msg` and `input_cmd`. Either `output_msg` or `input_cmd` should be provided.')
63+
elif output_msg is None and input_cmd is None:
64+
raise ValueError('Step must contain either `output_msg` or `input_cmd`. Both are `None`.')
65+
66+
# Define either `App.TV_APP` or `App.TV_CASTING_APP` on which we need to parse for `output_msg` or send `input_cmd`.
67+
self.app = app
68+
69+
# Define the maximum time in seconds for timeout while parsing for the `output_msg`. If not provided, then we use the DEFAULT_TIMEOUT_SEC.
70+
self.timeout_sec = timeout_sec
71+
72+
# Define the `output_msg` that we need to parse for in a list format.
73+
self.output_msg = output_msg
74+
75+
# Define the `input_cmd` that we need to send to either the `App.TV_APP` or `App.TV_CASTING_APP`.
76+
self.input_cmd = input_cmd
77+
78+
79+
class Sequence:
80+
"""A class representing a sequence of steps for testing the casting experience between the Linux tv-casting-app and the tv-app.
81+
82+
A Sequence object needs to be defined with an appropriate test sequence `name` along with its list of `Step` objects that will
83+
be used for validating the casting experience.
84+
"""
85+
86+
def __init__(self, name: str, steps: List[Step]):
87+
self.name = name
88+
self.steps = steps
89+
90+
91+
@staticmethod
92+
def get_test_sequence_by_name(test_sequences: List['Sequence'], test_sequence_name: str) -> Optional['Sequence']:
93+
"""Retrieve a test sequence from a list of sequences by its name."""
94+
95+
for sequence in test_sequences:
96+
if sequence.name == test_sequence_name:
97+
return sequence
98+
return None
99+
100+
101+
@staticmethod
102+
def get_test_sequences() -> List['Sequence']:
103+
"""Retrieve all the test sequences to validate the casting experience between the Linux tv-casting-app and the Linux tv-app."""
104+
105+
from linux.tv_casting_test_sequences import test_sequences
106+
107+
return test_sequences

0 commit comments

Comments
 (0)