Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / display / resolution_notification_controller_unittest.cc
index 0ec8852..4d1eedf 100644 (file)
@@ -5,15 +5,45 @@
 #include "ash/display/resolution_notification_controller.h"
 
 #include "ash/display/display_manager.h"
-#include "ash/screen_ash.h"
+#include "ash/screen_util.h"
 #include "ash/shell.h"
 #include "ash/test/ash_test_base.h"
 #include "base/bind.h"
+#include "base/strings/utf_string_conversions.h"
+#include "grit/ash_strings.h"
+#include "ui/base/l10n/l10n_util.h"
 #include "ui/gfx/size.h"
 #include "ui/message_center/message_center.h"
+#include "ui/message_center/notification.h"
+#include "ui/message_center/notification_list.h"
 
 namespace ash {
-namespace internal {
+namespace {
+
+base::string16 ExpectedNotificationMessage(int64 display_id,
+                                           const gfx::Size& new_resolution) {
+  return l10n_util::GetStringFUTF16(
+      IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
+      base::UTF8ToUTF16(
+          Shell::GetInstance()->display_manager()->GetDisplayNameForId(
+              display_id)),
+      base::UTF8ToUTF16(new_resolution.ToString()));
+}
+
+base::string16 ExpectedFallbackNotificationMessage(
+    int64 display_id,
+    const gfx::Size& specified_resolution,
+    const gfx::Size& fallback_resolution) {
+  return l10n_util::GetStringFUTF16(
+      IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
+      base::UTF8ToUTF16(
+          Shell::GetInstance()->display_manager()->GetDisplayNameForId(
+              display_id)),
+      base::UTF8ToUTF16(specified_resolution.ToString()),
+      base::UTF8ToUTF16(fallback_resolution.ToString()));
+}
+
+}  // namespace
 
 class ResolutionNotificationControllerTest : public ash::test::AshTestBase {
  public:
@@ -29,17 +59,18 @@ class ResolutionNotificationControllerTest : public ash::test::AshTestBase {
     ResolutionNotificationController::SuppressTimerForTest();
   }
 
-  void SetDisplayResolutionAndNotify(const gfx::Display& display,
-                                     const gfx::Size& new_resolution) {
+  void SetDisplayResolutionAndNotifyWithResolution(
+      const gfx::Display& display,
+      const gfx::Size& new_resolution,
+      const gfx::Size& actual_new_resolution) {
     DisplayManager* display_manager = Shell::GetInstance()->display_manager();
     const DisplayInfo& info = display_manager->GetDisplayInfo(display.id());
-    Shell::GetInstance()->resolution_notification_controller()->
-        SetDisplayResolutionAndNotify(
-            display.id(),
-            info.size_in_pixel(),
-            new_resolution,
-            base::Bind(&ResolutionNotificationControllerTest::OnAccepted,
-                       base::Unretained(this)));
+    controller()->SetDisplayResolutionAndNotify(
+        display.id(),
+        info.size_in_pixel(),
+        new_resolution,
+        base::Bind(&ResolutionNotificationControllerTest::OnAccepted,
+                   base::Unretained(this)));
 
     // OnConfigurationChanged event won't be emitted in the test environment,
     // so invoke UpdateDisplay() to emit that event explicitly.
@@ -49,7 +80,7 @@ class ResolutionNotificationControllerTest : public ash::test::AshTestBase {
       DisplayInfo info = display_manager->GetDisplayInfo(id);
       if (display.id() == id) {
         gfx::Rect bounds = info.bounds_in_native();
-        bounds.set_size(new_resolution);
+        bounds.set_size(actual_new_resolution);
         info.SetBounds(bounds);
       }
       info_list.push_back(info);
@@ -58,31 +89,49 @@ class ResolutionNotificationControllerTest : public ash::test::AshTestBase {
     RunAllPendingInMessageLoop();
   }
 
-  void ClickOnNotification() {
+  void SetDisplayResolutionAndNotify(const gfx::Display& display,
+                                     const gfx::Size& new_resolution) {
+    SetDisplayResolutionAndNotifyWithResolution(
+        display, new_resolution, new_resolution);
+  }
+
+  static base::string16 GetNotificationMessage() {
+    const message_center::NotificationList::Notifications& notifications =
+        message_center::MessageCenter::Get()->GetVisibleNotifications();
+    for (message_center::NotificationList::Notifications::const_iterator iter =
+             notifications.begin(); iter != notifications.end(); ++iter) {
+      if ((*iter)->id() == ResolutionNotificationController::kNotificationId)
+        return (*iter)->title();
+    }
+
+    return base::string16();
+  }
+
+  static void ClickOnNotification() {
     message_center::MessageCenter::Get()->ClickOnNotification(
         ResolutionNotificationController::kNotificationId);
   }
 
-  void ClickOnNotificationButton(int index) {
+  static void ClickOnNotificationButton(int index) {
     message_center::MessageCenter::Get()->ClickOnNotificationButton(
         ResolutionNotificationController::kNotificationId, index);
   }
 
-  void CloseNotification() {
+  static void CloseNotification() {
     message_center::MessageCenter::Get()->RemoveNotification(
         ResolutionNotificationController::kNotificationId, true /* by_user */);
   }
 
-  bool IsNotificationVisible() {
+  static bool IsNotificationVisible() {
     return message_center::MessageCenter::Get()->HasNotification(
         ResolutionNotificationController::kNotificationId);
   }
 
-  void TickTimer() {
+  static void TickTimer() {
     controller()->OnTimerTick();
   }
 
-  ResolutionNotificationController* controller() {
+  static ResolutionNotificationController* controller() {
     return Shell::GetInstance()->resolution_notification_controller();
   }
 
@@ -106,71 +155,74 @@ TEST_F(ResolutionNotificationControllerTest, Basic) {
   if (!SupportsMultipleDisplays())
     return;
 
-  UpdateDisplay("300x300#300x300|200x200,250x250#250x250|200x200");
-  int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id();
-  ash::internal::DisplayManager* display_manager =
+  UpdateDisplay("300x300#300x300%57|200x200%58,250x250#250x250%59|200x200%60");
+  int64 id2 = ash::ScreenUtil::GetSecondaryDisplay().id();
+  ash::DisplayManager* display_manager =
       ash::Shell::GetInstance()->display_manager();
   ASSERT_EQ(0, accept_count());
   EXPECT_FALSE(IsNotificationVisible());
 
   // Changes the resolution and apply the result.
   SetDisplayResolutionAndNotify(
-      ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200));
+      ScreenUtil::GetSecondaryDisplay(), gfx::Size(200, 200));
   EXPECT_TRUE(IsNotificationVisible());
   EXPECT_FALSE(controller()->DoesNotificationTimeout());
-  gfx::Size resolution;
-  EXPECT_TRUE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
-  EXPECT_EQ("200x200", resolution.ToString());
+  EXPECT_EQ(ExpectedNotificationMessage(id2, gfx::Size(200, 200)),
+            GetNotificationMessage());
+  DisplayMode mode;
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(60.0, mode.refresh_rate);
 
   // Click the revert button, which reverts to the best resolution.
   ClickOnNotificationButton(0);
   RunAllPendingInMessageLoop();
   EXPECT_FALSE(IsNotificationVisible());
   EXPECT_EQ(0, accept_count());
-  EXPECT_FALSE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("250x250", mode.size.ToString());
+  EXPECT_EQ(59.0, mode.refresh_rate);
 }
 
 TEST_F(ResolutionNotificationControllerTest, ClickMeansAccept) {
   if (!SupportsMultipleDisplays())
     return;
 
-  UpdateDisplay("300x300#300x300|200x200,250x250#250x250|200x200");
-  int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id();
-  ash::internal::DisplayManager* display_manager =
+  UpdateDisplay("300x300#300x300%57|200x200%58,250x250#250x250%59|200x200%60");
+  int64 id2 = ash::ScreenUtil::GetSecondaryDisplay().id();
+  ash::DisplayManager* display_manager =
       ash::Shell::GetInstance()->display_manager();
   ASSERT_EQ(0, accept_count());
   EXPECT_FALSE(IsNotificationVisible());
 
   // Changes the resolution and apply the result.
   SetDisplayResolutionAndNotify(
-      ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200));
+      ScreenUtil::GetSecondaryDisplay(), gfx::Size(200, 200));
   EXPECT_TRUE(IsNotificationVisible());
   EXPECT_FALSE(controller()->DoesNotificationTimeout());
-  gfx::Size resolution;
-  EXPECT_TRUE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
-  EXPECT_EQ("200x200", resolution.ToString());
+  DisplayMode mode;
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(60.0, mode.refresh_rate);
 
   // Click the revert button, which reverts the resolution.
   ClickOnNotification();
   RunAllPendingInMessageLoop();
   EXPECT_FALSE(IsNotificationVisible());
   EXPECT_EQ(1, accept_count());
-  EXPECT_TRUE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
-  EXPECT_EQ("200x200", resolution.ToString());
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(60.0, mode.refresh_rate);
 }
 
 TEST_F(ResolutionNotificationControllerTest, AcceptButton) {
   if (!SupportsMultipleDisplays())
     return;
 
-  ash::internal::DisplayManager* display_manager =
+  ash::DisplayManager* display_manager =
       ash::Shell::GetInstance()->display_manager();
 
-  UpdateDisplay("300x300#300x300|200x200");
+  UpdateDisplay("300x300#300x300%59|200x200%60");
   const gfx::Display& display = ash::Shell::GetScreen()->GetPrimaryDisplay();
   SetDisplayResolutionAndNotify(display, gfx::Size(200, 200));
   EXPECT_TRUE(IsNotificationVisible());
@@ -181,13 +233,14 @@ TEST_F(ResolutionNotificationControllerTest, AcceptButton) {
   ClickOnNotificationButton(0);
   EXPECT_FALSE(IsNotificationVisible());
   EXPECT_EQ(1, accept_count());
-  gfx::Size resolution;
-  EXPECT_TRUE(display_manager->GetSelectedResolutionForDisplayId(
-      display.id(), &resolution));
-  EXPECT_EQ("200x200", resolution.ToString());
+  DisplayMode mode;
+  EXPECT_TRUE(
+      display_manager->GetSelectedModeForDisplayId(display.id(), &mode));
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(60.0f, mode.refresh_rate);
 
   // In that case the second button is revert.
-  UpdateDisplay("300x300#300x300|200x200");
+  UpdateDisplay("300x300#300x300%59|200x200%60");
   SetDisplayResolutionAndNotify(display, gfx::Size(200, 200));
   EXPECT_TRUE(IsNotificationVisible());
 
@@ -195,30 +248,32 @@ TEST_F(ResolutionNotificationControllerTest, AcceptButton) {
   ClickOnNotificationButton(1);
   EXPECT_FALSE(IsNotificationVisible());
   EXPECT_EQ(1, accept_count());
-  EXPECT_FALSE(display_manager->GetSelectedResolutionForDisplayId(
-      display.id(), &resolution));
+  EXPECT_TRUE(
+      display_manager->GetSelectedModeForDisplayId(display.id(), &mode));
+  EXPECT_EQ("300x300", mode.size.ToString());
+  EXPECT_EQ(59.0f, mode.refresh_rate);
 }
 
 TEST_F(ResolutionNotificationControllerTest, Close) {
   if (!SupportsMultipleDisplays())
     return;
 
-  UpdateDisplay("100x100,150x150#150x150|200x200");
-  int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id();
-  ash::internal::DisplayManager* display_manager =
+  UpdateDisplay("100x100,150x150#150x150%59|200x200%60");
+  int64 id2 = ash::ScreenUtil::GetSecondaryDisplay().id();
+  ash::DisplayManager* display_manager =
       ash::Shell::GetInstance()->display_manager();
   ASSERT_EQ(0, accept_count());
   EXPECT_FALSE(IsNotificationVisible());
 
   // Changes the resolution and apply the result.
   SetDisplayResolutionAndNotify(
-      ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200));
+      ScreenUtil::GetSecondaryDisplay(), gfx::Size(200, 200));
   EXPECT_TRUE(IsNotificationVisible());
   EXPECT_FALSE(controller()->DoesNotificationTimeout());
-  gfx::Size resolution;
-  EXPECT_TRUE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
-  EXPECT_EQ("200x200", resolution.ToString());
+  DisplayMode mode;
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(60.0f, mode.refresh_rate);
 
   // Close the notification (imitates clicking [x] button). Also verifies if
   // this does not cause a crash.  See crbug.com/271784
@@ -232,7 +287,7 @@ TEST_F(ResolutionNotificationControllerTest, Timeout) {
   if (!SupportsMultipleDisplays())
     return;
 
-  UpdateDisplay("300x300#300x300|200x200");
+  UpdateDisplay("300x300#300x300%59|200x200%60");
   const gfx::Display& display = ash::Shell::GetScreen()->GetPrimaryDisplay();
   SetDisplayResolutionAndNotify(display, gfx::Size(200, 200));
 
@@ -244,71 +299,115 @@ TEST_F(ResolutionNotificationControllerTest, Timeout) {
   }
   EXPECT_FALSE(IsNotificationVisible());
   EXPECT_EQ(0, accept_count());
-  gfx::Size resolution;
-  ash::internal::DisplayManager* display_manager =
+  ash::DisplayManager* display_manager =
       ash::Shell::GetInstance()->display_manager();
-  EXPECT_FALSE(display_manager->GetSelectedResolutionForDisplayId(
-      display.id(), &resolution));
+  DisplayMode mode;
+  EXPECT_TRUE(
+      display_manager->GetSelectedModeForDisplayId(display.id(), &mode));
+  EXPECT_EQ("300x300", mode.size.ToString());
+  EXPECT_EQ(59.0f, mode.refresh_rate);
 }
 
 TEST_F(ResolutionNotificationControllerTest, DisplayDisconnected) {
   if (!SupportsMultipleDisplays())
     return;
 
-  UpdateDisplay("300x300#300x300|200x200,200x200#250x250|200x200|100x100");
-  int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id();
-  ash::internal::DisplayManager* display_manager =
+  UpdateDisplay("300x300#300x300%56|200x200%57,"
+                "200x200#250x250%58|200x200%59|100x100%60");
+  int64 id2 = ash::ScreenUtil::GetSecondaryDisplay().id();
+  ash::DisplayManager* display_manager =
       ash::Shell::GetInstance()->display_manager();
   SetDisplayResolutionAndNotify(
-      ScreenAsh::GetSecondaryDisplay(), gfx::Size(100, 100));
+      ScreenUtil::GetSecondaryDisplay(), gfx::Size(100, 100));
   ASSERT_TRUE(IsNotificationVisible());
 
   // Disconnects the secondary display and verifies it doesn't cause crashes.
-  UpdateDisplay("300x300#300x300|200x200");
+  UpdateDisplay("300x300#300x300%56|200x200%57");
   RunAllPendingInMessageLoop();
   EXPECT_FALSE(IsNotificationVisible());
   EXPECT_EQ(0, accept_count());
+  DisplayMode mode;
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
   gfx::Size resolution;
-  EXPECT_TRUE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
-  EXPECT_EQ("200x200", resolution.ToString());
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(59.0f, mode.refresh_rate);
 }
 
 TEST_F(ResolutionNotificationControllerTest, MultipleResolutionChange) {
   if (!SupportsMultipleDisplays())
     return;
 
-  UpdateDisplay("300x300#300x300|200x200,250x250#250x250|200x200");
-  int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id();
-  ash::internal::DisplayManager* display_manager =
+  UpdateDisplay("300x300#300x300%56|200x200%57,"
+                "250x250#250x250%58|200x200%59");
+  int64 id2 = ash::ScreenUtil::GetSecondaryDisplay().id();
+  ash::DisplayManager* display_manager =
       ash::Shell::GetInstance()->display_manager();
 
   SetDisplayResolutionAndNotify(
-      ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200));
+      ScreenUtil::GetSecondaryDisplay(), gfx::Size(200, 200));
   EXPECT_TRUE(IsNotificationVisible());
   EXPECT_FALSE(controller()->DoesNotificationTimeout());
-  gfx::Size resolution;
-  EXPECT_TRUE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
-  EXPECT_EQ("200x200", resolution.ToString());
+  DisplayMode mode;
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(59.0f, mode.refresh_rate);
 
   // Invokes SetDisplayResolutionAndNotify during the previous notification is
   // visible.
   SetDisplayResolutionAndNotify(
-      ScreenAsh::GetSecondaryDisplay(), gfx::Size(250, 250));
-  EXPECT_FALSE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
+      ScreenUtil::GetSecondaryDisplay(), gfx::Size(250, 250));
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("250x250", mode.size.ToString());
+  EXPECT_EQ(58.0f, mode.refresh_rate);
 
   // Then, click the revert button. Although |old_resolution| for the second
   // SetDisplayResolutionAndNotify is 200x200, it should revert to the original
-  // size 150x150.
+  // size 250x250.
+  ClickOnNotificationButton(0);
+  RunAllPendingInMessageLoop();
+  EXPECT_FALSE(IsNotificationVisible());
+  EXPECT_EQ(0, accept_count());
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("250x250", mode.size.ToString());
+  EXPECT_EQ(58.0f, mode.refresh_rate);
+}
+
+TEST_F(ResolutionNotificationControllerTest, Fallback) {
+  if (!SupportsMultipleDisplays())
+    return;
+
+  UpdateDisplay("300x300#300x300%56|200x200%57,"
+                "250x250#250x250%58|220x220%59|200x200%60");
+  int64 id2 = ash::ScreenUtil::GetSecondaryDisplay().id();
+  ash::DisplayManager* display_manager =
+      ash::Shell::GetInstance()->display_manager();
+  ASSERT_EQ(0, accept_count());
+  EXPECT_FALSE(IsNotificationVisible());
+
+  // Changes the resolution and apply the result.
+  SetDisplayResolutionAndNotifyWithResolution(
+      ScreenUtil::GetSecondaryDisplay(),
+      gfx::Size(220, 220),
+      gfx::Size(200, 200));
+  EXPECT_TRUE(IsNotificationVisible());
+  EXPECT_FALSE(controller()->DoesNotificationTimeout());
+  EXPECT_EQ(
+      ExpectedFallbackNotificationMessage(
+          id2, gfx::Size(220, 220), gfx::Size(200, 200)),
+      GetNotificationMessage());
+  DisplayMode mode;
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("200x200", mode.size.ToString());
+  EXPECT_EQ(60.0f, mode.refresh_rate);
+
+  // Click the revert button, which reverts to the best resolution.
   ClickOnNotificationButton(0);
   RunAllPendingInMessageLoop();
   EXPECT_FALSE(IsNotificationVisible());
   EXPECT_EQ(0, accept_count());
-  EXPECT_FALSE(
-      display_manager->GetSelectedResolutionForDisplayId(id2, &resolution));
+  EXPECT_TRUE(display_manager->GetSelectedModeForDisplayId(id2, &mode));
+  EXPECT_EQ("250x250", mode.size.ToString());
+  EXPECT_EQ(58.0f, mode.refresh_rate);
 }
 
-}  // namespace internal
 }  // namespace ash