File: shelf_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (211 lines) | stat: -rw-r--r-- 7,281 bytes parent folder | download | duplicates (6)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/shelf/shelf.h"

#include <memory>

#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/shelf_prefs.h"
#include "ash/public/cpp/shelf_test_api.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/test/app_list_test_api.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_layout_manager_observer.h"
#include "ash/shell.h"
#include "base/command_line.h"
#include "base/i18n/base_i18n_switches.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user_names.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/test/event_generator.h"

namespace ash {
namespace {

// Class that waits for a change in shelf auto-hide state.
class ShelfAutoHideWaiter : public ShelfLayoutManagerObserver {
 public:
  explicit ShelfAutoHideWaiter(Shelf* shelf) : shelf_(shelf) {
    shelf_->shelf_layout_manager()->AddObserver(this);
  }

  ~ShelfAutoHideWaiter() override {
    shelf_->shelf_layout_manager()->RemoveObserver(this);
  }

  void WaitForState(ShelfAutoHideState expected_state) {
    if (shelf_->GetAutoHideState() == expected_state) {
      return;
    }
    expected_state_ = expected_state;
    run_loop_ = std::make_unique<base::RunLoop>();
    run_loop_->Run();
    run_loop_.reset();
  }

  // ShelfLayoutManagerObserver:
  void OnAutoHideStateChanged(ShelfAutoHideState new_state) override {
    if (run_loop_ && new_state == expected_state_) {
      run_loop_->Quit();
    }
  }

  const raw_ptr<Shelf> shelf_;
  ShelfAutoHideState expected_state_ = SHELF_AUTO_HIDE_SHOWN;
  std::unique_ptr<base::RunLoop> run_loop_;
};

class ShelfBrowserTest : public InProcessBrowserTest,
                         public testing::WithParamInterface<bool> {
 public:
  ShelfBrowserTest() = default;
  ShelfBrowserTest(const ShelfBrowserTest&) = delete;
  ShelfBrowserTest& operator=(const ShelfBrowserTest&) = delete;
  ~ShelfBrowserTest() override = default;

  bool IsRTL() { return GetParam(); }

  // InProcessBrowserTest:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    InProcessBrowserTest::SetUpCommandLine(command_line);
    if (IsRTL()) {
      command_line->AppendSwitchASCII(::switches::kForceUIDirection, "rtl");
    }
    // The tests assume no windows are open at test startup.
    command_line->AppendSwitch(::switches::kNoStartupWindow);
  }
};

INSTANTIATE_TEST_SUITE_P(Rtl, ShelfBrowserTest, testing::Bool());

IN_PROC_BROWSER_TEST_P(ShelfBrowserTest, AutoHideSmoke) {
  Shelf* shelf = Shell::GetPrimaryRootWindowController()->shelf();
  ASSERT_TRUE(shelf);
  ASSERT_EQ(shelf->auto_hide_behavior(), ShelfAutoHideBehavior::kNever);
  ASSERT_EQ(shelf->GetVisibilityState(), SHELF_VISIBLE);
  ShelfAutoHideWaiter shelf_waiter(shelf);

  // Ensure the mouse is not over the shelf.
  aura::Window* root_window = Shell::GetPrimaryRootWindow();
  ui::test::EventGenerator generator(root_window);
  generator.MoveMouseTo(0, 0);

  // Enable auto-hide.
  shelf->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
  EXPECT_EQ(shelf->GetVisibilityState(), SHELF_AUTO_HIDE);

  // No windows are open, so the shelf is visible.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_SHOWN);

  // Open a browser window.
  Browser* browser = CreateBrowser(ProfileManager::GetLastUsedProfile());
  ASSERT_TRUE(browser);

  // The shelf auto-hides.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_HIDDEN);

  // Move the mouse to the bottom right corner of the screen.
  generator.MoveMouseTo(root_window->bounds().bottom_right());

  // The shelf shows.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_SHOWN);

  // Move the mouse to the top left of the screen again, outside the shelf.
  generator.MoveMouseTo(0, 0);

  // The shelf auto-hides.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_HIDDEN);

  // Close the browser window.
  CloseAllBrowsers();

  // The shelf shows.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_SHOWN);
}

////////////////////////////////////////////////////////////////////////////////

using ShelfTabletModeBrowserTest = InProcessBrowserTest;

IN_PROC_BROWSER_TEST_F(ShelfTabletModeBrowserTest, AutoHideSmoke) {
  Shelf* shelf = Shell::GetPrimaryRootWindowController()->shelf();
  ASSERT_TRUE(shelf);
  ASSERT_EQ(shelf->auto_hide_behavior(), ShelfAutoHideBehavior::kNever);
  ASSERT_EQ(shelf->GetVisibilityState(), SHELF_VISIBLE);
  ShelfAutoHideWaiter shelf_waiter(shelf);

  // Enable auto-hide.
  shelf->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
  EXPECT_EQ(shelf->GetVisibilityState(), SHELF_AUTO_HIDE);

  // The test starts with a browser window open, so the shelf auto-hides.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_HIDDEN);

  // Enter tablet mode.
  ShellTestApi().SetTabletModeEnabledForTest(true);

  // Swipe up from the bottom-center of the display.
  aura::Window* root_window = Shell::GetPrimaryRootWindow();
  ui::test::EventGenerator generator(root_window);
  gfx::Point start_point = root_window->bounds().bottom_center();
  generator.set_current_screen_location(start_point);
  generator.PressMoveAndReleaseTouchBy(0, -80);

  // The shelf shows.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_SHOWN);

  // Swipe down from the last point to the bottom of the screen.
  generator.PressMoveAndReleaseTouchBy(0, 80);

  // The shelf hides.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_HIDDEN);

  // Close the browser window.
  CloseAllBrowsers();

  // The shelf shows.
  shelf_waiter.WaitForState(SHELF_AUTO_HIDE_SHOWN);
}

////////////////////////////////////////////////////////////////////////////////

class ShelfGuestSessionBrowserTest : public InProcessBrowserTest {
 protected:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(switches::kGuestSession);
    command_line->AppendSwitch(::switches::kIncognito);
    command_line->AppendSwitchASCII(switches::kLoginProfile, "hash");
    command_line->AppendSwitchASCII(
        switches::kLoginUser, user_manager::GuestAccountId().GetUserEmail());
  }
};

// Tests that in guest session, shelf alignment could be initialized to bottom
// aligned, instead of bottom locked (crbug.com/699661).
IN_PROC_BROWSER_TEST_F(ShelfGuestSessionBrowserTest, ShelfAlignment) {
  // Check the alignment pref for the primary display.
  ShelfAlignment alignment = GetShelfAlignmentPref(
      browser()->profile()->GetPrefs(),
      display::Screen::GetScreen()->GetPrimaryDisplay().id());
  EXPECT_EQ(ShelfAlignment::kBottom, alignment);

  // Check the locked state, which is not exposed via prefs.
  EXPECT_FALSE(ShelfTestApi().IsAlignmentBottomLocked());
}

}  // namespace
}  // namespace ash