oobe_browsertest.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/command_line.h"
6#include "base/path_service.h"
7#include "chrome/browser/chrome_browser_main.h"
8#include "chrome/browser/chrome_browser_main_extra_parts.h"
9#include "chrome/browser/chrome_content_browser_client.h"
10#include "chrome/browser/chrome_notification_types.h"
11#include "chrome/browser/chromeos/login/existing_user_controller.h"
12#include "chrome/browser/chromeos/login/webui_login_display.h"
13#include "chrome/browser/chromeos/login/wizard_controller.h"
14#include "chrome/browser/ui/browser.h"
15#include "chrome/common/chrome_paths.h"
16#include "chrome/common/chrome_switches.h"
17#include "chrome/test/base/in_process_browser_test.h"
18#include "chrome/test/base/interactive_test_utils.h"
19#include "chrome/test/base/ui_test_utils.h"
20#include "chromeos/chromeos_switches.h"
21#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
23#include "content/public/browser/notification_service.h"
24#include "content/public/test/test_utils.h"
25#include "google_apis/gaia/fake_gaia.h"
26#include "google_apis/gaia/gaia_switches.h"
27#include "net/test/embedded_test_server/embedded_test_server.h"
28#include "net/test/embedded_test_server/http_response.h"
29#include "testing/gmock/include/gmock/gmock.h"
30#include "testing/gtest/include/gtest/gtest.h"
31
32using namespace net::test_server;
33
34namespace {
35
36// Used to add an observer to NotificationService after it's created.
37class TestBrowserMainExtraParts
38    : public ChromeBrowserMainExtraParts,
39      public content::NotificationObserver {
40 public:
41  TestBrowserMainExtraParts()
42      : webui_visible_(false),
43        browsing_data_removed_(false),
44        signin_screen_shown_(false) {}
45  virtual ~TestBrowserMainExtraParts() {}
46
47  // ChromeBrowserMainExtraParts implementation.
48  virtual void PreEarlyInitialization() OVERRIDE {
49    registrar_.Add(this, chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
50                   content::NotificationService::AllSources());
51    registrar_.Add(this, chrome::NOTIFICATION_SESSION_STARTED,
52                   content::NotificationService::AllSources());
53    registrar_.Add(this, chrome::NOTIFICATION_BROWSING_DATA_REMOVED,
54                   content::NotificationService::AllSources());
55  }
56
57  void set_quit_task(const base::Closure& quit_task) { quit_task_ = quit_task; }
58
59 private:
60  // Overridden from content::NotificationObserver:
61  virtual void Observe(int type,
62                       const content::NotificationSource& source,
63                       const content::NotificationDetails& details) OVERRIDE {
64    if (type == chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE) {
65      LOG(INFO) << "NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE";
66      webui_visible_ = true;
67      if (browsing_data_removed_ && !signin_screen_shown_) {
68        signin_screen_shown_ = true;
69        ShowSigninScreen();
70      }
71    } else if (type == chrome::NOTIFICATION_BROWSING_DATA_REMOVED) {
72      LOG(INFO) << "chrome::NOTIFICATION_BROWSING_DATA_REMOVED";
73      browsing_data_removed_ = true;
74      if (webui_visible_ && !signin_screen_shown_) {
75        signin_screen_shown_ = true;
76        ShowSigninScreen();
77      }
78    } else if (type == chrome::NOTIFICATION_SESSION_STARTED) {
79      LOG(INFO) << "chrome::NOTIFICATION_SESSION_STARTED";
80      quit_task_.Run();
81    } else {
82      NOTREACHED();
83    }
84  }
85
86  void ShowSigninScreen() {
87    chromeos::ExistingUserController* controller =
88        chromeos::ExistingUserController::current_controller();
89    CHECK(controller);
90    chromeos::WebUILoginDisplay* webui_login_display =
91        static_cast<chromeos::WebUILoginDisplay*>(
92            controller->login_display());
93    CHECK(webui_login_display);
94    webui_login_display->ShowSigninScreenForCreds("username", "password");
95    // TODO(glotov): mock GAIA server (test_server()) should support
96    // username/password configuration.
97  }
98
99  bool webui_visible_, browsing_data_removed_, signin_screen_shown_;
100  content::NotificationRegistrar registrar_;
101  base::Closure quit_task_;
102  GURL gaia_url_;
103
104  DISALLOW_COPY_AND_ASSIGN(TestBrowserMainExtraParts);
105};
106
107class TestContentBrowserClient : public chrome::ChromeContentBrowserClient {
108 public:
109  TestContentBrowserClient() {}
110  virtual ~TestContentBrowserClient() {}
111
112  virtual content::BrowserMainParts* CreateBrowserMainParts(
113      const content::MainFunctionParams& parameters) OVERRIDE {
114    ChromeBrowserMainParts* main_parts = static_cast<ChromeBrowserMainParts*>(
115        ChromeContentBrowserClient::CreateBrowserMainParts(parameters));
116
117    browser_main_extra_parts_ = new TestBrowserMainExtraParts();
118    main_parts->AddParts(browser_main_extra_parts_);
119    return main_parts;
120  }
121
122  TestBrowserMainExtraParts* browser_main_extra_parts_;
123
124 private:
125  DISALLOW_COPY_AND_ASSIGN(TestContentBrowserClient);
126};
127
128class OobeTest : public InProcessBrowserTest {
129 protected:
130  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
131    command_line->AppendSwitch(chromeos::switches::kLoginManager);
132    command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
133    command_line->AppendSwitch(
134        chromeos::switches::kDisableChromeCaptivePortalDetector);
135    command_line->AppendSwitchASCII(chromeos::switches::kLoginProfile, "user");
136    command_line->AppendSwitchASCII(
137        chromeos::switches::kAuthExtensionPath, "gaia_auth");
138  }
139
140  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
141    content_browser_client_.reset(new TestContentBrowserClient());
142    original_content_browser_client_ = content::SetBrowserClientForTesting(
143        content_browser_client_.get());
144  }
145
146  virtual void SetUpOnMainThread() OVERRIDE {
147    CHECK(embedded_test_server()->InitializeAndWaitUntilReady());
148    embedded_test_server()->RegisterRequestHandler(
149        base::Bind(&FakeGaia::HandleRequest, base::Unretained(&fake_gaia_)));
150    LOG(INFO) << "Set up http server at " << embedded_test_server()->base_url();
151
152    CommandLine::ForCurrentProcess()->AppendSwitchASCII(
153        ::switches::kGaiaUrl, embedded_test_server()->base_url().spec());
154  }
155
156  scoped_ptr<TestContentBrowserClient> content_browser_client_;
157  content::ContentBrowserClient* original_content_browser_client_;
158  FakeGaia fake_gaia_;
159};
160
161IN_PROC_BROWSER_TEST_F(OobeTest, NewUser) {
162  chromeos::WizardController::SkipPostLoginScreensForTesting();
163  chromeos::WizardController* wizard_controller =
164      chromeos::WizardController::default_controller();
165  CHECK(wizard_controller);
166  wizard_controller->SkipToLoginForTesting();
167
168  scoped_refptr<content::MessageLoopRunner> runner =
169      new content::MessageLoopRunner;
170  content_browser_client_->browser_main_extra_parts_->set_quit_task(
171      runner->QuitClosure());
172  runner->Run();
173}
174
175}  // namespace
176