Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: ui/views/view_unittest.cc

Issue 108063004: Give up focus if the focused view becomes unfocusable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Extracted to common code into FocusManager class Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <map> 5 #include <map>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/rand_util.h" 8 #include "base/rand_util.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 2752 matching lines...) Expand 10 before | Expand all | Expand 10 after
2763 2763
2764 View* child = new View(); 2764 View* child = new View();
2765 root.AddChildView(child); 2765 root.AddChildView(child);
2766 2766
2767 View* foo1 = new View(); 2767 View* foo1 = new View();
2768 child->AddChildView(foo1); 2768 child->AddChildView(foo1);
2769 View* foo2 = new View(); 2769 View* foo2 = new View();
2770 child->AddChildView(foo2); 2770 child->AddChildView(foo2);
2771 View* foo3 = new View(); 2771 View* foo3 = new View();
2772 child->AddChildView(foo3); 2772 child->AddChildView(foo3);
2773 foo1->set_focusable(true); 2773 foo1->SetFocusable(true);
2774 foo2->set_focusable(true); 2774 foo2->SetFocusable(true);
2775 foo3->set_focusable(true); 2775 foo3->SetFocusable(true);
2776 2776
2777 ASSERT_EQ(0, child->GetIndexOf(foo1)); 2777 ASSERT_EQ(0, child->GetIndexOf(foo1));
2778 ASSERT_EQ(1, child->GetIndexOf(foo2)); 2778 ASSERT_EQ(1, child->GetIndexOf(foo2));
2779 ASSERT_EQ(2, child->GetIndexOf(foo3)); 2779 ASSERT_EQ(2, child->GetIndexOf(foo3));
2780 ASSERT_EQ(foo2, foo1->GetNextFocusableView()); 2780 ASSERT_EQ(foo2, foo1->GetNextFocusableView());
2781 ASSERT_EQ(foo3, foo2->GetNextFocusableView()); 2781 ASSERT_EQ(foo3, foo2->GetNextFocusableView());
2782 ASSERT_EQ(NULL, foo3->GetNextFocusableView()); 2782 ASSERT_EQ(NULL, foo3->GetNextFocusableView());
2783 2783
2784 // Move |foo2| at the end. 2784 // Move |foo2| at the end.
2785 child->ReorderChildView(foo2, -1); 2785 child->ReorderChildView(foo2, -1);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
2890 // Check that calling |AddChildView()| does not change the order. 2890 // Check that calling |AddChildView()| does not change the order.
2891 v1.AddChildView(&v2); 2891 v1.AddChildView(&v2);
2892 EXPECT_EQ(0, v1.GetIndexOf(&v2)); 2892 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2893 EXPECT_EQ(1, v1.GetIndexOf(&v3)); 2893 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2894 v1.AddChildView(&v3); 2894 v1.AddChildView(&v3);
2895 EXPECT_EQ(0, v1.GetIndexOf(&v2)); 2895 EXPECT_EQ(0, v1.GetIndexOf(&v2));
2896 EXPECT_EQ(1, v1.GetIndexOf(&v3)); 2896 EXPECT_EQ(1, v1.GetIndexOf(&v3));
2897 } 2897 }
2898 2898
2899 //////////////////////////////////////////////////////////////////////////////// 2899 ////////////////////////////////////////////////////////////////////////////////
2900 // FocusManager
2901 ////////////////////////////////////////////////////////////////////////////////
2902
2903 TEST_F(ViewTest, ClearFocusIfUnfocusable) {
2904 // Create a View and focus it.
2905 View* view = new View();
2906 view->SetFocusable(true);
2907
2908 scoped_ptr<Widget> widget(new Widget());
2909 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
2910 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
2911 widget->Init(params);
2912 widget->GetRootView()->AddChildView(view);
2913
2914 FocusManager* focus_manager = widget->GetFocusManager();
2915 ASSERT_TRUE(focus_manager);
2916
2917 focus_manager->SetFocusedView(view);
2918 EXPECT_EQ(view, focus_manager->GetFocusedView());
2919
2920 // Disable the focused view and check if it loses focus.
2921 view->SetEnabled(false);
2922 EXPECT_EQ(NULL, focus_manager->GetFocusedView());
2923
2924 // Re-enable and re-focus.
2925 view->SetEnabled(true);
2926 focus_manager->SetFocusedView(view);
2927 EXPECT_EQ(view, focus_manager->GetFocusedView());
2928
2929 // Hide the focused view and check it it loses focus.
2930 view->SetVisible(false);
2931 EXPECT_EQ(NULL, focus_manager->GetFocusedView());
2932
2933 // Re-show and re-focus.
2934 view->SetVisible(true);
2935 focus_manager->SetFocusedView(view);
2936 EXPECT_EQ(view, focus_manager->GetFocusedView());
2937
2938 // Set as not focusable and check if it loses focus.
2939 view->SetFocusable(false);
2940 EXPECT_EQ(NULL, focus_manager->GetFocusedView());
2941 }
2942
2943 ////////////////////////////////////////////////////////////////////////////////
2900 // Layers 2944 // Layers
2901 //////////////////////////////////////////////////////////////////////////////// 2945 ////////////////////////////////////////////////////////////////////////////////
2902 2946
2903 #if defined(USE_AURA) 2947 #if defined(USE_AURA)
2904 2948
2905 namespace { 2949 namespace {
2906 2950
2907 // Test implementation of LayerAnimator. 2951 // Test implementation of LayerAnimator.
2908 class TestLayerAnimator : public ui::LayerAnimator { 2952 class TestLayerAnimator : public ui::LayerAnimator {
2909 public: 2953 public:
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
3482 const std::vector<ui::Layer*>& child_layers_post = root_layer->children(); 3526 const std::vector<ui::Layer*>& child_layers_post = root_layer->children();
3483 ASSERT_EQ(3u, child_layers_post.size()); 3527 ASSERT_EQ(3u, child_layers_post.size());
3484 EXPECT_EQ(v1->layer(), child_layers_post[0]); 3528 EXPECT_EQ(v1->layer(), child_layers_post[0]);
3485 EXPECT_EQ(v2->layer(), child_layers_post[1]); 3529 EXPECT_EQ(v2->layer(), child_layers_post[1]);
3486 EXPECT_EQ(v1_old_layer, child_layers_post[2]); 3530 EXPECT_EQ(v1_old_layer, child_layers_post[2]);
3487 } 3531 }
3488 3532
3489 #endif // USE_AURA 3533 #endif // USE_AURA
3490 3534
3491 } // namespace views 3535 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698