Home | History | Annotate | Download | only in controls
      1 // Copyright (c) 2012 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 
      5 #include "ui/views/controls/slider.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/strings/string_util.h"
      9 #include "base/strings/utf_string_conversions.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "ui/base/accessibility/accessible_view_state.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 #include "ui/views/view.h"
     14 
     15 namespace {
     16 
     17 void ClickAt(views::View* view, int x, int y) {
     18   gfx::Point point(x, y);
     19   ui::MouseEvent press(ui::ET_MOUSE_PRESSED, point, point,
     20                        ui::EF_LEFT_MOUSE_BUTTON);
     21   view->OnMousePressed(press);
     22   ui::MouseEvent release(ui::ET_MOUSE_RELEASED, point, point,
     23                          ui::EF_LEFT_MOUSE_BUTTON);
     24   view->OnMouseReleased(release);
     25 }
     26 
     27 }
     28 
     29 namespace views {
     30 
     31 TEST(SliderTest, UpdateFromClickHorizontal) {
     32   scoped_ptr<Slider> slider(new Slider(NULL, Slider::HORIZONTAL));
     33   View* view = slider.get();
     34   gfx::Size size = view->GetPreferredSize();
     35   view->SetSize(size);
     36 
     37   ClickAt(view, 0, 0);
     38   EXPECT_EQ(0.0f, slider->value());
     39 
     40   ClickAt(view, view->width(), 0);
     41   EXPECT_EQ(1.0f, slider->value());
     42 }
     43 
     44 TEST(SliderTest, UpdateFromClickVertical) {
     45   scoped_ptr<Slider> slider(new Slider(NULL, Slider::VERTICAL));
     46   View* view = slider.get();
     47   gfx::Size size = view->GetPreferredSize();
     48   view->SetSize(size);
     49 
     50   ClickAt(view, 0, 0);
     51   EXPECT_EQ(1.0f, slider->value());
     52 
     53   ClickAt(view, 0, view->height());
     54   EXPECT_EQ(0.0f, slider->value());
     55 }
     56 
     57 TEST(SliderTest, UpdateFromClickRTLHorizontal) {
     58   std::string locale = l10n_util::GetApplicationLocale("");
     59   base::i18n::SetICUDefaultLocale("he");
     60 
     61   scoped_ptr<Slider> slider(new Slider(NULL, Slider::HORIZONTAL));
     62   View* view = slider.get();
     63   gfx::Size size = view->GetPreferredSize();
     64   view->SetSize(size);
     65 
     66   ClickAt(view, 0, 0);
     67   EXPECT_EQ(1.0f, slider->value());
     68 
     69   ClickAt(view, view->width(), 0);
     70   EXPECT_EQ(0.0f, slider->value());
     71 
     72   // Reset locale.
     73   base::i18n::SetICUDefaultLocale(locale);
     74 }
     75 
     76 }  // namespace views
     77