- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / media_galleries_private / media_galleries_private_apitest.cc
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
3 // found in the LICENSE file.
4
5 #include "base/strings/stringprintf.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/storage_monitor/storage_info.h"
13 #include "chrome/browser/storage_monitor/storage_monitor.h"
14 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/extension.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "url/gurl.h"
23
24 namespace {
25
26 // Id of test extension from
27 // chrome/test/data/extensions/api_test/|kTestExtensionPath|
28 const char kTestExtensionId[] = "lkegdcleigedmkiikoijjgfchobofdbe";
29 const char kTestExtensionPath[] = "media_galleries_private/attachdetach";
30
31 // JS commands.
32 const char kAddAttachListenerCmd[] = "addAttachListener()";
33 const char kAddDetachListenerCmd[] = "addDetachListener()";
34 const char kAddDummyDetachListenerCmd[] = "addDummyDetachListener()";
35 const char kRemoveAttachListenerCmd[] = "removeAttachListener()";
36 const char kRemoveDummyDetachListenerCmd[] = "removeDummyDetachListener()";
37
38 // And JS reply messages.
39 const char kAddAttachListenerOk[] = "add_attach_ok";
40 const char kAddDetachListenerOk[] = "add_detach_ok";
41 const char kAddDummyDetachListenerOk[] = "add_dummy_detach_ok";
42 const char kRemoveAttachListenerOk[] = "remove_attach_ok";
43 const char kRemoveDummyDetachListenerOk[] = "remove_dummy_detach_ok";
44
45 // Test reply messages.
46 const char kAttachTestOk[] = "attach_test_ok";
47 const char kDetachTestOk[] = "detach_test_ok";
48
49 // Dummy device properties.
50 const char kDeviceId[] = "testDeviceId";
51 const char kDeviceName[] = "foobar";
52 base::FilePath::CharType kDevicePath[] = FILE_PATH_LITERAL("/qux");
53
54 }  // namespace
55
56 class MediaGalleriesPrivateApiTest : public ExtensionApiTest {
57  public:
58   MediaGalleriesPrivateApiTest() {}
59   virtual ~MediaGalleriesPrivateApiTest() {}
60
61   // ExtensionApiTest overrides.
62   virtual void SetUp() OVERRIDE {
63     device_id_ = StorageInfo::MakeDeviceId(
64         StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, kDeviceId);
65     ExtensionApiTest::SetUp();
66   }
67
68  protected:
69   // ExtensionApiTest overrides.
70   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
71     ExtensionApiTest::SetUpCommandLine(command_line);
72     command_line->AppendSwitchASCII(switches::kWhitelistedExtensionID,
73                                     kTestExtensionId);
74   }
75
76   void ChangeListener(content::RenderViewHost* host,
77                       const std::string& js_command,
78                       const std::string& ok_message) {
79     ExtensionTestMessageListener listener(ok_message, false  /* no reply */);
80     host->ExecuteJavascriptInWebFrame(string16(), ASCIIToUTF16(js_command));
81     EXPECT_TRUE(listener.WaitUntilSatisfied());
82   }
83
84   void AttachDetach() {
85     Attach();
86     Detach();
87   }
88
89   void Attach() {
90     DCHECK(StorageMonitor::GetInstance()->IsInitialized());
91     StorageInfo info(device_id_, ASCIIToUTF16(kDeviceName), kDevicePath,
92                      string16(), string16(), string16(), 0);
93     StorageMonitor::GetInstance()->receiver()->ProcessAttach(info);
94     content::RunAllPendingInMessageLoop();
95   }
96
97   void Detach() {
98     DCHECK(StorageMonitor::GetInstance()->IsInitialized());
99     StorageMonitor::GetInstance()->receiver()->ProcessDetach(device_id_);
100     content::RunAllPendingInMessageLoop();
101   }
102
103  private:
104   std::string device_id_;
105
106   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPrivateApiTest);
107 };
108
109 IN_PROC_BROWSER_TEST_F(MediaGalleriesPrivateApiTest, DeviceAttachDetachEvents) {
110   // Setup.
111   TestStorageMonitor::SyncInitialize();
112   const extensions::Extension* extension =
113       LoadExtension(test_data_dir_.AppendASCII(kTestExtensionPath));
114   ASSERT_TRUE(extension);
115
116   content::RenderViewHost* host =
117       extensions::ExtensionSystem::Get(browser()->profile())->
118           process_manager()->GetBackgroundHostForExtension(extension->id())->
119               render_view_host();
120   ASSERT_TRUE(host);
121
122   // No listeners, attach and detach a couple times.
123   AttachDetach();
124   AttachDetach();
125
126   // Add attach listener.
127   ChangeListener(host, kAddAttachListenerCmd, kAddAttachListenerOk);
128
129   // Attach / detach
130   const std::string expect_attach_msg =
131       base::StringPrintf("%s,%s", kAttachTestOk, kDeviceName);
132   ExtensionTestMessageListener attach_finished_listener(expect_attach_msg,
133                                                         false  /* no reply */);
134   Attach();
135   EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied());
136   Detach();
137
138   // Attach / detach
139   Attach();
140   EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied());
141   // Detach
142   Detach();
143
144   // Remove attach listener.
145   ChangeListener(host, kRemoveAttachListenerCmd, kRemoveAttachListenerOk);
146
147   // No listeners, attach and detach a couple times.
148   AttachDetach();
149   AttachDetach();
150
151   // Add detach listener.
152   ChangeListener(host, kAddDummyDetachListenerCmd, kAddDummyDetachListenerOk);
153
154   // Attach / detach
155   Attach();
156
157   ExtensionTestMessageListener detach_finished_listener(kDetachTestOk,
158                                                         false  /* no reply */);
159   Detach();
160   EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied());
161
162   // Attach / detach
163   Attach();
164   Detach();
165   EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied());
166
167   // Switch ok dummy detach listener for the regular one.
168   ChangeListener(host, kRemoveDummyDetachListenerCmd,
169                  kRemoveDummyDetachListenerOk);
170   ChangeListener(host, kAddDetachListenerCmd, kAddDetachListenerOk);
171
172   // Add attach listener.
173   ChangeListener(host, kAddAttachListenerCmd, kAddAttachListenerOk);
174
175   Attach();
176   EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied());
177   Detach();
178   EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied());
179
180   Attach();
181   EXPECT_TRUE(attach_finished_listener.WaitUntilSatisfied());
182   Detach();
183   EXPECT_TRUE(detach_finished_listener.WaitUntilSatisfied());
184 }