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)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/options/options_ui_browsertest.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/prefs/pref_service.h"
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/scoped_observer.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/chrome_notification_types.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/browser.h"
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/browser/ui/chrome_pages.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
16effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/ui/webui/options/options_ui.h"
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "chrome/browser/ui/webui/uber/uber_ui.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/pref_names.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/url_constants.h"
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/grit/generated_resources.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/ui_test_utils.h"
22e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/public/browser/notification_service.h"
240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "content/public/browser/render_frame_host.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/browser_test_utils.h"
27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/test/test_utils.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if !defined(OS_CHROMEOS)
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include <string>
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/basictypes.h"
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/bind.h"
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/callback.h"
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/files/file_path.h"
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/run_loop.h"
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/browser_process.h"
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser_commands.h"
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/test/test_navigation_observer.h"
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "ui/base/window_open_disposition.h"
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "url/gurl.h"
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochusing content::MessageLoopRunner;
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace options {
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
53effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass SignOutWaiter : public SigninManagerBase::Observer {
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public:
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  explicit SignOutWaiter(SigninManagerBase* signin_manager)
56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      : seen_(false), running_(false), scoped_observer_(this) {
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    scoped_observer_.Add(signin_manager);
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual ~SignOutWaiter() {}
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void Wait() {
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (seen_)
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    running_ = true;
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    message_loop_runner_ = new MessageLoopRunner;
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    message_loop_runner_->Run();
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    EXPECT_TRUE(seen_);
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void GoogleSignedOut(const std::string& account_id,
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                               const std::string& username) OVERRIDE {
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    seen_ = true;
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (!running_)
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return;
76effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    message_loop_runner_->Quit();
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    running_ = false;
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
80effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
81effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch private:
82effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  bool seen_;
83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  bool running_;
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ScopedObserver<SigninManagerBase, SignOutWaiter> scoped_observer_;
85effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_refptr<MessageLoopRunner> message_loop_runner_;
86effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
87effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if !defined(OS_CHROMEOS)
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void RunClosureWhenProfileInitialized(const base::Closure& closure,
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                      Profile* profile,
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                      Profile::CreateStatus status) {
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (status == Profile::CREATE_STATUS_INITIALIZED)
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    closure.Run();
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
970529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbool FrameHasSettingsSourceHost(content::RenderFrameHost* frame) {
980529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return frame->GetLastCommittedURL().DomainIs(
990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      chrome::kChromeUISettingsFrameHost);
1000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
104558790d6acca3451cf3a6b497803a5f07d0bec58Ben MurdochOptionsUIBrowserTest::OptionsUIBrowserTest() {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
107558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochvoid OptionsUIBrowserTest::NavigateToSettings() {
1080529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  NavigateToSettingsSubpage("");
1090529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1100529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1110529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid OptionsUIBrowserTest::NavigateToSettingsSubpage(
1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const std::string& sub_page) {
1130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  const GURL& url = chrome::GetSettingsUrl(sub_page);
114effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ui_test_utils::NavigateToURLWithDisposition(browser(), url, CURRENT_TAB, 0);
115effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
116effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  content::WebContents* web_contents =
117effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      browser()->tab_strip_model()->GetActiveWebContents();
118effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ASSERT_TRUE(web_contents);
119effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ASSERT_TRUE(web_contents->GetWebUI());
1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  content::WebUIController* controller =
1226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      web_contents->GetWebUI()->GetController();
1236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if !defined(OS_CHROMEOS)
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  controller = static_cast<UberUI*>(controller)->
1256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      GetSubpage(chrome::kChromeUISettingsFrameURL)->GetController();
1266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
1276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  OptionsUI* options_ui = static_cast<OptionsUI*>(controller);
128effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
129effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // It is not possible to subscribe to the OnFinishedLoading event before the
130effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // call to NavigateToURL(), because the WebUI does not yet exist at that time.
131effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // However, it is safe to subscribe afterwards, because the event will always
132effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // be posted asynchronously to the message loop.
133effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_refptr<MessageLoopRunner> message_loop_runner(new MessageLoopRunner);
134effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_ptr<OptionsUI::OnFinishedLoadingCallbackList::Subscription>
135effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      subscription = options_ui->RegisterOnFinishedLoadingCallback(
136effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch          message_loop_runner->QuitClosure());
137effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  message_loop_runner->Run();
13846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
13946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // The OnFinishedLoading event, which indicates that all WebUI initialization
14046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // methods have been called on the JS side, is temporally unrelated to whether
14146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // or not the WebContents considers itself to have finished loading. We want
14246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // to wait for this too, however, because, e.g. this is a sufficient condition
14346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // to get the focus properly placed on a form element.
14446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  content::WaitForLoadStop(web_contents);
145effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
146effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
147effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid OptionsUIBrowserTest::NavigateToSettingsFrame() {
148effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  const GURL& url = GURL(chrome::kChromeUISettingsFrameURL);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ui_test_utils::NavigateToURL(browser(), url);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
152558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochvoid OptionsUIBrowserTest::VerifyNavbar() {
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool navbar_exist = false;
1546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(OS_CHROMEOS)
1556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool should_navbar_exist = false;
1566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#else
1576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool should_navbar_exist = true;
1586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents(),
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "domAutomationController.send("
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      "    !!document.getElementById('navigation'))",
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      &navbar_exist));
1646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  EXPECT_EQ(should_navbar_exist, navbar_exist);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
167558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochvoid OptionsUIBrowserTest::VerifyTitle() {
168a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 title =
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents()->GetTitle();
170a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 expected_title = l10n_util::GetStringUTF16(IDS_SETTINGS_TITLE);
171a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_NE(title.find(expected_title), base::string16::npos);
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochcontent::RenderFrameHost* OptionsUIBrowserTest::GetSettingsFrame() {
1750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // NB: The utility function content::FrameHasSourceUrl can't be used because
1760529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // the settings frame navigates itself to chrome://settings-frame/settings
1770529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // to indicate that it's showing the top-level settings. Therefore, just
1780529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // match the host.
1790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return content::FrameMatchingPredicate(
1800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      browser()->tab_strip_model()->GetActiveWebContents(),
1810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      base::Bind(&FrameHasSettingsSourceHost));
1820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
184effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochIN_PROC_BROWSER_TEST_F(OptionsUIBrowserTest, LoadOptionsByURL) {
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToSettings();
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VerifyTitle();
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  VerifyNavbar();
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Flaky on win_rel when the profile is deleted crbug.com/103355
1915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Also related to crbug.com/104851
1925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#if defined(OS_WIN)
1935c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#define MAYBE_VerifyManagedSignout DISABLED_VerifyManagedSignout
1945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#else
1955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#define MAYBE_VerifyManagedSignout VerifyManagedSignout
1965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#endif
1975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if !defined(OS_CHROMEOS)
1995c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuIN_PROC_BROWSER_TEST_F(OptionsUIBrowserTest, MAYBE_VerifyManagedSignout) {
2005c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  SigninManager* signin =
2015c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      SigninManagerFactory::GetForProfile(browser()->profile());
2025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  signin->OnExternalSigninCompleted("test@example.com");
2035c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  signin->ProhibitSignout(true);
2045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  NavigateToSettingsFrame();
2065c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2075c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // This script simulates a click on the "Disconnect your Google Account"
2085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // button and returns true if the hidden flag of the appropriate dialog gets
2095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // flipped.
2105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  bool result = false;
2115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
2125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      browser()->tab_strip_model()->GetActiveWebContents(),
2135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      "var dialog = $('manage-profile-overlay-disconnect-managed');"
2145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      "var original_status = dialog.hidden;"
215f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "var original = ManageProfileOverlay.showDisconnectManagedProfileDialog;"
216f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "var teststub = function(event) {"
217f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "  original(event);"
218f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "  domAutomationController.send(original_status && !dialog.hidden);"
219f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "};"
220f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "ManageProfileOverlay.showDisconnectManagedProfileDialog = teststub;"
221f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      "$('start-stop-sync').click();",
2225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      &result));
2235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  EXPECT_TRUE(result);
2255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  base::FilePath profile_dir = browser()->profile()->GetPath();
2275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ProfileInfoCache& profile_info_cache =
2285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      g_browser_process->profile_manager()->GetProfileInfoCache();
2295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  EXPECT_TRUE(DirectoryExists(profile_dir));
2315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  EXPECT_TRUE(profile_info_cache.GetIndexOfProfileWithPath(profile_dir) !=
2325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu              std::string::npos);
2335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  content::WindowedNotificationObserver wait_for_profile_deletion(
2355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED,
2365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      content::NotificationService::AllSources());
2375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // TODO(kaliamoorthi): Get the macos problem fixed and remove this code.
2395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // Deleting the Profile also destroys all browser windows of that Profile.
2405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // Wait for the current browser to close before resuming, otherwise
2415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // the browser_tests shutdown code will be confused on the Mac.
2425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  content::WindowedNotificationObserver wait_for_browser_closed(
2435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      chrome::NOTIFICATION_BROWSER_CLOSED,
2445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      content::NotificationService::AllSources());
2455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ASSERT_TRUE(content::ExecuteScript(
2475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      browser()->tab_strip_model()->GetActiveWebContents(),
2485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      "$('disconnect-managed-profile-ok').click();"));
2495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  wait_for_profile_deletion.Wait();
2515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  EXPECT_TRUE(profile_info_cache.GetIndexOfProfileWithPath(profile_dir) ==
2535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu              std::string::npos);
2545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  wait_for_browser_closed.Wait();
2565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(OptionsUIBrowserTest, VerifyUnmanagedSignout) {
259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SigninManager* signin =
260a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      SigninManagerFactory::GetForProfile(browser()->profile());
261a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const std::string user = "test@example.com";
262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin->OnExternalSigninCompleted(user);
263a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
264effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  NavigateToSettingsFrame();
265a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
266a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This script simulates a click on the "Disconnect your Google Account"
267a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // button and returns true if the hidden flag of the appropriate dialog gets
268a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // flipped.
269a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool result = false;
270a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
271a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents(),
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "var dialog = $('sync-setup-stop-syncing');"
273a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "var original_status = dialog.hidden;"
274a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "$('start-stop-sync').click();"
275a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "domAutomationController.send(original_status && !dialog.hidden);",
276a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      &result));
277a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
278a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(result);
279a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
280effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  SignOutWaiter sign_out_waiter(signin);
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ASSERT_TRUE(content::ExecuteScript(
283a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents(),
284a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      "$('stop-syncing-ok').click();"));
285a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
286effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  sign_out_waiter.Wait();
287a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
288a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EXPECT_TRUE(browser()->profile()->GetProfileName() != user);
2891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EXPECT_FALSE(signin->IsAuthenticated());
290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
291a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Regression test for http://crbug.com/301436, excluded on Chrome OS because
2938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// profile management in the settings UI exists on desktop platforms only.
2948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(OptionsUIBrowserTest, NavigateBackFromOverlayDialog) {
295effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  NavigateToSettingsFrame();
2968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Click a button that opens an overlay dialog.
2988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  content::WebContents* contents =
2998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      browser()->tab_strip_model()->GetActiveWebContents();
3008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(content::ExecuteScript(
3018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      contents, "$('manage-default-search-engines').click();"));
3028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Go back to the settings page.
3048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  content::TestNavigationObserver observer(contents);
3058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  chrome::GoBack(browser(), CURRENT_TAB);
3068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  observer.Wait();
3078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Verify that the settings page lists one profile.
3098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const char javascript[] =
3108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "domAutomationController.send("
3118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "    document.querySelectorAll('list#profiles-list > div[role=listitem]')"
3128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      "        .length);";
3138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int profiles;
3148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
3158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      contents, javascript, &profiles));
3168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(1, profiles);
3178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Create a second profile.
3198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ProfileManager* profile_manager = g_browser_process->profile_manager();
3208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const base::FilePath profile_path =
3218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      profile_manager->GenerateNextProfileDirectoryPath();
3228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::RunLoop run_loop;
3248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  profile_manager->CreateProfileAsync(
3258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      profile_manager->GenerateNextProfileDirectoryPath(),
3268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      base::Bind(&RunClosureWhenProfileInitialized,
3278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 run_loop.QuitClosure()),
328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                 base::string16(),
329a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                 base::string16(),
3308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 std::string());
3318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  run_loop.Run();
3328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Verify that the settings page has updated and lists two profiles.
3348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
3358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      contents, javascript, &profiles));
3368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  EXPECT_EQ(2, profiles);
3378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
3398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace options
341