/* * nextpnr -- Next Generation Place and Route * * Copyright (C) 2018 Miodrag Milanovic * Copyright (C) 2018 Serge Bazanski * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * */ #include #include #include #include #include #include #include #include #include #include #include "designwidget.h" #include "fpgaviewwidget.h" #include "json_frontend.h" #include "jsonwrite.h" #include "log.h" #include "mainwindow.h" #include "pythontab.h" #include "version.h" static void initBasenameResource() { Q_INIT_RESOURCE(base); } NEXTPNR_NAMESPACE_BEGIN BaseMainWindow::BaseMainWindow(std::unique_ptr context, CommandHandler *handler, QWidget *parent) : QMainWindow(parent), handler(handler), ctx(std::move(context)), timing_driven(false) { initBasenameResource(); qRegisterMetaType(); log_streams.clear(); setObjectName("BaseMainWindow"); resize(1024, 768); task = new TaskManager(); // Create and deploy widgets on main screen QWidget *centralWidget = new QWidget(this); QGridLayout *gridLayout = new QGridLayout(centralWidget); gridLayout->setSpacing(6); gridLayout->setContentsMargins(11, 11, 11, 11); QSplitter *splitter_h = new QSplitter(Qt::Horizontal, centralWidget); QSplitter *splitter_v = new QSplitter(Qt::Vertical, splitter_h); splitter_h->addWidget(splitter_v); gridLayout->addWidget(splitter_h, 0, 0, 1, 1); setCentralWidget(centralWidget); designview = new DesignWidget(); designview->setMinimumWidth(300); splitter_h->addWidget(designview); tabWidget = new QTabWidget(); console = new PythonTab(); tabWidget->addTab(console, "Console"); centralTabWidget = new QTabWidget(); centralTabWidget->setTabsClosable(true); fpgaView = new FPGAViewWidget(); centralTabWidget->addTab(fpgaView, "Device"); centralTabWidget->tabBar()->setTabButton(0, QTabBar::RightSide, 0); centralTabWidget->tabBar()->setTabButton(0, QTabBar::LeftSide, 0); splitter_v->addWidget(centralTabWidget); splitter_v->addWidget(tabWidget); // Connect Worker connect(task, &TaskManager::log, this, &BaseMainWindow::writeInfo); connect(task, &TaskManager::pack_finished, this, &BaseMainWindow::pack_finished); connect(task, &TaskManager::budget_finish, this, &BaseMainWindow::budget_finish); connect(task, &TaskManager::place_finished, this, &BaseMainWindow::place_finished); connect(task, &TaskManager::route_finished, this, &BaseMainWindow::route_finished); connect(task, &TaskManager::taskCanceled, this, &BaseMainWindow::taskCanceled); connect(task, &TaskManager::taskStarted, this, &BaseMainWindow::taskStarted); connect(task, &TaskManager::taskPaused, this, &BaseMainWindow::taskPaused); // Events for context change connect(this, &BaseMainWindow::contextChanged, task, &TaskManager::contextChanged); connect(this, &BaseMainWindow::contextChanged, console, &PythonTab::newContext); connect(this, &BaseMainWindow::contextChanged, fpgaView, &FPGAViewWidget::newContext); connect(this, &BaseMainWindow::contextChanged, designview, &DesignWidget::newContext); // Catch close tab events connect(centralTabWidget, &QTabWidget::tabCloseRequested, this, &BaseMainWindow::closeTab); // Propagate events from design view to device view connect(designview, &DesignWidget::selected, fpgaView, &FPGAViewWidget::onSelectedArchItem); connect(designview, &DesignWidget::zoomSelected, fpgaView, &FPGAViewWidget::zoomSelected); connect(designview, &DesignWidget::highlight, fpgaView, &FPGAViewWidget::onHighlightGroupChanged); connect(designview, &DesignWidget::hover, fpgaView, &FPGAViewWidget::onHoverItemChanged); // Click event on device view connect(fpgaView, &FPGAViewWidget::clickedBel, designview, &DesignWidget::onClickedBel); connect(fpgaView, &FPGAViewWidget::clickedWire, designview, &DesignWidget::onClickedWire); connect(fpgaView, &FPGAViewWidget::clickedPip, designview, &DesignWidget::onClickedPip); // Update tree event connect(this, &BaseMainWindow::updateTreeView, designview, &DesignWidget::updateTree); createMenusAndBars(); } BaseMainWindow::~BaseMainWindow() { delete task; } void BaseMainWindow::closeTab(int index) { delete centralTabWidget->widget(index); } void BaseMainWindow::writeInfo(std::string text) { console->info(text); } void BaseMainWindow::about() { QString msg; QTextStream out(&msg); out << "nextpnr-" << NPNR_STRINGIFY_MACRO(ARCHNAME) << "\n"; out << "Version " << GIT_DESCRIBE_STR; QMessageBox::information(this, "About nextpnr", msg); } void BaseMainWindow::createMenusAndBars() { // File menu / project toolbar actions QAction *actionExit = new QAction("Exit", this); actionExit->setIcon(QIcon(":/icons/resources/exit.png")); actionExit->setShortcuts(QKeySequence::Quit); actionExit->setStatusTip("Exit the application"); connect(actionExit, &QAction::triggered, this, &BaseMainWindow::close); // Help menu actions QAction *actionAbout = new QAction("About", this); connect(actionAbout, &QAction::triggered, this, &BaseMainWindow::about); // Gile menu options actionNew = new QAction("New", this); actionNew->setIcon(QIcon(":/icons/resources/new.png")); actionNew->setShortcuts(QKeySequence::New); actionNew->setStatusTip("New project"); connect(actionNew, &QAction::triggered, this, &BaseMainWindow::new_proj); actionLoadJSON = new QAction("Open JSON", this); actionLoadJSON->setIcon(QIcon(":/icons/resources/open_json.png")); actionLoadJSON->setStatusTip("Open an existing JSON file"); actionLoadJSON->setEnabled(true); connect(actionLoadJSON, &QAction::triggered, this, &BaseMainWindow::open_json); actionSaveJSON = new QAction("Save JSON", this); actionSaveJSON->setIcon(QIcon(":/icons/resources/save_json.png")); actionSaveJSON->setStatusTip("Write to JSON file"); actionSaveJSON->setEnabled(true); connect(actionSaveJSON, &QAction::triggered, this, &BaseMainWindow::save_json); // Design menu options actionPack = new QAction("Pack", this); actionPack->setIcon(QIcon(":/icons/resources/pack.png")); actionPack->setStatusTip("Pack current design"); actionPack->setEnabled(false); connect(actionPack, &QAction::triggered, task, &TaskManager::pack); actionAssignBudget = new QAction("Assign Budget", this); actionAssignBudget->setIcon(QIcon(":/icons/resources/time_add.png")); actionAssignBudget->setStatusTip("Assign timing budget for current design"); actionAssignBudget->setEnabled(false); connect(actionAssignBudget, &QAction::triggered, this, &BaseMainWindow::budget); actionPlace = new QAction("Place", this); actionPlace->setIcon(QIcon(":/icons/resources/place.png")); actionPlace->setStatusTip("Place current design"); actionPlace->setEnabled(false); connect(actionPlace, &QAction::triggered, this, &BaseMainWindow::place); actionRoute = new QAction("Route", this); actionRoute->setIcon(QIcon(":/icons/resources/route.png")); actionRoute->setStatusTip("Route current design"); actionRoute->setEnabled(false); connect(actionRoute, &QAction::triggered, task, &TaskManager::route); actionExecutePy = new QAction("Execute Python", this); actionExecutePy->setIcon(QIcon(":/icons/resources/py.png")); actionExecutePy->setStatusTip("Execute Python script"); actionExecutePy->setEnabled(true); connect(actionExecutePy, &QAction::triggered, this, &BaseMainWindow::execute_python); // Worker control toolbar actions actionPlay = new QAction("Play", this); actionPlay->setIcon(QIcon(":/icons/resources/control_play.png")); actionPlay->setStatusTip("Continue running task"); actionPlay->setEnabled(false); connect(actionPlay, &QAction::triggered, task, &TaskManager::continue_thread); actionPause = new QAction("Pause", this); actionPause->setIcon(QIcon(":/icons/resources/control_pause.png")); actionPause->setStatusTip("Pause running task"); actionPause->setEnabled(false); connect(actionPause, &QAction::triggered, task, &TaskManager::pause_thread); actionStop = new QAction("Stop", this); actionStop->setIcon(QIcon(":/icons/resources/control_stop.png")); actionStop->setStatusTip("Stop running task"); actionStop->setEnabled(false); connect(actionStop, &QAction::triggered, task, &TaskManager::terminate_thread); // Device view control toolbar actions QAction *actionZoomIn = new QAction("Zoom In", this); actionZoomIn->setIcon(QIcon(":/icons/resources/zoom_in.png")); connect(actionZoomIn, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomIn); QAction *actionZoomOut = new QAction("Zoom Out", this); actionZoomOut->setIcon(QIcon(":/icons/resources/zoom_out.png")); connect(actionZoomOut, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomOut); QAction *actionZoomSelected = new QAction("Zoom Selected", this); actionZoomSelected->setIcon(QIcon(":/icons/resources/shape_handles.png")); connect(actionZoomSelected, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomSelected); QAction *actionZoomOutbound = new QAction("Zoom Outbound", this); actionZoomOutbound->setIcon(QIcon(":/icons/resources/shape_square.png")); connect(actionZoomOutbound, &QAction::triggered, fpgaView, &FPGAViewWidget::zoomOutbound); actionDisplayBel = new QAction("Enable/Disable Bels", this); actionDisplayBel->setIcon(QIcon(":/icons/resources/bel.png")); actionDisplayBel->setCheckable(true); actionDisplayBel->setChecked(true); connect(actionDisplayBel, &QAction::triggered, this, &BaseMainWindow::enableDisableDecals); actionDisplayWire = new QAction("Enable/Disable Wires", this); actionDisplayWire->setIcon(QIcon(":/icons/resources/wire.png")); actionDisplayWire->setCheckable(true); actionDisplayWire->setChecked(true); connect(actionDisplayWire, &QAction::triggered, this, &BaseMainWindow::enableDisableDecals); actionDisplayPip = new QAction("Enable/Disable Pips", this); actionDisplayPip->setIcon(QIcon(":/icons/resources/pip.png")); actionDisplayPip->setCheckable(true); #ifdef ARCH_ECP5 actionDisplayPip->setChecked(false); #else actionDisplayPip->setChecked(true); #endif connect(actionDisplayPip, &QAction::triggered, this, &BaseMainWindow::enableDisableDecals); actionDisplayGroups = new QAction("Enable/Disable Groups", this); actionDisplayGroups->setIcon(QIcon(":/icons/resources/group.png")); actionDisplayGroups->setCheckable(true); actionDisplayGroups->setChecked(true); connect(actionDisplayGroups, &QAction::triggered, this, &BaseMainWindow::enableDisableDecals); actionScreenshot = new QAction("Screenshot", this); actionScreenshot->setIcon(QIcon(":/icons/resources/camera.png")); actionScreenshot->setStatusTip("Taking a screenshot"); connect(actionScreenshot, &QAction::triggered, this, &BaseMainWindow::screenshot); actionMovie = new QAction("Recording", this); actionMovie->setIcon(QIcon(":/icons/resources/film.png")); actionMovie->setStatusTip("Saving a movie"); actionMovie->setCheckable(true); actionMovie->setChecked(false); connect(actionMovie, &QAction::triggered, this, &BaseMainWindow::saveMovie); actionSaveSVG = new QAction("Save SVG", this); actionSaveSVG->setIcon(QIcon(":/icons/resources/save_svg.png")); actionSaveSVG->setStatusTip("Saving a SVG"); connect(actionSaveSVG, &QAction::triggered, this, &BaseMainWindow::saveSVG); // set initial state fpgaView->enableDisableDecals(actionDisplayBel->isChecked(), actionDisplayWire->isChecked(), actionDisplayPip->isChecked(), actionDisplayGroups->isChecked()); // Add main menu menuBar = new QMenuBar(); menuBar->setGeometry(QRect(0, 0, 1024, 27)); setMenuBar(menuBar); QMenu *menuFile = new QMenu("&File", menuBar); QMenu *menuHelp = new QMenu("&Help", menuBar); menuDesign = new QMenu("&Design", menuBar); menuBar->addAction(menuFile->menuAction()); menuBar->addAction(menuDesign->menuAction()); menuBar->addAction(menuHelp->menuAction()); // Add File menu actions menuFile->addAction(actionNew); menuFile->addAction(actionLoadJSON); menuFile->addAction(actionSaveJSON); menuFile->addSeparator(); menuFile->addAction(actionExit); // Add Design menu actions menuDesign->addAction(actionPack); menuDesign->addAction(actionAssignBudget); menuDesign->addAction(actionPlace); menuDesign->addAction(actionRoute); menuDesign->addSeparator(); menuDesign->addAction(actionExecutePy); // Add Help menu actions menuHelp->addAction(actionAbout); // Main action bar mainActionBar = new QToolBar("Main"); addToolBar(Qt::TopToolBarArea, mainActionBar); mainActionBar->addAction(actionNew); mainActionBar->addAction(actionLoadJSON); mainActionBar->addAction(actionSaveJSON); mainActionBar->addSeparator(); mainActionBar->addAction(actionPack); mainActionBar->addAction(actionAssignBudget); mainActionBar->addAction(actionPlace); mainActionBar->addAction(actionRoute); mainActionBar->addAction(actionExecutePy); // Add worker control toolbar QToolBar *workerControlToolBar = new QToolBar("Worker"); addToolBar(Qt::TopToolBarArea, workerControlToolBar); workerControlToolBar->addAction(actionPlay); workerControlToolBar->addAction(actionPause); workerControlToolBar->addAction(actionStop); // Add device view control toolbar QToolBar *deviceViewToolBar = new QToolBar("Device"); addToolBar(Qt::TopToolBarArea, deviceViewToolBar); deviceViewToolBar->addAction(actionZoomIn); deviceViewToolBar->addAction(actionZoomOut); deviceViewToolBar->addAction(actionZoomSelected); deviceViewToolBar->addAction(actionZoomOutbound); deviceViewToolBar->addSeparator(); deviceViewToolBar->addAction(actionDisplayBel); deviceViewToolBar->addAction(actionDisplayWire); deviceViewToolBar->addAction(actionDisplayPip); deviceViewToolBar->addAction(actionDisplayGroups); deviceViewToolBar->addSeparator(); deviceViewToolBar->addAction(actionScreenshot); deviceViewToolBar->addAction(actionMovie); deviceViewToolBar->addAction(actionSaveSVG); // Add status bar with progress bar statusBar = new QStatusBar(); progressBar = new QProgressBar(statusBar); progressBar->setAlignment(Qt::AlignRight); progressBar->setMaximumSize(180, 19); statusBar->addPermanentWidget(progressBar); progressBar->setValue(0); progressBar->setEnabled(false); setStatusBar(statusBar); } void BaseMainWindow::enableDisableDecals() { fpgaView->enableDisableDecals(actionDisplayBel->isChecked(), actionDisplayWire->isChecked(), actionDisplayPip->isChecked(), actionDisplayGroups->isChecked()); ctx->refreshUi(); } void BaseMainWindow::open_json() { QString fileName = QFileDialog::getOpenFileName(this, QString("Open JSON"), QString(), QString("*.json")); if (!fileName.isEmpty()) { disableActions(); if (ctx->settings.find(ctx->id("synth")) == ctx->settings.end()) { ArchArgs chipArgs = ctx->getArchArgs(); ctx = std::unique_ptr(new Context(chipArgs)); Q_EMIT contextChanged(ctx.get()); } handler->load_json(ctx.get(), fileName.toStdString()); Q_EMIT updateTreeView(); log("Loading design successful.\n"); updateActions(); } } void BaseMainWindow::save_json() { QString fileName = QFileDialog::getSaveFileName(this, QString("Save JSON"), QString(), QString("*.json")); if (!fileName.isEmpty()) { std::string fn = fileName.toStdString(); std::ofstream f(fn); if (write_json_file(f, fn, ctx.get())) log("Saving JSON successful.\n"); else log("Saving JSON failed.\n"); } } void BaseMainWindow::screenshot() { QString fileName = QFileDialog::getSaveFileName(this, QString("Save screenshot"), QString(), QString("*.png")); if (!fileName.isEmpty()) { QImage image = fpgaView->grabFramebuffer(); if (!fileName.endsWith(".png")) fileName += ".png"; QImageWriter imageWriter(fileName, "png"); if (imageWriter.write(image)) log("Saving screenshot successful.\n"); else log("Saving screenshot failed.\n"); } } void BaseMainWindow::saveMovie() { if (actionMovie->isChecked()) { QString dir = QFileDialog::getExistingDirectory(this, tr("Select Movie Directory"), QDir::currentPath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); if (!dir.isEmpty()) { bool ok; int frames = QInputDialog::getInt(this, "Recording", tr("Frames to skip (1 frame = 50ms):"), 5, 0, 1000, 1, &ok); if (ok) { QMessageBox::StandardButton reply = QMessageBox::question(this, "Recording", "Skip identical frames ?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); fpgaView->movieStart(dir, frames, (reply == QMessageBox::Yes)); } else actionMovie->setChecked(false); } else actionMovie->setChecked(false); } else { fpgaView->movieStop(); } } void BaseMainWindow::saveSVG() { QString fileName = QFileDialog::getSaveFileName(this, QString("Save SVG"), QString(), QString("*.svg")); if (!fileName.isEmpty()) { if (!fileName.endsWith(".svg")) fileName += ".svg"; bool ok; QString options = QInputDialog::getText(this, "Save SVG", tr("Save options:"), QLineEdit::Normal, "scale=500", &ok); if (ok) { try { ctx->writeSVG(fileName.toStdString(), options.toStdString()); log("Saving SVG successful.\n"); } catch (const log_execution_error_exception &ex) { log("Saving SVG failed.\n"); } } } } void BaseMainWindow::pack_finished(bool status) { disableActions(); if (status) { log("Packing design successful.\n"); Q_EMIT updateTreeView(); updateActions(); } else { log("Packing design failed.\n"); } } void BaseMainWindow::budget_finish(bool status) { disableActions(); if (status) { log("Assigning timing budget successful.\n"); updateActions(); } else { log("Assigning timing budget failed.\n"); } } void BaseMainWindow::place_finished(bool status) { disableActions(); if (status) { log("Placing design successful.\n"); Q_EMIT updateTreeView(); updateActions(); } else { log("Placing design failed.\n"); } } void BaseMainWindow::route_finished(bool status) { disableActions(); if (status) { log("Routing design successful.\n"); Q_EMIT updateTreeView(); updateActions(); } else log("Routing design failed.\n"); } void BaseMainWindow::taskCanceled() { log("CANCELED\n"); disableActions(); } void BaseMainWindow::taskStarted() { disableActions(); actionPause->setEnabled(true); actionStop->setEnabled(true); } void BaseMainWindow::taskPaused() { disableActions(); actionPlay->setEnabled(true); actionStop->setEnabled(true); } void BaseMainWindow::budget() { bool ok; double freq = QInputDialog::getDouble(this, "Assign timing budget", "Frequency [MHz]:", 50, 0, 250, 2, &ok); if (ok) { freq *= 1e6; timing_driven = true; Q_EMIT task->budget(freq); } } void BaseMainWindow::place() { Q_EMIT task->place(timing_driven); } void BaseMainWindow::disableActions() { actionLoadJSON->setEnabled(true); actionPack->setEnabled(false); actionAssignBudget->setEnabled(false); actionPlace->setEnabled(false); actionRoute->setEnabled(false); actionExecutePy->setEnabled(true); actionPlay->setEnabled(false); actionPause->setEnabled(false); actionStop->setEnabled(false); onDisableActions(); } void BaseMainWindow::updateActions() { if (ctx->settings.find(ctx->id("pack")) == ctx->settings.end()) actionPack->setEnabled(true); else if (ctx->settings.find(ctx->id("place")) == ctx->settings.end()) { actionAssignBudget->setEnabled(true); actionPlace->setEnabled(true); } else if (ctx->settings.find(ctx->id("route")) == ctx->settings.end()) actionRoute->setEnabled(true); onUpdateActions(); } void BaseMainWindow::execute_python() { QString fileName = QFileDialog::getOpenFileName(this, QString("Execute Python"), QString(), QString("*.py")); if (!fileName.isEmpty()) { console->execute_python(fileName.toStdString()); } } void BaseMainWindow::notifyChangeContext() { Q_EMIT contextChanged(ctx.get()); } NEXTPNR_NAMESPACE_END oc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
(**Note:** If you get compiler errors that you don't understand, be sure to consult [Google Mock Doctor](http://code.google.com/p/googlemock/wiki/V1_7_FrequentlyAskedQuestions#How_am_I_supposed_to_make_sense_of_these_horrible_template_error).)

# What Is Google C++ Mocking Framework? #
When you write a prototype or test, often it's not feasible or wise to rely on real objects entirely. A **mock object** implements the same interface as a real object (so it can be used as one), but lets you specify at run time how it will be used and what it should do (which methods will be called? in which order? how many times? with what arguments? what will they return? etc).

**Note:** It is easy to confuse the term _fake objects_ with mock objects. Fakes and mocks actually mean very different things in the Test-Driven Development (TDD) community:

  * **Fake** objects have working implementations, but usually take some shortcut (perhaps to make the operations less expensive), which makes them not suitable for production. An in-memory file system would be an example of a fake.
  * **Mocks** are objects pre-programmed with _expectations_, which form a specification of the calls they are expected to receive.

If all this seems too abstract for you, don't worry - the most important thing to remember is that a mock allows you to check the _interaction_ between itself and code that uses it. The difference between fakes and mocks will become much clearer once you start to use mocks.

**Google C++ Mocking Framework** (or **Google Mock** for short) is a library (sometimes we also call it a "framework" to make it sound cool) for creating mock classes and using them. It does to C++ what [jMock](http://www.jmock.org/) and [EasyMock](http://www.easymock.org/) do to Java.

Using Google Mock involves three basic steps:

  1. Use some simple macros to describe the interface you want to mock, and they will expand to the implementation of your mock class;
  1. Create some mock objects and specify its expectations and behavior using an intuitive syntax;
  1. Exercise code that uses the mock objects. Google Mock will catch any violation of the expectations as soon as it arises.

# Why Google Mock? #
While mock objects help you remove unnecessary dependencies in tests and make them fast and reliable, using mocks manually in C++ is _hard_:

  * Someone has to implement the mocks. The job is usually tedious and error-prone. No wonder people go great distance to avoid it.
  * The quality of those manually written mocks is a bit, uh, unpredictable. You may see some really polished ones, but you may also see some that were hacked up in a hurry and have all sorts of ad hoc restrictions.
  * The knowledge you gained from using one mock doesn't transfer to the next.

In contrast, Java and Python programmers have some fine mock frameworks, which automate the creation of mocks. As a result, mocking is a proven effective technique and widely adopted practice in those communities. Having the right tool absolutely makes the difference.

Google Mock was built to help C++ programmers. It was inspired by [jMock](http://www.jmock.org/) and [EasyMock](http://www.easymock.org/), but designed with C++'s specifics in mind. It is your friend if any of the following problems is bothering you:

  * You are stuck with a sub-optimal design and wish you had done more prototyping before it was too late, but prototyping in C++ is by no means "rapid".
  * Your tests are slow as they depend on too many libraries or use expensive resources (e.g. a database).
  * Your tests are brittle as some resources they use are unreliable (e.g. the network).
  * You want to test how your code handles a failure (e.g. a file checksum error), but it's not easy to cause one.
  * You need to make sure that your module interacts with other modules in the right way, but it's hard to observe the interaction; therefore you resort to observing the side effects at the end of the action, which is awkward at best.
  * You want to "mock out" your dependencies, except that they don't have mock implementations yet; and, frankly, you aren't thrilled by some of those hand-written mocks.

We encourage you to use Google Mock as:

  * a _design_ tool, for it lets you experiment with your interface design early and often. More iterations lead to better designs!
  * a _testing_ tool to cut your tests' outbound dependencies and probe the interaction between your module and its collaborators.

# Getting Started #
Using Google Mock is easy! Inside your C++ source file, just `#include` `"gtest/gtest.h"` and `"gmock/gmock.h"`, and you are ready to go.

# A Case for Mock Turtles #
Let's look at an example. Suppose you are developing a graphics program that relies on a LOGO-like API for drawing. How would you test that it does the right thing? Well, you can run it and compare the screen with a golden screen snapshot, but let's admit it: tests like this are expensive to run and fragile (What if you just upgraded to a shiny new graphics card that has better anti-aliasing? Suddenly you have to update all your golden images.). It would be too painful if all your tests are like this. Fortunately, you learned about Dependency Injection and know the right thing to do: instead of having your application talk to the drawing API directly, wrap the API in an interface (say, `Turtle`) and code to that interface:

```
class Turtle {
  ...
  virtual ~Turtle() {}
  virtual void PenUp() = 0;
  virtual void PenDown() = 0;
  virtual void Forward(int distance) = 0;
  virtual void Turn(int degrees) = 0;
  virtual void GoTo(int x, int y) = 0;
  virtual int GetX() const = 0;
  virtual int GetY() const = 0;
};
```

(Note that the destructor of `Turtle` **must** be virtual, as is the case for **all** classes you intend to inherit from - otherwise the destructor of the derived class will not be called when you delete an object through a base pointer, and you'll get corrupted program states like memory leaks.)

You can control whether the turtle's movement will leave a trace using `PenUp()` and `PenDown()`, and control its movement using `Forward()`, `Turn()`, and `GoTo()`. Finally, `GetX()` and `GetY()` tell you the current position of the turtle.

Your program will normally use a real implementation of this interface. In tests, you can use a mock implementation instead. This allows you to easily check what drawing primitives your program is calling, with what arguments, and in which order. Tests written this way are much more robust (they won't break because your new machine does anti-aliasing differently), easier to read and maintain (the intent of a test is expressed in the code, not in some binary images), and run _much, much faster_.

# Writing the Mock Class #
If you are lucky, the mocks you need to use have already been implemented by some nice people. If, however, you find yourself in the position to write a mock class, relax - Google Mock turns this task into a fun game! (Well, almost.)

## How to Define It ##
Using the `Turtle` interface as example, here are the simple steps you need to follow:

  1. Derive a class `MockTurtle` from `Turtle`.
  1. Take a _virtual_ function of `Turtle` (while it's possible to [mock non-virtual methods using templates](http://code.google.com/p/googlemock/wiki/V1_7_CookBook#Mocking_Nonvirtual_Methods), it's much more involved). Count how many arguments it has.
  1. In the `public:` section of the child class, write `MOCK_METHODn();` (or `MOCK_CONST_METHODn();` if you are mocking a `const` method), where `n` is the number of the arguments; if you counted wrong, shame on you, and a compiler error will tell you so.
  1. Now comes the fun part: you take the function signature, cut-and-paste the _function name_ as the _first_ argument to the macro, and leave what's left as the _second_ argument (in case you're curious, this is the _type of the function_).
  1. Repeat until all virtual functions you want to mock are done.

After the process, you should have something like:

```
#include "gmock/gmock.h"  // Brings in Google Mock.
class MockTurtle : public Turtle {
 public:
  ...
  MOCK_METHOD0(PenUp, void());
  MOCK_METHOD0(PenDown, void());
  MOCK_METHOD1(Forward, void(int distance));
  MOCK_METHOD1(Turn, void(int degrees));
  MOCK_METHOD2(GoTo, void(int x, int y));
  MOCK_CONST_METHOD0(GetX, int());
  MOCK_CONST_METHOD0(GetY, int());
};
```

You don't need to define these mock methods somewhere else - the `MOCK_METHOD*` macros will generate the definitions for you. It's that simple! Once you get the hang of it, you can pump out mock classes faster than your source-control system can handle your check-ins.