Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / system_storage / system_storage_eject_apitest.cc
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/process_manager.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/test/extension_test_message_listener.h"
22
23 namespace {
24
25 using extensions::test::TestStorageUnitInfo;
26 using extensions::test::kRemovableStorageData;
27 using storage_monitor::StorageMonitor;
28 using storage_monitor::TestStorageMonitor;
29
30 }  // namespace
31
32 class SystemStorageEjectApiTest : public ExtensionApiTest {
33  public:
34   SystemStorageEjectApiTest() : monitor_(NULL) {}
35   ~SystemStorageEjectApiTest() override {}
36
37  protected:
38   void SetUpOnMainThread() override {
39     monitor_ = TestStorageMonitor::CreateForBrowserTests();
40     ExtensionApiTest::SetUpOnMainThread();
41   }
42
43   content::RenderViewHost* GetHost() {
44     const extensions::Extension* extension =
45         LoadExtension(test_data_dir_.AppendASCII("system/storage_eject"));
46     return extensions::ProcessManager::Get(browser()->profile())
47         ->GetBackgroundHostForExtension(extension->id())
48         ->render_view_host();
49   }
50
51   void ExecuteCmdAndCheckReply(content::RenderViewHost* host,
52                                const std::string& js_command,
53                                const std::string& ok_message) {
54     ExtensionTestMessageListener listener(ok_message, false);
55     host->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(js_command));
56     EXPECT_TRUE(listener.WaitUntilSatisfied());
57   }
58
59   void Attach() {
60     DCHECK(StorageMonitor::GetInstance()->IsInitialized());
61     StorageMonitor::GetInstance()->receiver()->ProcessAttach(
62         extensions::test::BuildStorageInfoFromTestStorageUnitInfo(
63             kRemovableStorageData));
64     content::RunAllPendingInMessageLoop();
65   }
66
67   void Detach() {
68     DCHECK(StorageMonitor::GetInstance()->IsInitialized());
69     StorageMonitor::GetInstance()->receiver()->ProcessDetach(
70         kRemovableStorageData.device_id);
71     content::RunAllPendingInMessageLoop();
72   }
73
74  protected:
75   TestStorageMonitor* monitor_;
76
77  private:
78   DISALLOW_COPY_AND_ASSIGN(SystemStorageEjectApiTest);
79 };
80
81
82 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest, EjectTest) {
83   content::RenderViewHost* host = GetHost();
84   ExecuteCmdAndCheckReply(host, "addAttachListener()", "add_attach_ok");
85
86   // Attach / detach
87   const std::string expect_attach_msg =
88       base::StringPrintf("%s,%s", "attach_test_ok",
89                          kRemovableStorageData.name);
90   ExtensionTestMessageListener attach_finished_listener(expect_attach_msg,
91                                                         false  /* no reply */);
92   Attach();
93   EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied());
94
95   ExecuteCmdAndCheckReply(host, "ejectTest()", "eject_ok");
96   EXPECT_EQ(kRemovableStorageData.device_id, monitor_->ejected_device());
97
98   Detach();
99 }
100
101 IN_PROC_BROWSER_TEST_F(SystemStorageEjectApiTest, EjectBadDeviceTest) {
102   ExecuteCmdAndCheckReply(GetHost(), "ejectFailTest()", "eject_no_such_device");
103
104   EXPECT_EQ("", monitor_->ejected_device());
105 }