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)
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <string>
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h"
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/prefs/pref_service.h"
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "base/run_loop.h"
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/browser_process.h"
127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch#include "chrome/browser/chrome_notification_types.h"
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/chromeos/login/existing_user_controller.h"
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/chromeos/login/ui/webui_login_display.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/chromeos/login/wizard_controller.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/rlz/rlz.h"
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h"
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
21c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chromeos/chromeos_switches.h"
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "content/public/browser/notification_service.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_utils.h"
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "google_apis/gaia/fake_gaia.h"
255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "google_apis/gaia/gaia_switches.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "net/test/embedded_test_server/embedded_test_server.h"
285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "net/test/embedded_test_server/http_response.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace chromeos {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#if defined(ENABLE_RLZ)
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void GetAccessPointRlzInBackgroundThread(rlz_lib::AccessPoint point,
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                         base::string16* rlz) {
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ASSERT_FALSE(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ASSERT_TRUE(RLZTracker::GetAccessPointRlz(point, rlz));
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace
44c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class LoginUtilsTest : public InProcessBrowserTest {
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  LoginUtilsTest() {}
4823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Initialize the test server early, so that we can use its base url for
515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // the command line flags.
525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
5323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Use the login manager screens and the gaia auth extension.
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    command_line->AppendSwitch(switches::kLoginManager);
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    command_line->AppendSwitch(switches::kForceLoginManagerInTests);
575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    command_line->AppendSwitchASCII(switches::kLoginProfile, "user");
585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    command_line->AppendSwitchASCII(::switches::kAuthExtensionPath,
595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                    "gaia_auth");
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Redirect requests to gaia and the policy server to the test server.
625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    command_line->AppendSwitchASCII(::switches::kGaiaUrl,
635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                    embedded_test_server()->base_url().spec());
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    command_line->AppendSwitchASCII(::switches::kLsoUrl,
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                    embedded_test_server()->base_url().spec());
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    fake_gaia_.Initialize();
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    embedded_test_server()->RegisterRequestHandler(
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        base::Bind(&FakeGaia::HandleRequest, base::Unretained(&fake_gaia_)));
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void TearDownOnMainThread() OVERRIDE {
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    RunUntilIdle();
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RunUntilIdle() {
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    base::RunLoop().RunUntilIdle();
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  PrefService* local_state() {
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return g_browser_process->local_state();
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void Login(const std::string& username) {
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    content::WindowedNotificationObserver session_started_observer(
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        chrome::NOTIFICATION_SESSION_STARTED,
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        content::NotificationService::AllSources());
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ExistingUserController* controller =
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        ExistingUserController::current_controller();
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ASSERT_TRUE(controller);
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    WebUILoginDisplay* login_display =
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        static_cast<WebUILoginDisplay*>(controller->login_display());
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ASSERT_TRUE(login_display);
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    login_display->ShowSigninScreenForCreds(username, "password");
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // Wait for the session to start after submitting the credentials. This
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // will wait until all the background requests are done.
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    session_started_observer.Wait();
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  FakeGaia fake_gaia_;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(LoginUtilsTest);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#if defined(ENABLE_RLZ)
1135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)IN_PROC_BROWSER_TEST_F(LoginUtilsTest, RlzInitialized) {
1145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Skip to the signin screen.
1155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  WizardController::SkipPostLoginScreensForTesting();
1165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  WizardController* wizard_controller = WizardController::default_controller();
1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  ASSERT_TRUE(wizard_controller);
1185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  wizard_controller->SkipToLoginForTesting(LoginScreenContext());
1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  content::WindowedNotificationObserver(
1215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      content::NotificationService::AllSources()).Wait();
1235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  RunUntilIdle();
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // No RLZ brand code set initially.
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  EXPECT_FALSE(local_state()->HasPrefPath(prefs::kRLZBrand));
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Wait for blocking RLZ tasks to complete.
1295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  {
1305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    base::RunLoop loop;
1315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    PrefChangeRegistrar registrar;
1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    registrar.Init(local_state());
1335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    registrar.Add(prefs::kRLZBrand, loop.QuitClosure());
1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    Login("username");
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    loop.Run();
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // RLZ brand code has been set to empty string.
1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  EXPECT_TRUE(local_state()->HasPrefPath(prefs::kRLZBrand));
1405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  EXPECT_EQ(std::string(), local_state()->GetString(prefs::kRLZBrand));
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // RLZ value for homepage access point should have been initialized.
1435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // This value must be obtained in a background thread.
1445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  {
1455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    base::RunLoop loop;
1465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    base::string16 rlz_string;
1475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    content::BrowserThread::PostBlockingPoolTaskAndReply(
1485f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        FROM_HERE,
1495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        base::Bind(&GetAccessPointRlzInBackgroundThread,
1505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   RLZTracker::ChromeHomePage(),
1515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                   &rlz_string),
1525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        loop.QuitClosure());
1535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    loop.Run();
1545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    EXPECT_EQ(base::string16(), rlz_string);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch}  // namespace chromeos
160