1 // Copyright 2019 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 #ifndef CHROME_BROWSER_VR_TEST_UI_UTILS_H_
6 #define CHROME_BROWSER_VR_TEST_UI_UTILS_H_
7 
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/macros.h"
11 #include "base/run_loop.h"
12 #include "base/threading/thread.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/vr/ui_test_input.h"
15 
16 namespace vr {
17 
18 class BrowserRenderer;
19 class BrowserTestBrowserRendererBrowserInterface;
20 class VRBrowserRendererThreadWin;
21 
22 // Port of the equivalent NativeUiUtils.java for instrumentation tests. Contains
23 // utility functions for interacting with the native VR UI, e.g. notifications
24 // shown in the headset.
25 class UiUtils {
26  public:
27   static constexpr int kDefaultUiQuiescenceTimeout = 2000;
28 
29   // Ensures that the renderer thread and BrowserRenderer instance exist before
30   // creating a UiUtils.
31   // TODO(https://crbug.com/920697): Remove this once the BrowserRenderer's
32   // lifetime is tied to the renderer thread and we can assume that they both
33   // exist if we're in VR.
34   static std::unique_ptr<UiUtils> Create();
35 
36   UiUtils();
37   ~UiUtils();
38 
39   // Runs |action| and waits until the native UI reports that |element_name|'s
40   // visibility matches |visible|. Fails if the visibility is not matched in
41   // an allotted amount of time.
42   void PerformActionAndWaitForVisibilityStatus(
43       const UserFriendlyElementName& element_name,
44       const bool& visible,
45       base::OnceCallback<void()> action);
46 
47   // Not meant to be called directly by a test.
48   void ReportUiOperationResult(const UiTestOperationType& action_type,
49                                const UiTestOperationResult& result);
50 
51   static void DisableFrameTimeoutForTesting();
52 
53  private:
54   static void PollForBrowserRenderer(base::RunLoop* wait_loop);
55   static VRBrowserRendererThreadWin* GetRendererThread();
56   static BrowserRenderer* GetBrowserRenderer();
57 
58   void WatchElementForVisibilityStatusForTesting(
59       VisibilityChangeExpectation visibility_expectation);
60   std::string UiTestOperationResultToString(UiTestOperationResult& result);
61 
62   std::unique_ptr<BrowserTestBrowserRendererBrowserInterface> interface_;
63   std::vector<UiTestOperationResult> ui_operation_results_;
64   std::vector<base::OnceCallback<void()>> ui_operation_callbacks_;
65 
66   scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
67 
68   DISALLOW_COPY_AND_ASSIGN(UiUtils);
69 };
70 
71 }  // namespace vr
72 
73 #endif  // CHROME_BROWSER_VR_TEST_UI_UTILS_H_
74