blob: f320f82c829068484428ec9217843835efd78bda [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
23using testing::_;
24using testing::AtLeast;
25
26class BrowserShutdownBrowserTest : public InProcessBrowserTest {
27 public:
28 BrowserShutdownBrowserTest() {}
29 ~BrowserShutdownBrowserTest() override {}
30
31 protected:
32 base::HistogramTester histogram_tester_;
33
34 private:
35 DISALLOW_COPY_AND_ASSIGN(BrowserShutdownBrowserTest);
36};
37
cm.sanchia0d6add2017-12-13 04:59:3338class BrowserClosingObserver : public BrowserListObserver {
Aleksei Seren121533e2017-11-28 20:11:1839 public:
40 BrowserClosingObserver() {}
41 MOCK_METHOD1(OnBrowserClosing, void(Browser* browser));
42
43 private:
44 DISALLOW_COPY_AND_ASSIGN(BrowserClosingObserver);
45};
46
47// ChromeOS has the different shutdown flow on user initiated exit process.
48// See the comment for chrome::AttemptUserExit() function declaration.
49#if !defined(OS_CHROMEOS)
50IN_PROC_BROWSER_TEST_F(BrowserShutdownBrowserTest,
51 PRE_TwoBrowsersClosingShutdownHistograms) {
52 ui_test_utils::NavigateToURL(browser(), GURL("browser://version"));
53 Browser* browser2 = CreateBrowser(browser()->profile());
54 ui_test_utils::NavigateToURL(browser2, GURL("browser://help"));
55
56 BrowserClosingObserver closing_observer;
57 BrowserList::AddObserver(&closing_observer);
58 EXPECT_CALL(closing_observer, OnBrowserClosing(_)).Times(AtLeast(1));
59
60 content::WindowedNotificationObserver terminate_observer(
61 chrome::NOTIFICATION_APP_TERMINATING,
62 content::NotificationService::AllSources());
63 chrome::ExecuteCommand(browser(), IDC_EXIT);
64 terminate_observer.Wait();
65
66 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
67 EXPECT_TRUE(BrowserList::GetInstance()->empty());
68 EXPECT_EQ(browser_shutdown::GetShutdownType(),
69 browser_shutdown::WINDOW_CLOSE);
70 BrowserList::RemoveObserver(&closing_observer);
71}
72
73IN_PROC_BROWSER_TEST_F(BrowserShutdownBrowserTest,
74 TwoBrowsersClosingShutdownHistograms) {
75 histogram_tester_.ExpectUniqueSample("Shutdown.ShutdownType",
76 browser_shutdown::WINDOW_CLOSE, 1);
77 histogram_tester_.ExpectTotalCount("Shutdown.renderers.total", 1);
78 histogram_tester_.ExpectTotalCount("Shutdown.window_close.time2", 1);
79 histogram_tester_.ExpectTotalCount("Shutdown.window_close.time_per_process",
80 1);
81}
82#endif // !defined(OS_CHROMEOS)
Evan Stade5883ad52018-04-25 17:13:4583
84// EventGenerator doesn't work on Mac. See https://crbug.com/814675
85#if defined(OS_MACOSX)
86#define MAYBE_ShutdownConfirmation DISABLED_ShutdownConfirmation
87#else
88#define MAYBE_ShutdownConfirmation ShutdownConfirmation
89#endif
90
91// On Chrome OS, the shutdown accelerator is handled by Ash and requires
92// confirmation, so Chrome shouldn't try to shut down after it's been hit one
93// time. Regression test for crbug.com/834092
94IN_PROC_BROWSER_TEST_F(BrowserShutdownBrowserTest, MAYBE_ShutdownConfirmation) {
95#if defined(OS_MACOSX)
96 const int modifiers = ui::EF_COMMAND_DOWN;
97#else
98 const int modifiers = ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN;
99#endif
100
101 ui::test::EventGenerator generator(browser()->window()->GetNativeWindow());
102
103 // Press the accelerator for quitting.
104 generator.PressKey(ui::VKEY_Q, modifiers);
105 generator.ReleaseKey(ui::VKEY_Q, modifiers);
106 base::RunLoop().RunUntilIdle();
107
108#if defined(OS_CHROMEOS)
109 EXPECT_FALSE(browser_shutdown::IsTryingToQuit());
110#else
111 EXPECT_TRUE(browser_shutdown::IsTryingToQuit());
112#endif
113}