// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
// Include this first to avoid type conflicts with CEF headers.
#include "tests/unittests/chromium_includes.h"
#include "include/wrapper/cef_closure_task.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "tests/unittests/test_handler.h"
namespace {
const char kTestURLWithRegions[] = "http://test.com/regions";
const char kTestHTMLWithRegions[] =
    ""
    "  
"
    "    "
    "  "
    "";
const char kTestURLWithoutRegions[] = "http://test.com/no-regions";
const char kTestHTMLWithoutRegions[] =
    "Hello World!";
const char kTestURLWithChangingRegions[] = "http://test.com/changing-regions";
const char kTestHTMLWithChangingRegions[] =
    ""
    "  "
    "    "
    "    "
    "  "
    "";
class DraggableRegionsTestHandler : public TestHandler {
 public:
  DraggableRegionsTestHandler()
    : step_(kStepWithRegions) {}
  void RunTest() override {
    // Add HTML documents with and without draggable regions.
    AddResource(kTestURLWithRegions, kTestHTMLWithRegions, "text/html");
    AddResource(kTestURLWithoutRegions, kTestHTMLWithoutRegions, "text/html");
    AddResource(kTestURLWithChangingRegions, kTestHTMLWithChangingRegions,
                "text/html");
    // Create the browser
    CreateBrowser(kTestURLWithRegions);
    // Time out the test after a reasonable period of time.
    SetTestTimeout();
  }
  void OnDraggableRegionsChanged(
      CefRefPtr browser,
      const std::vector& regions) override {
    EXPECT_TRUE(CefCurrentlyOn(TID_UI));
    EXPECT_TRUE(browser->IsSame(GetBrowser()));
    did_call_on_draggable_regions_changed_.yes();
    switch (step_) {
      case kStepWithRegions:
      case kStepWithChangingRegions1:
        EXPECT_EQ(2U, regions.size());
        EXPECT_EQ(50, regions[0].bounds.x);
        EXPECT_EQ(50, regions[0].bounds.y);
        EXPECT_EQ(200, regions[0].bounds.width);
        EXPECT_EQ(200, regions[0].bounds.height);
        EXPECT_EQ(1, regions[0].draggable);
        EXPECT_EQ(125, regions[1].bounds.x);
        EXPECT_EQ(125, regions[1].bounds.y);
        EXPECT_EQ(50, regions[1].bounds.width);
        EXPECT_EQ(50, regions[1].bounds.height);
        EXPECT_EQ(0, regions[1].draggable);
        break;
      case kStepWithChangingRegions2:
        EXPECT_EQ(2U, regions.size());
        EXPECT_EQ(0, regions[0].bounds.x);
        EXPECT_EQ(0, regions[0].bounds.y);
        EXPECT_EQ(200, regions[0].bounds.width);
        EXPECT_EQ(200, regions[0].bounds.height);
        EXPECT_EQ(1, regions[0].draggable);
        EXPECT_EQ(75, regions[1].bounds.x);
        EXPECT_EQ(75, regions[1].bounds.y);
        EXPECT_EQ(50, regions[1].bounds.width);
        EXPECT_EQ(50, regions[1].bounds.height);
        EXPECT_EQ(0, regions[1].draggable);
        break;
      case kStepWithoutRegions:
        // Should not be reached.
        EXPECT_TRUE(false);
        break;
    }
    NextTest(browser);
  }
  void DestroyTest() override {
    EXPECT_EQ(false, did_call_on_draggable_regions_changed_);
    TestHandler::DestroyTest();
  }
 private:
  void NextTest(CefRefPtr browser) {
    CefRefPtr frame(browser->GetMainFrame());
    did_call_on_draggable_regions_changed_.reset();
    switch (step_) {
      case kStepWithRegions:
        step_ = kStepWithChangingRegions1;
        frame->LoadURL(kTestURLWithChangingRegions);
        break;
      case kStepWithChangingRegions1:
        step_ = kStepWithChangingRegions2;
        break;
      case kStepWithChangingRegions2:
        step_ = kStepWithoutRegions;
        frame->LoadURL(kTestURLWithoutRegions);
        // Needed because this test doesn't call OnDraggableRegionsChanged.
        CefPostDelayedTask(TID_UI,
            base::Bind(&DraggableRegionsTestHandler::DestroyTest, this), 500);
        break;
      case kStepWithoutRegions: {
        // Should not be reached.
        EXPECT_TRUE(false);
        break;
      }
    }
  }
  enum Step {
    kStepWithRegions,
    kStepWithChangingRegions1,
    kStepWithChangingRegions2,
    kStepWithoutRegions,
  } step_;
  TrackCallback did_call_on_draggable_regions_changed_;
};
}  // namespace
// Verify that draggable regions work.
TEST(DraggableRegionsTest, DraggableRegions) {
  CefRefPtr handler =
      new DraggableRegionsTestHandler();
  handler->ExecuteTest();
  ReleaseAndWaitForDestructor(handler);
}