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.
5 // SystemStorage eject API browser tests.
7 #include "base/files/file_path.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/storage_monitor/storage_info.h"
11 #include "components/storage_monitor/storage_monitor.h"
12 #include "components/storage_monitor/test_storage_monitor.h"
13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/test/test_utils.h"
16 #include "extensions/browser/api/system_storage/storage_api_test_util.h"
17 #include "extensions/browser/api/system_storage/storage_info_provider.h"
18 #include "extensions/browser/extension_host.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/shell/test/shell_apitest.h"
22 #include "extensions/test/extension_test_message_listener.h"
26 using extensions::test::TestStorageUnitInfo
;
27 using extensions::test::kRemovableStorageData
;
28 using storage_monitor::StorageMonitor
;
29 using storage_monitor::TestStorageMonitor
;
33 class SystemStorageEjectApiTest
: public extensions::ShellApiTest
{
35 SystemStorageEjectApiTest() : monitor_(NULL
) {}
36 ~SystemStorageEjectApiTest() override
{}
39 void SetUpOnMainThread() override
{
40 monitor_
= TestStorageMonitor::CreateForBrowserTests();
41 ShellApiTest::SetUpOnMainThread();
44 content::RenderViewHost
* GetHost() {
45 ExtensionTestMessageListener
listener("loaded", false);
46 const extensions::Extension
* extension
= LoadApp("system/storage_eject");
48 // Wait for the extension to load completely so we can execute
49 // the JavaScript function from test case.
50 EXPECT_TRUE(listener
.WaitUntilSatisfied());
52 return extensions::ProcessManager::Get(browser_context())
53 ->GetBackgroundHostForExtension(extension
->id())
57 void ExecuteCmdAndCheckReply(content::RenderViewHost
* host
,
58 const std::string
& js_command
,
59 const std::string
& ok_message
) {
60 ExtensionTestMessageListener
listener(ok_message
, false);
61 host
->GetMainFrame()->ExecuteJavaScriptForTests(
62 base::ASCIIToUTF16(js_command
));
63 EXPECT_TRUE(listener
.WaitUntilSatisfied());
67 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
68 StorageMonitor::GetInstance()->receiver()->ProcessAttach(
69 extensions::test::BuildStorageInfoFromTestStorageUnitInfo(
70 kRemovableStorageData
));
71 content::RunAllPendingInMessageLoop();
75 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
76 StorageMonitor::GetInstance()->receiver()->ProcessDetach(
77 kRemovableStorageData
.device_id
);
78 content::RunAllPendingInMessageLoop();
82 TestStorageMonitor
* monitor_
;
85 DISALLOW_COPY_AND_ASSIGN(SystemStorageEjectApiTest
);
88 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest
, EjectTest
) {
89 content::RenderViewHost
* host
= GetHost();
90 ExecuteCmdAndCheckReply(host
, "addAttachListener()", "add_attach_ok");
93 const std::string expect_attach_msg
=
94 base::StringPrintf("%s,%s", "attach_test_ok", kRemovableStorageData
.name
);
95 ExtensionTestMessageListener
attach_finished_listener(expect_attach_msg
,
96 false /* no reply */);
98 EXPECT_TRUE(attach_finished_listener
.WaitUntilSatisfied());
100 ExecuteCmdAndCheckReply(host
, "ejectTest()", "eject_ok");
101 EXPECT_EQ(kRemovableStorageData
.device_id
, monitor_
->ejected_device());
106 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest
, EjectBadDeviceTest
) {
107 ExecuteCmdAndCheckReply(GetHost(), "ejectFailTest()", "eject_no_such_device");
109 EXPECT_EQ("", monitor_
->ejected_device());