15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <set>
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <string>
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <vector>
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
9a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "base/command_line.h"
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "base/prefs/pref_service.h"
115e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string_util.h"
125e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/stringprintf.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/api/identity/identity_api.h"
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/extensions/component_loader.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_apitest.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_browsertest.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/extension_function_test_utils.h"
20b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/signin/account_reconcilor_factory.h"
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/signin/fake_account_reconcilor.h"
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
2546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
2646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/signin/fake_signin_manager.h"
2746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
31b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/common/extensions/api/identity.h"
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/common/extensions/api/identity/oauth2_manifest_handler.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
35a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)#include "chrome/test/base/test_switches.h"
3603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "components/crx_file/id_util.h"
37e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/signin/core/common/profile_management_switches.h"
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/signin/core/common/signin_pref_names.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_service.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_utils.h"
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "extensions/browser/guest_view/guest_view_base.h"
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/common/test_util.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "google_apis/gaia/google_service_auth_error.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "google_apis/gaia/oauth2_mint_token_flow.h"
47b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)#include "net/test/spawned_test_server/spawned_test_server.h"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "url/gurl.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::_;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::Return;
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::ReturnRef;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace errors = identity_constants;
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace utils = extension_function_test_utils;
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const char kAccessToken[] = "auth_token";
64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)static const char kExtensionId[] = "ext_id";
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
66010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// This helps us be able to wait until an UIThreadExtensionFunction calls
67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// SendResponse.
68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class SendResponseDelegate
69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    : public UIThreadExtensionFunction::DelegateForTests {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  SendResponseDelegate() : should_post_quit_(false) {}
72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual ~SendResponseDelegate() {}
74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void set_should_post_quit(bool should_quit) {
76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    should_post_quit_ = should_quit;
77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool HasResponse() {
80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return response_.get() != NULL;
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool GetResponse() {
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_TRUE(HasResponse());
85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return *response_.get();
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void OnSendResponse(UIThreadExtensionFunction* function,
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              bool success,
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                              bool bad_message) OVERRIDE {
91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ASSERT_FALSE(bad_message);
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    ASSERT_FALSE(HasResponse());
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    response_.reset(new bool);
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    *response_ = success;
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (should_post_quit_) {
9690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      base::MessageLoopForUI::current()->Quit();
97c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
98c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<bool> response_;
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool should_post_quit_;
103c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
104c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
105c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class AsyncExtensionBrowserTest : public ExtensionBrowserTest {
106c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) protected:
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Asynchronous function runner allows tests to manipulate the browser window
108c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // after the call happens.
109c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void RunFunctionAsync(
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      UIThreadExtensionFunction* function,
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      const std::string& args) {
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    response_delegate_.reset(new SendResponseDelegate);
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    function->set_test_delegate(response_delegate_.get());
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    scoped_ptr<base::ListValue> parsed_args(utils::ParseList(args));
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_TRUE(parsed_args.get()) <<
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        "Could not parse extension function arguments: " << args;
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    function->SetArgs(parsed_args.get());
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (!function->extension()) {
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      scoped_refptr<Extension> empty_extension(
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          test_util::CreateEmptyExtension());
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      function->set_extension(empty_extension.get());
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    function->set_browser_context(browser()->profile());
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    function->set_has_callback(true);
127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    function->Run()->Execute();
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string WaitForError(UIThreadExtensionFunction* function) {
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunMessageLoopUntilResponse();
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_FALSE(function->GetResultList()) << "Did not expect a result";
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return function->GetError();
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::Value* WaitForSingleResult(UIThreadExtensionFunction* function) {
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    RunMessageLoopUntilResponse();
138c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_TRUE(function->GetError().empty()) << "Unexpected error: "
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                              << function->GetError();
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const base::Value* single_result = NULL;
141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (function->GetResultList() != NULL &&
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        function->GetResultList()->Get(0, &single_result)) {
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return single_result->DeepCopy();
144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return NULL;
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) private:
149c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void RunMessageLoopUntilResponse() {
150010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // If the RunAsync of |function| didn't already call SendResponse, run the
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // message loop until they do.
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!response_delegate_->HasResponse()) {
153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      response_delegate_->set_should_post_quit(true);
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      content::RunMessageLoop();
155c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    EXPECT_TRUE(response_delegate_->HasResponse());
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
159c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<SendResponseDelegate> response_delegate_;
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
162e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass TestHangOAuth2MintTokenFlow : public OAuth2MintTokenFlow {
163e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch public:
164e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  TestHangOAuth2MintTokenFlow()
165e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      : OAuth2MintTokenFlow(NULL, NULL, OAuth2MintTokenFlow::Parameters()) {}
166e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
167e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual void Start() OVERRIDE {
168e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    // Do nothing, simulating a hanging network call.
169e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
170e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch};
171e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestOAuth2MintTokenFlow : public OAuth2MintTokenFlow {
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum ResultType {
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ISSUE_ADVICE_SUCCESS,
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MINT_TOKEN_SUCCESS,
177c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    MINT_TOKEN_FAILURE,
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    MINT_TOKEN_BAD_CREDENTIALS,
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    MINT_TOKEN_SERVICE_ERROR
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TestOAuth2MintTokenFlow(ResultType result,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          OAuth2MintTokenFlow::Delegate* delegate)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : OAuth2MintTokenFlow(NULL, delegate, OAuth2MintTokenFlow::Parameters()),
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result_(result),
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      delegate_(delegate) {
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Start() OVERRIDE {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    switch (result_) {
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case ISSUE_ADVICE_SUCCESS: {
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        IssueAdviceInfo info;
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        delegate_->OnIssueAdviceSuccess(info);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MINT_TOKEN_SUCCESS: {
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        delegate_->OnMintTokenSuccess(kAccessToken, 3600);
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      case MINT_TOKEN_FAILURE: {
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        delegate_->OnMintTokenFailure(error);
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        break;
204c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      }
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      case MINT_TOKEN_BAD_CREDENTIALS: {
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        GoogleServiceAuthError error(
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        delegate_->OnMintTokenFailure(error);
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        break;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      case MINT_TOKEN_SERVICE_ERROR: {
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GoogleServiceAuthError error =
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            GoogleServiceAuthError::FromServiceError("invalid_scope");
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        delegate_->OnMintTokenFailure(error);
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        break;
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      }
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ResultType result_;
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  OAuth2MintTokenFlow::Delegate* delegate_;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Waits for a specific GURL to generate a NOTIFICATION_LOAD_STOP event and
2267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// saves a pointer to the window embedding the WebContents, which can be later
2277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// closed.
228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class WaitForGURLAndCloseWindow : public content::WindowedNotificationObserver {
229868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) public:
230868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  explicit WaitForGURLAndCloseWindow(GURL url)
231868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      : WindowedNotificationObserver(
232868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            content::NOTIFICATION_LOAD_STOP,
233868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)            content::NotificationService::AllSources()),
234868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        url_(url) {}
235868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
236868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // NotificationObserver:
237868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void Observe(int type,
238868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       const content::NotificationSource& source,
239868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE {
240868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content::NavigationController* web_auth_flow_controller =
241868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        content::Source<content::NavigationController>(source).ptr();
242868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    content::WebContents* web_contents =
243868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        web_auth_flow_controller->GetWebContents();
244868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
245868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    if (web_contents->GetURL() == url_) {
2467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // It is safe to keep the pointer here, because we know in a test, that
2477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // the WebContents won't go away before CloseEmbedderWebContents is
2487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      // called. Don't copy this code to production.
249cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      GuestViewBase* guest = GuestViewBase::FromWebContents(web_contents);
250cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      embedder_web_contents_ = guest->embedder_web_contents();
251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Condtionally invoke parent class so that Wait will not exit
252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // until the target URL arrives.
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      content::WindowedNotificationObserver::Observe(type, source, details);
254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    }
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Closes the window embedding the WebContents. The action is separated from
2587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // the Observe method to make sure the list of observers is not deleted,
2597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // while some event is already being processed. (That causes ASAN failures.)
2607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void CloseEmbedderWebContents() {
2617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (embedder_web_contents_)
2627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      embedder_web_contents_->Close();
2637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
265868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
266868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  GURL url_;
2677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  content::WebContents* embedder_web_contents_;
268868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
269868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
27246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class FakeGetAuthTokenFunction : public IdentityGetAuthTokenFunction {
27346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
27446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  FakeGetAuthTokenFunction()
27546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      : login_access_token_result_(true),
2765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        auto_login_access_token_(true),
27746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        login_ui_result_(true),
27846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        scope_ui_result_(true),
27946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        login_ui_shown_(false),
28046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        scope_ui_shown_(false) {}
28146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
28246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void set_login_access_token_result(bool result) {
28346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    login_access_token_result_ = result;
28446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
28546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void set_auto_login_access_token(bool automatic) {
2875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    auto_login_access_token_ = automatic;
2885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
29046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void set_login_ui_result(bool result) { login_ui_result_ = result; }
29146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
29246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void set_mint_token_flow(scoped_ptr<OAuth2MintTokenFlow> flow) {
29346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    flow_ = flow.Pass();
29446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
29546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
2965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void set_mint_token_result(TestOAuth2MintTokenFlow::ResultType result_type) {
2975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    set_mint_token_flow(scoped_ptr<TestOAuth2MintTokenFlow>(
2985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                            new TestOAuth2MintTokenFlow(result_type, this))
2995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                            .PassAs<OAuth2MintTokenFlow>());
3005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
3015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
30246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void set_scope_ui_failure(GaiaWebAuthFlow::Failure failure) {
30346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scope_ui_result_ = false;
30446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scope_ui_failure_ = failure;
30546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
30646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
30746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void set_scope_ui_oauth_error(const std::string& oauth_error) {
30846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scope_ui_result_ = false;
30946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scope_ui_failure_ = GaiaWebAuthFlow::OAUTH_ERROR;
31046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scope_ui_oauth_error_ = oauth_error;
31146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
31246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
31346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool login_ui_shown() const { return login_ui_shown_; }
31446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
31546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool scope_ui_shown() const { return scope_ui_shown_; }
31646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
31746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string login_access_token() const { return login_access_token_; }
31846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void StartLoginAccessTokenRequest() OVERRIDE {
3205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (auto_login_access_token_) {
3215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      if (login_access_token_result_) {
3225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        OnGetTokenSuccess(login_token_request_.get(),
3235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                          "access_token",
3245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                          base::Time::Now() + base::TimeDelta::FromHours(1LL));
3255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      } else {
3265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        GoogleServiceAuthError error(
3275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
3285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        OnGetTokenFailure(login_token_request_.get(), error);
3295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      }
3305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    } else {
3315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      // Make a request to the token service. The test now must tell
3325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      // the token service to issue an access token (or an error).
3335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      IdentityGetAuthTokenFunction::StartLoginAccessTokenRequest();
3345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
3355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
3365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
33746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void ShowLoginPopup() OVERRIDE {
33846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_FALSE(login_ui_shown_);
33946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    login_ui_shown_ = true;
34046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (login_ui_result_)
34146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      SigninSuccess();
34246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    else
34346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      SigninFailed();
34446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
34546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
34646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void ShowOAuthApprovalDialog(
34746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const IssueAdviceInfo& issue_advice) OVERRIDE {
34846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    scope_ui_shown_ = true;
34946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
35046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (scope_ui_result_) {
35146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      OnGaiaFlowCompleted(kAccessToken, "3600");
35246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    } else if (scope_ui_failure_ == GaiaWebAuthFlow::SERVICE_AUTH_ERROR) {
35346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
35446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      OnGaiaFlowFailure(scope_ui_failure_, error, "");
35546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    } else {
35646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
35746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      OnGaiaFlowFailure(scope_ui_failure_, error, scope_ui_oauth_error_);
35846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    }
35946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
36046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
36146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual OAuth2MintTokenFlow* CreateMintTokenFlow(
36246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      const std::string& login_access_token) OVERRIDE {
36346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    EXPECT_TRUE(login_access_token_.empty());
3645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Save the login token used to create the flow so tests can see
3655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // what account was used.
36646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    login_access_token_ = login_access_token;
36746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return flow_.release();
36846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
36946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
37046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) private:
37146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual ~FakeGetAuthTokenFunction() {}
37246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool login_access_token_result_;
3735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  bool auto_login_access_token_;
37446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool login_ui_result_;
37546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool scope_ui_result_;
37646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GaiaWebAuthFlow::Failure scope_ui_failure_;
37746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string scope_ui_oauth_error_;
37846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool login_ui_shown_;
37946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool scope_ui_shown_;
38046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
38146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<OAuth2MintTokenFlow> flow_;
38246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
38346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::string login_access_token_;
38446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
38546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
386c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MockQueuedMintRequest : public IdentityMintRequestQueue::Request {
387c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) public:
388c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  MOCK_METHOD1(StartMintToken, void(IdentityMintRequestQueue::MintType));
389c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)};
390c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
391116680a4aac90f2aa7413d9095a592090648e557Ben Murdochgaia::AccountIds CreateIds(std::string email, std::string obfid) {
392116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  gaia::AccountIds ids;
39346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ids.account_key = email;
39446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ids.email = email;
39546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ids.gaia = obfid;
39646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return ids;
39746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
39846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
399cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class IdentityGetAccountsFunctionTest : public ExtensionBrowserTest {
400cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
40146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ExtensionBrowserTest::SetUpCommandLine(command_line);
402cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    command_line->AppendSwitch(switches::kExtensionsMultiAccount);
403cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
404cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
405cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) protected:
406116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void SetAccountState(gaia::AccountIds ids, bool is_signed_in) {
407cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
408cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        ids, is_signed_in);
409cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
410cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
411cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  testing::AssertionResult ExpectGetAccounts(
412cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const std::vector<std::string>& accounts) {
413cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_refptr<IdentityGetAccountsFunction> func(
414cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        new IdentityGetAccountsFunction);
4151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    func->set_extension(test_util::CreateEmptyExtension(kExtensionId).get());
416cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!utils::RunFunction(
417cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            func.get(), std::string("[]"), browser(), utils::NONE)) {
418cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return GenerateFailureResult(accounts, NULL)
419cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)             << "getAccounts did not return a result.";
420cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
421cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::ListValue* callback_arguments = func->GetResultList();
422cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!callback_arguments)
423cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return GenerateFailureResult(accounts, NULL) << "NULL result";
424cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
425cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (callback_arguments->GetSize() != 1) {
426cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return GenerateFailureResult(accounts, NULL)
427cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)             << "Expected 1 argument but got " << callback_arguments->GetSize();
428cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
429cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
430cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::ListValue* results;
431cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!callback_arguments->GetList(0, &results))
432cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      GenerateFailureResult(accounts, NULL) << "Result was not an array";
433cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
434cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    std::set<std::string> result_ids;
435cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for (base::ListValue::const_iterator it = results->begin();
436cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         it != results->end();
437cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         ++it) {
438cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      scoped_ptr<api::identity::AccountInfo> info =
439cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          api::identity::AccountInfo::FromValue(**it);
440cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      if (info.get())
441cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        result_ids.insert(info->id);
442cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      else
443cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return GenerateFailureResult(accounts, results);
444cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
445cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
446cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for (std::vector<std::string>::const_iterator it = accounts.begin();
447cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         it != accounts.end();
448cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         ++it) {
449cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      if (result_ids.find(*it) == result_ids.end())
450cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        return GenerateFailureResult(accounts, results);
451cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
452cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
453cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return testing::AssertionResult(true);
454cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
455cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
456cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  testing::AssertionResult GenerateFailureResult(
457cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const ::std::vector<std::string>& accounts,
458cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const base::ListValue* results) {
459cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    testing::Message msg("Expected: ");
460cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for (std::vector<std::string>::const_iterator it = accounts.begin();
461cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         it != accounts.end();
462cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)         ++it) {
463cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      msg << *it << " ";
464cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
465cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    msg << "Actual: ";
466cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!results) {
467cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      msg << "NULL";
468cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else {
469cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      for (base::ListValue::const_iterator it = results->begin();
470cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)           it != results->end();
471cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)           ++it) {
472cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        scoped_ptr<api::identity::AccountInfo> info =
473cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            api::identity::AccountInfo::FromValue(**it);
474cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        if (info.get())
475cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          msg << info->id << " ";
476cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        else
477cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          msg << *it << "<-" << (*it)->GetType() << " ";
478cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      }
479cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
480cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
481cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return testing::AssertionFailure(msg);
482cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
483cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
484cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
485cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, MultiAccountOn) {
486cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(switches::IsExtensionsMultiAccount());
487cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
488cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
489cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, NoneSignedIn) {
490cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(ExpectGetAccounts(std::vector<std::string>()));
491cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
492cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
493cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest,
494cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       PrimaryAccountSignedIn) {
495cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetAccountState(CreateIds("primary@example.com", "1"), true);
496cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<std::string> primary;
497cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  primary.push_back("1");
498cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(ExpectGetAccounts(primary));
499cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
500cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
501cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityGetAccountsFunctionTest, TwoAccountsSignedIn) {
502cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetAccountState(CreateIds("primary@example.com", "1"), true);
503cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetAccountState(CreateIds("secondary@example.com", "2"), true);
504cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<std::string> two_accounts;
505cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  two_accounts.push_back("1");
506cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  two_accounts.push_back("2");
507cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(ExpectGetAccounts(two_accounts));
508cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
509cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
510cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class IdentityOldProfilesGetAccountsFunctionTest
511cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : public IdentityGetAccountsFunctionTest {
512cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
513cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Don't add the multi-account switch that parent class would have.
514cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
515cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
516cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
517cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest,
518cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       MultiAccountOff) {
519cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_FALSE(switches::IsExtensionsMultiAccount());
520cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
521cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
522cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityOldProfilesGetAccountsFunctionTest,
523cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       TwoAccountsSignedIn) {
524cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetAccountState(CreateIds("primary@example.com", "1"), true);
525cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SetAccountState(CreateIds("secondary@example.com", "2"), true);
526cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::vector<std::string> only_primary;
527cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  only_primary.push_back("1");
528cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_TRUE(ExpectGetAccounts(only_primary));
529cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
530cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
531f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class IdentityGetProfileUserInfoFunctionTest : public ExtensionBrowserTest {
532f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) protected:
533f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  scoped_ptr<api::identity::ProfileUserInfo> RunGetProfileUserInfo() {
534f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    scoped_refptr<IdentityGetProfileUserInfoFunction> func(
535f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        new IdentityGetProfileUserInfoFunction);
5361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    func->set_extension(test_util::CreateEmptyExtension(kExtensionId).get());
537f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    scoped_ptr<base::Value> value(
538f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        utils::RunFunctionAndReturnSingleResult(func.get(), "[]", browser()));
539f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return api::identity::ProfileUserInfo::FromValue(*value.get());
540f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
541116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
542116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<api::identity::ProfileUserInfo> RunGetProfileUserInfoWithEmail() {
543116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_refptr<IdentityGetProfileUserInfoFunction> func(
544116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        new IdentityGetProfileUserInfoFunction);
545116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    func->set_extension(CreateExtensionWithEmailPermission());
546116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_ptr<base::Value> value(
547116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        utils::RunFunctionAndReturnSingleResult(func.get(), "[]", browser()));
548116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return api::identity::ProfileUserInfo::FromValue(*value.get());
549116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
550116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
551116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
552116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_refptr<Extension> CreateExtensionWithEmailPermission() {
553116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_ptr<base::DictionaryValue> test_extension_value(
554116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        utils::ParseDictionary(
555116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            "{\"name\": \"Test\", \"version\": \"1.0\", "
556116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            "\"permissions\": [\"identity.email\"]}"));
557116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return utils::CreateExtension(test_extension_value.get());
558116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
559f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)};
560f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
561f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, NotSignedIn) {
562116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<api::identity::ProfileUserInfo> info =
563116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      RunGetProfileUserInfoWithEmail();
564f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(info->email.empty());
565f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(info->id.empty());
566f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
567f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
568f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest, SignedIn) {
569f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  profile()->GetPrefs()
570f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ->SetString(prefs::kGoogleServicesUsername, "president@example.com");
571f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  profile()->GetPrefs()
572f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ->SetString(prefs::kGoogleServicesUserAccountId, "12345");
573f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
574116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<api::identity::ProfileUserInfo> info =
575116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      RunGetProfileUserInfoWithEmail();
576f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ("president@example.com", info->email);
577f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ("12345", info->id);
578f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
579f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
580116680a4aac90f2aa7413d9095a592090648e557Ben MurdochIN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest,
581116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       NotSignedInNoEmail) {
582116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<api::identity::ProfileUserInfo> info = RunGetProfileUserInfo();
583116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(info->email.empty());
584116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(info->id.empty());
585116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
586116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
587116680a4aac90f2aa7413d9095a592090648e557Ben MurdochIN_PROC_BROWSER_TEST_F(IdentityGetProfileUserInfoFunctionTest,
588116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       SignedInNoEmail) {
589116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile()->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
590116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   "president@example.com");
591116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  profile()->GetPrefs()->SetString(prefs::kGoogleServicesUserAccountId,
592116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                   "12345");
593116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
594116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<api::identity::ProfileUserInfo> info = RunGetProfileUserInfo();
595116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_TRUE(info->email.empty());
596116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  EXPECT_EQ("12345", info->id);
597116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
598116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
599c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class GetAuthTokenFunctionTest : public AsyncExtensionBrowserTest {
60046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
60146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
60246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    AsyncExtensionBrowserTest::SetUpCommandLine(command_line);
60346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    command_line->AppendSwitch(switches::kExtensionsMultiAccount);
60446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
60546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
60646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
60746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    AsyncExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
60846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
60946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    will_create_browser_context_services_subscription_ =
61046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        BrowserContextDependencyManager::GetInstance()
61146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)            ->RegisterWillCreateBrowserContextServicesCallbackForTesting(
61246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                  base::Bind(&GetAuthTokenFunctionTest::
61346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                                 OnWillCreateBrowserContextServices,
61446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                             base::Unretained(this)))
61546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)            .Pass();
61646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
61746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
61846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void OnWillCreateBrowserContextServices(content::BrowserContext* context) {
61946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // Replace the signin manager and token service with fakes. Do this ahead of
62046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // creating the browser so that a bunch of classes don't register as
62146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // observers and end up needing to unregister when the fake is substituted.
62246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    SigninManagerFactory::GetInstance()->SetTestingFactory(
62346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        context, &FakeSigninManagerBase::Build);
62446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
62546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        context, &BuildFakeProfileOAuth2TokenService);
626116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    AccountReconcilorFactory::GetInstance()->SetTestingFactory(
627116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        context, &FakeAccountReconcilor::Build);
62846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
62946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
63046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
63146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    AsyncExtensionBrowserTest::SetUpOnMainThread();
63246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
63346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // Grab references to the fake signin manager and token service.
63446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    signin_manager_ = static_cast<FakeSigninManagerForTesting*>(
63546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        SigninManagerFactory::GetInstance()->GetForProfile(profile()));
63646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ASSERT_TRUE(signin_manager_);
63746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    token_service_ = static_cast<FakeProfileOAuth2TokenService*>(
63846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        ProfileOAuth2TokenServiceFactory::GetInstance()->GetForProfile(
63946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)            profile()));
64046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ASSERT_TRUE(token_service_);
64146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
64246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
64346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void SignIn(const std::string account_key) {
64446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if defined(OS_CHROMEOS)
64546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    signin_manager_->SetAuthenticatedUsername(account_key);
64646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#else
64746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    signin_manager_->SignIn(account_key, "password");
64846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
6495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    token_service_->IssueRefreshTokenForUser(account_key, "refresh_token");
6505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
6515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
6525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void IssueLoginRefreshTokenForAccount(const std::string account_key) {
6535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    token_service_->IssueRefreshTokenForUser(account_key, "refresh_token");
6545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
6555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
6565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void IssueLoginAccessTokenForAccount(const std::string account_key) {
6575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    token_service_->IssueAllTokensForAccount(
6585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        account_key,
6595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        "access_token-" + account_key,
6605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        base::Time::Now() + base::TimeDelta::FromSeconds(3600));
66146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
66246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
663116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void SetAccountState(gaia::AccountIds ids, bool is_signed_in) {
66446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    IdentityAPI::GetFactoryInstance()->Get(profile())->SetAccountStateForTest(
66546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        ids, is_signed_in);
66646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
66746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum OAuth2Fields {
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NONE = 0,
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CLIENT_ID = 1,
672eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    SCOPES = 2,
673eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    AS_COMPONENT = 4
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
67646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  FakeSigninManagerForTesting* signin_manager_;
67746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  FakeProfileOAuth2TokenService* token_service_;
67846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~GetAuthTokenFunctionTest() {}
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper to create an extension with specific OAuth2Info fields set.
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |fields_to_set| should be computed by using fields of Oauth2Fields enum.
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const Extension* CreateExtension(int fields_to_set) {
684eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const Extension* ext;
685eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FilePath manifest_path =
686eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        test_data_dir_.AppendASCII("platform_apps/oauth2");
687eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::FilePath component_manifest_path =
688eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        test_data_dir_.AppendASCII("packaged_app/component_oauth2");
689eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if ((fields_to_set & AS_COMPONENT) == 0)
690eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ext = LoadExtension(manifest_path);
691eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    else
692eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ext = LoadExtensionAsComponent(component_manifest_path);
693eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    OAuth2Info& oauth2_info =
694eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        const_cast<OAuth2Info&>(OAuth2Info::GetOAuth2Info(ext));
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ((fields_to_set & CLIENT_ID) != 0)
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      oauth2_info.client_id = "client1";
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if ((fields_to_set & SCOPES) != 0) {
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      oauth2_info.scopes.push_back("scope1");
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      oauth2_info.scopes.push_back("scope2");
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
701a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
702a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    extension_id_ = ext->id();
703a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    oauth_scopes_ = std::set<std::string>(oauth2_info.scopes.begin(),
704a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                          oauth2_info.scopes.end());
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return ext;
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
708c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  IdentityAPI* id_api() {
709a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return IdentityAPI::GetFactoryInstance()->Get(browser()->profile());
710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
711a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
712a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const std::string GetPrimaryAccountId() {
7135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SigninManagerBase* signin_manager =
7145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        SigninManagerFactory::GetForProfile(browser()->profile());
7155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return signin_manager->GetAuthenticatedAccountId();
716a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
717a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
718a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void SetCachedToken(const IdentityTokenCacheValue& token_data) {
719a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_);
720a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    id_api()->SetCachedToken(key, token_data);
721a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
722a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
72346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const IdentityTokenCacheValue& GetCachedToken(std::string account_id) {
72446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    if (account_id.empty())
72546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      account_id = GetPrimaryAccountId();
72646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ExtensionTokenKey key(extension_id_, account_id, oauth_scopes_);
727a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return id_api()->GetCachedToken(key);
728a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
729a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
730a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void QueueRequestStart(IdentityMintRequestQueue::MintType type,
731a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                         IdentityMintRequestQueue::Request* request) {
732a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_);
733a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    id_api()->mint_queue()->RequestStart(type, key, request);
734a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
735a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
736a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void QueueRequestComplete(IdentityMintRequestQueue::MintType type,
737a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                            IdentityMintRequestQueue::Request* request) {
738a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ExtensionTokenKey key(extension_id_, GetPrimaryAccountId(), oauth_scopes_);
739a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    id_api()->mint_queue()->RequestComplete(type, key, request);
740a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
741a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
742a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) private:
743a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  std::string extension_id_;
744a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  std::set<std::string> oauth_scopes_;
74546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
74646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription>
74746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      will_create_browser_context_services_subscription_;
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       NoClientId) {
7525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  func->set_extension(CreateExtension(SCOPES));
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string error = utils::RunFunctionAndReturnError(
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      func.get(), "[{}]", browser());
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(std::string(errors::kInvalidClientId), error);
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
75890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       NoScopes) {
7635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID));
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string error = utils::RunFunctionAndReturnError(
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      func.get(), "[{}]", browser());
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(std::string(errors::kInvalidScopes), error);
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
76990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       NonInteractiveNotSignedIn) {
7745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string error = utils::RunFunctionAndReturnError(
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      func.get(), "[{}]", browser());
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
78090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       NonInteractiveMintFailure) {
7855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
7865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
7885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE);
7895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string error =
7905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
79390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
7977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                       NonInteractiveLoginAccessTokenFailure) {
7985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
7995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
8007dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
8017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  func->set_login_access_token_result(false);
8027dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  std::string error = utils::RunFunctionAndReturnError(
8037dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      func.get(), "[{}]", browser());
8047dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
8057dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}
8067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
8077dbb3d5cf0c15f500944d211057644d6a2f37371Ben MurdochIN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
808c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       NonInteractiveMintAdviceSuccess) {
8095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
810c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
8115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
812868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  func->set_extension(extension.get());
8135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(TestOAuth2MintTokenFlow::ISSUE_ADVICE_SUCCESS);
8145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string error =
8155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
816c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(std::string(errors::kNoGrant), error);
817c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
81890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
819c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
820a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_ADVICE,
82146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)            GetCachedToken(std::string()).status());
822c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
823c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
824c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
825c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       NonInteractiveMintBadCredentials) {
8265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
8275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
8295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(
8305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS);
8315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string error =
8325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
833c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
834c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
83590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
836a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
837a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
838a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
839a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       NonInteractiveMintServiceError) {
8405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
8415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
842a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
8435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(
8445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      TestOAuth2MintTokenFlow::MINT_TOKEN_SERVICE_ERROR);
845a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string error =
846a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      utils::RunFunctionAndReturnError(func.get(), "[{}]", browser());
847a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
848a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
849a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
850c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
851c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
852c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
853f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       NoOptionsSuccess) {
8545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
855f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if defined(OS_WIN) && defined(USE_ASH)
856f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
857f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
858f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return;
859f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
860f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
8615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
862f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
863f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  func->set_extension(extension.get());
8645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
8655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_ptr<base::Value> value(
8665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      utils::RunFunctionAndReturnSingleResult(func.get(), "[]", browser()));
867f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string access_token;
868f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_TRUE(value->GetAsString(&access_token));
869f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(std::string(kAccessToken), access_token);
870f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
871f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
872f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
873f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            GetCachedToken(std::string()).status());
874f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
875f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
876f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
877c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       NonInteractiveSuccess) {
8785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
879ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#if defined(OS_WIN) && defined(USE_ASH)
880ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Disable this test in Metro+Ash for now (http://crbug.com/262796).
881a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
882ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return;
883ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#endif
884ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
8855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
886c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_refptr<const Extension> extension(CreateExtension(CLIENT_ID | SCOPES));
887868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  func->set_extension(extension.get());
8885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_SUCCESS);
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::Value> value(utils::RunFunctionAndReturnSingleResult(
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      func.get(), "[{}]", browser()));
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string access_token;
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(value->GetAsString(&access_token));
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(std::string(kAccessToken), access_token);
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(func->login_ui_shown());
89590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
896c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(IdentityTokenCacheValue::CACHE_STATUS_TOKEN,
89746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)            GetCachedToken(std::string()).status());
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       InteractiveLoginCanceled) {
9025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
904c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  func->set_login_ui_result(false);
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string error = utils::RunFunctionAndReturnError(
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      func.get(), "[{\"interactive\": true}]", browser());
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(func->login_ui_shown());
90990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
913c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       InteractiveMintBadCredentialsLoginCanceled) {
9145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SignIn("primary@example.com");
9155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
9175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(
9185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      TestOAuth2MintTokenFlow::MINT_TOKEN_BAD_CREDENTIALS);
919c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  func->set_login_ui_result(false);
920c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string error = utils::RunFunctionAndReturnError(
921c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      func.get(), "[{\"interactive\": true}]", browser());
922c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
923c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(func->login_ui_shown());
92490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
925c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
926c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
927c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
928c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       InteractiveLoginSuccessNoToken) {
9295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
930c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
931c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  func->set_login_ui_result(false);
932c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string error = utils::RunFunctionAndReturnError(
933c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      func.get(), "[{\"interactive\": true}]", browser());
934c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_EQ(std::string(errors::kUserNotSignedIn), error);
935c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  EXPECT_TRUE(func->login_ui_shown());
93690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
937c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
938c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
939c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(GetAuthTokenFunctionTest,
940c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       InteractiveLoginSuccessMintFailure) {
9415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_refptr<FakeGetAuthTokenFunction> func(new FakeGetAuthTokenFunction());
942c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  func->set_extension(CreateExtension(CLIENT_ID | SCOPES));
943c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  func->set_login_ui_result(true);
9445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  func->set_mint_token_result(TestOAuth2MintTokenFlow::MINT_TOKEN_FAILURE);
9455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string error = utils::RunFunctionAndReturnError(
9465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      func.get(), "[{\"interactive\": true}]", browser());
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(StartsWithASCII(error, errors::kAuthFailure, false));
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(func->login_ui_shown());
94990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  EXPECT_FALSE(func->scope_ui_shown());
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
951