Skip to content

Commit

Permalink
Add LoginScreenExtensionUiHandlerBrowsertest
Browse files Browse the repository at this point in the history
The browser test checks that an installed extension with permissions
can call the chrome.loginScreenUi APIs.

The tests are:
- check that a window can be opened
- check that multiple windows cannot be opened
- check that a window can be opened and closed
- check that calling close fails when there is no open window

whitelisted_extension has been updated for the test logic.

Change-Id: I51be44d8bcf2089e77d39b8e4f2753e80576f7a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1640406
Reviewed-by: Alexander Hendrich <hendrich@chromium.org>
Reviewed-by: Devlin <rdevlin.cronin@chromium.org>
Commit-Queue: Jit Yao Yap <jityao@google.com>
Cr-Commit-Position: refs/heads/master@{#669623}
  • Loading branch information
Jit Yao Yap authored and Commit Bot committed Jun 17, 2019
1 parent b127cb7 commit f89d5ff
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2019 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 "chrome/browser/chromeos/extensions/login_screen_ui/login_screen_extension_ui_handler.h"

#include <memory>
#include <string>

#include "base/logging.h"
#include "base/macros.h"
#include "chrome/browser/chromeos/extensions/login_screen_ui/login_screen_extension_ui_handler.h"
#include "chrome/browser/chromeos/policy/signin_profile_extensions_policy_test_base.h"
#include "components/version_info/version_info.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// The extension files can be found under the directory
// chrome/test/data/extensions/api_test/login_screen_ui/whitelisted_extension/
// This extension has been whitelisted to use the chrome.loginScreenUi API
// under chrome/common/extensions/api/_permission_features.json
const char kExtensionId[] = "oclffehlkdgibkainkilopaalpdobkan";
const char kExtensionUpdateManifestPath[] =
"/extensions/api_test/login_screen_ui/update_manifest.xml";

const char kWaitingForTestName[] = "Waiting for test name";

const char kCanOpenWindow[] = "CanOpenWindow";
const char kCannotOpenMultipleWindows[] = "CannotOpenMultipleWindows";
const char kCanOpenAndCloseWindow[] = "CanOpenAndCloseWindow";
const char kCannotCloseNoWindow[] = "CannotCloseNoWindow";

} // namespace

namespace chromeos {

class LoginScreenExtensionUiHandlerBrowsertest
: public policy::SigninProfileExtensionsPolicyTestBase {
public:
LoginScreenExtensionUiHandlerBrowsertest()
: SigninProfileExtensionsPolicyTestBase(version_info::Channel::DEV) {}

~LoginScreenExtensionUiHandlerBrowsertest() override = default;

void SetUpExtensionAndRunTest(std::string testName) {
extensions::ResultCatcher catcher;

ExtensionTestMessageListener listener(kWaitingForTestName,
/*will_reply=*/true);

AddExtensionForForceInstallation(kExtensionId,
kExtensionUpdateManifestPath);

ASSERT_TRUE(listener.WaitUntilSatisfied());
listener.Reply(testName);

ASSERT_TRUE(catcher.GetNextResult());
}

bool HasOpenWindow() {
LoginScreenExtensionUiHandler* ui_handler =
LoginScreenExtensionUiHandler::Get(false);
CHECK(ui_handler);
return ui_handler->HasOpenWindow(kExtensionId);
}

private:
DISALLOW_COPY_AND_ASSIGN(LoginScreenExtensionUiHandlerBrowsertest);
};

IN_PROC_BROWSER_TEST_F(LoginScreenExtensionUiHandlerBrowsertest,
ExtensionCanOpenWindow) {
SetUpExtensionAndRunTest(kCanOpenWindow);
EXPECT_TRUE(HasOpenWindow());
}

IN_PROC_BROWSER_TEST_F(LoginScreenExtensionUiHandlerBrowsertest,
ExtensionCannotOpenMultipleWindows) {
SetUpExtensionAndRunTest(kCannotOpenMultipleWindows);
EXPECT_TRUE(HasOpenWindow());
}

IN_PROC_BROWSER_TEST_F(LoginScreenExtensionUiHandlerBrowsertest,
ExtensionCanOpenAndCloseWindow) {
SetUpExtensionAndRunTest(kCanOpenAndCloseWindow);
EXPECT_FALSE(HasOpenWindow());
}

IN_PROC_BROWSER_TEST_F(LoginScreenExtensionUiHandlerBrowsertest,
ExtensionCannotCloseNoWindow) {
SetUpExtensionAndRunTest(kCannotCloseNoWindow);
EXPECT_FALSE(HasOpenWindow());
}

} // namespace chromeos
1 change: 1 addition & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,7 @@ if (!is_android) {
"../browser/chromeos/extensions/incoming_native_messaging_apitest.cc",
"../browser/chromeos/extensions/info_private_apitest.cc",
"../browser/chromeos/extensions/input_method_apitest_chromeos.cc",
"../browser/chromeos/extensions/login_screen_ui/login_screen_extension_ui_handler_browsertest.cc",
"../browser/chromeos/extensions/login_screen_ui/login_screen_ui_apitest.cc",
"../browser/chromeos/extensions/users_private/users_private_apitest.cc",
"../browser/chromeos/extensions/wallpaper_apitest.cc",
Expand Down
5 changes: 3 additions & 2 deletions chrome/test/data/extensions/api_test/login_screen_ui/README
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ The not_whitelisted_extension.crx can be packed using the private key
./not_whitelisted_extension.pem. For the instructions, refer to:
<https://developer.chrome.com/extensions/packaging#creating>.

These extensions are intended to be used for the manual testing of the
chrome.loginScreenUi API.
These extensions are used in the API tests and browser tests for the
chrome.loginScreenUi API. The code for the tests can be found under the
chrome/browser/chromeos/extensions/login_screen_ui/ directory.

In case a new version of the test extension has to be published on WebStore,
please reach out to the managed-devices@ mailing list.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version='1.0' encoding='UTF-8'?>
<!--
This update manifest points to the ./extension.crx file. "mock.http" is a
placeholder that gets substituted with the test server address in runtime.
-->
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
<app appid='oclffehlkdgibkainkilopaalpdobkan'>
<updatecheck
codebase='http://mock.http/extensions/api_test/login_screen_ui/whitelisted_extension.crx'
version='1.10' />
</app>
</gupdate>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2019 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.

var cannotCreateMultipleWindowsErrorMessage =
"Can't create more than one window per extension.";
var cannotCloseNoWindowErrorMessage = "No open window to close.";

var tests = {
'CanOpenWindow': function CanOpenWindow() {
chrome.loginScreenUi.show({url: 'some/path.html'}, function() {
chrome.test.assertNoLastError();
chrome.test.succeed();
});
},
'CannotOpenMultipleWindows': function CannotOpenMultipleWindows() {
chrome.loginScreenUi.show({url: 'some/path.html'}, function() {
chrome.test.assertNoLastError();
chrome.loginScreenUi.show({url: 'some/path.html'}, function() {
chrome.test.assertLastError(cannotCreateMultipleWindowsErrorMessage);
chrome.test.succeed();
});
});
},
'CanOpenAndCloseWindow': function CanOpenAndCloseWindow() {
chrome.loginScreenUi.show({url: 'some/path.html'}, function() {
chrome.test.assertNoLastError();
chrome.loginScreenUi.close(function() {
chrome.test.assertNoLastError();
chrome.test.succeed();
});
});
},
'CannotCloseNoWindow': function CannotCloseNoWindow() {
chrome.loginScreenUi.close(function() {
chrome.test.assertLastError(cannotCloseNoWindowErrorMessage);
chrome.test.succeed();
});
},
};

// |waitForTestName()| waits for the browser test to reply with a test name and
// runs the specified test. The browser test logic can be found at
// chrome/browser/chromeos/extensions/login_screen_ui/
// login_screen_extension_ui_handler_browsertest.cc
function waitForTestName(testName) {
// No observer for NOTIFICATION_EXTENSION_TEST_MESSAGE or observer did not
// reply with a test name.
// This check is to prevent the API test under login_screen_ui_apitest.cc
// from failing. This check can be removed once the API is stable and the API
// test is no longer needed.
if (testName === '') {
return;
}

if (!tests.hasOwnProperty(testName) ||
typeof tests[testName] !== 'function') {
chrome.test.fail('Test not found: ' + testName);
return;
}

chrome.test.runTests([tests[testName]]);
}

chrome.test.sendMessage('Waiting for test name', waitForTestName);
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"name": "chrome.loginScreenUi extension tests",
"version": "1.3",
"version": "1.10",
"manifest_version": 2,
"description": "Test of chrome.loginScreenUi interface",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"loginScreenUi"
]
Expand Down
3 changes: 2 additions & 1 deletion extensions/common/api/_behavior_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@
"location": "policy",
"platforms": ["chromeos"],
"whitelist": [
"FA84F98B32AFC3013F5711F8711F8F38DB210AB7" // Sign-in Screen Test Extension
"FA84F98B32AFC3013F5711F8711F8F38DB210AB7", // Sign-in Screen Test Extension
"7FE4A999359A456C4B0FB7B7AD85CEA29CA50519" // chrome.loginScreenUi Test Extension
]
}],
"allow_deprecated_audio_api": {
Expand Down

0 comments on commit f89d5ff

Please sign in to comment.