assistant_web_container_view_unittest.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2019 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. #include "ash/assistant/ui/assistant_web_container_view.h"
  5. #include "ash/assistant/assistant_controller_impl.h"
  6. #include "ash/assistant/assistant_web_ui_controller.h"
  7. #include "ash/assistant/test/assistant_ash_test_base.h"
  8. #include "ash/frame/non_client_frame_view_ash.h"
  9. #include "ash/shell.h"
  10. #include "ash/test/ash_test_views_delegate.h"
  11. #include "base/run_loop.h"
  12. #include "chromeos/ui/frame/default_frame_header.h"
  13. #include "ui/views/window/frame_caption_button.h"
  14. namespace ash {
  15. namespace {
  16. class TestViewsDelegate : public ash::AshTestViewsDelegate {
  17. public:
  18. TestViewsDelegate() = default;
  19. TestViewsDelegate(const TestViewsDelegate& other) = delete;
  20. TestViewsDelegate& operator=(const TestViewsDelegate& other) = delete;
  21. ~TestViewsDelegate() override = default;
  22. // views::ViewsDelegate:
  23. std::unique_ptr<views::NonClientFrameView> CreateDefaultNonClientFrameView(
  24. views::Widget* widget) override {
  25. return ash::Shell::Get()->CreateDefaultNonClientFrameView(widget);
  26. }
  27. };
  28. class AssistantWebContainerViewTest : public AssistantAshTestBase {
  29. public:
  30. AssistantWebContainerViewTest() = default;
  31. AssistantWebContainerViewTest(const AssistantWebContainerViewTest&) = delete;
  32. AssistantWebContainerViewTest& operator=(
  33. const AssistantWebContainerViewTest&) = delete;
  34. ~AssistantWebContainerViewTest() override = default;
  35. protected:
  36. AssistantWebContainerView* view() {
  37. return Shell::Get()
  38. ->assistant_controller()
  39. ->web_ui_controller()
  40. ->GetViewForTest();
  41. }
  42. void OpenAssistantSettings() {
  43. Shell::Get()->assistant_controller()->OpenAssistantSettings();
  44. }
  45. views::FrameCaptionButton* GetBackButton(views::Widget* widget) {
  46. views::NonClientView* non_client_view = widget->non_client_view();
  47. NonClientFrameViewAsh* frame_view_ash =
  48. static_cast<NonClientFrameViewAsh*>(non_client_view->frame_view());
  49. return frame_view_ash->GetHeaderView()->GetFrameHeader()->GetBackButton();
  50. }
  51. private:
  52. std::unique_ptr<TestViewsDelegate> views_delegate_ =
  53. std::make_unique<TestViewsDelegate>();
  54. };
  55. } // namespace
  56. TEST_F(AssistantWebContainerViewTest, ShowAndCloseWindow) {
  57. // Show Assistant Settings UI.
  58. OpenAssistantSettings();
  59. AssistantWebContainerView* container_view = view();
  60. ASSERT_TRUE(container_view);
  61. // Close Assistant Settings UI.
  62. container_view->GetWidget()->CloseNow();
  63. container_view = view();
  64. ASSERT_FALSE(container_view);
  65. }
  66. TEST_F(AssistantWebContainerViewTest, CenterWindow) {
  67. // Test large and small screens.
  68. std::vector<std::string> resolutions{"1200x1000", "800x600"};
  69. for (const auto& resolution : resolutions) {
  70. UpdateDisplay(resolution);
  71. // Show Assistant Settings UI and grab a reference to our view under test.
  72. OpenAssistantSettings();
  73. AssistantWebContainerView* container_view = view();
  74. // We expect the view to appear in the work area where new windows will
  75. // open.
  76. gfx::Rect expected_work_area =
  77. display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
  78. // We expect the view to be centered in screen.
  79. gfx::Rect expected_bounds = gfx::Rect(expected_work_area);
  80. expected_bounds.ClampToCenteredSize(
  81. container_view->GetWidget()->GetNativeWindow()->bounds().size());
  82. ASSERT_EQ(expected_bounds,
  83. container_view->GetWidget()->GetWindowBoundsInScreen());
  84. container_view->GetWidget()->CloseNow();
  85. }
  86. }
  87. TEST_F(AssistantWebContainerViewTest, CloseWindowByKeyEvent) {
  88. // Show Assistant Settings UI.
  89. OpenAssistantSettings();
  90. ASSERT_TRUE(view());
  91. // Close Assistant Settings UI by key event.
  92. PressAndReleaseKey(ui::VKEY_W, ui::EF_CONTROL_DOWN);
  93. base::RunLoop().RunUntilIdle();
  94. ASSERT_FALSE(view());
  95. }
  96. TEST_F(AssistantWebContainerViewTest, ShouldHaveBackButton) {
  97. // Show Assistant Settings UI.
  98. OpenAssistantSettings();
  99. ASSERT_TRUE(view());
  100. views::FrameCaptionButton* back_button = nullptr;
  101. back_button = GetBackButton(view()->GetWidget());
  102. ASSERT_FALSE(back_button);
  103. view()->SetCanGoBackForTesting(/*can_go_back=*/true);
  104. back_button = GetBackButton(view()->GetWidget());
  105. ASSERT_TRUE(back_button);
  106. ASSERT_TRUE(back_button->GetVisible());
  107. view()->SetCanGoBackForTesting(/*can_go_back=*/false);
  108. back_button = GetBackButton(view()->GetWidget());
  109. ASSERT_FALSE(back_button);
  110. }
  111. TEST_F(AssistantWebContainerViewTest, BackButtonShouldPaintAsActive) {
  112. // Show Assistant Settings UI.
  113. OpenAssistantSettings();
  114. ASSERT_TRUE(view());
  115. view()->SetCanGoBackForTesting(/*can_go_back=*/true);
  116. views::FrameCaptionButton* back_button = GetBackButton(view()->GetWidget());
  117. ASSERT_TRUE(back_button);
  118. ASSERT_TRUE(back_button->GetVisible());
  119. ASSERT_TRUE(back_button->GetPaintAsActive());
  120. // Activate another window will paint the back button as inactive.
  121. std::unique_ptr<aura::Window> window(CreateTestWindow());
  122. ASSERT_TRUE(back_button->GetVisible());
  123. ASSERT_FALSE(back_button->GetPaintAsActive());
  124. // Activate Assistant web container will paint the back button as active.
  125. view()->GetWidget()->Activate();
  126. ASSERT_TRUE(back_button->GetVisible());
  127. ASSERT_TRUE(back_button->GetPaintAsActive());
  128. }
  129. TEST_F(AssistantWebContainerViewTest, ShouldRemoveBackButton) {
  130. // Show Assistant Settings UI.
  131. OpenAssistantSettings();
  132. ASSERT_TRUE(view());
  133. views::FrameCaptionButton* back_button = nullptr;
  134. back_button = GetBackButton(view()->GetWidget());
  135. ASSERT_FALSE(back_button);
  136. view()->OpenUrl(GURL("test1"));
  137. view()->DidStopLoading();
  138. back_button = GetBackButton(view()->GetWidget());
  139. ASSERT_FALSE(back_button);
  140. view()->SetCanGoBackForTesting(/*can_go_back=*/true);
  141. back_button = GetBackButton(view()->GetWidget());
  142. ASSERT_TRUE(back_button);
  143. ASSERT_TRUE(back_button->GetVisible());
  144. // Open another URL will remove the back button.
  145. view()->OpenUrl(GURL("test2"));
  146. view()->DidStopLoading();
  147. back_button = GetBackButton(view()->GetWidget());
  148. ASSERT_FALSE(back_button);
  149. }
  150. } // namespace ash