OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" |
5 #include "chrome/browser/ui/native_web_contents_modal_dialog_manager.h" | 6 #include "chrome/browser/ui/native_web_contents_modal_dialog_manager.h" |
6 #include "chrome/browser/ui/web_contents_modal_dialog_manager.h" | 7 #include "chrome/browser/ui/web_contents_modal_dialog_manager.h" |
| 8 #include "chrome/common/render_messages.h" |
7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 9 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 10 #include "content/public/test/mock_render_process_host.h" |
8 #include "content/public/test/test_browser_thread.h" | 11 #include "content/public/test/test_browser_thread.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
10 | 13 |
11 using content::BrowserThread; | 14 using content::BrowserThread; |
12 | 15 |
13 class WebContentsModalDialogManagerTest | 16 class StubNativeManager : public NativeWebContentsModalDialogManager { |
14 : public ChromeRenderViewHostTestHarness { | |
15 public: | 17 public: |
16 WebContentsModalDialogManagerTest() | 18 StubNativeManager( |
17 : ChromeRenderViewHostTestHarness(), | |
18 ui_thread_(BrowserThread::UI, &message_loop_) { | |
19 } | |
20 | |
21 virtual void SetUp() { | |
22 ChromeRenderViewHostTestHarness::SetUp(); | |
23 WebContentsModalDialogManager::CreateForWebContents(web_contents()); | |
24 } | |
25 | |
26 private: | |
27 content::TestBrowserThread ui_thread_; | |
28 }; | |
29 | |
30 class NativeWebContentsModalDialogManagerCloseTest | |
31 : public NativeWebContentsModalDialogManager { | |
32 public: | |
33 NativeWebContentsModalDialogManagerCloseTest( | |
34 NativeWebContentsModalDialogManagerDelegate* delegate) | 19 NativeWebContentsModalDialogManagerDelegate* delegate) |
35 : delegate_(delegate) {} | 20 : close_count(0), delegate_(delegate) {} |
36 virtual void ManageDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 21 virtual void ManageDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
37 } | 22 } |
38 virtual void ShowDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 23 virtual void ShowDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
39 } | 24 } |
40 virtual void HideDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 25 virtual void HideDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
41 } | 26 } |
42 virtual void CloseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 27 virtual void CloseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
43 delegate_->WillClose(dialog); | 28 delegate_->WillClose(dialog); |
44 close_count++; | 29 close_count++; |
45 } | 30 } |
46 virtual void FocusDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 31 virtual void FocusDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
47 } | 32 } |
48 virtual void PulseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { | 33 virtual void PulseDialog(NativeWebContentsModalDialog dialog) OVERRIDE { |
49 } | 34 } |
50 | 35 |
51 int close_count; | 36 int close_count; |
| 37 |
| 38 private: |
52 NativeWebContentsModalDialogManagerDelegate* delegate_; | 39 NativeWebContentsModalDialogManagerDelegate* delegate_; |
53 }; | 40 }; |
54 | 41 |
55 TEST_F(WebContentsModalDialogManagerTest, WebContentsModalDialogs) { | 42 class WebContentsModalDialogManagerTest |
56 WebContentsModalDialogManager* web_contents_modal_dialog_manager = | 43 : public ChromeRenderViewHostTestHarness { |
57 WebContentsModalDialogManager::FromWebContents(web_contents()); | 44 public: |
58 WebContentsModalDialogManager::TestApi test_api( | 45 WebContentsModalDialogManagerTest() |
59 web_contents_modal_dialog_manager); | 46 : ChromeRenderViewHostTestHarness(), |
| 47 manager_(NULL), |
| 48 test_api_(NULL), |
| 49 native_manager_(NULL), |
| 50 ui_thread_(BrowserThread::UI, &message_loop_) { |
| 51 } |
60 | 52 |
61 NativeWebContentsModalDialogManagerCloseTest* native_manager = | 53 virtual void SetUp() { |
62 new NativeWebContentsModalDialogManagerCloseTest( | 54 ChromeRenderViewHostTestHarness::SetUp(); |
63 web_contents_modal_dialog_manager); | 55 WebContentsModalDialogManager::CreateForWebContents(web_contents()); |
64 native_manager->close_count = 0; | |
65 | 56 |
66 test_api.ResetNativeManager(native_manager); | 57 manager_ = WebContentsModalDialogManager::FromWebContents(web_contents()); |
| 58 test_api_.reset(new WebContentsModalDialogManager::TestApi(manager_)); |
| 59 native_manager_ = new StubNativeManager(manager_); |
| 60 test_api_->ResetNativeManager(native_manager_); |
| 61 } |
67 | 62 |
68 const int kWindowCount = 4; | 63 virtual void TearDown() { |
69 for (int i = 0; i < kWindowCount; i++) | 64 manager_ = NULL; |
| 65 test_api_.reset(); |
| 66 native_manager_ = NULL; |
| 67 |
| 68 ChromeRenderViewHostTestHarness::TearDown(); |
| 69 } |
| 70 |
| 71 protected: |
| 72 static NativeWebContentsModalDialog GenerateFakeDialog() { |
| 73 static int current_id = 0; |
| 74 |
70 // WebContentsModalDialogManager treats the NativeWebContentsModalDialog as | 75 // WebContentsModalDialogManager treats the NativeWebContentsModalDialog as |
71 // an opaque type, so creating fake NativeWebContentsModalDialogs using | 76 // an opaque type, so creating fake NativeWebContentsModalDialogs using |
72 // reinterpret_cast is valid. | 77 // reinterpret_cast is valid. |
73 web_contents_modal_dialog_manager->ShowDialog( | 78 return reinterpret_cast<NativeWebContentsModalDialog>(current_id++); |
74 reinterpret_cast<NativeWebContentsModalDialog>(i)); | 79 } |
75 EXPECT_EQ(native_manager->close_count, 0); | |
76 | 80 |
77 test_api.CloseAllDialogs(); | 81 void ExpectShieldingChanged(bool expected_shielded) { |
78 EXPECT_EQ(native_manager->close_count, kWindowCount); | 82 typedef ChromeViewMsg_SetVisuallyDeemphasized MsgType; |
| 83 |
| 84 const IPC::Message* message = |
| 85 process()->sink().GetUniqueMessageMatching(MsgType::ID); |
| 86 ASSERT_TRUE(message != NULL); |
| 87 MsgType::Param params; |
| 88 MsgType::Read(message, ¶ms); |
| 89 EXPECT_EQ(expected_shielded, params.a); // shielded |
| 90 } |
| 91 |
| 92 void ExpectShieldingUnchanged(bool currently_shielded) { |
| 93 typedef ChromeViewMsg_SetVisuallyDeemphasized MsgType; |
| 94 |
| 95 const IPC::Message* message = |
| 96 process()->sink().GetUniqueMessageMatching(MsgType::ID); |
| 97 if (message == NULL) |
| 98 return; |
| 99 |
| 100 MsgType::Param params; |
| 101 MsgType::Read(message, ¶ms); |
| 102 EXPECT_EQ(currently_shielded, params.a); // shielded |
| 103 } |
| 104 |
| 105 WebContentsModalDialogManager* manager_; |
| 106 scoped_ptr<WebContentsModalDialogManager::TestApi> test_api_; |
| 107 StubNativeManager* native_manager_; |
| 108 |
| 109 private: |
| 110 content::TestBrowserThread ui_thread_; |
| 111 }; |
| 112 |
| 113 TEST_F(WebContentsModalDialogManagerTest, WebContentsModalDialogs) { |
| 114 const int kWindowCount = 4; |
| 115 for (int i = 0; i < kWindowCount; i++) |
| 116 manager_->ShowDialog(GenerateFakeDialog()); |
| 117 EXPECT_EQ(0, native_manager_->close_count); |
| 118 |
| 119 test_api_->CloseAllDialogs(); |
| 120 EXPECT_EQ(kWindowCount, native_manager_->close_count); |
79 } | 121 } |
| 122 |
| 123 // Validate the shielding activation and deactivation in the presence of |
| 124 // multiple shielded dialogs. |
| 125 TEST_F(WebContentsModalDialogManagerTest, ShowDialogsWithShield) { |
| 126 IPC::TestSink& sink = process()->sink(); |
| 127 |
| 128 NativeWebContentsModalDialog shieldedDialog1 = GenerateFakeDialog(); |
| 129 manager_->ShowDialogWithShield(shieldedDialog1); |
| 130 ExpectShieldingChanged(true); |
| 131 sink.ClearMessages(); |
| 132 |
| 133 NativeWebContentsModalDialog unshieldedDialog = GenerateFakeDialog(); |
| 134 manager_->ShowDialog(unshieldedDialog); |
| 135 ExpectShieldingUnchanged(true); |
| 136 sink.ClearMessages(); |
| 137 |
| 138 NativeWebContentsModalDialog shieldedDialog2 = GenerateFakeDialog(); |
| 139 manager_->ShowDialogWithShield(shieldedDialog2); |
| 140 ExpectShieldingUnchanged(true); |
| 141 sink.ClearMessages(); |
| 142 |
| 143 // Close dialogs in an order different from the reverse that they were opened. |
| 144 manager_->WillClose(shieldedDialog1); |
| 145 ExpectShieldingUnchanged(true); |
| 146 sink.ClearMessages(); |
| 147 |
| 148 manager_->WillClose(shieldedDialog2); |
| 149 ExpectShieldingChanged(false); |
| 150 sink.ClearMessages(); |
| 151 |
| 152 manager_->WillClose(unshieldedDialog); |
| 153 ExpectShieldingUnchanged(false); |
| 154 sink.ClearMessages(); |
| 155 } |
OLD | NEW |