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 "ash/public/cpp/overview_test_api.h"
6 #include "ash/public/cpp/test/shell_test_api.h"
7 #include "base/callback_forward.h"
8 #include "base/macros.h"
9 #include "base/test/bind.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/chromeos/arc/arc_util.h"
12 #include "chrome/browser/chromeos/arc/session/arc_session_manager.h"
13 #include "chrome/browser/chromeos/arc/tracing/arc_app_performance_tracing.h"
14 #include "chrome/browser/chromeos/arc/tracing/arc_app_performance_tracing_session.h"
15 #include "chrome/browser/chromeos/arc/tracing/arc_app_performance_tracing_test_helper.h"
16 #include "chrome/browser/chromeos/extensions/autotest_private/autotest_private_api.h"
17 #include "chrome/browser/extensions/extension_apitest.h"
18 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h"
19 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
20 #include "chrome/browser/ui/browser_window.h"
21 #include "chrome/browser/web_applications/test/test_system_web_app_installation.h"
22 #include "components/arc/arc_prefs.h"
23 #include "components/arc/arc_util.h"
24 #include "components/arc/session/connection_holder.h"
25 #include "components/arc/test/connection_holder_util.h"
26 #include "components/arc/test/fake_app_instance.h"
27 #include "components/policy/core/browser/browser_policy_connector.h"
28 #include "components/policy/core/common/mock_configuration_policy_provider.h"
29 #include "components/policy/core/common/policy_map.h"
30 #include "components/policy/core/common/policy_types.h"
31 #include "components/policy/policy_constants.h"
32 #include "components/prefs/pref_service.h"
33 #include "content/public/browser/render_frame_host.h"
34 #include "content/public/browser/web_contents.h"
35 #include "content/public/test/browser_test.h"
36 #include "content/public/test/test_navigation_observer.h"
37 #include "content/public/test/test_utils.h"
38 #include "services/tracing/public/cpp/perfetto/perfetto_traced_process.h"
39 #include "ui/aura/window.h"
40 #include "ui/events/event_utils.h"
41 #include "ui/events/test/event_generator.h"
42 #include "ui/views/widget/widget.h"
43
44 using testing::_;
45 using testing::Return;
46
47 namespace extensions {
48
49 class AutotestPrivateApiTest : public ExtensionApiTest {
50 public:
AutotestPrivateApiTest()51 AutotestPrivateApiTest() {
52 // SplitSettingsSync makes an untitled Play Store icon appear in the shelf
53 // due to app pin syncing code. Sync isn't relevant to this test, so skip
54 // pinned app sync. https://crbug.com/1085597
55 SkipPinnedAppsFromSyncForTest();
56 }
57 ~AutotestPrivateApiTest() override = default;
58
SetUpCommandLine(base::CommandLine * command_line)59 void SetUpCommandLine(base::CommandLine* command_line) override {
60 ExtensionApiTest::SetUpCommandLine(command_line);
61 // Make ARC enabled for tests.
62 arc::SetArcAvailableCommandLineForTesting(command_line);
63 }
64
SetUpInProcessBrowserTestFixture()65 void SetUpInProcessBrowserTestFixture() override {
66 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
67 arc::ArcSessionManager::SetUiEnabledForTesting(false);
68 }
69
SetUpOnMainThread()70 void SetUpOnMainThread() override {
71 ExtensionApiTest::SetUpOnMainThread();
72 // Turn on testing mode so we don't kill the browser.
73 AutotestPrivateAPI::GetFactoryInstance()
74 ->Get(browser()->profile())
75 ->set_test_mode(true);
76 }
77
78 private:
79 DISALLOW_COPY_AND_ASSIGN(AutotestPrivateApiTest);
80 };
81
IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest,AutotestPrivate)82 IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest, AutotestPrivate) {
83 ASSERT_TRUE(RunComponentExtensionTestWithArg("autotest_private", "default"))
84 << message_;
85 }
86
87 // Set of tests where ARC is enabled and test apps and packages are registered.
IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest,AutotestPrivateArcEnabled)88 IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest, AutotestPrivateArcEnabled) {
89 ArcAppListPrefs* const prefs = ArcAppListPrefs::Get(browser()->profile());
90 ASSERT_TRUE(prefs);
91
92 arc::SetArcPlayStoreEnabledForProfile(profile(), true);
93 // Provisioning is completed.
94 browser()->profile()->GetPrefs()->SetBoolean(arc::prefs::kArcSignedIn, true);
95 browser()->profile()->GetPrefs()->SetBoolean(arc::prefs::kArcTermsAccepted,
96 true);
97
98 std::unique_ptr<arc::FakeAppInstance> app_instance;
99 app_instance.reset(new arc::FakeAppInstance(prefs));
100 prefs->app_connection_holder()->SetInstance(app_instance.get());
101 arc::WaitForInstanceReady(prefs->app_connection_holder());
102
103 arc::mojom::AppInfo app;
104 app.name = "Fake App";
105 app.package_name = "fake.package";
106 app.activity = "fake.package.activity";
107 app_instance->SendRefreshAppList(std::vector<arc::mojom::AppInfo>(1, app));
108
109 std::vector<arc::mojom::ArcPackageInfoPtr> packages;
110 packages.emplace_back(arc::mojom::ArcPackageInfo::New(
111 app.package_name, 10 /* package_version */,
112 100 /* last_backup_android_id */,
113 base::Time::Now()
114 .ToDeltaSinceWindowsEpoch()
115 .InMicroseconds() /* last_backup_time */,
116 true /* sync */));
117 app_instance->SendRefreshPackageList(std::move(packages));
118
119 ASSERT_TRUE(
120 RunComponentExtensionTestWithArg("autotest_private", "arcEnabled"))
121 << message_;
122
123 arc::SetArcPlayStoreEnabledForProfile(profile(), false);
124 }
125
IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest,ScrollableShelfAPITest)126 IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest, ScrollableShelfAPITest) {
127 ASSERT_TRUE(
128 RunComponentExtensionTestWithArg("autotest_private", "scrollableShelf"))
129 << message_;
130 }
131
IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest,ShelfAPITest)132 IN_PROC_BROWSER_TEST_F(AutotestPrivateApiTest, ShelfAPITest) {
133 ASSERT_TRUE(RunComponentExtensionTestWithArg("autotest_private", "shelf"))
134 << message_;
135 }
136
137 class AutotestPrivateApiOverviewTest : public AutotestPrivateApiTest {
138 public:
139 AutotestPrivateApiOverviewTest() = default;
140
141 // AutotestPrivateApiTest:
SetUpOnMainThread()142 void SetUpOnMainThread() override {
143 AutotestPrivateApiTest::SetUpOnMainThread();
144
145 // Create one additional browser window to make total of 2 windows.
146 CreateBrowser(browser()->profile());
147
148 // Enters tablet overview mode.
149 ash::ShellTestApi().SetTabletModeEnabledForTest(true);
150 base::RunLoop run_loop;
151 ash::OverviewTestApi().SetOverviewMode(
152 /*start=*/true, base::BindLambdaForTesting([&run_loop](bool finished) {
153 if (!finished)
154 ADD_FAILURE() << "Failed to enter overview.";
155 run_loop.Quit();
156 }));
157 run_loop.Run();
158
159 // We should get 2 overview items from the 2 browser windows.
160 ASSERT_EQ(2u, ash::OverviewTestApi().GetOverviewInfo()->size());
161 }
162
GetRootWindow() const163 gfx::NativeWindow GetRootWindow() const {
164 return browser()->window()->GetNativeWindow()->GetRootWindow();
165 }
166 };
167
IN_PROC_BROWSER_TEST_F(AutotestPrivateApiOverviewTest,Default)168 IN_PROC_BROWSER_TEST_F(AutotestPrivateApiOverviewTest, Default) {
169 ASSERT_TRUE(
170 RunComponentExtensionTestWithArg("autotest_private", "overviewDefault"))
171 << message_;
172 }
173
IN_PROC_BROWSER_TEST_F(AutotestPrivateApiOverviewTest,Drag)174 IN_PROC_BROWSER_TEST_F(AutotestPrivateApiOverviewTest, Drag) {
175 const ash::OverviewInfo info =
176 ash::OverviewTestApi().GetOverviewInfo().value();
177 const gfx::Point start_point =
178 info.begin()->second.bounds_in_screen.CenterPoint();
179
180 // Long press to pick up an overview item and drag it a bit.
181 ui::test::EventGenerator generator(GetRootWindow());
182
183 generator.set_current_screen_location(start_point);
184 generator.PressTouch();
185
186 ui::GestureEvent long_press(
187 start_point.x(), start_point.y(), 0, ui::EventTimeForNow(),
188 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS));
189 generator.Dispatch(&long_press);
190
191 // 50 is arbitrary number of dip to move a bit to ensure the item is being
192 // dragged.
193 const gfx::Point end_point(start_point.x() + 50, start_point.y());
194 generator.MoveTouch(end_point);
195
196 ASSERT_TRUE(
197 RunComponentExtensionTestWithArg("autotest_private", "overviewDrag"))
198 << message_;
199 }
200
IN_PROC_BROWSER_TEST_F(AutotestPrivateApiOverviewTest,LeftSnapped)201 IN_PROC_BROWSER_TEST_F(AutotestPrivateApiOverviewTest, LeftSnapped) {
202 const ash::OverviewInfo info =
203 ash::OverviewTestApi().GetOverviewInfo().value();
204 const gfx::Point start_point =
205 info.begin()->second.bounds_in_screen.CenterPoint();
206 const gfx::Point end_point(0, start_point.y());
207
208 // Long press to pick up an overview item, drag all the way to the left
209 // to snap it on left.
210 ui::test::EventGenerator generator(GetRootWindow());
211
212 generator.set_current_screen_location(start_point);
213 generator.PressTouch();
214
215 ui::GestureEvent long_press(
216 start_point.x(), start_point.y(), 0, ui::EventTimeForNow(),
217 ui::GestureEventDetails(ui::ET_GESTURE_LONG_PRESS));
218 generator.Dispatch(&long_press);
219
220 generator.MoveTouch(end_point);
221 generator.ReleaseTouch();
222
223 ASSERT_TRUE(RunComponentExtensionTestWithArg("autotest_private",
224 "splitviewLeftSnapped"))
225 << message_;
226 }
227
228 class AutotestPrivateWithPolicyApiTest : public AutotestPrivateApiTest {
229 public:
AutotestPrivateWithPolicyApiTest()230 AutotestPrivateWithPolicyApiTest() {}
231
SetUpInProcessBrowserTestFixture()232 void SetUpInProcessBrowserTestFixture() override {
233 EXPECT_CALL(provider_, IsInitializationComplete(_))
234 .WillRepeatedly(Return(true));
235 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
236 AutotestPrivateApiTest::SetUpInProcessBrowserTestFixture();
237 }
238
SetUpOnMainThread()239 void SetUpOnMainThread() override {
240 AutotestPrivateApiTest::SetUpOnMainThread();
241 // Set a fake policy
242 policy::PolicyMap policy;
243 policy.Set(policy::key::kAllowDinosaurEasterEgg,
244 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
245 policy::POLICY_SOURCE_CLOUD, base::Value(true), nullptr);
246 provider_.UpdateChromePolicy(policy);
247 base::RunLoop().RunUntilIdle();
248 }
249
250 protected:
251 policy::MockConfigurationPolicyProvider provider_;
252
253 private:
254 DISALLOW_COPY_AND_ASSIGN(AutotestPrivateWithPolicyApiTest);
255 };
256
257 // GetAllEnterprisePolicies Sanity check.
IN_PROC_BROWSER_TEST_F(AutotestPrivateWithPolicyApiTest,PolicyAPITest)258 IN_PROC_BROWSER_TEST_F(AutotestPrivateWithPolicyApiTest, PolicyAPITest) {
259 ASSERT_TRUE(RunComponentExtensionTestWithArg("autotest_private",
260 "enterprisePolicies"))
261 << message_;
262 }
263
264 class AutotestPrivateArcPerformanceTracing : public AutotestPrivateApiTest {
265 public:
266 AutotestPrivateArcPerformanceTracing() = default;
267 ~AutotestPrivateArcPerformanceTracing() override = default;
268
269 protected:
270 // AutotestPrivateApiTest:
SetUpOnMainThread()271 void SetUpOnMainThread() override {
272 AutotestPrivateApiTest::SetUpOnMainThread();
273 tracing_helper_.SetUp(profile());
274 performance_tracing()->SetCustomSessionReadyCallbackForTesting(
275 base::BindRepeating(
276 &arc::ArcAppPerformanceTracingTestHelper::PlayDefaultSequence,
277 base::Unretained(&tracing_helper())));
278 }
279
TearDownOnMainThread()280 void TearDownOnMainThread() override {
281 performance_tracing()->SetCustomSessionReadyCallbackForTesting(
282 arc::ArcAppPerformanceTracing::CustomSessionReadyCallback());
283 tracing_helper_.TearDown();
284 AutotestPrivateApiTest::TearDownOnMainThread();
285 }
286
tracing_helper()287 arc::ArcAppPerformanceTracingTestHelper& tracing_helper() {
288 return tracing_helper_;
289 }
290
performance_tracing()291 arc::ArcAppPerformanceTracing* performance_tracing() {
292 return tracing_helper_.GetTracing();
293 }
294
295 private:
296 arc::ArcAppPerformanceTracingTestHelper tracing_helper_;
297
298 DISALLOW_COPY_AND_ASSIGN(AutotestPrivateArcPerformanceTracing);
299 };
300
IN_PROC_BROWSER_TEST_F(AutotestPrivateArcPerformanceTracing,Basic)301 IN_PROC_BROWSER_TEST_F(AutotestPrivateArcPerformanceTracing, Basic) {
302 views::Widget* const arc_widget =
303 arc::ArcAppPerformanceTracingTestHelper::CreateArcWindow(
304 "org.chromium.arc.1");
305 performance_tracing()->OnWindowActivated(
306 wm::ActivationChangeObserver::ActivationReason::ACTIVATION_CLIENT,
307 arc_widget->GetNativeWindow(), arc_widget->GetNativeWindow());
308
309 ASSERT_TRUE(RunComponentExtensionTestWithArg("autotest_private",
310 "arcPerformanceTracing"))
311 << message_;
312 }
313
314 class AutotestPrivateStartStopTracing : public AutotestPrivateApiTest {
315 public:
316 AutotestPrivateStartStopTracing() = default;
317 ~AutotestPrivateStartStopTracing() override = default;
318 AutotestPrivateStartStopTracing(const AutotestPrivateStartStopTracing&) =
319 delete;
320 AutotestPrivateStartStopTracing& operator=(
321 const AutotestPrivateStartStopTracing&) = delete;
322
323 protected:
324 // AutotestPrivateApiTest:
SetUpOnMainThread()325 void SetUpOnMainThread() override {
326 AutotestPrivateApiTest::SetUpOnMainThread();
327 tracing::PerfettoTracedProcess::Get()->ClearDataSourcesForTesting();
328 }
329 };
330
IN_PROC_BROWSER_TEST_F(AutotestPrivateStartStopTracing,StartStopTracing)331 IN_PROC_BROWSER_TEST_F(AutotestPrivateStartStopTracing, StartStopTracing) {
332 ASSERT_TRUE(
333 RunComponentExtensionTestWithArg("autotest_private", "startStopTracing"))
334 << message_;
335 }
336
337 class AutotestPrivateSystemWebAppsTest : public AutotestPrivateApiTest {
338 public:
AutotestPrivateSystemWebAppsTest()339 AutotestPrivateSystemWebAppsTest() {
340 installation_ =
341 web_app::TestSystemWebAppInstallation::SetUpStandaloneSingleWindowApp(
342 true);
343 }
344 ~AutotestPrivateSystemWebAppsTest() override = default;
345
346 private:
347 std::unique_ptr<web_app::TestSystemWebAppInstallation> installation_;
348 };
349
IN_PROC_BROWSER_TEST_F(AutotestPrivateSystemWebAppsTest,SystemWebApps)350 IN_PROC_BROWSER_TEST_F(AutotestPrivateSystemWebAppsTest, SystemWebApps) {
351 ASSERT_TRUE(
352 RunComponentExtensionTestWithArg("autotest_private", "systemWebApps"))
353 << message_;
354 }
355
356 } // namespace extensions
357