Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / tabs / tab_strip_model_unittest.cc
index 0285ce2..445f2be 100644 (file)
 #include "chrome/browser/ui/tabs/tab_strip_model_order_controller.h"
 #include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
-#include "chrome/common/extensions/extension.h"
 #include "chrome/common/url_constants.h"
 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
 #include "chrome/test/base/testing_profile.h"
+#include "components/web_modal/popup_manager.h"
 #include "components/web_modal/web_contents_modal_dialog_manager.h"
 #include "content/public/browser/navigation_controller.h"
 #include "content/public/browser/navigation_entry.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/browser/web_contents_observer.h"
+#include "extensions/common/extension.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 using content::SiteInstance;
@@ -58,7 +59,7 @@ class DeleteWebContentsOnDestroyedObserver
         tab_strip_(tab_strip) {
   }
 
-  virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
+  virtual void WebContentsDestroyed() OVERRIDE {
     WebContents* tab_to_delete = tab_to_delete_;
     tab_to_delete_ = NULL;
     TabStripModel* tab_strip_to_delete = tab_strip_;
@@ -104,29 +105,32 @@ class TabStripModelTestIDUserData : public base::SupportsUserData::Data {
   int id_;
 };
 
-class DummyNativeWebContentsModalDialogManager
-    : public web_modal::NativeWebContentsModalDialogManager {
+class DummySingleWebContentsDialogManager
+    : public web_modal::SingleWebContentsDialogManager {
  public:
-  explicit DummyNativeWebContentsModalDialogManager(
-      web_modal::NativeWebContentsModalDialogManagerDelegate* delegate)
-      : delegate_(delegate) {}
-  virtual ~DummyNativeWebContentsModalDialogManager() {}
-
-  virtual void ManageDialog(NativeWebContentsModalDialog dialog) OVERRIDE {}
-  virtual void ShowDialog(NativeWebContentsModalDialog dialog) OVERRIDE {}
-  virtual void HideDialog(NativeWebContentsModalDialog dialog) OVERRIDE {}
-  virtual void CloseDialog(NativeWebContentsModalDialog dialog) OVERRIDE {
-    delegate_->WillClose(dialog);
-  }
-  virtual void FocusDialog(NativeWebContentsModalDialog dialog) OVERRIDE {}
-  virtual void PulseDialog(NativeWebContentsModalDialog dialog) OVERRIDE {}
+  explicit DummySingleWebContentsDialogManager(
+      NativeWebContentsModalDialog dialog,
+      web_modal::SingleWebContentsDialogManagerDelegate* delegate)
+      : delegate_(delegate),
+        dialog_(dialog) {}
+  virtual ~DummySingleWebContentsDialogManager() {}
+
+  virtual void Show() OVERRIDE {}
+  virtual void Hide() OVERRIDE {}
+  virtual void Close() OVERRIDE {
+    delegate_->WillClose(dialog_);
+  }
+  virtual void Focus() OVERRIDE {}
+  virtual void Pulse() OVERRIDE {}
   virtual void HostChanged(
       web_modal::WebContentsModalDialogHost* new_host) OVERRIDE {}
+  virtual NativeWebContentsModalDialog dialog() OVERRIDE { return dialog_; }
 
  private:
-  web_modal::NativeWebContentsModalDialogManagerDelegate* delegate_;
+  web_modal::SingleWebContentsDialogManagerDelegate* delegate_;
+  NativeWebContentsModalDialog dialog_;
 
-  DISALLOW_COPY_AND_ASSIGN(DummyNativeWebContentsModalDialogManager);
+  DISALLOW_COPY_AND_ASSIGN(DummySingleWebContentsDialogManager);
 };
 
 // Test Browser-like class for TabStripModelTest.TabBlockedState.
@@ -277,7 +281,9 @@ class MockTabStripModelObserver : public TabStripModelObserver {
     MOVE,
     CHANGE,
     PINNED,
-    REPLACED
+    REPLACED,
+    CLOSE_ALL,
+    CLOSE_ALL_CANCELED,
   };
 
   struct State {
@@ -306,13 +312,32 @@ class MockTabStripModelObserver : public TabStripModelObserver {
     return static_cast<int>(states_.size());
   }
 
-  State GetStateAt(int index) const {
+  // Returns (by way of parameters) the number of state's with CLOSE_ALL and
+  // CLOSE_ALL_CANCELED.
+  void GetCloseCounts(int* close_all_count,
+                      int* close_all_canceled_count) {
+    *close_all_count = *close_all_canceled_count = 0;
+    for (int i = 0; i < GetStateCount(); ++i) {
+      switch (GetStateAt(i).action) {
+        case CLOSE_ALL:
+          (*close_all_count)++;
+          break;
+        case CLOSE_ALL_CANCELED:
+          (*close_all_canceled_count)++;
+          break;
+        default:
+          break;
+      }
+    }
+  }
+
+  const State& GetStateAt(int index) const {
     DCHECK(index >= 0 && index < GetStateCount());
     return states_[index];
   }
 
   bool StateEquals(int index, const State& state) {
-    State s = GetStateAt(index);
+    const State& s = GetStateAt(index);
     return (s.src_contents == state.src_contents &&
             s.dst_contents == state.dst_contents &&
             s.src_index == state.src_index &&
@@ -387,6 +412,12 @@ class MockTabStripModelObserver : public TabStripModelObserver {
   virtual void TabStripEmpty() OVERRIDE {
     empty_ = true;
   }
+  virtual void WillCloseAllTabs() OVERRIDE {
+    states_.push_back(State(NULL, -1, CLOSE_ALL));
+  }
+  virtual void CloseAllTabsCanceled() OVERRIDE {
+    states_.push_back(State(NULL, -1, CLOSE_ALL_CANCELED));
+  }
   virtual void TabStripModelDeleted() OVERRIDE {
     deleted_ = true;
   }
@@ -632,7 +663,14 @@ TEST_F(TabStripModelTest, TestBasicAPI) {
     EXPECT_EQ(0, tabstrip.active_index());
   }
 
+  observer.ClearStates();
   tabstrip.CloseAllTabs();
+
+  int close_all_count = 0, close_all_canceled_count = 0;
+  observer.GetCloseCounts(&close_all_count, &close_all_canceled_count);
+  EXPECT_EQ(1, close_all_count);
+  EXPECT_EQ(0, close_all_canceled_count);
+
   // TabStripModel should now be empty.
   EXPECT_TRUE(tabstrip.empty());
 
@@ -1701,8 +1739,8 @@ TEST_F(TabStripModelTest, NavigationForgettingDoesntAffectNewTab) {
 }
 
 // This fails on Linux when run with the rest of unit_tests (crbug.com/302156)
-// and fails consistently on Mac.
-#if defined(OS_LINUX) || defined(OS_MACOSX)
+// and fails consistently on Mac and Windows.
+#if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_WIN)
 #define MAYBE_FastShutdown \
     DISABLED_FastShutdown
 #else
@@ -1783,7 +1821,7 @@ TEST_F(TabStripModelTest, Apps) {
   base::FilePath path(FILE_PATH_LITERAL("/foo"));
 #endif
 
-  DictionaryValue manifest;
+  base::DictionaryValue manifest;
   manifest.SetString("name", "hi!");
   manifest.SetString("version", "1");
   manifest.SetString("app.launch.web_url", "http://www.google.com");
@@ -2184,12 +2222,22 @@ TEST_F(TabStripModelTest, DeleteFromDestroy) {
   TabStripModel strip(&delegate, profile());
   WebContents* contents1 = CreateWebContents();
   WebContents* contents2 = CreateWebContents();
+  MockTabStripModelObserver tab_strip_model_observer(&strip);
   strip.AppendWebContents(contents1, true);
   strip.AppendWebContents(contents2, true);
   // DeleteWebContentsOnDestroyedObserver deletes contents1 when contents2 sends
   // out notification that it is being destroyed.
   DeleteWebContentsOnDestroyedObserver observer(contents2, contents1, NULL);
+  strip.AddObserver(&tab_strip_model_observer);
   strip.CloseAllTabs();
+
+  int close_all_count = 0, close_all_canceled_count = 0;
+  tab_strip_model_observer.GetCloseCounts(&close_all_count,
+                                          &close_all_canceled_count);
+  EXPECT_EQ(1, close_all_count);
+  EXPECT_EQ(0, close_all_canceled_count);
+
+  strip.RemoveObserver(&tab_strip_model_observer);
 }
 
 // Makes sure TabStripModel handles the case of deleting another tab and the
@@ -2513,19 +2561,23 @@ TEST_F(TabStripModelTest, TabBlockedState) {
   TabStripModel strip_dst(&dummy_tab_strip_delegate, profile());
   TabBlockedStateTestBrowser browser_dst(&strip_dst);
 
-  // Setup a NativeWebContentsModalDialogManager for tab |contents2|.
+  // Setup a SingleWebContentsDialogManager for tab |contents2|.
   web_modal::WebContentsModalDialogManager* modal_dialog_manager =
       web_modal::WebContentsModalDialogManager::FromWebContents(contents2);
-  web_modal::WebContentsModalDialogManager::TestApi test_api(
-      modal_dialog_manager);
-  test_api.ResetNativeManager(
-      new DummyNativeWebContentsModalDialogManager(modal_dialog_manager));
+  web_modal::PopupManager popup_manager(NULL);
+  popup_manager.RegisterWith(contents2);
 
   // Show a dialog that blocks tab |contents2|.
-  // DummyNativeWebContentsModalDialogManager doesn't care about the
+  // DummySingleWebContentsDialogManager doesn't care about the
   // NativeWebContentsModalDialog value, so any dummy value works.
-  modal_dialog_manager->ShowDialog(
-      reinterpret_cast<NativeWebContentsModalDialog>(0));
+  DummySingleWebContentsDialogManager* native_manager =
+      new DummySingleWebContentsDialogManager(
+          reinterpret_cast<NativeWebContentsModalDialog>(0),
+          modal_dialog_manager);
+  modal_dialog_manager->ShowDialogWithManager(
+      reinterpret_cast<NativeWebContentsModalDialog>(0),
+      scoped_ptr<web_modal::SingleWebContentsDialogManager>(
+          native_manager).Pass());
   EXPECT_TRUE(strip_src.IsTabBlocked(1));
 
   // Detach the tab.