Home | History | Annotate | Download | only in widget
      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 
      5 #include "ui/views/test/views_test_base.h"
      6 #include "ui/views/widget/root_view_test_helper.h"
      7 
      8 namespace views {
      9 namespace test {
     10 
     11 typedef ViewsTestBase RootViewTest;
     12 
     13 class DeleteOnKeyEventView : public View {
     14  public:
     15   explicit DeleteOnKeyEventView(bool* set_on_key) : set_on_key_(set_on_key) {}
     16   virtual ~DeleteOnKeyEventView() {}
     17 
     18   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE {
     19     *set_on_key_ = true;
     20     delete this;
     21     return true;
     22   }
     23 
     24  private:
     25   // Set to true in OnKeyPressed().
     26   bool* set_on_key_;
     27 
     28   DISALLOW_COPY_AND_ASSIGN(DeleteOnKeyEventView);
     29 };
     30 
     31 // Verifies deleting a View in OnKeyPressed() doesn't crash.
     32 TEST_F(RootViewTest, DeleteViewDuringKeyEventDispatch) {
     33   Widget widget;
     34   Widget::InitParams init_params =
     35       CreateParams(Widget::InitParams::TYPE_POPUP);
     36   init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
     37   widget.Init(init_params);
     38 
     39   bool got_key_event = false;
     40 
     41   View* content = new View;
     42   widget.SetContentsView(content);
     43 
     44   View* child = new DeleteOnKeyEventView(&got_key_event);
     45   content->AddChildView(child);
     46 
     47   ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_ESCAPE, 0, false);
     48   RootViewTestHelper test_helper(
     49       static_cast<internal::RootView*>(widget.GetRootView()));
     50   test_helper.DispatchKeyEventStartAt(child, &key_event);
     51   EXPECT_TRUE(got_key_event);
     52 }
     53 
     54 }  // namespace test
     55 }  // namespace views
     56