1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/status_bubble_views.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/test_simple_task_runner.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/views/status_bubble_views_browsertest_mac.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "ui/gfx/animation/animation.h"
#include "ui/views/widget/widget.h"
class StatusBubbleViewsTest : public InProcessBrowserTest {
public:
StatusBubbleViews* GetBubble() {
std::vector<StatusBubble*> status_bubbles =
browser()->window()->GetStatusBubbles();
if (status_bubbles.size() > 0) {
return static_cast<StatusBubbleViews*>(status_bubbles.front());
}
return nullptr;
}
views::Widget* GetWidget() { return GetBubble()->popup(); }
bool IsDestroyPopupTimerRunning() {
return GetBubble()->IsDestroyPopupTimerRunningForTest();
}
gfx::Animation* GetShowHideAnimationForTesting() {
return GetBubble()->GetShowHideAnimationForTest();
}
void SetTaskRunner(base::SequencedTaskRunner* task_runner) {
GetBubble()->task_runner_ = task_runner;
}
};
IN_PROC_BROWSER_TEST_F(StatusBubbleViewsTest, WidgetLifetime) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
base::MakeRefCounted<base::TestSimpleTaskRunner>();
SetTaskRunner(task_runner.get());
// The widget does not exist until it needs to be shown.
StatusBubble* bubble = GetBubble();
ASSERT_TRUE(bubble);
EXPECT_FALSE(GetWidget());
// Setting status text shows the widget.
bubble->SetStatus(u"test");
views::Widget* widget = GetWidget();
ASSERT_TRUE(widget);
EXPECT_TRUE(widget->IsVisible());
// Changing status text keeps the widget visible.
bubble->SetStatus(u"foo");
EXPECT_TRUE(widget->IsVisible());
// Setting the URL keeps the widget visible.
bubble->SetURL(GURL("http://www.foo.com"));
EXPECT_TRUE(widget->IsVisible());
#if !BUILDFLAG(IS_MAC)
// Clearing the URL and status closes the widget on platforms other than Mac.
EXPECT_FALSE(IsDestroyPopupTimerRunning());
bubble->SetStatus(std::u16string());
bubble->SetURL(GURL());
// The widget is not hidden immediately, instead a task is scheduled. Run that
// now.
task_runner->RunPendingTasks();
// After the task, a timer is created that animates hidden. Advance that.
ASSERT_TRUE(GetShowHideAnimationForTesting());
// Advance well past the time for the animation to ensure it completes.
static_cast<gfx::AnimationContainerElement*>(GetShowHideAnimationForTesting())
->Step(base::TimeTicks::Now() + base::Minutes(1));
// Widget should still exist.
ASSERT_TRUE(GetWidget());
EXPECT_FALSE(widget->IsVisible());
EXPECT_TRUE(IsDestroyPopupTimerRunning());
// Run until idle, which should trigger deleting the widget.
task_runner->RunUntilIdle();
EXPECT_FALSE(IsDestroyPopupTimerRunning());
EXPECT_FALSE(GetWidget());
#endif
SetTaskRunner(base::SingleThreadTaskRunner::GetCurrentDefault().get());
}
// Mac does not delete the widget after a delay, so this test only runs on
// non-mac platforms.
#if !BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(StatusBubbleViewsTest, ShowHideDestroyShow) {
scoped_refptr<base::TestSimpleTaskRunner> task_runner =
base::MakeRefCounted<base::TestSimpleTaskRunner>();
SetTaskRunner(task_runner.get());
// The widget does not exist until it needs to be shown.
StatusBubble* bubble = GetBubble();
ASSERT_TRUE(bubble);
// Setting status text shows the widget.
bubble->SetStatus(u"test");
views::Widget* widget = GetWidget();
ASSERT_TRUE(widget);
EXPECT_TRUE(widget->IsVisible());
bubble->SetStatus(std::u16string());
// The widget is not hidden immediately, instead a task is scheduled. Run that
// now.
task_runner->RunPendingTasks();
// After the task, a timer is created that animates hidden. Advance that.
ASSERT_TRUE(GetShowHideAnimationForTesting());
// Advance well past the time for the animation to ensure it completes.
static_cast<gfx::AnimationContainerElement*>(GetShowHideAnimationForTesting())
->Step(base::TimeTicks::Now() + base::Minutes(1));
// Widget should still exist.
ASSERT_TRUE(GetWidget());
EXPECT_FALSE(widget->IsVisible());
EXPECT_TRUE(IsDestroyPopupTimerRunning());
// Run until idle, which should trigger deleting the widget.
task_runner->RunUntilIdle();
EXPECT_FALSE(IsDestroyPopupTimerRunning());
EXPECT_FALSE(GetWidget());
// Setting status text shows the widget.
bubble->SetStatus(u"test");
widget = GetWidget();
ASSERT_TRUE(widget);
EXPECT_TRUE(widget->IsVisible());
SetTaskRunner(base::SingleThreadTaskRunner::GetCurrentDefault().get());
}
#endif
|