Refactor trace_integration_test.

Put a bunch of hard wired params through test args so more testing using
tracing mechanism can be added.

This is the necessary step allowing us to reuse this harness for video frame
presentation mode testing.

BUG=867136
TEST=GPU bots, trace_test
R=piman@chromium.org,kbr@chromium.org

Change-Id: I00b30e4273570c2ba05ab8af1bc1ebe2c596e5a8
Reviewed-on: https://chromium-review.googlesource.com/c/1428405
Commit-Queue: Zhenyao Mo <zmo@chromium.org>
Reviewed-by: Antoine Labour <piman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#625064}
diff --git a/content/test/gpu/gpu_tests/gpu_integration_test.py b/content/test/gpu/gpu_tests/gpu_integration_test.py
index 70db515..9f991d50 100644
--- a/content/test/gpu/gpu_tests/gpu_integration_test.py
+++ b/content/test/gpu/gpu_tests/gpu_integration_test.py
@@ -73,7 +73,7 @@
     cls.SetBrowserOptions(cls._finder_options)
 
   @classmethod
-  def RestartBrowserIfNecessaryWithArgs(cls, browser_args):
+  def RestartBrowserIfNecessaryWithArgs(cls, browser_args, force_restart=False):
     if not browser_args:
       browser_args = []
     elif '--disable-gpu' in browser_args:
@@ -83,12 +83,16 @@
       os_name = cls.browser.platform.GetOSName()
       if os_name == 'android' or os_name == 'chromeos':
         browser_args.remove('--disable-gpu')
-    if set(browser_args) != cls._last_launched_browser_args:
+    if force_restart or set(browser_args) != cls._last_launched_browser_args:
       logging.info('Restarting browser with arguments: ' + str(browser_args))
       cls.StopBrowser()
       cls.CustomizeBrowserArgs(browser_args)
       cls.StartBrowser()
 
+  @classmethod
+  def RestartBrowserWithArgs(cls, browser_args):
+    cls.RestartBrowserIfNecessaryWithArgs(browser_args, force_restart=True)
+
   # The following is the rest of the framework for the GPU integration tests.
 
   @classmethod
diff --git a/content/test/gpu/gpu_tests/trace_integration_test.py b/content/test/gpu/gpu_tests/trace_integration_test.py
index a0f8c08..2d2a6c95 100644
--- a/content/test/gpu/gpu_tests/trace_integration_test.py
+++ b/content/test/gpu/gpu_tests/trace_integration_test.py
@@ -13,10 +13,8 @@
 from telemetry.timeline import model as model_module
 from telemetry.timeline import tracing_config
 
-TOPLEVEL_GL_CATEGORY = 'gpu_toplevel'
 TOPLEVEL_SERVICE_CATEGORY = 'disabled-by-default-gpu.service'
 TOPLEVEL_DEVICE_CATEGORY = 'disabled-by-default-gpu.device'
-TOPLEVEL_CATEGORIES = [TOPLEVEL_SERVICE_CATEGORY, TOPLEVEL_DEVICE_CATEGORY]
 
 gpu_relative_path = "content/test/data/gpu/"
 
@@ -25,7 +23,7 @@
               os.path.join(
                   path_util.GetChromiumSrcDir(), 'media', 'test', 'data')]
 
-test_harness_script = r"""
+webgl_test_harness_script = r"""
   var domAutomationController = {};
 
   domAutomationController._finished = false;
@@ -67,21 +65,42 @@
     # currently skipped on all platforms, to give a hint that they
     # should perhaps be enabled in the future.
     for p in pixel_test_pages.DefaultPages('TraceTest'):
-      yield (p.name, gpu_relative_path + p.url, (TOPLEVEL_SERVICE_CATEGORY))
+      yield (p.name, gpu_relative_path + p.url,
+             {'browser_args': [],
+              'category': TOPLEVEL_SERVICE_CATEGORY,
+              'test_harness_script': webgl_test_harness_script,
+              'finish_js_condition': 'domAutomationController._finished',
+              'expected_event_args': {'gl_category': 'gpu_toplevel'}})
     for p in pixel_test_pages.DefaultPages('DeviceTraceTest'):
-      yield (p.name, gpu_relative_path + p.url, (TOPLEVEL_DEVICE_CATEGORY))
+      yield (p.name, gpu_relative_path + p.url,
+             {'browser_args': [],
+              'category': TOPLEVEL_DEVICE_CATEGORY,
+              'test_harness_script': webgl_test_harness_script,
+              'finish_js_condition': 'domAutomationController._finished',
+              'expected_event_args': {'gl_category': 'gpu_toplevel'}})
 
   def RunActualGpuTest(self, test_path, *args):
+    test_params = args[0]
+    assert 'browser_args' in test_params
+    assert 'category' in test_params
+    assert 'test_harness_script' in test_params
+    assert 'finish_js_condition' in test_params
+    assert 'expected_event_args' in test_params
+    browser_args = test_params['browser_args']
+    category = test_params['category']
+    test_harness_script = test_params['test_harness_script']
+    finish_js_condition = test_params['finish_js_condition']
+    expected_event_args = test_params['expected_event_args']
+
     # The version of this test in the old GPU test harness restarted
     # the browser after each test, so continue to do that to match its
     # behavior.
-    self._RestartBrowser('Restarting browser to ensure clean traces')
+    self.RestartBrowserWithArgs(self._AddDefaultArgs(browser_args))
 
     # Set up tracing.
     config = tracing_config.TracingConfig()
     config.chrome_trace_config.category_filter.AddExcludedCategory('*')
-    for cat in TOPLEVEL_CATEGORIES:
-      config.chrome_trace_config.category_filter.AddDisabledByDefault(cat)
+    config.chrome_trace_config.category_filter.AddDisabledByDefault(category)
     config.enable_chrome_trace = True
     tab = self.tab
     tab.browser.platform.tracing_controller.StartTracing(config, 60)
@@ -90,26 +109,31 @@
     url = self.UrlOfStaticFilePath(test_path)
     tab.Navigate(url, script_to_evaluate_on_commit=test_harness_script)
     tab.action_runner.WaitForJavaScriptCondition(
-      'domAutomationController._finished', timeout=30)
+      finish_js_condition, timeout=30)
 
     # Stop tracing.
     timeline_data = tab.browser.platform.tracing_controller.StopTracing()[0]
 
     # Evaluate success.
     timeline_model = model_module.TimelineModel(timeline_data)
-    category_name = args[0]
     event_iter = timeline_model.IterAllEvents(
         event_type_predicate=timeline_model.IsSliceOrAsyncSlice)
-    for event in event_iter:
-      if (event.args.get('gl_category', None) == TOPLEVEL_GL_CATEGORY and
-          event.category == category_name):
-        print 'Found event with category name ' + category_name
+    self._EvaluateSuccess(event_iter, category, expected_event_args)
+
+  def _EvaluateSuccess(self, event_iterator, expected_category_name,
+                       expected_event_args):
+    for event in event_iterator:
+      if event.category != expected_category_name:
+        continue
+      for arg_name, arg_value in expected_event_args.iteritems():
+        if event.args.get(arg_name, None) != arg_value:
+          break
+      else:
+        print 'Found event with category name ' + expected_category_name
         break
     else:
-      self.fail(self._FormatException(category_name))
-
-  def _FormatException(self, category):
-    return 'Trace markers for GPU category were not found: %s' % category
+      self.fail('Trace markers for GPU category were not found: %s' %
+                expected_category_name)
 
   @classmethod
   def _CreateExpectations(cls):
@@ -119,12 +143,17 @@
   def SetUpProcess(cls):
     super(TraceIntegrationTest, cls).SetUpProcess()
     path_util.SetupTelemetryPaths()
-    cls.CustomizeBrowserArgs([
-      '--enable-logging',
-      '--enable-experimental-web-platform-features'])
+    cls.CustomizeBrowserArgs(cls._AddDefaultArgs([]))
     cls.StartBrowser()
     cls.SetStaticServerDirs(data_paths)
 
+  @staticmethod
+  def _AddDefaultArgs(browser_args):
+    # All tests receive the following options.
+    return [
+      '--enable-logging',
+      '--enable-experimental-web-platform-features'] + browser_args
+
 def load_tests(loader, tests, pattern):
   del loader, tests, pattern  # Unused.
   return gpu_integration_test.LoadAllTestsInModule(sys.modules[__name__])