• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from __future__ import annotations
6
7import hjson
8
9from crossbench.benchmarks.loading.config.pages import PagesConfig
10from crossbench.helper import ChangeCWD
11from tests import test_helper
12from tests.crossbench.base import CrossbenchFakeFsTestCase
13
14
15class TestExamplePageConfig(CrossbenchFakeFsTestCase):
16
17  CNN_JS_INSTRUMENTATION_PATH = (
18      test_helper.config_dir() / "benchmark/loadline/cnn_instrumentation.js")
19
20  GLOBO_JS_INSTRUMENTATION_PATH = (
21      test_helper.config_dir() / "benchmark/loadline/globo_instrumentation.js")
22
23  YT_JS_INSTRUMENTATION_PATH = (
24      test_helper.config_dir() /
25      "benchmark/loadline/youtube_instrumentation.js")
26
27  def test_parse_example_page_config_file(self):
28    example_config_file = test_helper.config_dir() / "doc/page.config.hjson"
29    self.fs.add_real_file(example_config_file)
30    file_config = PagesConfig.parse(example_config_file)
31    with example_config_file.open(encoding="utf-8") as f:
32      data = hjson.load(f)
33    dict_config = PagesConfig.parse_dict(data)
34    self.assertTrue(dict_config.pages)
35    self.assertTrue(file_config.pages)
36    for page in dict_config.pages:
37      self.assertEqual(len(page.blocks), 1)
38      self.assertGreater(len(page.blocks[0].actions), 1)
39
40  def test_parse_android_page_config_file(self):
41    example_config_file = (
42        test_helper.config_dir() / "team/woa/android_input_page_config.hjson")
43    self.fs.add_real_file(example_config_file)
44    file_config = PagesConfig.parse(example_config_file)
45    with example_config_file.open(encoding="utf-8") as f:
46      data = hjson.load(f)
47    dict_config = PagesConfig.parse_dict(data)
48    self.assertTrue(dict_config.pages)
49    self.assertTrue(file_config.pages)
50    for page in dict_config.pages:
51      self.assertEqual(len(page.blocks), 1)
52      self.assertGreater(len(page.blocks[0].actions), 1)
53
54  def test_parse_loadline_page_config_phone(self):
55    self.fs.add_real_file(self.CNN_JS_INSTRUMENTATION_PATH)
56    self.fs.add_real_file(self.GLOBO_JS_INSTRUMENTATION_PATH)
57
58    config_file = (
59        test_helper.config_dir() / "benchmark/loadline/page_config_phone.hjson")
60    self.fs.add_real_file(config_file)
61    file_config = PagesConfig.parse(config_file)
62    with config_file.open(encoding="utf-8") as f:
63      data = hjson.load(f)
64    with ChangeCWD(test_helper.config_dir() / "benchmark/loadline"):
65      dict_config = PagesConfig.parse_dict(data)
66    self.assertTrue(dict_config.pages)
67    self.assertTrue(file_config.pages)
68    for page in dict_config.pages:
69      self.assertEqual(len(page.blocks), 1)
70      self.assertGreater(len(page.blocks[0].actions), 1)
71
72  def test_parse_loadline_page_config_tablet(self):
73    self.fs.add_real_file(self.CNN_JS_INSTRUMENTATION_PATH)
74    self.fs.add_real_file(self.YT_JS_INSTRUMENTATION_PATH)
75
76    config_file = (
77        test_helper.config_dir() /
78        "benchmark/loadline/page_config_tablet.hjson")
79    self.fs.add_real_file(config_file)
80    file_config = PagesConfig.parse(config_file)
81    with config_file.open(encoding="utf-8") as f:
82      data = hjson.load(f)
83    with ChangeCWD(test_helper.config_dir() / "benchmark/loadline"):
84      dict_config = PagesConfig.parse_dict(data)
85    self.assertTrue(dict_config.pages)
86    self.assertTrue(file_config.pages)
87    for page in dict_config.pages:
88      self.assertEqual(len(page.blocks), 1)
89      self.assertGreater(len(page.blocks[0].actions), 1)
90
91
92if __name__ == "__main__":
93  test_helper.run_pytest(__file__)
94