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.
5 #include "ui/views/test/views_test_base.h"
6 #include "ui/views/widget/root_view_test_helper.h"
11 typedef ViewsTestBase RootViewTest;
13 class DeleteOnKeyEventView : public View {
15 explicit DeleteOnKeyEventView(bool* set_on_key) : set_on_key_(set_on_key) {}
16 virtual ~DeleteOnKeyEventView() {}
18 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE {
25 // Set to true in OnKeyPressed().
28 DISALLOW_COPY_AND_ASSIGN(DeleteOnKeyEventView);
31 // Verifies deleting a View in OnKeyPressed() doesn't crash.
32 TEST_F(RootViewTest, DeleteViewDuringKeyEventDispatch) {
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);
39 bool got_key_event = false;
41 View* content = new View;
42 widget.SetContentsView(content);
44 View* child = new DeleteOnKeyEventView(&got_key_event);
45 content->AddChildView(child);
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);