1// Copyright (c) 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 "ash/wm/partial_screenshot_view.h"
6
7#include "ash/screenshot_delegate.h"
8#include "ash/shell.h"
9#include "ash/shell_window_ids.h"
10#include "ash/test/ash_test_base.h"
11#include "ash/test/test_overlay_delegate.h"
12#include "ash/test/test_screenshot_delegate.h"
13#include "ui/aura/window_event_dispatcher.h"
14#include "ui/events/test/event_generator.h"
15#include "ui/views/widget/widget.h"
16#include "ui/views/widget/widget_observer.h"
17
18namespace ash {
19
20class PartialScreenshotViewTest : public test::AshTestBase,
21                                  public views::WidgetObserver {
22 public:
23  PartialScreenshotViewTest() : view_(NULL) {}
24  virtual ~PartialScreenshotViewTest() {
25    if (view_)
26      view_->GetWidget()->RemoveObserver(this);
27  }
28
29  void StartPartialScreenshot() {
30    std::vector<PartialScreenshotView*> views =
31        PartialScreenshotView::StartPartialScreenshot(GetScreenshotDelegate());
32    if (!views.empty()) {
33      view_ = views[0];
34      view_->GetWidget()->AddObserver(this);
35    }
36  }
37
38 protected:
39  PartialScreenshotView* view_;
40
41 private:
42  // views::WidgetObserver:
43  virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE {
44    if (view_ && view_->GetWidget() == widget)
45      view_ = NULL;
46    widget->RemoveObserver(this);
47  }
48
49  DISALLOW_COPY_AND_ASSIGN(PartialScreenshotViewTest);
50};
51
52TEST_F(PartialScreenshotViewTest, BasicMouse) {
53  StartPartialScreenshot();
54  ASSERT_TRUE(view_);
55
56  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
57
58  generator.MoveMouseTo(100, 100);
59  generator.PressLeftButton();
60  EXPECT_FALSE(view_->is_dragging_);
61  EXPECT_EQ("100,100", view_->start_position_.ToString());
62
63  generator.MoveMouseTo(200, 200);
64  EXPECT_TRUE(view_->is_dragging_);
65  EXPECT_EQ("200,200", view_->current_position_.ToString());
66
67  generator.ReleaseLeftButton();
68  EXPECT_FALSE(view_->is_dragging_);
69  EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
70  EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
71}
72
73TEST_F(PartialScreenshotViewTest, BasicTouch) {
74  StartPartialScreenshot();
75  ASSERT_TRUE(view_);
76
77  ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
78
79  generator.set_current_location(gfx::Point(100,100));
80  generator.GestureTapDownAndUp(gfx::Point(100,100));
81  EXPECT_FALSE(view_->is_dragging_);
82  EXPECT_EQ(0, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
83
84  generator.PressTouch();
85  EXPECT_FALSE(view_->is_dragging_);
86  EXPECT_EQ("100,100", view_->start_position_.ToString());
87
88  generator.MoveTouch(gfx::Point(200, 200));
89  EXPECT_TRUE(view_->is_dragging_);
90  EXPECT_EQ("200,200", view_->current_position_.ToString());
91
92  generator.ReleaseTouch();
93  EXPECT_FALSE(view_->is_dragging_);
94  EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count());
95  EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString());
96}
97
98// Partial screenshot session should not start when there is already another
99// overlay. See: http://crbug.com/341958
100TEST_F(PartialScreenshotViewTest, DontStartOverOverlay) {
101  OverlayEventFilter* overlay_filter = Shell::GetInstance()->overlay_filter();
102  test::TestOverlayDelegate delegate;
103  overlay_filter->Activate(&delegate);
104  EXPECT_EQ(&delegate, overlay_filter->delegate_);
105
106  StartPartialScreenshot();
107  EXPECT_TRUE(view_ == NULL);
108  EXPECT_EQ(&delegate, overlay_filter->delegate_);
109  overlay_filter->Deactivate(&delegate);
110
111  StartPartialScreenshot();
112  EXPECT_TRUE(view_ != NULL);
113
114  // Someone else attempts to activate the overlay session, which should cancel
115  // the current partial screenshot session.
116  overlay_filter->Activate(&delegate);
117  RunAllPendingInMessageLoop();
118  EXPECT_EQ(&delegate, overlay_filter->delegate_);
119  EXPECT_TRUE(view_ == NULL);
120}
121
122}  // namespace ash
123