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

ash / assistant / ui / assistant_web_container_view_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/assistant/ui/assistant_web_container_view.h"

#include "ash/assistant/assistant_controller_impl.h"
#include "ash/assistant/assistant_web_ui_controller.h"
#include "ash/assistant/test/assistant_ash_test_base.h"
#include "ash/frame/non_client_frame_view_ash.h"
#include "ash/shell.h"
#include "ash/test/ash_test_views_delegate.h"
#include "base/run_loop.h"
#include "chromeos/ui/frame/default_frame_header.h"
#include "ui/views/window/frame_caption_button.h"

namespace ash {

namespace {

class TestViewsDelegate : public ash::AshTestViewsDelegate {
 public:
  TestViewsDelegate() = default;
  TestViewsDelegate(const TestViewsDelegate& other) = delete;
  TestViewsDelegate& operator=(const TestViewsDelegate& other) = delete;
  ~TestViewsDelegate() override = default;

  // views::ViewsDelegate:
  std::unique_ptr<views::NonClientFrameView> CreateDefaultNonClientFrameView(
      views::Widget* widget) override {
    return ash::Shell::Get()->CreateDefaultNonClientFrameView(widget);
  }
};

class AssistantWebContainerViewTest : public AssistantAshTestBase {
 public:
  AssistantWebContainerViewTest() = default;

  AssistantWebContainerViewTest(const AssistantWebContainerViewTest&) = delete;
  AssistantWebContainerViewTest& operator=(
      const AssistantWebContainerViewTest&) = delete;

  ~AssistantWebContainerViewTest() override = default;

 protected:
  AssistantWebContainerView* view() {
    return Shell::Get()
        ->assistant_controller()
        ->web_ui_controller()
        ->GetViewForTest();
  }

  void OpenAssistantSettings() {
    Shell::Get()->assistant_controller()->OpenAssistantSettings();
  }

  views::FrameCaptionButton* GetBackButton(views::Widget* widget) {
    views::NonClientView* non_client_view = widget->non_client_view();
    NonClientFrameViewAsh* frame_view_ash =
        static_cast<NonClientFrameViewAsh*>(non_client_view->frame_view());
    return frame_view_ash->GetHeaderView()->GetFrameHeader()->GetBackButton();
  }

 private:
  std::unique_ptr<TestViewsDelegate> views_delegate_ =
      std::make_unique<TestViewsDelegate>();
};

}  // namespace

TEST_F(AssistantWebContainerViewTest, ShowAndCloseWindow) {
  // Show Assistant Settings UI.
  OpenAssistantSettings();
  AssistantWebContainerView* container_view = view();
  ASSERT_TRUE(container_view);

  // Close Assistant Settings UI.
  container_view->GetWidget()->CloseNow();
  container_view = view();
  ASSERT_FALSE(container_view);
}

TEST_F(AssistantWebContainerViewTest, CenterWindow) {
  // Test large and small screens.
  std::vector<std::string> resolutions{"1200x1000", "800x600"};

  for (const auto& resolution : resolutions) {
    UpdateDisplay(resolution);

    // Show Assistant Settings UI and grab a reference to our view under test.
    OpenAssistantSettings();
    AssistantWebContainerView* container_view = view();

    // We expect the view to appear in the work area where new windows will
    // open.
    gfx::Rect expected_work_area =
        display::Screen::GetScreen()->GetPrimaryDisplay().work_area();

    // We expect the view to be centered in screen.
    gfx::Rect expected_bounds = gfx::Rect(expected_work_area);
    expected_bounds.ClampToCenteredSize(
        container_view->GetWidget()->GetNativeWindow()->bounds().size());
    ASSERT_EQ(expected_bounds,
              container_view->GetWidget()->GetWindowBoundsInScreen());
    container_view->GetWidget()->CloseNow();
  }
}

TEST_F(AssistantWebContainerViewTest, CloseWindowByKeyEvent) {
  // Show Assistant Settings UI.
  OpenAssistantSettings();
  ASSERT_TRUE(view());

  // Close Assistant Settings UI by key event.
  PressAndReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  base::RunLoop().RunUntilIdle();
  ASSERT_FALSE(view());
}

TEST_F(AssistantWebContainerViewTest, ShouldHaveBackButton) {
  // Show Assistant Settings UI.
  OpenAssistantSettings();
  ASSERT_TRUE(view());

  views::FrameCaptionButton* back_button = nullptr;
  back_button = GetBackButton(view()->GetWidget());
  ASSERT_FALSE(back_button);

  view()->SetCanGoBackForTesting(/*can_go_back=*/true);
  back_button = GetBackButton(view()->GetWidget());
  ASSERT_TRUE(back_button);
  ASSERT_TRUE(back_button->GetVisible());

  view()->SetCanGoBackForTesting(/*can_go_back=*/false);
  back_button = GetBackButton(view()->GetWidget());
  ASSERT_FALSE(back_button);
}

TEST_F(AssistantWebContainerViewTest, BackButtonShouldPaintAsActive) {
  // Show Assistant Settings UI.
  OpenAssistantSettings();
  ASSERT_TRUE(view());

  view()->SetCanGoBackForTesting(/*can_go_back=*/true);
  views::FrameCaptionButton* back_button = GetBackButton(view()->GetWidget());
  ASSERT_TRUE(back_button);
  ASSERT_TRUE(back_button->GetVisible());
  ASSERT_TRUE(back_button->GetPaintAsActive());

  // Activate another window will paint the back button as inactive.
  std::unique_ptr<aura::Window> window(CreateTestWindow());
  ASSERT_TRUE(back_button->GetVisible());
  ASSERT_FALSE(back_button->GetPaintAsActive());

  // Activate Assistant web container will paint the back button as active.
  view()->GetWidget()->Activate();
  ASSERT_TRUE(back_button->GetVisible());
  ASSERT_TRUE(back_button->GetPaintAsActive());
}

TEST_F(AssistantWebContainerViewTest, ShouldRemoveBackButton) {
  // Show Assistant Settings UI.
  OpenAssistantSettings();
  ASSERT_TRUE(view());

  views::FrameCaptionButton* back_button = nullptr;
  back_button = GetBackButton(view()->GetWidget());
  ASSERT_FALSE(back_button);

  view()->OpenUrl(GURL("test1"));
  view()->DidStopLoading();
  back_button = GetBackButton(view()->GetWidget());
  ASSERT_FALSE(back_button);

  view()->SetCanGoBackForTesting(/*can_go_back=*/true);
  back_button = GetBackButton(view()->GetWidget());
  ASSERT_TRUE(back_button);
  ASSERT_TRUE(back_button->GetVisible());

  // Open another URL will remove the back button.
  view()->OpenUrl(GURL("test2"));
  view()->DidStopLoading();
  back_button = GetBackButton(view()->GetWidget());
  ASSERT_FALSE(back_button);
}

}  // namespace ash