Cleans up FocusCycler test so that it doesn't trigger calling
ShouldActivate on a deleted object.

BUG=156827
TEST=primarily test only change
[email protected]


Review URL: https://chromiumcodereview.appspot.com/11237003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@163112 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/ash/focus_cycler_unittest.cc b/ash/focus_cycler_unittest.cc
index fab93d0f..a55472b 100644
--- a/ash/focus_cycler_unittest.cc
+++ b/ash/focus_cycler_unittest.cc
@@ -33,49 +33,87 @@
       widget->GetContentsView());
 }
 
-SystemTray* CreateSystemTray() {
-  internal::StatusAreaWidget* widget = new internal::StatusAreaWidget;
-  widget->CreateTrayViews(NULL);
-  widget->Show();
-  return widget->system_tray();
-}
-
 }  // namespace
 
-typedef AshTestBase FocusCyclerTest;
+class FocusCyclerTest : public AshTestBase {
+ public:
+  FocusCyclerTest() {}
 
-// FocusCyclerTest and FocusCyclerLauncherTest tests are disabled due to
-//  use-after-free bugs in them detectable by both AddressSanitizer and Valgrind.
-// See also http://crbug.com/156827.
-TEST_F(FocusCyclerTest, DISABLED_CycleFocusBrowserOnly) {
-  scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
+  virtual void SetUp() OVERRIDE {
+    AshTestBase::SetUp();
 
+    focus_cycler_.reset(new FocusCycler());
+
+    ASSERT_TRUE(Launcher::ForPrimaryDisplay());
+  }
+
+  virtual void TearDown() OVERRIDE {
+    if (tray_.get()) {
+      GetStatusAreaWidgetDelegate(tray_->GetWidget())->
+          SetFocusCyclerForTesting(NULL);
+      tray_.reset();
+    }
+
+    Launcher::ForPrimaryDisplay()->SetFocusCycler(NULL);
+
+    focus_cycler_.reset();
+
+    AshTestBase::TearDown();
+  }
+
+ protected:
+  // Creates the system tray, returning true on success.
+  bool CreateTray() {
+    if (tray_.get())
+      return false;
+    internal::StatusAreaWidget* widget = new internal::StatusAreaWidget;
+    widget->CreateTrayViews(NULL);
+    widget->Show();
+    tray_.reset(widget->system_tray());
+    if (!tray_->GetWidget())
+      return false;
+    focus_cycler_->AddWidget(tray()->GetWidget());
+    GetStatusAreaWidgetDelegate(tray_->GetWidget())->SetFocusCyclerForTesting(
+        focus_cycler());
+    return true;
+  }
+
+  FocusCycler* focus_cycler() { return focus_cycler_.get(); }
+
+  SystemTray* tray() { return tray_.get(); }
+
+  views::Widget* launcher_widget() {
+    return Launcher::ForPrimaryDisplay()->widget();
+  }
+
+  void InstallFocusCycleOnLauncher() {
+    // Add the launcher
+    Launcher* launcher = Launcher::ForPrimaryDisplay();
+    launcher->SetFocusCycler(focus_cycler());
+  }
+
+ private:
+  scoped_ptr<FocusCycler> focus_cycler_;
+  scoped_ptr<SystemTray> tray_;
+
+  DISALLOW_COPY_AND_ASSIGN(FocusCyclerTest);
+};
+
+TEST_F(FocusCyclerTest, CycleFocusBrowserOnly) {
   // Create a single test window.
   scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
   wm::ActivateWindow(window0.get());
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 
   // Cycle the window
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 }
 
-TEST_F(FocusCyclerTest, DISABLED_CycleFocusForward) {
-  scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
+TEST_F(FocusCyclerTest, CycleFocusForward) {
+  ASSERT_TRUE(CreateTray());
 
-  // Add the Status area
-  scoped_ptr<SystemTray> tray(CreateSystemTray());
-  ASSERT_TRUE(tray->GetWidget());
-  focus_cycler->AddWidget(tray->GetWidget());
-  GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
-      focus_cycler.get());
-
-  // Add the launcher
-  Launcher* launcher = Launcher::ForPrimaryDisplay();
-  ASSERT_TRUE(launcher);
-  views::Widget* launcher_widget = launcher->widget();
-  ASSERT_TRUE(launcher_widget);
-  launcher->SetFocusCycler(focus_cycler.get());
+  InstallFocusCycleOnLauncher();
 
   // Create a single test window.
   scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
@@ -83,34 +121,22 @@
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the launcher
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(launcher_widget->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(launcher_widget()->IsActive());
 
   // Cycle focus to the browser
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 }
 
-TEST_F(FocusCyclerTest, DISABLED_CycleFocusBackward) {
-  scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
+TEST_F(FocusCyclerTest, CycleFocusBackward) {
+  ASSERT_TRUE(CreateTray());
 
-  // Add the Status area
-  scoped_ptr<SystemTray> tray(CreateSystemTray());
-  ASSERT_TRUE(tray->GetWidget());
-  focus_cycler->AddWidget(tray->GetWidget());
-  GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
-      focus_cycler.get());
-
-  // Add the launcher
-  Launcher* launcher = Launcher::ForPrimaryDisplay();
-  ASSERT_TRUE(launcher);
-  views::Widget* launcher_widget = launcher->widget();
-  ASSERT_TRUE(launcher_widget);
-  launcher->SetFocusCycler(focus_cycler.get());
+  InstallFocusCycleOnLauncher();
 
   // Create a single test window.
   scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
@@ -118,34 +144,22 @@
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 
   // Cycle focus to the launcher
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
-  EXPECT_TRUE(launcher_widget->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
+  EXPECT_TRUE(launcher_widget()->IsActive());
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the browser
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 }
 
-TEST_F(FocusCyclerTest, DISABLED_CycleFocusForwardBackward) {
-  scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
+TEST_F(FocusCyclerTest, CycleFocusForwardBackward) {
+  ASSERT_TRUE(CreateTray());
 
-  // Add the Status area
-  scoped_ptr<SystemTray> tray(CreateSystemTray());
-  ASSERT_TRUE(tray->GetWidget());
-  focus_cycler->AddWidget(tray->GetWidget());
-  GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
-      focus_cycler.get());
-
-  // Add the launcher
-  Launcher* launcher = Launcher::ForPrimaryDisplay();
-  ASSERT_TRUE(launcher);
-  views::Widget* launcher_widget = launcher->widget();
-  ASSERT_TRUE(launcher_widget);
-  launcher->SetFocusCycler(focus_cycler.get());
+  InstallFocusCycleOnLauncher();
 
   // Create a single test window.
   scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
@@ -153,116 +167,63 @@
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 
   // Cycle focus to the launcher
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
-  EXPECT_TRUE(launcher_widget->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
+  EXPECT_TRUE(launcher_widget()->IsActive());
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the browser
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the launcher
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(launcher_widget->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(launcher_widget()->IsActive());
 
   // Cycle focus to the browser
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 }
 
-TEST_F(FocusCyclerTest, DISABLED_CycleFocusNoBrowser) {
-  scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
+TEST_F(FocusCyclerTest, CycleFocusNoBrowser) {
+  ASSERT_TRUE(CreateTray());
 
-  // Add the Status area
-  scoped_ptr<SystemTray> tray(CreateSystemTray());
-  ASSERT_TRUE(tray->GetWidget());
-  focus_cycler->AddWidget(tray->GetWidget());
-  GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
-      focus_cycler.get());
+  InstallFocusCycleOnLauncher();
 
   // Add the launcher and focus it
-  Launcher* launcher = Launcher::ForPrimaryDisplay();
-  ASSERT_TRUE(launcher);
-  views::Widget* launcher_widget = launcher->widget();
-  ASSERT_TRUE(launcher_widget);
-  launcher->SetFocusCycler(focus_cycler.get());
-  focus_cycler->FocusWidget(launcher_widget);
+  focus_cycler()->FocusWidget(launcher_widget());
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the launcher
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(launcher_widget->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(launcher_widget()->IsActive());
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the launcher
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
-  EXPECT_TRUE(launcher_widget->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
+  EXPECT_TRUE(launcher_widget()->IsActive());
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 }
 
-class FocusCyclerLauncherTest : public AshTestBase {
- public:
-  FocusCyclerLauncherTest() : AshTestBase() {}
-  virtual ~FocusCyclerLauncherTest() {}
-
-  virtual void SetUp() OVERRIDE {
-    AshTestBase::SetUp();
-
-    // Hide the launcher
-    Launcher* launcher = Launcher::ForPrimaryDisplay();
-    ASSERT_TRUE(launcher);
-    views::Widget* launcher_widget = launcher->widget();
-    ASSERT_TRUE(launcher_widget);
-    launcher_widget->Hide();
-  }
-
-  virtual void TearDown() OVERRIDE {
-    // Show the launcher
-    Launcher* launcher = Launcher::ForPrimaryDisplay();
-    ASSERT_TRUE(launcher);
-    views::Widget* launcher_widget = launcher->widget();
-    ASSERT_TRUE(launcher_widget);
-    launcher_widget->Show();
-
-    AshTestBase::TearDown();
-  }
-
- private:
-  DISALLOW_COPY_AND_ASSIGN(FocusCyclerLauncherTest);
-};
-
-TEST_F(FocusCyclerLauncherTest, DISABLED_CycleFocusForwardInvisible) {
-  scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
-
-  // Add the Status area
-  scoped_ptr<SystemTray> tray(CreateSystemTray());
-  ASSERT_TRUE(tray->GetWidget());
-  focus_cycler->AddWidget(tray->GetWidget());
-  GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
-      focus_cycler.get());
-
-  // Add the launcher
-  Launcher* launcher = Launcher::ForPrimaryDisplay();
-  ASSERT_TRUE(launcher);
-  views::Widget* launcher_widget = launcher->widget();
-  ASSERT_TRUE(launcher_widget);
-  launcher->SetFocusCycler(focus_cycler.get());
+TEST_F(FocusCyclerTest, Launcher_CycleFocusForward) {
+  ASSERT_TRUE(CreateTray());
+  InstallFocusCycleOnLauncher();
+  launcher_widget()->Hide();
 
   // Create a single test window.
   scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
@@ -270,30 +231,18 @@
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the browser
-  focus_cycler->RotateFocus(FocusCycler::FORWARD);
+  focus_cycler()->RotateFocus(FocusCycler::FORWARD);
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 }
 
-TEST_F(FocusCyclerLauncherTest, DISABLED_CycleFocusBackwardInvisible) {
-  scoped_ptr<FocusCycler> focus_cycler(new FocusCycler());
-
-  // Add the Status area
-  scoped_ptr<SystemTray> tray(CreateSystemTray());
-  ASSERT_TRUE(tray->GetWidget());
-  focus_cycler->AddWidget(tray->GetWidget());
-  GetStatusAreaWidgetDelegate(tray->GetWidget())->SetFocusCyclerForTesting(
-      focus_cycler.get());
-
-  // Add the launcher
-  Launcher* launcher = Launcher::ForPrimaryDisplay();
-  ASSERT_TRUE(launcher);
-  views::Widget* launcher_widget = launcher->widget();
-  ASSERT_TRUE(launcher_widget);
-  launcher->SetFocusCycler(focus_cycler.get());
+TEST_F(FocusCyclerTest, Launcher_CycleFocusBackwardInvisible) {
+  ASSERT_TRUE(CreateTray());
+  InstallFocusCycleOnLauncher();
+  launcher_widget()->Hide();
 
   // Create a single test window.
   scoped_ptr<Window> window0(CreateTestWindowWithId(0, NULL));
@@ -301,11 +250,11 @@
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 
   // Cycle focus to the status area
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
-  EXPECT_TRUE(tray->GetWidget()->IsActive());
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
+  EXPECT_TRUE(tray()->GetWidget()->IsActive());
 
   // Cycle focus to the browser
-  focus_cycler->RotateFocus(FocusCycler::BACKWARD);
+  focus_cycler()->RotateFocus(FocusCycler::BACKWARD);
   EXPECT_TRUE(wm::IsActiveWindow(window0.get()));
 }