1 // Copyright 2018 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 package org.chromium.chrome.browser.vr;
6 
7 import static org.chromium.chrome.browser.vr.WebXrArTestFramework.PAGE_LOAD_TIMEOUT_S;
8 import static org.chromium.chrome.browser.vr.WebXrArTestFramework.POLL_TIMEOUT_SHORT_MS;
9 
10 import android.os.Build;
11 
12 import androidx.test.filters.MediumTest;
13 
14 import org.junit.Before;
15 import org.junit.Rule;
16 import org.junit.Test;
17 import org.junit.rules.RuleChain;
18 import org.junit.runner.RunWith;
19 
20 import org.chromium.base.test.params.ParameterAnnotations.ClassParameter;
21 import org.chromium.base.test.params.ParameterAnnotations.UseRunnerDelegate;
22 import org.chromium.base.test.params.ParameterSet;
23 import org.chromium.base.test.params.ParameterizedRunner;
24 import org.chromium.base.test.util.CommandLineFlags;
25 import org.chromium.base.test.util.MinAndroidSdkLevel;
26 import org.chromium.chrome.browser.flags.ChromeSwitches;
27 import org.chromium.chrome.browser.vr.rules.XrActivityRestriction;
28 import org.chromium.chrome.browser.vr.util.ArTestRuleUtils;
29 import org.chromium.chrome.test.ChromeActivityTestRule;
30 import org.chromium.chrome.test.ChromeJUnit4RunnerDelegate;
31 import org.chromium.content_public.browser.WebContents;
32 
33 import java.util.List;
34 import java.util.concurrent.Callable;
35 
36 /**
37  * End-to-end tests for testing WebXR for AR's requestSession behavior.
38  */
39 @RunWith(ParameterizedRunner.class)
40 @UseRunnerDelegate(ChromeJUnit4RunnerDelegate.class)
41 @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
42         "enable-features=WebXR,WebXRARModule,WebXRHitTest,LogJsConsoleMessages"})
43 @MinAndroidSdkLevel(Build.VERSION_CODES.N) // WebXR for AR is only supported on N+
44 public class WebXrArSessionTest {
45     @ClassParameter
46     private static List<ParameterSet> sClassParams =
47             ArTestRuleUtils.generateDefaultTestRuleParameters();
48     @Rule
49     public RuleChain mRuleChain;
50 
51     private ChromeActivityTestRule mTestRule;
52     private WebXrArTestFramework mWebXrArTestFramework;
53 
WebXrArSessionTest(Callable<ChromeActivityTestRule> callable)54     public WebXrArSessionTest(Callable<ChromeActivityTestRule> callable) throws Exception {
55         mTestRule = callable.call();
56         mRuleChain = ArTestRuleUtils.wrapRuleInActivityRestrictionRule(mTestRule);
57     }
58 
59     @Before
setUp()60     public void setUp() {
61         mWebXrArTestFramework = new WebXrArTestFramework(mTestRule);
62     }
63 
64     /**
65      * Tests that a session request for AR succeeds.
66      */
67     @Test
68     @MediumTest
69     @XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
testArRequestSessionSucceeds()70     public void testArRequestSessionSucceeds() {
71         mWebXrArTestFramework.loadFileAndAwaitInitialization(
72                 "test_ar_request_session_succeeds", PAGE_LOAD_TIMEOUT_S);
73         mWebXrArTestFramework.enterSessionWithUserGestureOrFail();
74         mWebXrArTestFramework.assertNoJavaScriptErrors();
75     }
76 
77     /**
78      * Tests that consenting causes future attempts to skip the permission prompt as long as no
79      * navigation occurs.
80      */
81     @Test
82     @MediumTest
83     @XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
testArPermissionPersistance()84     public void testArPermissionPersistance() {
85         mWebXrArTestFramework.loadFileAndAwaitInitialization(
86                 "test_ar_request_session_succeeds", PAGE_LOAD_TIMEOUT_S);
87         WebContents contents = mWebXrArTestFramework.getCurrentWebContents();
88 
89         // Start new session, accepting the consent prompt
90         mWebXrArTestFramework.enterSessionWithUserGestureOrFail(contents);
91         mWebXrArTestFramework.endSession();
92         mWebXrArTestFramework.assertNoJavaScriptErrors();
93         mWebXrArTestFramework.pollJavaScriptBooleanOrFail(
94                 "sessionInfos[sessionTypes.AR].currentSession == null", POLL_TIMEOUT_SHORT_MS);
95 
96         // Start yet another session, but go through a path that doesn't automatically handle
97         // the permission prompt to ensure that it doesn't actually appear.
98         mWebXrArTestFramework.enterSessionWithUserGesture();
99         mWebXrArTestFramework.pollJavaScriptBooleanOrFail(
100                 "sessionInfos[sessionTypes.AR].currentSession != null", POLL_TIMEOUT_SHORT_MS);
101         mWebXrArTestFramework.assertNoJavaScriptErrors();
102     }
103 
104     /**
105      * Tests that repeatedly starting and stopping AR sessions does not cause any unexpected
106      * behavior. Regression test for https://crbug.com/837894.
107      */
108     @Test
109     @MediumTest
110     @XrActivityRestriction({XrActivityRestriction.SupportedActivity.ALL})
testRepeatedArSessionsSucceed()111     public void testRepeatedArSessionsSucceed() {
112         mWebXrArTestFramework.loadFileAndAwaitInitialization(
113                 "test_ar_request_session_succeeds", PAGE_LOAD_TIMEOUT_S);
114         for (int i = 0; i < 2; i++) {
115             mWebXrArTestFramework.enterSessionWithUserGestureOrFail();
116             mWebXrArTestFramework.endSession();
117         }
118         mWebXrArTestFramework.assertNoJavaScriptErrors();
119     }
120 }
121