1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158

ash / webui / sample_system_web_app_ui / test / sample_system_web_app_ui_browsertest.js [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Test suite for chrome://sample-system-web-app.
 */

GEN('#include "content/public/test/browser_test.h"');
GEN('#include "build/config/coverage/buildflags.h"');

const HOST_ORIGIN = 'chrome://sample-system-web-app';
const UNTRUSTED_HOST_ORIGIN = 'chrome-untrusted://sample-system-web-app';

// TODO:(crbug.com/1262025): We should avoid using `var`.
//
// js2gtest fixtures require var here (https://crbug.com/1033337).
// eslint-disable-next-line no-var
var SampleSystemWebAppUIBrowserTest = class extends testing.Test {
  /** @override */
  get browsePreload() {
    return HOST_ORIGIN;
  }

  /** @override */
  get isAsync() {
    return true;
  }
};

// Tests that chrome://sample-system-web-app runs js file and that it goes
// somewhere instead of 404ing or crashing.
// TODO(b/280457934): Skip as shared workers crash for JS coverage builds.
GEN('#if BUILDFLAG(USE_JAVASCRIPT_COVERAGE)');
GEN('#define MAYBE_HasChromeSchemeURL DISABLED_HasChromeSchemeURL');
GEN('#else');
GEN('#define MAYBE_HasChromeSchemeURL HasChromeSchemeURL');
GEN('#endif');
TEST_F(
    'SampleSystemWebAppUIBrowserTest', 'MAYBE_HasChromeSchemeURL', async () => {
      const {assertEquals} = await import('chrome://webui-test/chai_assert.js');
      const header = document.querySelector('header');

      assertEquals(header.innerText, 'Sample System Web App');
      assertEquals(document.location.origin, HOST_ORIGIN);
      testDone();
    });

// Test the ability to get information from the page handler.
// TODO(b/280457934): Skip as shared workers crash for JS coverage builds.
GEN('#if BUILDFLAG(USE_JAVASCRIPT_COVERAGE)');
GEN('#define MAYBE_FetchPreferences DISABLED_FetchPreferences');
GEN('#else');
GEN('#define MAYBE_FetchPreferences FetchPreferences');
GEN('#endif');
TEST_F(
    'SampleSystemWebAppUIBrowserTest', 'MAYBE_FetchPreferences', async () => {
      const {assertDeepEquals} =
          await import('chrome://webui-test/chai_assert.js');
      const {preferences} = await window.pageHandler.getPreferences();
      assertDeepEquals(
          {background: '#ffffff', foreground: '#000000'}, preferences);
      testDone();
    });

// Test the ability to trigger work in the page handler.
// TODO(b/280457934): Skip as shared workers crash for JS coverage builds.
GEN('#if BUILDFLAG(USE_JAVASCRIPT_COVERAGE)');
GEN('#define MAYBE_DoSomething DISABLED_DoSomething');
GEN('#else');
GEN('#define MAYBE_DoSomething DoSomething');
GEN('#endif');
TEST_F('SampleSystemWebAppUIBrowserTest', 'MAYBE_DoSomething', async () => {
  const {assertEquals} = await import('chrome://webui-test/chai_assert.js');
  const pageHandler = window.pageHandler;
  const callbackRouter = window.callbackRouter;

  // Now execute our test: zero the event count and call doSomething.
  window.eventCount.set('DoSomething is done', 0);
  pageHandler.doSomething();

  // Ensure the DoSomething() is called on the browser side.
  await pageHandler.$.flushForTesting();

  // Await the C++ process to call back with the event.
  await callbackRouter.$.flush();
  // Verify the expected event count.
  assertEquals(1, window.eventCount.get('DoSomething is done'));

  testDone();
});

// eslint-disable-next-line no-var
var SampleSystemWebAppUIUntrustedBrowserTest = class extends testing.Test {
  /** @override */
  get browsePreload() {
    return HOST_ORIGIN + '/inter_frame_communication.html';
  }

  /** @override */
  get runAccessibilityChecks() {
    return false;
  }

  /** @override */
  get isAsync() {
    return true;
  }
};

// Tests that chrome://sample-system-web-app/inter_frame_communication.html
// embeds a chrome-untrusted:// iframe.
TEST_F(
    'SampleSystemWebAppUIUntrustedBrowserTest', 'HasChromeUntrustedIframe',
    async () => {
      const {assertEquals} = await import('chrome://webui-test/chai_assert.js');
      const iframe = document.querySelector('iframe');
      window.onmessage = (event) => {
        if (event.data.id === 'post-message') {
          assertEquals(event.origin, UNTRUSTED_HOST_ORIGIN);
          assertEquals(event.data.success, true);
          testDone();
        }
      };
      iframe.contentWindow.postMessage('hello', UNTRUSTED_HOST_ORIGIN);
    });

// Tests that chrome://sample-system-web-app/inter_frame_communication.html
// can communicate with its embedded chrome-untrusted:// iframe via Mojo
// method calls.
TEST_F(
    'SampleSystemWebAppUIUntrustedBrowserTest', 'MojoMethodCall', async () => {
      const {assertEquals} = await import('chrome://webui-test/chai_assert.js');
      window.onmessage = (event) => {
        if (event.data.id === 'mojo-method-call-resp') {
          assertEquals(event.data.resp, 'Task done');
          testDone();
        }
      };

      const iframe = document.querySelector('iframe');
      iframe.contentWindow.postMessage(
          {id: 'test-mojo-method-call'}, UNTRUSTED_HOST_ORIGIN);
    });

TEST_F('SampleSystemWebAppUIUntrustedBrowserTest', 'MojoMessage', async () => {
  const {assertEquals} = await import('chrome://webui-test/chai_assert.js');
  window.onmessage = (event) => {
    if (event.data.id === 'mojo-did-receive-task') {
      assertEquals(event.data.task, 'Hello from chrome://');
      testDone();
    }
  };

  window.childPageReady.then(({childPage}) => {
    childPage.doSomethingForParent('Hello from chrome://');
  });
});