TEST_F(RenderWidgetSignalsTest, MultipleRenderWidgetsBecomeHiddenThenVisible) {
  IgnoreWidgetCreationCallbacks();
  std::unique_ptr<RenderWidgetSchedulingState> widget1_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  std::unique_ptr<RenderWidgetSchedulingState> widget2_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  std::unique_ptr<RenderWidgetSchedulingState> widget3_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  // Widgets are initially assumed to be visible so start hiding them, we should
  // not get any calls to SetAllRenderWidgetsHidden till the last one is hidden.
  EXPECT_CALL(*mock_observer_, SetAllRenderWidgetsHidden(_)).Times(0);
  widget1_state->SetHidden(true);
  widget2_state->SetHidden(true);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  EXPECT_CALL(*mock_observer_, SetAllRenderWidgetsHidden(true)).Times(1);
  widget3_state->SetHidden(true);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  // We should get a call back once the first widget is unhidden and no more
  // after that.
  EXPECT_CALL(*mock_observer_, SetAllRenderWidgetsHidden(false)).Times(1);
  widget1_state->SetHidden(false);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  EXPECT_CALL(*mock_observer_, SetAllRenderWidgetsHidden(_)).Times(0);
  widget2_state->SetHidden(false);
  widget3_state->SetHidden(false);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  IgnoreWidgetDestructionCallbacks();
}
TEST_F(RenderWidgetSignalsTest, RenderWidget_Hidden) {
  IgnoreWidgetCreationCallbacks();
  std::unique_ptr<RenderWidgetSchedulingState> widget1_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  EXPECT_CALL(*mock_observer_, SetAllRenderWidgetsHidden(true)).Times(1);
  widget1_state->SetHidden(true);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  IgnoreWidgetDestructionCallbacks();
}
TEST_F(RenderWidgetSignalsTest,
       TouchHandlerAddedThenWigetDeleted_HiddenWidget) {
  IgnoreWidgetCreationCallbacks();

  std::unique_ptr<RenderWidgetSchedulingState> widget_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  EXPECT_CALL(*mock_observer_, SetAllRenderWidgetsHidden(true)).Times(1);
  widget_state->SetHidden(true);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  EXPECT_CALL(*mock_observer_, SetHasVisibleRenderWidgetWithTouchHandler(_))
      .Times(0);
  IgnoreWidgetDestructionCallbacks();
}
TEST_F(RenderWidgetSignalsTest, TouchHandlerAddedAndRemoved_VisibleWidget) {
    IgnoreWidgetCreationCallbacks();

    scoped_ptr<RenderWidgetSchedulingState> widget_state =
        render_widget_signals_->NewRenderWidgetSchedulingState();
    Mock::VerifyAndClearExpectations(mock_observer_.get());

    EXPECT_CALL(*mock_observer_, SetHasVisibleRenderWidgetWithTouchHandler(true))
    .Times(1);
    widget_state->SetHasTouchHandler(true);
    Mock::VerifyAndClearExpectations(mock_observer_.get());

    EXPECT_CALL(*mock_observer_, SetHasVisibleRenderWidgetWithTouchHandler(false))
    .Times(1);
    widget_state->SetHasTouchHandler(false);
    Mock::VerifyAndClearExpectations(mock_observer_.get());

    IgnoreWidgetDestructionCallbacks();
}
TEST_F(RenderWidgetSignalsTest,
       MultipleTouchHandlerAddedAndRemoved_VisibleWidgets) {
  IgnoreWidgetCreationCallbacks();

  std::unique_ptr<RenderWidgetSchedulingState> widget1_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  std::unique_ptr<RenderWidgetSchedulingState> widget2_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  std::unique_ptr<RenderWidgetSchedulingState> widget3_state =
      render_widget_signals_->NewRenderWidgetSchedulingState();
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  // We should only get a callback for the first widget with a touch handler.
  EXPECT_CALL(*mock_observer_, SetHasVisibleRenderWidgetWithTouchHandler(true))
      .Times(1);
  widget1_state->SetHasTouchHandler(true);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  EXPECT_CALL(*mock_observer_, SetHasVisibleRenderWidgetWithTouchHandler(_))
      .Times(0);
  widget2_state->SetHasTouchHandler(true);
  widget3_state->SetHasTouchHandler(true);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  // We should only get a callback when the last touch handler is removed.
  EXPECT_CALL(*mock_observer_, SetHasVisibleRenderWidgetWithTouchHandler(_))
      .Times(0);
  widget1_state->SetHasTouchHandler(false);
  widget2_state->SetHasTouchHandler(false);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  EXPECT_CALL(*mock_observer_, SetHasVisibleRenderWidgetWithTouchHandler(false))
      .Times(1);
  widget3_state->SetHasTouchHandler(false);
  Mock::VerifyAndClearExpectations(mock_observer_.get());

  IgnoreWidgetDestructionCallbacks();
}