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

Side by Side 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: add IDENTITY_GETACCOUNTS to histograms.xml 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <set>
6 #include <string>
7 #include <vector>
8
5 #include "base/command_line.h" 9 #include "base/command_line.h"
6 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
7 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
8 #include "base/values.h" 12 #include "base/values.h"
9 #include "chrome/browser/chrome_notification_types.h" 13 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/identity/identity_api.h" 14 #include "chrome/browser/extensions/api/identity/identity_api.h"
11 #include "chrome/browser/extensions/component_loader.h" 15 #include "chrome/browser/extensions/component_loader.h"
12 #include "chrome/browser/extensions/extension_apitest.h" 16 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/extensions/extension_browsertest.h" 17 #include "chrome/browser/extensions/extension_browsertest.h"
14 #include "chrome/browser/extensions/extension_function_test_utils.h" 18 #include "chrome/browser/extensions/extension_function_test_utils.h"
15 #include "chrome/browser/extensions/extension_service.h" 19 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/signin/signin_manager_factory.h" 21 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_window.h" 23 #include "chrome/browser/ui/browser_window.h"
20 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/extensions/api/identity.h"
21 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h" 26 #include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
22 #include "chrome/test/base/in_process_browser_test.h" 27 #include "chrome/test/base/in_process_browser_test.h"
23 #include "chrome/test/base/test_switches.h" 28 #include "chrome/test/base/test_switches.h"
24 #include "components/signin/core/browser/signin_manager.h" 29 #include "components/signin/core/browser/signin_manager.h"
30 #include "components/signin/core/common/profile_management_switches.h"
25 #include "content/public/browser/notification_service.h" 31 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h" 32 #include "content/public/browser/notification_source.h"
27 #include "content/public/test/test_utils.h" 33 #include "content/public/test/test_utils.h"
28 #include "extensions/common/id_util.h" 34 #include "extensions/common/id_util.h"
29 #include "google_apis/gaia/google_service_auth_error.h" 35 #include "google_apis/gaia/google_service_auth_error.h"
30 #include "google_apis/gaia/oauth2_mint_token_flow.h" 36 #include "google_apis/gaia/oauth2_mint_token_flow.h"
31 #include "grit/browser_resources.h" 37 #include "grit/browser_resources.h"
32 #include "net/test/spawned_test_server/spawned_test_server.h" 38 #include "net/test/spawned_test_server/spawned_test_server.h"
33 #include "testing/gmock/include/gmock/gmock.h" 39 #include "testing/gmock/include/gmock/gmock.h"
34 #include "testing/gtest/include/gtest/gtest.h" 40 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 std::string scope_ui_oauth_error_; 343 std::string scope_ui_oauth_error_;
338 bool login_ui_shown_; 344 bool login_ui_shown_;
339 bool scope_ui_shown_; 345 bool scope_ui_shown_;
340 }; 346 };
341 347
342 class MockQueuedMintRequest : public IdentityMintRequestQueue::Request { 348 class MockQueuedMintRequest : public IdentityMintRequestQueue::Request {
343 public: 349 public:
344 MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType)); 350 MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType));
345 }; 351 };
346 352
353 class IdentityGetAccountsFunctionTest : public ExtensionBrowserTest {
354 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
355 command_line->AppendSwitch(switches::kExtensionsMultiAccount);
356 }
357
358 protected:
359 void SetAccountState(AccountIds ids, bool is_signed_in) {
360 IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
361 ids, is_signed_in);
362 }
363
364 AccountIds CreateIds(std::string email, std::string obfid) {
365 AccountIds ids;
366 ids.account_key = email;
367 ids.email = email;
368 ids.gaia = obfid;
369 return ids;
370 }
371
372 testing::AssertionResult ExpectGetAccounts(
373 const std::vector<std::string>& accounts) {
374 scoped_refptr<IdentityGetAccountsFunction> func(
375 new IdentityGetAccountsFunction);
376 func->set_extension(utils::CreateEmptyExtension(kExtensionId).get());
377 if (!utils::RunFunction(
378 func.get(), std::string("[]"), browser(), utils::NONE)) {
379 return GenerateFailureResult(accounts, NULL)
380 << "getAccounts did not return a result.";
381 }
382 const base::ListValue* results = func->GetResultList();
383 if (!results || (results->GetSize() != accounts.size()))
384 return GenerateFailureResult(accounts, results);
385
386 std::set<std::string> result_ids;
387 for (base::ListValue::const_iterator it = results->begin();
388 it != results->end();
389 ++it) {
390 scoped_ptr<api::identity::AccountInfo> info =
391 api::identity::AccountInfo::FromValue(**it);
392 if (info.get())
393 result_ids.insert(info->id);
394 else
395 return GenerateFailureResult(accounts, results);
396 }
397
398 for (std::vector<std::string>::const_iterator it = accounts.begin();
399 it != accounts.end();
400 ++it) {
401 if (result_ids.find(*it) == result_ids.end())
402 return GenerateFailureResult(accounts, results);
403 }
404
405 return testing::AssertionResult(true);
406 }
407
408 testing::AssertionResult GenerateFailureResult(
409 const ::std::vector<std::string>& accounts,
410 const base::ListValue* results) {
411 testing::Message msg("Expected: ");
412 for (std::vector<std::string>::const_iterator it = accounts.begin();
413 it != accounts.end();
414 ++it) {
415 msg << *it << " ";
416 }
417 msg << "Actual: ";
418 if (!results) {
419 msg << "NULL";
420 } else {
421 for (base::ListValue::const_iterator it = results->begin();
422 it != results->end();
423 ++it) {
424 scoped_ptr<api::identity::AccountInfo> info =
425 api::identity::AccountInfo::FromValue(**it);
426 if (info.get())
427 msg << info->id << " ";
428 else
429 msg << *it << "<-" << (*it)->GetType() << " ";
430 }
431 }
432
433 return testing::AssertionFailure(msg);
434 }
435 };
436
437 IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, MultiAccountOn) {
438 EXPECT_TRUE(switches::IsExtensionsMultiAccount());
439 }
440
441 IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, NoneSignedIn) {
442 EXPECT_TRUE(ExpectGetAccounts(std::vector<std::string>()));
443 }
444
445 IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest,
446 PrimaryAccountSignedIn) {
447 SetAccountState(CreateIds("primary@example.com", "1"), true);
448 std::vector<std::string> primary;
449 primary.push_back("1");
450 EXPECT_TRUE(ExpectGetAccounts(primary));
451 }
452
453 IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, TwoAccountsSignedIn) {
454 SetAccountState(CreateIds("primary@example.com", "1"), true);
455 SetAccountState(CreateIds("secondary@example.com", "2"), true);
456 std::vector<std::string> two_accounts;
457 two_accounts.push_back("1");
458 two_accounts.push_back("2");
459 EXPECT_TRUE(ExpectGetAccounts(two_accounts));
460 }
461
462 class IdentityOldProfilesGetAccountsFunctionTest
463 : public IdentityGetAccountsFunctionTest {
464 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
465 // Don't add the multi-account switch that parent class would have.
466 }
467 };
468
469 IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest,
470 MultiAccountOff) {
471 EXPECT_FALSE(switches::IsExtensionsMultiAccount());
472 }
473
474 IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest,
475 TwoAccountsSignedIn) {
476 SetAccountState(CreateIds("primary@example.com", "1"), true);
477 SetAccountState(CreateIds("secondary@example.com", "2"), true);
478 std::vector<std::string> only_primary;
479 only_primary.push_back("1");
480 EXPECT_TRUE(ExpectGetAccounts(only_primary));
481 }
482
347 class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest { 483 class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest {
348 protected: 484 protected:
349 enum OAuth2Fields { 485 enum OAuth2Fields {
350 NONE = 0, 486 NONE = 0,
351 CLIENT_ID = 1, 487 CLIENT_ID = 1,
352 SCOPES = 2, 488 SCOPES = 2,
353 AS_COMPONENT = 4 489 AS_COMPONENT = 4
354 }; 490 };
355 491
356 virtual ~GetAuthTokenFunctionTest() {} 492 virtual ~GetAuthTokenFunctionTest() {}
(...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after
1328 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"), 1464 EXPECT_EQ(std::string("https://abcdefghij.chromiumapp.org/callback#test"),
1329 url); 1465 url);
1330 } 1466 }
1331 1467
1332 } // namespace extensions 1468 } // namespace extensions
1333 1469
1334 // Tests the chrome.identity API implemented by custom JS bindings . 1470 // Tests the chrome.identity API implemented by custom JS bindings .
1335 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) { 1471 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeIdentityJsBindings) {
1336 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_; 1472 ASSERT_TRUE(RunExtensionTest("identity/js_bindings")) << message_;
1337 } 1473 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698