| 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/stringprintf.h" | 5 #include "base/stringprintf.h" |
| 6 #include "base/system_monitor/system_monitor.h" | 6 #include "base/system_monitor/system_monitor.h" |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_api.h" | 8 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_
private_api.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 } | 73 } |
| 74 | 74 |
| 75 void ChangeListener(content::RenderViewHost* host, | 75 void ChangeListener(content::RenderViewHost* host, |
| 76 const std::string& js_command, | 76 const std::string& js_command, |
| 77 const std::string& ok_message) { | 77 const std::string& ok_message) { |
| 78 ExtensionTestMessageListener listener(ok_message, false /* no reply */); | 78 ExtensionTestMessageListener listener(ok_message, false /* no reply */); |
| 79 host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16(js_command)); | 79 host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16(js_command)); |
| 80 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 80 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 81 } | 81 } |
| 82 | 82 |
| 83 void AttachDetach() { | 83 void Attach(bool listening) { |
| 84 Attach(); | 84 const std::string expect_attach_msg = |
| 85 Detach(); | 85 base::StringPrintf("%s,%s", kAttachTestOk, kDeviceName); |
| 86 } | 86 ExtensionTestMessageListener attach_finished_listener( |
| 87 | 87 expect_attach_msg, |
| 88 void Attach() { | 88 false /* no reply */); |
| 89 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( | 89 base::SystemMonitor::Get()->ProcessRemovableStorageAttached( |
| 90 device_id_, ASCIIToUTF16(kDeviceName), kDevicePath); | 90 device_id_, ASCIIToUTF16(kDeviceName), kDevicePath); |
| 91 WaitForDeviceEvents(); | 91 WaitForDeviceEvents(); |
| 92 if (listening) |
| 93 EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied()); |
| 92 } | 94 } |
| 93 | 95 |
| 94 void Detach() { | 96 void Detach(bool listening) { |
| 97 ExtensionTestMessageListener detach_finished_listener( |
| 98 kDetachTestOk, |
| 99 false /* no reply */); |
| 95 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id_); | 100 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id_); |
| 96 WaitForDeviceEvents(); | 101 WaitForDeviceEvents(); |
| 102 if (listening) |
| 103 EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied()); |
| 97 } | 104 } |
| 98 | 105 |
| 99 private: | 106 private: |
| 100 void WaitForDeviceEvents() { | 107 void WaitForDeviceEvents() { |
| 101 content::RunAllPendingInMessageLoop(); | 108 content::RunAllPendingInMessageLoop(); |
| 102 } | 109 } |
| 103 | 110 |
| 104 std::string device_id_; | 111 std::string device_id_; |
| 105 | 112 |
| 106 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateApiTest); | 113 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateApiTest); |
| 107 }; | 114 }; |
| 108 | 115 |
| 109 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateApiTest, DeviceAttachDetachEvents) { | 116 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateApiTest, DeviceAttachDetachEvents) { |
| 110 // Setup. | 117 // Setup. |
| 111 const extensions::Extension* extension = | 118 const extensions::Extension* extension = |
| 112 LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath)); | 119 LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath)); |
| 113 ASSERT_TRUE(extension); | 120 ASSERT_TRUE(extension); |
| 114 | 121 |
| 115 content::RenderViewHost* host = | 122 content::RenderViewHost* host = |
| 116 extensions::ExtensionSystem::Get(browser()->profile())-> | 123 extensions::ExtensionSystem::Get(browser()->profile())-> |
| 117 process_manager()->GetBackgroundHostForExtension(extension->id())-> | 124 process_manager()->GetBackgroundHostForExtension(extension->id())-> |
| 118 render_view_host(); | 125 render_view_host(); |
| 119 ASSERT_TRUE(host); | 126 ASSERT_TRUE(host); |
| 120 | 127 |
| 121 // No listeners, attach and detach a couple times. | 128 // No listeners, attach and detach a couple times. |
| 122 AttachDetach(); | 129 Attach(false); |
| 123 AttachDetach(); | 130 Detach(false); |
| 131 Attach(false); |
| 132 Detach(false); |
| 124 | 133 |
| 125 // Add attach listener. | 134 // Add attach listener. |
| 126 ChangeListener(host, kAddAttachListenerCmd, kAddAttachListenerOk); | 135 ChangeListener(host, kAddAttachListenerCmd, kAddAttachListenerOk); |
| 127 | 136 |
| 128 // Attach / detach | 137 // Attach / detach |
| 129 const std::string expect_attach_msg = | 138 Attach(true); |
| 130 base::StringPrintf("%s,%s", kAttachTestOk, kDeviceName); | 139 Detach(false); |
| 131 ExtensionTestMessageListener attach_finished_listener(expect_attach_msg, | |
| 132 false /* no reply */); | |
| 133 Attach(); | |
| 134 EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied()); | |
| 135 Detach(); | |
| 136 | 140 |
| 137 // Attach / detach | 141 // Attach / detach |
| 138 Attach(); | 142 Attach(true); |
| 139 EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied()); | 143 Detach(false); |
| 140 // Detach | |
| 141 Detach(); | |
| 142 | 144 |
| 143 // Remove attach listener. | 145 // Remove attach listener. |
| 144 ChangeListener(host, kRemoveAttachListenerCmd, kRemoveAttachListenerOk); | 146 ChangeListener(host, kRemoveAttachListenerCmd, kRemoveAttachListenerOk); |
| 145 | 147 |
| 146 // No listeners, attach and detach a couple times. | 148 // No listeners, attach and detach a couple times. |
| 147 AttachDetach(); | 149 Attach(false); |
| 148 AttachDetach(); | 150 Detach(false); |
| 151 Attach(false); |
| 152 Detach(false); |
| 149 | 153 |
| 150 // Add detach listener. | 154 // Add detach listener. |
| 151 ChangeListener(host, kAddDummyDetachListenerCmd, kAddDummyDetachListenerOk); | 155 ChangeListener(host, kAddDummyDetachListenerCmd, kAddDummyDetachListenerOk); |
| 152 | 156 |
| 153 // Attach / detach | 157 // Attach / detach |
| 154 Attach(); | 158 Attach(false); |
| 155 | 159 Detach(true); |
| 156 ExtensionTestMessageListener detach_finished_listener(kDetachTestOk, | |
| 157 false /* no reply */); | |
| 158 Detach(); | |
| 159 EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied()); | |
| 160 | 160 |
| 161 // Attach / detach | 161 // Attach / detach |
| 162 Attach(); | 162 Attach(false); |
| 163 Detach(); | 163 Detach(true); |
| 164 EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied()); | |
| 165 | 164 |
| 166 // Switch ok dummy detach listener for the regular one. | 165 // Switch ok dummy detach listener for the regular one. |
| 167 ChangeListener(host, kRemoveDummyDetachListenerCmd, | 166 ChangeListener(host, kRemoveDummyDetachListenerCmd, |
| 168 kRemoveDummyDetachListenerOk); | 167 kRemoveDummyDetachListenerOk); |
| 169 ChangeListener(host, kAddDetachListenerCmd, kAddDetachListenerOk); | 168 ChangeListener(host, kAddDetachListenerCmd, kAddDetachListenerOk); |
| 170 | 169 |
| 171 // Add attach listener. | 170 // Add attach listener. |
| 172 ChangeListener(host, kAddAttachListenerCmd, kAddAttachListenerOk); | 171 ChangeListener(host, kAddAttachListenerCmd, kAddAttachListenerOk); |
| 173 | 172 |
| 174 Attach(); | 173 Attach(true); |
| 175 EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied()); | 174 Detach(true); |
| 176 Detach(); | |
| 177 EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied()); | |
| 178 | 175 |
| 179 Attach(); | 176 Attach(true); |
| 180 EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied()); | 177 Detach(true); |
| 181 Detach(); | 178 CloseShellWindowsAndWaitForAppToExit(); |
| 182 EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied()); | |
| 183 } | 179 } |
| OLD | NEW |