Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3636)

Unified Diff: chrome/browser/extensions/api/identity/identity_apitest.cc

Issue 274853002: Identity API: add chrome.identity.getAccounts (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/identity/identity_apitest.cc
diff --git a/chrome/browser/extensions/api/identity/identity_apitest.cc b/chrome/browser/extensions/api/identity/identity_apitest.cc
index 48a392ac6e008bacedd3b032132a18ed9ed61198..942e66351ac8ee78b81e11a9262ca7543422b374 100644
--- a/chrome/browser/extensions/api/identity/identity_apitest.cc
+++ b/chrome/browser/extensions/api/identity/identity_apitest.cc
@@ -2,6 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <set>
+#include <string>
+#include <vector>
+
#include "base/command_line.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
@@ -18,10 +22,12 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_switches.h"
+#include "chrome/common/extensions/api/identity.h"
#include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/test_switches.h"
#include "components/signin/core/browser/signin_manager.h"
+#include "components/signin/core/common/profile_management_switches.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/test/test_utils.h"
@@ -344,6 +350,136 @@ class MockQueuedMintRequest : public IdentityMintRequestQueue::Request {
MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType));
};
+class IdentityGetAccountsFunctionTest : public ExtensionBrowserTest {
+ virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
+ command_line->AppendSwitch(switches::kExtensionsMultiAccount);
+ }
+
+ protected:
+ void SetAccountState(AccountIds ids, bool is_signed_in) {
+ IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
+ ids, is_signed_in);
+ }
+
+ AccountIds CreateIds(std::string email, std::string obfid) {
+ AccountIds ids;
+ ids.account_key = email;
+ ids.email = email;
+ ids.gaia = obfid;
+ return ids;
+ }
+
+ testing::AssertionResult ExpectGetAccounts(
+ const std::vector<std::string>& accounts) {
+ scoped_refptr<IdentityGetAccountsFunction> func(
+ new IdentityGetAccountsFunction);
+ func->set_extension(utils::CreateEmptyExtension(kExtensionId).get());
+ if (!utils::RunFunction(
+ func.get(), std::string("[]"), browser(), utils::NONE)) {
+ return GenerateFailureResult(accounts, NULL)
+ << "getAccounts did not return a result.";
+ }
+ const base::ListValue* results = func->GetResultList();
+ if (!results || (results->GetSize() != accounts.size()))
+ return GenerateFailureResult(accounts, results);
+
+ std::set<std::string> result_ids;
+ for (base::ListValue::const_iterator it = results->begin();
+ it != results->end();
+ ++it) {
+ scoped_ptr<api::identity::AccountInfo> info =
+ api::identity::AccountInfo::FromValue(**it);
+ if (info.get())
+ result_ids.insert(info->id);
+ else
+ return GenerateFailureResult(accounts, results);
+ }
+
+ for (std::vector<std::string>::const_iterator it = accounts.begin();
+ it != accounts.end();
+ ++it) {
+ if (result_ids.find(*it) == result_ids.end())
+ return GenerateFailureResult(accounts, results);
+ }
+
+ return testing::AssertionResult(true);
+ }
+
+ testing::AssertionResult GenerateFailureResult(
+ const ::std::vector<std::string>& accounts,
+ const base::ListValue* results) {
+ testing::Message msg("Expected: ");
+ for (std::vector<std::string>::const_iterator it = accounts.begin();
+ it != accounts.end();
+ ++it) {
+ msg << *it << " ";
+ }
+ msg << "Actual: ";
+ if (!results) {
+ msg << "NULL";
+ } else {
+ for (base::ListValue::const_iterator it = results->begin();
+ it != results->end();
+ ++it) {
+ scoped_ptr<api::identity::AccountInfo> info =
+ api::identity::AccountInfo::FromValue(**it);
+ if (info.get())
+ msg << info->id << " ";
+ else
+ msg << *it << "<-" << (*it)->GetType() << " ";
+ }
+ }
+
+ return testing::AssertionFailure(msg);
+ }
+};
+
+IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, MultiAccountOn) {
+ EXPECT_TRUE(switches::IsExtensionsMultiAccount());
+}
+
+IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, NoneSignedIn) {
+ EXPECT_TRUE(ExpectGetAccounts(std::vector<std::string>()));
+}
+
+IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest,
+ PrimaryAccountSignedIn) {
+ SetAccountState(CreateIds("primary@example.com", "1"), true);
+ std::vector<std::string> primary;
+ primary.push_back("1");
+ EXPECT_TRUE(ExpectGetAccounts(primary));
+}
+
+IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, TwoAccountsSignedIn) {
+ SetAccountState(CreateIds("primary@example.com", "1"), true);
+ SetAccountState(CreateIds("secondary@example.com", "2"), true);
+ std::vector<std::string> two_accounts;
+ two_accounts.push_back("1");
+ two_accounts.push_back("2");
+ EXPECT_TRUE(ExpectGetAccounts(two_accounts));
+}
+
+class IdentityOldProfilesGetAccountsFunctionTest
+ : public IdentityGetAccountsFunctionTest {
+ virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
+ // Don't add the multi-account switch that parent class would have.
+ }
+};
+
+IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest,
+ MultiAccountOff) {
+ EXPECT_FALSE(switches::IsExtensionsMultiAccount());
+}
+
+IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest,
+ TwoAccountsSignedIn) {
+ SetAccountState(CreateIds("primary@example.com", "1"), true);
+ SetAccountState(CreateIds("secondary@example.com", "2"), true);
+ std::vector<std::string> only_primary;
+ only_primary.push_back("1");
+ EXPECT_TRUE(ExpectGetAccounts(only_primary));
+}
+
class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest {
protected:
enum OAuth2Fields {
« no previous file with comments | « chrome/browser/extensions/api/identity/identity_api.cc ('k') | chrome/common/extensions/api/_api_features.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698