blob: 8990e8b57b2d54421214afd0baae8e0d2832a407 [file] [log] [blame]
Aleksei Seren121533e2017-11-28 20:11:181// Copyright 2017 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
Evan Stade5883ad52018-04-25 17:13:455#include "base/run_loop.h"
Devlin Cronin626d80c2018-06-01 01:08:366#include "base/test/metrics/histogram_tester.h"
Evan Stade5883ad52018-04-25 17:13:457#include "build/build_config.h"
Aleksei Seren121533e2017-11-28 20:11:188#include "chrome/app/chrome_command_ids.h"
Aleksei Seren121533e2017-11-28 20:11:189#include "chrome/browser/chrome_notification_types.h"
Avi Drissmand30927342018-05-22 15:04:2710#include "chrome/browser/lifetime/browser_shutdown.h"
Aleksei Seren121533e2017-11-28 20:11:1811#include "chrome/browser/ui/browser.h"
12#include "chrome/browser/ui/browser_commands.h"
13#include "chrome/browser/ui/browser_list.h"
Michael Giuffrida5d8b4252018-03-01 03:11:4914#include "chrome/browser/ui/browser_list_observer.h"
Evan Stade5883ad52018-04-25 17:13:4515#include "chrome/browser/ui/browser_window.h"
Aleksei Seren121533e2017-11-28 20:11:1816#include "chrome/test/base/in_process_browser_test.h"
17#include "chrome/test/base/ui_test_utils.h"
18#include "content/public/browser/notification_service.h"
19#include "testing/gmock/include/gmock/gmock.h"
20#include "testing/gtest/include/gtest/gtest.h"
Evan Stade5883ad52018-04-25 17:13:4521#include "ui/events/test/event_generator.h"
Aleksei Seren121533e2017-11-28 20:11:1822
Peter Kasting20e1d892018-11-16 03:08:3023#if defined(OS_CHROMEOS)
24#include "ui/aura/window.h"
25#endif
26
Aleksei Seren121533e2017-11-28 20:11:1827using testing::_;
28using testing::AtLeast;
29
30class BrowserShutdownBrowserTest : public InProcessBrowserTest {
31 public:
32 BrowserShutdownBrowserTest() {}
33 ~BrowserShutdownBrowserTest() override {}
34
35 protected:
36 base::HistogramTester histogram_tester_;
37
38 private:
39 DISALLOW_COPY_AND_ASSIGN(BrowserShutdownBrowserTest);
40};
41
cm.sanchia0d6add2017-12-13 04:59:3342class BrowserClosingObserver : public BrowserListObserver {
Aleksei Seren121533e2017-11-28 20:11:1843 public:
44 BrowserClosingObserver() {}
45 MOCK_METHOD1(OnBrowserClosing, void(Browser* browser));
46
47 private:
48 DISALLOW_COPY_AND_ASSIGN(BrowserClosingObserver);
49};
50
51// ChromeOS has the different shutdown flow on user initiated exit process.
52// See the comment for chrome::AttemptUserExit() function declaration.
53#if !defined(OS_CHROMEOS)
54IN_PROC_BROWSER_TEST_F(BrowserShutdownBrowserTest,
55 PRE_TwoBrowsersClosingShutdownHistograms) {
56 ui_test_utils::NavigateToURL(browser(), GURL("browser://version"));
57 Browser* browser2 = CreateBrowser(browser()->profile());
58 ui_test_utils::NavigateToURL(browser2, GURL("browser://help"));
59
60 BrowserClosingObserver closing_observer;
61 BrowserList::AddObserver(&closing_observer);
62 EXPECT_CALL(closing_observer, OnBrowserClosing(_)).Times(AtLeast(1));
63
64 content::WindowedNotificationObserver terminate_observer(
65 chrome::NOTIFICATION_APP_TERMINATING,
66 content::NotificationService::AllSources());
67 chrome::ExecuteCommand(browser(), IDC_EXIT);
68 terminate_observer.Wait();
69
70 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
71 EXPECT_TRUE(BrowserList::GetInstance()->empty());
72 EXPECT_EQ(browser_shutdown::GetShutdownType(),
Greg Thompsonae8a5b12019-11-21 12:35:3673 browser_shutdown::ShutdownType::kWindowClose);
Aleksei Seren121533e2017-11-28 20:11:1874 BrowserList::RemoveObserver(&closing_observer);
75}
76
77IN_PROC_BROWSER_TEST_F(BrowserShutdownBrowserTest,
78 TwoBrowsersClosingShutdownHistograms) {
Greg Thompsonae8a5b12019-11-21 12:35:3679 histogram_tester_.ExpectUniqueSample(
80 "Shutdown.ShutdownType",
81 static_cast<int>(browser_shutdown::ShutdownType::kWindowClose), 1);
Aleksei Seren121533e2017-11-28 20:11:1882 histogram_tester_.ExpectTotalCount("Shutdown.renderers.total", 1);
83 histogram_tester_.ExpectTotalCount("Shutdown.window_close.time2", 1);
84 histogram_tester_.ExpectTotalCount("Shutdown.window_close.time_per_process",
85 1);
86}
Evan Stade5883ad52018-04-25 17:13:4587#else
Evan Stade5883ad52018-04-25 17:13:4588// On Chrome OS, the shutdown accelerator is handled by Ash and requires
89// confirmation, so Chrome shouldn't try to shut down after it's been hit one
90// time. Regression test for crbug.com/834092
Tom Anderson30344af2018-08-16 20:00:3491IN_PROC_BROWSER_TEST_F(BrowserShutdownBrowserTest, ShutdownConfirmation) {
Evan Stade5883ad52018-04-25 17:13:4592 const int modifiers = ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN;
Evan Stade5883ad52018-04-25 17:13:4593
Peter Kasting20e1d892018-11-16 03:08:3094 ui::test::EventGenerator generator(
95 browser()->window()->GetNativeWindow()->GetRootWindow());
Evan Stade5883ad52018-04-25 17:13:4596
97 // Press the accelerator for quitting.
98 generator.PressKey(ui::VKEY_Q, modifiers);
99 generator.ReleaseKey(ui::VKEY_Q, modifiers);
100 base::RunLoop().RunUntilIdle();
101
Evan Stade5883ad52018-04-25 17:13:45102 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
Evan Stade5883ad52018-04-25 17:13:45103}
Tom Anderson30344af2018-08-16 20:00:34104#endif // !defined(OS_CHROMEOS)