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()->ExecuteJavaScript(base::ASCIIToUTF16(js_command
));
62 EXPECT_TRUE(listener
.WaitUntilSatisfied());
66 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
67 StorageMonitor::GetInstance()->receiver()->ProcessAttach(
68 extensions::test::BuildStorageInfoFromTestStorageUnitInfo(
69 kRemovableStorageData
));
70 content::RunAllPendingInMessageLoop();
74 DCHECK(StorageMonitor::GetInstance()->IsInitialized());
75 StorageMonitor::GetInstance()->receiver()->ProcessDetach(
76 kRemovableStorageData
.device_id
);
77 content::RunAllPendingInMessageLoop();
81 TestStorageMonitor
* monitor_
;
84 DISALLOW_COPY_AND_ASSIGN(SystemStorageEjectApiTest
);
87 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest
, EjectTest
) {
88 content::RenderViewHost
* host
= GetHost();
89 ExecuteCmdAndCheckReply(host
, "addAttachListener()", "add_attach_ok");
92 const std::string expect_attach_msg
=
93 base::StringPrintf("%s,%s", "attach_test_ok", kRemovableStorageData
.name
);
94 ExtensionTestMessageListener
attach_finished_listener(expect_attach_msg
,
95 false /* no reply */);
97 EXPECT_TRUE(attach_finished_listener
.WaitUntilSatisfied());
99 ExecuteCmdAndCheckReply(host
, "ejectTest()", "eject_ok");
100 EXPECT_EQ(kRemovableStorageData
.device_id
, monitor_
->ejected_device());
105 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest
, EjectBadDeviceTest
) {
106 ExecuteCmdAndCheckReply(GetHost(), "ejectFailTest()", "eject_no_such_device");
108 EXPECT_EQ("", monitor_
->ejected_device());