15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/first_run/first_run_helper.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/shell.h"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ash/system/tray/system_tray.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/chromeos/first_run/first_run.h"
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/chromeos/first_run/first_run_controller.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/chromeos/first_run/step_names.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/chromeos/login/test/js_checker.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/test/test_utils.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace chromeos {
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class FirstRunUIBrowserTest : public InProcessBrowserTest,
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              public FirstRunActor::Delegate {
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FirstRunUIBrowserTest()
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      : initialized_(false),
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        finalized_(false) {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // FirstRunActor::Delegate overrides.
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnActorInitialized() OVERRIDE {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    initialized_ = true;
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!on_initialized_callback_.is_null())
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      on_initialized_callback_.Run();
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnActorInitialized();
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnNextButtonClicked(const std::string& step_name) OVERRIDE {
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnNextButtonClicked(step_name);
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStepShown(const std::string& step_name) OVERRIDE {
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    current_step_name_ = step_name;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!on_step_shown_callback_.is_null())
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      on_step_shown_callback_.Run();
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnStepShown(step_name);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnStepHidden(const std::string& step_name) OVERRIDE {
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnStepHidden(step_name);
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnHelpButtonClicked() OVERRIDE {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnHelpButtonClicked();
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnActorFinalized() OVERRIDE {
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    finalized_ = true;
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!on_finalized_callback_.is_null())
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      on_finalized_callback_.Run();
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnActorFinalized();
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnActorDestroyed() OVERRIDE {
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->OnActorDestroyed();
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void LaunchTutorial() {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    chromeos::first_run::LaunchTutorial();
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_TRUE(controller() != NULL);
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Replacing delegate to observe all messages coming from WebUI to
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // controller.
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    controller()->actor_->set_delegate(this);
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    initialized_ = controller()->actor_->IsInitialized();
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void WaitForInitialization() {
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (initialized_)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    WaitUntilCalled(&on_initialized_callback_);
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_TRUE(initialized_);
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    js().set_web_contents(controller()->web_contents_for_tests_);
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void WaitForStep(const std::string& step_name) {
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (current_step_name_ == step_name)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    WaitUntilCalled(&on_step_shown_callback_);
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    EXPECT_EQ(current_step_name_, step_name);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void AdvanceStep() {
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    js().Evaluate("cr.FirstRun.currentStep_.nextButton_.click()");
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void WaitForFinalization() {
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (!finalized_) {
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      WaitUntilCalled(&on_finalized_callback_);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      EXPECT_TRUE(finalized_);
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void WaitUntilCalled(base::Closure* callback) {
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<content::MessageLoopRunner> runner =
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        new content::MessageLoopRunner;
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    *callback = runner->QuitClosure();
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    runner->Run();
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    callback->Reset();
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  test::JSChecker& js() { return js_; }
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ash::FirstRunHelper* shell_helper() {
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return controller()->shell_helper_.get();
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FirstRunController* controller() {
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return FirstRunController::GetInstanceForTest();
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string current_step_name_;
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool initialized_;
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool finalized_;
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Closure on_initialized_callback_;
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Closure on_step_shown_callback_;
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  base::Closure on_finalized_callback_;
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  test::JSChecker js_;
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)IN_PROC_BROWSER_TEST_F(FirstRunUIBrowserTest, FirstRunFlow) {
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  LaunchTutorial();
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WaitForInitialization();
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WaitForStep(first_run::kAppListStep);
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(shell_helper()->IsTrayBubbleOpened());
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AdvanceStep();
1325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WaitForStep(first_run::kTrayStep);
1335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(shell_helper()->IsTrayBubbleOpened());
1345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AdvanceStep();
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WaitForStep(first_run::kHelpStep);
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(shell_helper()->IsTrayBubbleOpened());
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AdvanceStep();
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WaitForFinalization();
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::RunAllPendingInMessageLoop();
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(controller(), (void*)NULL);
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // shell_helper() is destructed already, thats why we call Shell directly.
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(ash::Shell::GetInstance()->GetPrimarySystemTray()->
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      HasSystemBubble());
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace chromeos
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
148