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

ash / shelf / shelf_config_unittest.cc [blame]

// 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/public/cpp/shelf_config.h"

#include "ash/app_list/test/app_list_test_helper.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "base/command_line.h"
#include "ui/display/screen.h"
#include "ui/views/widget/widget.h"

namespace ash {

class ShelfConfigTest : public AshTestBase {
 public:
  ShelfConfigTest() = default;
  ~ShelfConfigTest() override = default;

 protected:
  bool is_dense() { return ShelfConfig::Get()->is_dense_; }

  bool IsTabletMode() { return display::Screen::GetScreen()->InTabletMode(); }

  void SetTabletMode(bool is_tablet_mode) {
    Shell::Get()->tablet_mode_controller()->SetEnabledForTest(is_tablet_mode);
  }
};

// Make sure ShelfConfig is dense when screen becomes small in tablet mode.
TEST_F(ShelfConfigTest, SmallDisplayIsDense) {
  UpdateDisplay("1100x1000");
  SetTabletMode(true);

  ASSERT_TRUE(IsTabletMode());
  ASSERT_FALSE(is_dense());

  // Change display to have a small width, and check that ShelfConfig is dense.
  UpdateDisplay("300x1000");
  ASSERT_TRUE(is_dense());

  // Set the display size back.
  UpdateDisplay("1100x1000");
  ASSERT_FALSE(is_dense());

  // Change display to have a small height, and check that ShelfConfig is dense.
  UpdateDisplay("1100x300");
  ASSERT_TRUE(is_dense());
}

// Make sure ShelfConfig switches between dense and not dense when switching
// between clamshell and tablet mode.
TEST_F(ShelfConfigTest, DenseChangeOnTabletModeChange) {
  UpdateDisplay("1100x1000");

  ASSERT_FALSE(IsTabletMode());
  ASSERT_TRUE(is_dense());

  SetTabletMode(true);

  ASSERT_TRUE(IsTabletMode());
  ASSERT_FALSE(is_dense());

  SetTabletMode(false);

  ASSERT_FALSE(IsTabletMode());
  ASSERT_TRUE(is_dense());
}

// Make sure that the shelf size changes when switching between contexts:
// home vs in-app, clamshell vs tablet, dense vs not dense.
TEST_F(ShelfConfigTest, ShelfSizeChangesWithContext) {
  // For shelf size, the ordering should be as follows, in increasing order:
  // * Tablet, dense, in-app
  // * Tablet, standard, in-app
  // * Tablet, dense, home == Clamshell (all modes)
  // * Tablet, standard, home

  // For shelf control button size, the ordering should be as follows, in
  // increasing order:
  // * Tablet, dense (in-app and home) == Clamshell (all modes)
  // * Tablet, standard (in-app and home)

  UpdateDisplay("300x1000");
  SetTabletMode(true);
  ASSERT_TRUE(IsTabletMode());
  std::unique_ptr<views::Widget> widget =
      CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
  GetAppListTestHelper()->CheckVisibility(false);
  const int tablet_dense_in_app = ShelfConfig::Get()->shelf_size();
  const int system_shelf_tablet_dense_in_app =
      ShelfConfig::Get()->system_shelf_size();
  const int control_tablet_dense_in_app = ShelfConfig::Get()->control_size();

  widget->Close();
  GetAppListTestHelper()->CheckVisibility(true);
  const int tablet_dense_home = ShelfConfig::Get()->shelf_size();
  const int system_shelf_tablet_dense_home =
      ShelfConfig::Get()->system_shelf_size();
  const int control_tablet_dense_home = ShelfConfig::Get()->control_size();

  UpdateDisplay("1100x1000");
  ASSERT_TRUE(IsTabletMode());
  widget =
      CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
  GetAppListTestHelper()->CheckVisibility(false);
  const int tablet_standard_in_app = ShelfConfig::Get()->shelf_size();
  const int system_shelf_tablet_standard_in_app =
      ShelfConfig::Get()->system_shelf_size();
  const int control_tablet_standard_in_app = ShelfConfig::Get()->control_size();

  widget->Close();
  GetAppListTestHelper()->CheckVisibility(true);
  const int tablet_standard_home = ShelfConfig::Get()->shelf_size();
  const int system_shelf_tablet_standard_home =
      ShelfConfig::Get()->system_shelf_size();
  const int control_tablet_standard_home = ShelfConfig::Get()->control_size();

  SetTabletMode(false);
  ASSERT_FALSE(IsTabletMode());
  GetAppListTestHelper()->Dismiss();
  const int clamshell_home = ShelfConfig::Get()->shelf_size();
  const int system_shelf_clamshell_home =
      ShelfConfig::Get()->system_shelf_size();
  const int control_clamshell_home = ShelfConfig::Get()->control_size();

  widget =
      CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
  widget->Maximize();
  const int clamshell_in_app = ShelfConfig::Get()->shelf_size();
  const int system_shelf_clamshell_in_app =
      ShelfConfig::Get()->system_shelf_size();
  const int control_clamshell_in_app = ShelfConfig::Get()->control_size();

  EXPECT_LT(tablet_dense_in_app, tablet_standard_in_app);
  EXPECT_LT(tablet_standard_in_app, tablet_dense_home);
  EXPECT_EQ(tablet_dense_home, clamshell_home);
  EXPECT_EQ(clamshell_home, clamshell_in_app);
  EXPECT_LT(tablet_dense_home, tablet_standard_home);

  EXPECT_EQ(control_tablet_dense_in_app, control_tablet_dense_home);
  EXPECT_EQ(control_tablet_dense_home, control_clamshell_in_app);
  EXPECT_EQ(control_clamshell_in_app, control_clamshell_home);
  EXPECT_LT(control_clamshell_home, control_tablet_standard_in_app);
  EXPECT_EQ(control_tablet_standard_in_app, control_tablet_standard_home);

  // System shelf size should return size that matches out-of-app (home) state.
  EXPECT_EQ(system_shelf_tablet_dense_in_app, tablet_dense_home);
  EXPECT_EQ(system_shelf_tablet_dense_home, tablet_dense_home);
  EXPECT_EQ(system_shelf_tablet_standard_in_app, tablet_standard_home);
  EXPECT_EQ(system_shelf_tablet_standard_home, tablet_standard_home);
  EXPECT_EQ(system_shelf_clamshell_home, clamshell_home);
  EXPECT_EQ(system_shelf_clamshell_in_app, clamshell_home);
}

// Make sure that we consider ourselves inside an app when appropriate.
TEST_F(ShelfConfigTest, InAppMode) {
  SessionInfo info;
  info.state = session_manager::SessionState::ACTIVE;
  Shell::Get()->session_controller()->SetSessionInfo(info);

  // Go into tablet mode, open a window. Now we're in an app.
  SetTabletMode(true);
  std::unique_ptr<views::Widget> widget =
      CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
  EXPECT_TRUE(ShelfConfig::Get()->is_in_app());

  // Close the window. We should be back on the home screen.
  widget->Close();
  EXPECT_FALSE(ShelfConfig::Get()->is_in_app());

  // Open a window again.
  widget =
      CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
  EXPECT_TRUE(ShelfConfig::Get()->is_in_app());

  // Now go into overview.
  EnterOverview();
  EXPECT_TRUE(ShelfConfig::Get()->is_in_app());

  // Back to the app.
  ExitOverview();
  EXPECT_TRUE(ShelfConfig::Get()->is_in_app());

  // Leave the session.
  info.state = session_manager::SessionState::LOGIN_PRIMARY;
  Shell::Get()->session_controller()->SetSessionInfo(info);
  EXPECT_FALSE(ShelfConfig::Get()->is_in_app());
}

}  // namespace ash