OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "ash/shell.h" | |
6 #include "ash/system/cast/tray_cast.h" | |
7 #include "ash/system/tray/system_tray.h" | |
8 #include "ash/system/tray/system_tray_delegate.h" | |
9 #include "ash/system/tray/system_tray_item.h" | |
10 #include "chrome/browser/extensions/extension_browsertest.h" | |
11 #include "chrome/browser/profiles/profile_manager.h" | |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
13 #include "content/public/browser/render_view_host.h" | |
14 #include "content/public/test/test_utils.h" | |
15 #include "extensions/browser/process_manager.h" | |
16 | |
17 namespace chromeos { | |
18 namespace { | |
19 | |
20 // Execute JavaScript within the context of the extension. Returns the result | |
21 // of the execution. | |
22 scoped_ptr<base::Value> ExecuteJavaScript( | |
23 const extensions::Extension* extension, | |
24 const std::string& javascript) { | |
25 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
26 if (!profile) { | |
achuithb
2015/07/14 18:22:20
Can this actually happen? If not, drop the check
jdufault
2015/07/15 17:35:27
Done.
| |
27 LOG(ERROR) << "Expected profile"; | |
28 return nullptr; | |
29 } | |
30 | |
31 auto pm = extensions::ProcessManager::Get(profile); | |
32 content::RenderViewHost* host = | |
33 pm->GetBackgroundHostForExtension(extension->id())->render_view_host(); | |
34 return content::ExecuteScriptAndGetValue(host->GetMainFrame(), javascript); | |
35 } | |
36 | |
37 // Ensures that all pending JavaScript execution callbacks are invoked. | |
38 void ExecutePendingJavaScript(const extensions::Extension* extension) { | |
39 ExecuteJavaScript(extension, ""); | |
40 } | |
41 | |
42 // Invokes tray->StartCastForTest(id) and verifies that launchDesktopMirroring | |
43 // was called with the same id. This automatically creates/destroys the detail | |
44 // view and notifies the tray that Chrome has begun casting. | |
45 void StartCastWithVerification(const extensions::Extension* extension, | |
46 ash::TrayCast* tray, | |
47 const std::string& id) { | |
achuithb
2015/07/14 18:22:19
receiver_id
jdufault
2015/07/15 17:35:28
Done.
| |
48 ash::SystemTrayItem* system_tray_item = tray; | |
49 ash::TrayCastTestMethods* test_tray = tray; | |
achuithb
2015/07/14 18:22:20
Is this necessary for compilation?
jdufault
2015/07/15 17:35:28
Yes, since the methods are private w.r.t. ash::Tra
achuithb
2015/07/15 18:59:32
Acknowledged.
| |
50 | |
51 // StartCastForTest will simulate a button click in the detail view to begin | |
52 // the cast, so we need to make a detail view available. | |
53 system_tray_item->CreateDetailedView(ash::user::LoginStatus::LOGGED_IN_USER); | |
54 | |
55 // Clear out any old state and execute any pending JS calls created from the | |
56 // CreateDetailedView call. | |
57 ExecuteJavaScript(extension, "launchDesktopMirroringReceiverId = ''"); | |
58 | |
59 test_tray->StartCastForTest(id); | |
60 | |
61 scoped_ptr<base::Value> getLaunchDesktopMirroringId = | |
achuithb
2015/07/14 18:22:20
variable name should have underscores, not camel c
jdufault
2015/07/15 17:35:27
Done.
| |
62 ExecuteJavaScript(extension, "getLaunchDesktopMirroringReceiverId()"); | |
63 std::string result; | |
64 EXPECT_TRUE(getLaunchDesktopMirroringId->GetAsString(&result)); | |
65 EXPECT_EQ(id, result); | |
66 system_tray_item->DestroyDetailedView(); | |
67 | |
68 // Tell the tray item that Chrome has started casting. | |
69 test_tray->OnCastingSessionStartedOrStopped(true); | |
70 ExecutePendingJavaScript(extension); | |
71 } | |
72 | |
73 // Invokes tray->StopCastForTest() and verifies that stopMirroring('user-stop') | |
74 // got called in the extension. | |
75 void StopCastWithVerification(const extensions::Extension* extension, | |
76 ash::TrayCastTestMethods* tray) { | |
77 ExecuteJavaScript(extension, "stopMirroringCalled = false"); | |
78 tray->StopCastForTest(); | |
79 scoped_ptr<base::Value> wasStopMirroringCalled = | |
80 ExecuteJavaScript(extension, "wasStopMirroringCalledWithUserStop()"); | |
81 bool result; | |
82 EXPECT_TRUE(wasStopMirroringCalled->GetAsBoolean(&result)); | |
83 EXPECT_TRUE(result); | |
84 | |
85 // Tell the tray item that Chrome has stopped casting. | |
86 tray->OnCastingSessionStartedOrStopped(false); | |
87 ExecutePendingJavaScript(extension); | |
88 } | |
89 | |
90 // Returns the cast tray. The tray initializer may have launched some | |
91 // JavaScript callbacks which have not finished executing. | |
92 ash::TrayCast* GetTrayCast(const extensions::Extension* extension) { | |
93 ash::SystemTray* tray = ash::Shell::GetInstance()->GetPrimarySystemTray(); | |
94 // Make sure we actually popup the tray, otherwise the TrayCast instance will | |
95 // not be created. | |
96 tray->ShowDefaultView(ash::BubbleCreationType::BUBBLE_CREATE_NEW); | |
97 | |
98 // Creating the tray causes some JavaScript to be executed. Let's try to make | |
99 // sure it is completed. | |
100 if (extension) | |
101 ExecutePendingJavaScript(extension); | |
102 | |
103 return tray->GetTrayCastForTesting(); | |
104 } | |
105 | |
106 } // namespace | |
107 | |
108 class SystemTrayTrayCastChromeOSTest : public ExtensionBrowserTest { | |
achuithb
2015/07/14 18:22:19
This cannot be in the anonymous namespace, is that
jdufault
2015/07/15 17:35:28
Oh, I just was following the pattern of another br
achuithb
2015/07/15 18:59:32
I think you want the IN_PROC_BROWSER_TEST_F to be
jdufault
2015/07/15 19:46:37
I've been testing with
--gtest_filter=SystemTrayT
| |
109 protected: | |
110 SystemTrayTrayCastChromeOSTest() : ExtensionBrowserTest() {} | |
111 ~SystemTrayTrayCastChromeOSTest() override {} | |
112 | |
113 const extensions::Extension* LoadCastTestExtension() { | |
114 return LoadExtension(test_data_dir_.AppendASCII("tray_cast")); | |
115 } | |
116 | |
117 private: | |
118 DISALLOW_COPY_AND_ASSIGN(SystemTrayTrayCastChromeOSTest); | |
119 }; | |
120 | |
121 // A simple sanity check to make sure that the cast config delegate actually | |
122 // recognizes the cast extension. | |
123 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
124 CastTraySanityCheckTestExtensionGetsRecognized) { | |
125 ash::CastConfigDelegate* cast_config_delegate = | |
126 ash::Shell::GetInstance()->system_tray_delegate()->GetCastConfigDelegate(); | |
127 | |
128 EXPECT_FALSE(cast_config_delegate->HasCastExtension()); | |
129 LoadCastTestExtension(); | |
130 EXPECT_TRUE(cast_config_delegate->HasCastExtension()); | |
131 } | |
132 | |
133 // Verifies that the cast tray is hidden when there is no extension installed. | |
134 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
135 CastTrayIsHiddenWhenThereIsNoExtension) { | |
136 ash::TrayCastTestMethods* tray = GetTrayCast(nullptr); | |
137 EXPECT_TRUE(tray->IsTrayInitializedForTest()); | |
138 EXPECT_FALSE(tray->IsTrayVisibleForTest()); | |
139 } | |
140 | |
141 // Verifies that the cast tray is hidden if there are no available receivers, | |
142 // even if there is an extension installed. | |
143 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
144 CastTrayIsHiddenWhenThereIsAnExtensionButNoReceivers) { | |
145 const extensions::Extension* extension = LoadCastTestExtension(); | |
146 | |
147 ash::TrayCastTestMethods* tray = GetTrayCast(extension); | |
148 EXPECT_TRUE(tray->IsTrayInitializedForTest()); | |
149 EXPECT_FALSE(tray->IsTrayVisibleForTest()); | |
150 } | |
151 | |
152 // Verifies that the cast tray is displayed when there are receivers available. | |
153 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
154 CastTrayIsDisplayedWhenThereIsAnExtensionWithReceivers) { | |
155 const extensions::Extension* extension = LoadCastTestExtension(); | |
156 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); | |
157 | |
158 ash::TrayCastTestMethods* tray = GetTrayCast(extension); | |
159 | |
160 EXPECT_TRUE(tray->IsTrayInitializedForTest()); | |
161 EXPECT_TRUE(tray->IsTrayVisibleForTest()); | |
162 } | |
163 | |
164 // Verifies that we can cast to a specific receiver, stop casting, and then cast | |
165 // to another receiver when there is more than one receiver | |
166 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
167 CastTrayMultipleReceivers) { | |
168 const extensions::Extension* extension = LoadCastTestExtension(); | |
169 ExecuteJavaScript(extension, "addReceiver('test_id_1', 'name')"); | |
achuithb
2015/07/14 18:22:20
Use different names for variety
jdufault
2015/07/15 17:35:27
I want to verify that the having the same name doe
achuithb
2015/07/15 18:59:32
Acknowledged.
| |
170 ExecuteJavaScript(extension, "addReceiver('not_used_0', 'name')"); | |
171 ExecuteJavaScript(extension, "addReceiver('test_id_0', 'name')"); | |
172 ExecuteJavaScript(extension, "addReceiver('not_used_1', 'name')"); | |
173 | |
174 ash::TrayCast* tray = GetTrayCast(extension); | |
175 ash::TrayCastTestMethods* test_tray = tray; | |
176 StartCastWithVerification(extension, tray, "test_id_0"); | |
177 | |
178 EXPECT_TRUE(test_tray->IsTrayCastViewVisibleForTest()); | |
179 StopCastWithVerification(extension, GetTrayCast(extension)); | |
180 EXPECT_TRUE(test_tray->IsTraySelectViewVisibleForTest()); | |
181 | |
182 StartCastWithVerification(extension, GetTrayCast(extension), "test_id_1"); | |
183 EXPECT_TRUE(test_tray->IsTrayCastViewVisibleForTest()); | |
184 StopCastWithVerification(extension, GetTrayCast(extension)); | |
185 EXPECT_TRUE(test_tray->IsTraySelectViewVisibleForTest()); | |
186 } | |
187 | |
188 // Verifies the stop cast button invokes the JavaScript function | |
189 // "stopMirroring('user-stop')". | |
190 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
191 CastTrayStopButtonStopsCast) { | |
192 // Add a receiver that is casting. | |
193 const extensions::Extension* extension = LoadCastTestExtension(); | |
194 ExecuteJavaScript(extension, "addReceiver('test_id', 'name', 'title', 1)"); | |
195 | |
196 ash::TrayCastTestMethods* test_tray = GetTrayCast(extension); | |
197 test_tray->OnCastingSessionStartedOrStopped(true); | |
198 ExecutePendingJavaScript(extension); | |
199 EXPECT_TRUE(test_tray->IsTrayCastViewVisibleForTest()); | |
200 | |
201 // Stop the cast using the UI. | |
202 StopCastWithVerification(extension, test_tray); | |
203 EXPECT_TRUE(test_tray->IsTraySelectViewVisibleForTest()); | |
204 } | |
205 | |
206 // Verifies that the start cast button invokes "launchDesktopMirroring(...)". | |
207 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, | |
208 CastTrayStartButtonStartsCast) { | |
209 const extensions::Extension* extension = LoadCastTestExtension(); | |
210 ExecuteJavaScript(extension, "addReceiver('test_id', 'name')"); | |
211 ash::TrayCast* tray = GetTrayCast(extension); | |
212 StartCastWithVerification(extension, tray, "test_id"); | |
213 } | |
214 | |
215 // Verifies that the CastConfigDelegate opens up a tab called "options.html". | |
216 IN_PROC_BROWSER_TEST_F(SystemTrayTrayCastChromeOSTest, CastTrayOpenOptions) { | |
217 LoadCastTestExtension(); | |
218 | |
219 ash::CastConfigDelegate* cast_config_delegate = | |
220 ash::Shell::GetInstance()->system_tray_delegate()->GetCastConfigDelegate(); | |
221 cast_config_delegate->LaunchCastOptions(); | |
222 | |
223 GURL url = browser()->tab_strip_model()->GetActiveWebContents()->GetURL(); | |
achuithb
2015/07/14 18:22:20
const
jdufault
2015/07/15 17:35:28
Done.
| |
224 EXPECT_TRUE(base::StringPiece(url.GetContent()).ends_with("options.html")); | |
225 } | |
226 | |
227 } // namespace chromeos | |
OLD | NEW |