1 // Copyright (c) 2012 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 "components/web_modal/native_web_contents_modal_dialog_manager.h" 6 #include "components/web_modal/web_contents_modal_dialog_manager.h" 7 #include "content/public/browser/browser_thread.h" 8 #include "content/public/test/test_renderer_host.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 using content::BrowserThread; 12 13 namespace web_modal { 14 15 class WebContentsModalDialogManagerTest 16 : public content::RenderViewHostTestHarness { 17 public: 18 virtual void SetUp() { 19 content::RenderViewHostTestHarness::SetUp(); 20 WebContentsModalDialogManager::CreateForWebContents(web_contents()); 21 } 22 }; 23 24 class NativeWebContentsModalDialogManagerCloseTest 25 : public NativeWebContentsModalDialogManager { 26 public: 27 NativeWebContentsModalDialogManagerCloseTest( 28 NativeWebContentsModalDialogManagerDelegate* delegate) 29 : delegate_(delegate) {} 30 virtual void ManageDialog(NativeWebContentsModalDialog dialog) OVERRIDE { 31 } 32 virtual void ShowDialog(NativeWebContentsModalDialog dialog) OVERRIDE { 33 } 34 virtual void HideDialog(NativeWebContentsModalDialog dialog) OVERRIDE { 35 } 36 virtual void CloseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { 37 delegate_->WillClose(dialog); 38 close_count++; 39 } 40 virtual void FocusDialog(NativeWebContentsModalDialog dialog) OVERRIDE { 41 } 42 virtual void PulseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { 43 } 44 45 int close_count; 46 NativeWebContentsModalDialogManagerDelegate* delegate_; 47 }; 48 49 NativeWebContentsModalDialogManager* WebContentsModalDialogManager:: 50 CreateNativeManager( 51 NativeWebContentsModalDialogManagerDelegate* native_delegate) { 52 return new NativeWebContentsModalDialogManagerCloseTest(native_delegate); 53 } 54 55 TEST_F(WebContentsModalDialogManagerTest, WebContentsModalDialogs) { 56 WebContentsModalDialogManager* web_contents_modal_dialog_manager = 57 WebContentsModalDialogManager::FromWebContents(web_contents()); 58 WebContentsModalDialogManager::TestApi test_api( 59 web_contents_modal_dialog_manager); 60 61 NativeWebContentsModalDialogManagerCloseTest* native_manager = 62 new NativeWebContentsModalDialogManagerCloseTest( 63 web_contents_modal_dialog_manager); 64 native_manager->close_count = 0; 65 66 test_api.ResetNativeManager(native_manager); 67 68 const int kWindowCount = 4; 69 for (int i = 0; i < kWindowCount; i++) 70 // WebContentsModalDialogManager treats the NativeWebContentsModalDialog as 71 // an opaque type, so creating fake NativeWebContentsModalDialogs using 72 // reinterpret_cast is valid. 73 web_contents_modal_dialog_manager->ShowDialog( 74 reinterpret_cast<NativeWebContentsModalDialog>(i)); 75 EXPECT_EQ(native_manager->close_count, 0); 76 77 test_api.CloseAllDialogs(); 78 EXPECT_EQ(native_manager->close_count, kWindowCount); 79 } 80 81 } // namespace web_modal 82