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 #include "chrome/browser/ui/screen_capture_notification_ui.h"
6 
7 #include <memory>
8 
9 #include "base/bind.h"
10 #include "base/macros.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/ui/test/test_browser_dialog.h"
15 #include "content/public/test/browser_test.h"
16 #include "ui/gfx/native_widget_types.h"
17 
18 class ScreenCaptureNotificationUiBrowserTest : public DialogBrowserTest {
19  public:
20   ScreenCaptureNotificationUiBrowserTest() = default;
21   ~ScreenCaptureNotificationUiBrowserTest() override = default;
22 
23   // TestBrowserUi:
ShowUi(const std::string & name)24   void ShowUi(const std::string& name) override {
25     screen_capture_notification_ui_ =
26         ScreenCaptureNotificationUI::Create(base::string16(
27             base::ASCIIToUTF16("ScreenCaptureNotificationUI Browser Test")));
28     on_started_result_ = screen_capture_notification_ui_->OnStarted(
29         base::BindOnce(
30             [](ScreenCaptureNotificationUiBrowserTest* test) {
31               if (test->run_loop_)
32                 test->run_loop_->QuitWhenIdle();
33             },
34             base::Unretained(this)),
35         content::MediaStreamUI::SourceCallback());
36   }
37 
VerifyUi()38   bool VerifyUi() override {
39     // on_started_result_ == 0 is a loose signal that indicates
40     // DialogBrowserTest::VerifyUi() should be called instead of checking the
41     // value of |on_started_result_|.
42     // on_started_result_ will occur under the following circumstances:
43     //   * Views ScreenCaptureNotificationUI except for Windows.
44     //   * ChromeOS (Currently unsupported and not built for this test as the
45     //     CrOS system tray is used).
46     // TODO(robliao): Remove this override once Views is the only toolkit.
47     return (on_started_result_ != 0) || DialogBrowserTest::VerifyUi();
48   }
49 
DismissUi()50   void DismissUi() override { screen_capture_notification_ui_.reset(); }
51 
WaitForUserDismissal()52   void WaitForUserDismissal() override {
53     run_loop_ = std::make_unique<base::RunLoop>();
54     run_loop_->Run();
55     run_loop_.reset();
56     screen_capture_notification_ui_.reset();
57   }
58 
GetNonDialogName()59   std::string GetNonDialogName() override {
60     // This class tests a non-dialog widget with the following name.
61     return "ScreenCaptureNotificationUIViews";
62   }
63 
64  private:
65   std::unique_ptr<ScreenCaptureNotificationUI> screen_capture_notification_ui_;
66   gfx::NativeViewId on_started_result_;
67   std::unique_ptr<base::RunLoop> run_loop_;
68 
69   DISALLOW_COPY_AND_ASSIGN(ScreenCaptureNotificationUiBrowserTest);
70 };
71 
IN_PROC_BROWSER_TEST_F(ScreenCaptureNotificationUiBrowserTest,InvokeUi)72 IN_PROC_BROWSER_TEST_F(ScreenCaptureNotificationUiBrowserTest, InvokeUi) {
73   ShowAndVerifyUi();
74 }
75