1 // Copyright 2013 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 // SystemStorage eject API browser tests. 6 7 #include "base/files/file_path.h" 8 #include "base/strings/stringprintf.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "chrome/browser/extensions/api/system_storage/storage_api_test_util.h" 11 #include "chrome/browser/extensions/extension_apitest.h" 12 #include "components/storage_monitor/storage_info.h" 13 #include "components/storage_monitor/storage_monitor.h" 14 #include "components/storage_monitor/test_storage_monitor.h" 15 #include "content/public/browser/render_frame_host.h" 16 #include "content/public/browser/render_view_host.h" 17 #include "content/public/test/test_utils.h" 18 #include "extensions/browser/api/system_storage/storage_info_provider.h" 19 #include "extensions/browser/extension_system.h" 20 #include "extensions/browser/process_manager.h" 21 #include "extensions/common/extension.h" 22 #include "extensions/test/extension_test_message_listener.h" 23 24 namespace { 25 26 using extensions::test::TestStorageUnitInfo; 27 using extensions::test::kRemovableStorageData; 28 using storage_monitor::StorageMonitor; 29 using storage_monitor::TestStorageMonitor; 30 31 } // namespace 32 33 class SystemStorageEjectApiTest : public ExtensionApiTest { 34 public: 35 SystemStorageEjectApiTest() : monitor_(NULL) {} 36 virtual ~SystemStorageEjectApiTest() {} 37 38 protected: 39 virtual void SetUpOnMainThread() OVERRIDE { 40 monitor_ = TestStorageMonitor::CreateForBrowserTests(); 41 ExtensionApiTest::SetUpOnMainThread(); 42 } 43 44 content::RenderViewHost* GetHost() { 45 const extensions::Extension* extension = 46 LoadExtension(test_data_dir_.AppendASCII("system/storage_eject")); 47 return extensions::ExtensionSystem::Get(browser()->profile())-> 48 process_manager()->GetBackgroundHostForExtension(extension->id())-> 49 render_view_host(); 50 } 51 52 void ExecuteCmdAndCheckReply(content::RenderViewHost* host, 53 const std::string& js_command, 54 const std::string& ok_message) { 55 ExtensionTestMessageListener listener(ok_message, false); 56 host->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(js_command)); 57 EXPECT_TRUE(listener.WaitUntilSatisfied()); 58 } 59 60 void Attach() { 61 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); 62 StorageMonitor::GetInstance()->receiver()->ProcessAttach( 63 extensions::test::BuildStorageInfoFromTestStorageUnitInfo( 64 kRemovableStorageData)); 65 content::RunAllPendingInMessageLoop(); 66 } 67 68 void Detach() { 69 DCHECK(StorageMonitor::GetInstance()->IsInitialized()); 70 StorageMonitor::GetInstance()->receiver()->ProcessDetach( 71 kRemovableStorageData.device_id); 72 content::RunAllPendingInMessageLoop(); 73 } 74 75 protected: 76 TestStorageMonitor* monitor_; 77 78 private: 79 DISALLOW_COPY_AND_ASSIGN(SystemStorageEjectApiTest); 80 }; 81 82 83 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest, EjectTest) { 84 content::RenderViewHost* host = GetHost(); 85 ExecuteCmdAndCheckReply(host, "addAttachListener()", "add_attach_ok"); 86 87 // Attach / detach 88 const std::string expect_attach_msg = 89 base::StringPrintf("%s,%s", "attach_test_ok", 90 kRemovableStorageData.name); 91 ExtensionTestMessageListener attach_finished_listener(expect_attach_msg, 92 false /* no reply */); 93 Attach(); 94 EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied()); 95 96 ExecuteCmdAndCheckReply(host, "ejectTest()", "eject_ok"); 97 EXPECT_EQ(kRemovableStorageData.device_id, monitor_->ejected_device()); 98 99 Detach(); 100 } 101 102 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest, EjectBadDeviceTest) { 103 ExecuteCmdAndCheckReply(GetHost(), "ejectFailTest()", "eject_no_such_device"); 104 105 EXPECT_EQ("", monitor_->ejected_device()); 106 } 107