Commit 6a72afce authored by liyanhou@chromium.org's avatar liyanhou@chromium.org

Add a browser test for functions in notification provider API

Add a basic browser test for functions in notification provider API.
This is mainly to test the connection and provide a skeleton for future
tests. The functions are not fully implemented yet, and more tests will
be added as the functions are implemented.

BUG=

Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=287692

Review URL: https://codereview.chromium.org/416423006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287972 0039d316-1c4b-4281-b951-d872f2087c98
parent cf8c4908
......@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/extensions/api/notification_provider/notification_provider_api.h"
#include "chrome/browser/extensions/chrome_extension_function.h"
#include "chrome/browser/extensions/extension_apitest.h"
......@@ -11,16 +10,27 @@
typedef ExtensionApiTest NotificationProviderApiTest;
namespace {
void CreateNotificationOptionsForTest(
extensions::api::notifications::NotificationOptions* options) {
options->type = extensions::api::notifications::ParseTemplateType("basic");
options->icon_url = scoped_ptr<std::string>(new std::string("icon.png"));
options->title = scoped_ptr<std::string>(new std::string("Title"));
options->message =
scoped_ptr<std::string>(new std::string("Here goes the message"));
return;
}
} // namespace
IN_PROC_BROWSER_TEST_F(NotificationProviderApiTest, Events) {
std::string sender_id1 = "SenderId1";
std::string notification_id1 = "NotificationId1";
extensions::api::notifications::NotificationOptions options;
options.type = extensions::api::notifications::ParseTemplateType("basic");
options.icon_url = scoped_ptr<std::string>(new std::string("icon.png"));
options.title = scoped_ptr<std::string>(new std::string("Title"));
options.message =
scoped_ptr<std::string>(new std::string("Here goes the message"));
scoped_ptr<extensions::api::notifications::NotificationOptions> options(
new extensions::api::notifications::NotificationOptions());
CreateNotificationOptionsForTest(options.get());
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
......@@ -34,11 +44,37 @@ IN_PROC_BROWSER_TEST_F(NotificationProviderApiTest, Events) {
new extensions::NotificationProviderEventRouter(browser()->profile()));
event_router->CreateNotification(
extension->id(), sender_id1, notification_id1, options);
extension->id(), sender_id1, notification_id1, *options);
event_router->UpdateNotification(
extension->id(), sender_id1, notification_id1, options);
extension->id(), sender_id1, notification_id1, *options);
event_router->ClearNotification(
extension->id(), sender_id1, notification_id1);
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
IN_PROC_BROWSER_TEST_F(NotificationProviderApiTest, TestBasicUsage) {
// set up content of a notification
std::string sender_id1 = "SenderId";
std::string notification_id1 = "NotificationId";
scoped_ptr<extensions::api::notifications::NotificationOptions> options(
new extensions::api::notifications::NotificationOptions());
CreateNotificationOptionsForTest(options.get());
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
// Test notification provider extension
const extensions::Extension* extension = LoadExtension(
test_data_dir_.AppendASCII("notification_provider/basic_usage"));
ASSERT_TRUE(extension);
scoped_ptr<extensions::NotificationProviderEventRouter> event_router(
new extensions::NotificationProviderEventRouter(browser()->profile()));
event_router->CreateNotification(
extension->id(), sender_id1, notification_id1, *options);
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
// Copyright 2014 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.
const notificationProvider = chrome.notificationProvider;
function notifyOnCleared(senderId, notificationId) {
return new Promise(function (resolve, reject) {
notificationProvider.notifyOnCleared(senderId,
notificationId,
function (wasCleared) {
if (chrome.runtime.lastError || !wasCleared) {
reject(new Error("Unable to send onClear message"));
return;
}
resolve(wasCleared);
return;
});
});
};
function notifyOnClicked(senderId, notificationId) {
return new Promise(function (resolve, reject) {
notificationProvider.notifyOnClicked(senderId,
notificationId,
function (matchExists) {
if (chrome.runtime.lastError || !matchExists) {
reject(new Error("Unable to send onClick message"));
return;
}
resolve(matchExists);
return;
});
});
};
function notifyOnButtonClicked(senderId, notificationId, buttonIndex) {
return new Promise(function (resolve, reject) {
notificationProvider.notifyOnButtonClicked(senderId,
notificationId,
buttonIndex,
function (matchExists) {
if (chrome.runtime.lastError || !matchExists) {
reject(new Error("Unable to send onButtonClick message"));
return;
}
resolve(matchExists);
return;
});
});
};
function notifyOnPermissionLevelChanged(senderId, permissionLevel) {
return new Promise(function (resolve, reject) {
notificationProvider.notifyOnPermissionLevelChanged(
senderId,
permissionLevel,
function (notifierExists) {
if (chrome.runtime.lastError || !notifierExists) {
reject(new Error("Unable to send onPermissionLevelChanged message"));
return;
}
resolve(notifierExists);
return;
});
});
};
function notifyOnShowSettings(senderId) {
return new Promise(function (resolve, reject) {
notificationProvider.notifyOnShowSettings(senderId,
function (notifierExists) {
if (chrome.runtime.lastError || !notifierExists) {
reject(new Error("Unable to send onShowSettings message"));
return;
}
resolve(notifierExists);
return;
});
});
};
function failTest(testName) {
chrome.test.fail(testName);
};
function testFunctions() {
var testName = "testFunctions";
var notifierId;
var notId;
notificationProvider.onCreated.addListener(function(senderId,
notificationId,
content) {
notifierId = senderId;
notId = notificationId;
notifyOnClicked(notifierId, notId)
.catch(function() { failTest("NotifyOnClickedFailed"); })
.then(function() { return notifyOnButtonClicked(notifierId, notId, 0); })
.catch(function() { failTest("NotifyOnButtonClickedFailed"); })
.then(function () { return notifyOnCleared(notifierId, notId); })
.catch(function() { failTest("NotifyOnClearedFailed"); })
.then(function () { return notifyOnPermissionLevelChanged(notifierId,
"granted"); })
.catch(function() { failTest("NotifyOnPermissionLevelChangedFailed"); })
.then(function () { return notifyOnShowSettings(notifierId); })
.catch(function() { failTest("NotifyOnShowSettingsFailed"); })
.then(function() { chrome.test.succeed(testName); });
});
};
chrome.test.runTests([ testFunctions ]);
{
"name": "chrome.notificationProvider",
"version": "0.1",
"description": "Tests chrome.notificationProvider API functions",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"permissions": [
"notificationProvider"
]
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment