Skip to content

Commit

Permalink
SystemApps: create test with custom data source
Browse files Browse the repository at this point in the history
Create test with custom data source.

Bug: b:159927590
Change-Id: I69fad503e8927cf3ea46e8c6cc47680f8af099f6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2283033
Reviewed-by: Kyle Horimoto <khorimoto@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: Oleh Lamzin <lamzin@google.com>
Commit-Queue: Oleh Lamzin <lamzin@google.com>
Cr-Commit-Position: refs/heads/master@{#789085}
  • Loading branch information
Giovanni Ortuño Urquidi authored and Commit Bot committed Jul 16, 2020
1 parent 75b3d39 commit 4714f88
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 2 deletions.
1 change: 1 addition & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,7 @@ if (!is_android) {
"//chrome/browser/resources/gaia_auth_host:browser_tests",
"//chromeos/components/help_app_ui:browser_tests_js",
"//chromeos/components/media_app_ui:browser_tests_js",
"//chromeos/components/system_apps:browser_tests",
]

if (!is_official_build) {
Expand Down
6 changes: 6 additions & 0 deletions chromeos/components/system_apps/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
# found in the LICENSE file.

group("closure_compile") {
testonly = true
deps = [ "public/js:closure_compile" ]
}

group("browser_tests") {
testonly = true
deps = [ "public/js:browser_tests_js" ]
}
56 changes: 54 additions & 2 deletions chromeos/components/system_apps/public/js/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,65 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//chrome/test/base/js2gtest.gni")
import("//chromeos/components/web_applications/system_apps.gni")
import("//third_party/closure_compiler/compile_js.gni")

js_type_check("closure_compile") {
js_library("message_pipe") {
}

js2gtest("browser_tests_js") {
test_type = "mojo_lite_webui"

sources = [ "message_pipe_browsertest.js" ]

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

deps = [ ":browser_test_support" ]

data = [ "message_pipe_test.html" ]
}

source_set("browser_test_support") {
testonly = true
sources = [
"message_pipe_browsertest.cc",
"message_pipe_browsertest.h",
]

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]

deps = [
"//chrome/test:test_support_ui",
"//chromeos/components/web_applications/test:test_support",
]
}

group("closure_compile") {
testonly = true
deps = [
":closure_compile_message_pipe",
":closure_compile_message_pipe_browsertest_js",
]
}

js_type_check("closure_compile_message_pipe") {
closure_flags = system_app_closure_flags_strict
deps = [ ":message_pipe" ]
}

js_library("message_pipe") {
js_type_check("closure_compile_message_pipe_browsertest_js") {
testonly = true
closure_flags = system_app_closure_flags
deps = [ ":message_pipe_browsertest_js" ]
}

js_library("message_pipe_browsertest_js") {
testonly = true
sources = [ "message_pipe_browsertest.js" ]
externs_list = [
"//chromeos/components/web_applications/js2gtest_support.externs.js",
"//third_party/chaijs/externs/chai-3.5.js",
]
deps = [ ":message_pipe" ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/components/system_apps/public/js/message_pipe_browsertest.h"

#include "base/files/file_path.h"

namespace {

constexpr base::FilePath::CharType kRootDir[] =
FILE_PATH_LITERAL("chromeos/components/system_apps/public/js/");

} // namespace

MessagePipeBrowserTestBase::MessagePipeBrowserTestBase()
: JsLibraryTest(base::FilePath(kRootDir)) {}

MessagePipeBrowserTestBase::~MessagePipeBrowserTestBase() = default;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_COMPONENTS_SYSTEM_APPS_PUBLIC_JS_MESSAGE_PIPE_BROWSERTEST_H_
#define CHROMEOS_COMPONENTS_SYSTEM_APPS_PUBLIC_JS_MESSAGE_PIPE_BROWSERTEST_H_

#include "chromeos/components/web_applications/test/js_library_test.h"

class MessagePipeBrowserTestBase : public JsLibraryTest {
public:
MessagePipeBrowserTestBase();
~MessagePipeBrowserTestBase() override;

MessagePipeBrowserTestBase(const MessagePipeBrowserTestBase&) = delete;
MessagePipeBrowserTestBase& operator=(const MessagePipeBrowserTestBase&) =
delete;
};

#endif // CHROMEOS_COMPONENTS_SYSTEM_APPS_PUBLIC_JS_MESSAGE_PIPE_BROWSERTEST_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
* @fileoverview Test suite for message_pipe.js
*/

GEN('#include "chromeos/components/system_apps/public/js/message_pipe_browsertest.h"');
GEN('#include "content/public/test/browser_test.h"');

var MessagePipeBrowserTest = class extends testing.Test {
/** @override */
get browsePreload() {
return 'chrome://system-app-test/message_pipe_test.html';
}

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

/** @override */
get typedefCppFixture() {
return 'MessagePipeBrowserTestBase';
}

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

TEST_F('MessagePipeBrowserTest', 'Empty', () => {
testDone();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- Copyright 2020 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<!DOCTYPE html>

<script src="chrome://resources/mojo/mojo/public/js/mojo_bindings_lite.js"></script>
3 changes: 3 additions & 0 deletions chromeos/components/web_applications/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import("//chrome/test/base/js2gtest.gni")
source_set("test_support") {
testonly = true
sources = [
"js_library_test.cc",
"js_library_test.h",
"sandboxed_web_ui_test_base.cc",
"sandboxed_web_ui_test_base.h",
]
Expand All @@ -15,5 +17,6 @@ source_set("test_support") {
"//chrome/test:test_support_ui",
"//chromeos/constants:constants",
"//content/test:test_support",
"//ui/webui:webui",
]
}
3 changes: 3 additions & 0 deletions chromeos/components/web_applications/test/DEPS
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
include_rules = [
# Tests run in browser_tests, so can access things under chrome/test/base*.
"+chrome/test/base",
"+content/public/browser",
"+content/public/common",
"+content/public/test",
"+ui/webui",
]
111 changes: 111 additions & 0 deletions chromeos/components/web_applications/test/js_library_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/components/web_applications/test/js_library_test.h"

#include <string>
#include <utility>

#include "base/base_paths.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted_memory.h"
#include "base/path_service.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_controller_factory.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/common/url_constants.h"
#include "ui/webui/mojo_web_ui_controller.h"
#include "url/gurl.h"

namespace {

constexpr char kSystemAppTestHost[] = "system-app-test";

bool IsSystemAppTestURL(const GURL& url) {
return url.SchemeIs(content::kChromeUIScheme) &&
url.host() == kSystemAppTestHost;
}

void HandleRequest(const base::FilePath& root_dir,
const std::string& url_path,
content::WebUIDataSource::GotDataCallback callback) {
base::FilePath path;
CHECK(base::PathService::Get(base::BasePathKey::DIR_SOURCE_ROOT, &path));
path = path.Append(root_dir);
path = path.AppendASCII(url_path.substr(0, url_path.find("?")));

std::string contents;
{
base::ScopedAllowBlockingForTesting allow_blocking;
CHECK(base::ReadFileToString(path, &contents)) << path.value();
}

scoped_refptr<base::RefCountedString> ref_contents(
new base::RefCountedString);
ref_contents->data() = contents;
std::move(callback).Run(ref_contents);
}

class JsLibraryTestWebUIController : public ui::MojoWebUIController {
public:
explicit JsLibraryTestWebUIController(const base::FilePath& root_dir,
content::WebUI* web_ui)
: ui::MojoWebUIController(web_ui) {
auto* data_source = content::WebUIDataSource::Create(kSystemAppTestHost);
data_source->SetRequestFilter(
base::BindRepeating([](const std::string& path) { return true; }),
base::BindRepeating(&HandleRequest, root_dir));

content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(),
data_source);
}
};

class JsLibraryTestWebUIControllerFactory
: public content::WebUIControllerFactory {
public:
explicit JsLibraryTestWebUIControllerFactory(const base::FilePath& root_dir)
: root_dir_(root_dir) {}

std::unique_ptr<content::WebUIController> CreateWebUIControllerForURL(
content::WebUI* web_ui,
const GURL& url) override {
return std::make_unique<JsLibraryTestWebUIController>(root_dir_, web_ui);
}

content::WebUI::TypeID GetWebUIType(content::BrowserContext* browser_context,
const GURL& url) override {
if (IsSystemAppTestURL(url)) {
return reinterpret_cast<content::WebUI::TypeID>(this);
}
return content::WebUI::kNoWebUI;
}

bool UseWebUIForURL(content::BrowserContext* browser_context,
const GURL& url) override {
return IsSystemAppTestURL(url);
}

bool UseWebUIBindingsForURL(content::BrowserContext* browser_context,
const GURL& url) override {
return IsSystemAppTestURL(url);
}

private:
const base::FilePath root_dir_;
};

} // namespace

JsLibraryTest::JsLibraryTest(const base::FilePath& root_dir)
: factory_(
std::make_unique<JsLibraryTestWebUIControllerFactory>(root_dir)) {
content::WebUIControllerFactory::RegisterFactory(factory_.get());
}

JsLibraryTest::~JsLibraryTest() {
content::WebUIControllerFactory::UnregisterFactoryForTesting(factory_.get());
}
34 changes: 34 additions & 0 deletions chromeos/components/web_applications/test/js_library_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_COMPONENTS_WEB_APPLICATIONS_TEST_JS_LIBRARY_TEST_H_
#define CHROMEOS_COMPONENTS_WEB_APPLICATIONS_TEST_JS_LIBRARY_TEST_H_

#include <memory>

#include "chrome/test/base/mojo_web_ui_browser_test.h"

namespace base {
class FilePath;
} // namespace base

namespace content {
class WebUIControllerFactory;
} // namespace content

// Base test class used to test JS libraries for System Apps. It runs tests from
// chrome://system-app-test and loads files from |root_dir|.
class JsLibraryTest : public MojoWebUIBrowserTest {
public:
explicit JsLibraryTest(const base::FilePath& root_dir);
~JsLibraryTest() override;

JsLibraryTest(const JsLibraryTest&) = delete;
JsLibraryTest& operator=(const JsLibraryTest&) = delete;

private:
std::unique_ptr<content::WebUIControllerFactory> factory_;
};

#endif // CHROMEOS_COMPONENTS_WEB_APPLICATIONS_TEST_JS_LIBRARY_TEST_H_

0 comments on commit 4714f88

Please sign in to comment.