screen_layout_observer_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Copyright 2013 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/system/screen_layout_observer.h"
  5. #include <string>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/shell.h"
  8. #include "ash/strings/grit/ash_strings.h"
  9. #include "ash/test/ash_test_base.h"
  10. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  11. #include "base/command_line.h"
  12. #include "base/run_loop.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "base/strings/string_util.h"
  15. #include "base/strings/utf_string_conversions.h"
  16. #include "base/test/scoped_feature_list.h"
  17. #include "ui/accessibility/ax_node_data.h"
  18. #include "ui/base/l10n/l10n_util.h"
  19. #include "ui/chromeos/devicetype_utils.h"
  20. #include "ui/display/display_layout_builder.h"
  21. #include "ui/display/display_switches.h"
  22. #include "ui/display/manager/display_manager.h"
  23. #include "ui/display/manager/display_manager_utilities.h"
  24. #include "ui/display/test/display_manager_test_api.h"
  25. #include "ui/display/util/display_util.h"
  26. #include "ui/message_center/message_center.h"
  27. #include "ui/message_center/notification_list.h"
  28. #include "ui/message_center/public/cpp/notification.h"
  29. #include "ui/views/controls/label.h"
  30. namespace ash {
  31. class ScreenLayoutObserverTest : public AshTestBase {
  32. public:
  33. ScreenLayoutObserverTest();
  34. ScreenLayoutObserverTest(const ScreenLayoutObserverTest&) = delete;
  35. ScreenLayoutObserverTest& operator=(const ScreenLayoutObserverTest&) = delete;
  36. ~ScreenLayoutObserverTest() override;
  37. // AshTestBase:
  38. void SetUp() override;
  39. protected:
  40. ScreenLayoutObserver* GetScreenLayoutObserver();
  41. void CheckUpdate();
  42. void CloseNotification();
  43. std::u16string GetDisplayNotificationText() const;
  44. std::u16string GetDisplayNotificationAdditionalText() const;
  45. std::u16string GetFirstDisplayName();
  46. std::u16string GetSecondDisplayName();
  47. std::u16string GetMirroringDisplayNames();
  48. std::u16string GetUnifiedDisplayName();
  49. bool IsNotificationShown() const;
  50. base::test::ScopedFeatureList scoped_feature_list_;
  51. private:
  52. const message_center::Notification* GetDisplayNotification() const;
  53. };
  54. ScreenLayoutObserverTest::ScreenLayoutObserverTest() = default;
  55. ScreenLayoutObserverTest::~ScreenLayoutObserverTest() = default;
  56. void ScreenLayoutObserverTest::SetUp() {
  57. AshTestBase::SetUp();
  58. GetScreenLayoutObserver()->set_show_notifications_for_testing(true);
  59. }
  60. ScreenLayoutObserver* ScreenLayoutObserverTest::GetScreenLayoutObserver() {
  61. return Shell::Get()->screen_layout_observer();
  62. }
  63. void ScreenLayoutObserverTest::CloseNotification() {
  64. message_center::MessageCenter::Get()->RemoveNotification(
  65. ScreenLayoutObserver::kNotificationId, false);
  66. base::RunLoop().RunUntilIdle();
  67. }
  68. std::u16string ScreenLayoutObserverTest::GetDisplayNotificationText() const {
  69. const message_center::Notification* notification = GetDisplayNotification();
  70. return notification ? notification->title() : std::u16string();
  71. }
  72. std::u16string ScreenLayoutObserverTest::GetDisplayNotificationAdditionalText()
  73. const {
  74. const message_center::Notification* notification = GetDisplayNotification();
  75. return notification ? notification->message() : std::u16string();
  76. }
  77. std::u16string ScreenLayoutObserverTest::GetFirstDisplayName() {
  78. return base::UTF8ToUTF16(display_manager()->GetDisplayNameForId(
  79. display_manager()->first_display_id()));
  80. }
  81. std::u16string ScreenLayoutObserverTest::GetSecondDisplayName() {
  82. return base::UTF8ToUTF16(display_manager()->GetDisplayNameForId(
  83. display::test::DisplayManagerTestApi(display_manager())
  84. .GetSecondaryDisplay()
  85. .id()));
  86. }
  87. std::u16string ScreenLayoutObserverTest::GetMirroringDisplayNames() {
  88. DCHECK(display_manager()->IsInMirrorMode());
  89. std::u16string display_names;
  90. for (auto& id : display_manager()->GetMirroringDestinationDisplayIdList()) {
  91. if (!display_names.empty())
  92. display_names.append(u",");
  93. display_names.append(
  94. base::UTF8ToUTF16(display_manager()->GetDisplayNameForId(id)));
  95. }
  96. return display_names;
  97. }
  98. std::u16string ScreenLayoutObserverTest::GetUnifiedDisplayName() {
  99. return base::UTF8ToUTF16(
  100. display_manager()->GetDisplayNameForId(display::kUnifiedDisplayId));
  101. }
  102. bool ScreenLayoutObserverTest::IsNotificationShown() const {
  103. return !(GetDisplayNotificationText().empty() &&
  104. GetDisplayNotificationAdditionalText().empty());
  105. }
  106. const message_center::Notification*
  107. ScreenLayoutObserverTest::GetDisplayNotification() const {
  108. const message_center::NotificationList::Notifications notifications =
  109. message_center::MessageCenter::Get()->GetVisibleNotifications();
  110. for (const auto* notification : notifications) {
  111. if (notification->id() == ScreenLayoutObserver::kNotificationId)
  112. return notification;
  113. }
  114. return nullptr;
  115. }
  116. // This test is flaky. crbug.com/1222612
  117. TEST_F(ScreenLayoutObserverTest, DISABLED_DisplayNotifications) {
  118. UpdateDisplay("500x400");
  119. display::SetInternalDisplayIds({display_manager()->first_display_id()});
  120. EXPECT_TRUE(GetDisplayNotificationText().empty());
  121. // No-update
  122. CloseNotification();
  123. UpdateDisplay("500x400");
  124. EXPECT_FALSE(IsNotificationShown());
  125. // Extended.
  126. CloseNotification();
  127. UpdateDisplay("500x400,300x200");
  128. EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_EXTENDED,
  129. GetSecondDisplayName()),
  130. GetDisplayNotificationText());
  131. EXPECT_TRUE(GetDisplayNotificationAdditionalText().empty());
  132. const int64_t first_display_id =
  133. display::Screen::GetScreen()->GetPrimaryDisplay().id();
  134. const int64_t second_display_id =
  135. display::GetNextSynthesizedDisplayId(first_display_id);
  136. display::ManagedDisplayInfo first_display_info =
  137. display::CreateDisplayInfo(first_display_id, gfx::Rect(1, 1, 500, 500));
  138. display::ManagedDisplayInfo second_display_info =
  139. display::CreateDisplayInfo(second_display_id, gfx::Rect(2, 2, 500, 500));
  140. std::vector<display::ManagedDisplayInfo> display_info_list;
  141. display_info_list.push_back(first_display_info);
  142. display_info_list.push_back(second_display_info);
  143. display_manager()->OnNativeDisplaysChanged(display_info_list);
  144. display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
  145. .set_maximum_display(2u);
  146. UpdateDisplay("500x400,300x200,200x100");
  147. EXPECT_TRUE(GetDisplayNotificationText().empty());
  148. EXPECT_EQ(l10n_util::GetStringUTF16(
  149. IDS_ASH_STATUS_TRAY_DISPLAY_REMOVED_EXCEEDED_MAXIMUM),
  150. GetDisplayNotificationAdditionalText());
  151. EXPECT_TRUE(GetDisplayNotificationText().empty());
  152. UpdateDisplay("500x400,300x200");
  153. CloseNotification();
  154. // Start tablet mode and wait until display mode is updated.
  155. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  156. base::RunLoop().RunUntilIdle();
  157. // Exit mirror mode manually. Now display mode should be extending mode.
  158. display_manager()->SetMirrorMode(display::MirrorMode::kOff, absl::nullopt);
  159. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_MIRROR_EXIT),
  160. GetDisplayNotificationText());
  161. CloseNotification();
  162. // Simulate that device can support at most two displays and user connects
  163. // it with three displays. Because device is in tablet mode, display mode
  164. // becomes mirror mode from extending mode. Under this circumstance, user is
  165. // still notified of connecting more displays than maximum. See issue 827406
  166. // (https://crbug.com/827406).
  167. UpdateDisplay("500x400,300x200,200x100");
  168. EXPECT_EQ(l10n_util::GetStringUTF16(
  169. IDS_ASH_STATUS_TRAY_DISPLAY_REMOVED_EXCEEDED_MAXIMUM),
  170. GetDisplayNotificationAdditionalText());
  171. EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING,
  172. GetMirroringDisplayNames()),
  173. GetDisplayNotificationText());
  174. // Reset the parameter. Close tablet mode and wait until display mode is
  175. // updated.
  176. display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
  177. .ResetMaximumDisplay();
  178. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
  179. base::RunLoop().RunUntilIdle();
  180. // Turn on mirror mode.
  181. CloseNotification();
  182. display_manager()->SetMirrorMode(display::MirrorMode::kNormal, absl::nullopt);
  183. EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING,
  184. GetMirroringDisplayNames()),
  185. GetDisplayNotificationText());
  186. EXPECT_TRUE(GetDisplayNotificationAdditionalText().empty());
  187. // Disconnect a display to end mirror mode.
  188. CloseNotification();
  189. display_info_list.erase(display_info_list.end() - 1);
  190. display_manager()->OnNativeDisplaysChanged(display_info_list);
  191. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_MIRROR_EXIT),
  192. GetDisplayNotificationText());
  193. EXPECT_TRUE(GetDisplayNotificationAdditionalText().empty());
  194. // Restore mirror mode.
  195. CloseNotification();
  196. display_info_list.push_back(second_display_info);
  197. display_manager()->OnNativeDisplaysChanged(display_info_list);
  198. EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_MIRRORING,
  199. GetMirroringDisplayNames()),
  200. GetDisplayNotificationText());
  201. EXPECT_TRUE(GetDisplayNotificationAdditionalText().empty());
  202. // Turn off mirror mode.
  203. CloseNotification();
  204. display_manager()->SetMirrorMode(display::MirrorMode::kOff, absl::nullopt);
  205. EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_DISPLAY_MIRROR_EXIT),
  206. GetDisplayNotificationText());
  207. EXPECT_TRUE(GetDisplayNotificationAdditionalText().empty());
  208. // Enters closed lid mode.
  209. UpdateDisplay("500x400@1.5,300x200");
  210. display::SetInternalDisplayIds(
  211. {display::test::DisplayManagerTestApi(display_manager())
  212. .GetSecondaryDisplay()
  213. .id()});
  214. UpdateDisplay("500x400@1.5");
  215. EXPECT_TRUE(GetDisplayNotificationText().empty());
  216. }
  217. TEST_F(ScreenLayoutObserverTest, DisplayNotificationsDisabled) {
  218. scoped_feature_list_.Reset();
  219. UpdateDisplay("500x400");
  220. display::SetInternalDisplayIds({display_manager()->first_display_id()});
  221. EXPECT_TRUE(GetDisplayNotificationText().empty());
  222. // Adding a display.
  223. UpdateDisplay("500x400,300x200");
  224. EXPECT_FALSE(IsNotificationShown());
  225. const int64_t first_display_id =
  226. display::Screen::GetScreen()->GetPrimaryDisplay().id();
  227. const int64_t second_display_id =
  228. display::GetNextSynthesizedDisplayId(first_display_id);
  229. display::ManagedDisplayInfo first_display_info =
  230. display::CreateDisplayInfo(first_display_id, gfx::Rect(1, 1, 500, 400));
  231. display::ManagedDisplayInfo second_display_info =
  232. display::CreateDisplayInfo(second_display_id, gfx::Rect(2, 2, 500, 400));
  233. std::vector<display::ManagedDisplayInfo> display_info_list;
  234. display_info_list.push_back(first_display_info);
  235. display_info_list.push_back(second_display_info);
  236. display_manager()->OnNativeDisplaysChanged(display_info_list);
  237. // Simulate that device can support at most two displays and user
  238. // connects it with three displays. Notification should still be created to
  239. // warn user of it. See issue 827406 (https://crbug.com/827406).
  240. display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
  241. .set_maximum_display(2u);
  242. UpdateDisplay("500x400,300x200,200x100");
  243. EXPECT_TRUE(GetDisplayNotificationText().empty());
  244. EXPECT_EQ(l10n_util::GetStringUTF16(
  245. IDS_ASH_STATUS_TRAY_DISPLAY_REMOVED_EXCEEDED_MAXIMUM),
  246. GetDisplayNotificationAdditionalText());
  247. EXPECT_TRUE(GetDisplayNotificationText().empty());
  248. UpdateDisplay("500x400,300x200");
  249. CloseNotification();
  250. // Start tablet mode and wait until display mode is updated.
  251. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  252. base::RunLoop().RunUntilIdle();
  253. // Exit mirror mode manually. Now display mode should be extending mode.
  254. display_manager()->SetMirrorMode(display::MirrorMode::kOff, absl::nullopt);
  255. EXPECT_FALSE(IsNotificationShown());
  256. // Simulate that device can support at most two displays and user connects
  257. // it with three displays. Because device is in tablet mode, display mode
  258. // becomes mirror mode from extending mode. Under this circumstance, user is
  259. // still notified of connecting more displays than maximum. See issue 827406
  260. // (https://crbug.com/827406). Notification should still be shown.
  261. UpdateDisplay("500x400,300x200,200x100");
  262. EXPECT_EQ(l10n_util::GetStringUTF16(
  263. IDS_ASH_STATUS_TRAY_DISPLAY_REMOVED_EXCEEDED_MAXIMUM),
  264. GetDisplayNotificationAdditionalText());
  265. // The tablet should no longer be in mirror mode.
  266. EXPECT_FALSE(display_manager()->IsInMirrorMode());
  267. EXPECT_TRUE(GetDisplayNotificationText().empty());
  268. CloseNotification();
  269. }
  270. // Verify that no notification is shown when overscan of a screen is changed.
  271. TEST_F(ScreenLayoutObserverTest, OverscanDisplay) {
  272. UpdateDisplay("500x400, 400x300");
  273. // Close the notification that is shown from initially adding a monitor.
  274. CloseNotification();
  275. display::SetInternalDisplayIds({display_manager()->first_display_id()});
  276. // /o creates the default overscan.
  277. UpdateDisplay("500x400, 400x300/o");
  278. EXPECT_FALSE(IsNotificationShown());
  279. // Reset the overscan.
  280. Shell::Get()->display_manager()->SetOverscanInsets(
  281. display::test::DisplayManagerTestApi(display_manager())
  282. .GetSecondaryDisplay()
  283. .id(),
  284. gfx::Insets());
  285. EXPECT_FALSE(IsNotificationShown());
  286. }
  287. } // namespace ash