-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SystemApps: create test with custom data source
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
Showing
11 changed files
with
294 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
chromeos/components/system_apps/public/js/message_pipe_browsertest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
20 changes: 20 additions & 0 deletions
20
chromeos/components/system_apps/public/js/message_pipe_browsertest.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
36 changes: 36 additions & 0 deletions
36
chromeos/components/system_apps/public/js/message_pipe_browsertest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
7 changes: 7 additions & 0 deletions
7
chromeos/components/system_apps/public/js/message_pipe_test.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
111
chromeos/components/web_applications/test/js_library_test.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
chromeos/components/web_applications/test/js_library_test.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |