diff options
| -rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | tests/logger_helper.cpp | 99 | ||||
| -rw-r--r-- | tests/logger_helper.h | 50 | ||||
| -rw-r--r-- | tests/test_launcher_controller.cpp | 45 | ||||
| -rw-r--r-- | tests/test_main.cpp | 5 | ||||
| -rw-r--r-- | tests/test_mock_devices.h | 6 | 
6 files changed, 183 insertions, 23 deletions
| diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7f35cdebb..548da6bcc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -205,6 +205,7 @@ if (GTEST_SRC_DIR AND  if (ENABLE_X_SUPPORT)  # Tests that require X  add_executable(test-gtest + logger_helper.cpp  test_main.cpp  test_application_launcher_icon.cpp  test_bfb_launcher_icon.cpp diff --git a/tests/logger_helper.cpp b/tests/logger_helper.cpp new file mode 100644 index 000000000..278b74e33 --- /dev/null +++ b/tests/logger_helper.cpp @@ -0,0 +1,99 @@ +// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- +/* + * Copyright (C) 2012 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authored by: Tim Penhey <tim.penhey@canonical.com> + */ + +#include "logger_helper.h" + +#include <glib.h> + +#include "NuxCore/Logger.h" +#include "NuxCore/LoggingWriter.h" + +namespace unity +{ +namespace helper +{ +namespace +{ + +nux::logging::Level glog_level_to_nux(GLogLevelFlags log_level) +{ + // For some weird reason, ERROR is more critical than CRITICAL in gnome. + if (log_level & G_LOG_LEVEL_ERROR) + return nux::logging::Critical; + if (log_level & G_LOG_LEVEL_CRITICAL) + return nux::logging::Error; + if (log_level & G_LOG_LEVEL_WARNING) + return nux::logging::Warning; + if (log_level & G_LOG_LEVEL_MESSAGE || + log_level & G_LOG_LEVEL_INFO) + return nux::logging::Info; + // default to debug. + return nux::logging::Debug; +} + +void capture_g_log_calls(const gchar* log_domain, + GLogLevelFlags log_level, + const gchar* message, + gpointer user_data) +{ + // If nothing else, all log messages from unity should be identified as such + std::string module("unity"); + if (log_domain) + { + module += std::string(".") + log_domain; + } + nux::logging::Logger logger(module); + nux::logging::Level level = glog_level_to_nux(log_level); + if (level >= logger.GetEffectiveLogLevel()) + { + nux::logging::LogStream(level, logger.module(), "<unknown>", 0).stream() + << message; + } +} + +} + + +CaptureLogOutput::CaptureLogOutput() +{ + nux::logging::Writer::Instance().SetOutputStream(sout_); +} + +CaptureLogOutput::~CaptureLogOutput() +{ + nux::logging::Writer::Instance().SetOutputStream(std::cout); +} + +std::string CaptureLogOutput::GetOutput() +{ + std::string result = sout_.str(); + sout_.str(""); + return result; +} + +void configure_logging(std::string const& env_var) +{ + const char* env = (env_var.empty() ? "UNITY_LOG_SEVERITY" : env_var.c_str()); + nux::logging::configure_logging(::getenv(env)); + g_log_set_default_handler(capture_g_log_calls, NULL); +} + + +} +} diff --git a/tests/logger_helper.h b/tests/logger_helper.h new file mode 100644 index 000000000..812cf85de --- /dev/null +++ b/tests/logger_helper.h @@ -0,0 +1,50 @@ +// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*- +/* + * Copyright (C) 2012 Canonical Ltd + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authored by: Tim Penhey <tim.penhey@canonical.com> + */ + +#ifndef TEST_LOGGER_HELPER_H +#define TEST_LOGGER_HELPER_H + +#include <string> +#include <sstream> + + +namespace unity +{ +namespace helper +{ + +class CaptureLogOutput +{ +public: + CaptureLogOutput(); + ~CaptureLogOutput(); + std::string GetOutput(); +private: + std::ostringstream sout_; +}; + + +void configure_logging(std::string const& env_var = ""); + +} +} + + + +#endif diff --git a/tests/test_launcher_controller.cpp b/tests/test_launcher_controller.cpp index 516125484..cf6cb1c6f 100644 --- a/tests/test_launcher_controller.cpp +++ b/tests/test_launcher_controller.cpp @@ -33,6 +33,7 @@  #include "PanelStyle.h"  #include "UnitySettings.h"  #include "UBusMessages.h" +#include "logger_helper.h"  #include "test_utils.h"  #include "test_uscreen_mock.h"  #include "test_mock_devices.h" @@ -179,8 +180,8 @@ struct MockVolumeLauncherIcon : public VolumeLauncherIcon  typedef nux::ObjectPtr<MockVolumeLauncherIcon> Ptr;  MockVolumeLauncherIcon() - : VolumeLauncherIcon(Volume::Ptr(volume_ = new MockVolume()), - std::make_shared<MockDevicesSettings>()) + : VolumeLauncherIcon(Volume::Ptr(volume_ = new NiceMock<MockVolume>()), + std::make_shared<NiceMock<MockDevicesSettings>>())  , uuid_(std::to_string(g_random_int()))  {  ON_CALL(*volume_, GetIdentifier()).WillByDefault(Return(uuid_)); @@ -201,12 +202,14 @@ namespace launcher  struct TestLauncherController : public testing::Test  {  TestLauncherController() - : xdnd_manager_(std::make_shared<XdndManager>()) + : logger_output_(std::make_shared<helper::CaptureLogOutput>()) + , xdnd_manager_(std::make_shared<XdndManager>())  , lc(xdnd_manager_)  {}  virtual void SetUp()  { + logger_output_->GetOutput(); // discard old output.  lc.multiple_launchers = true;  } @@ -263,6 +266,7 @@ protected:  }  }; + std::shared_ptr<helper::CaptureLogOutput> logger_output_;  MockUScreen uscreen;  Settings settings;  panel::Style panel_style; @@ -655,7 +659,7 @@ TEST_F(TestLauncherController, RegisterIconDevice)  TEST_F(TestLauncherController, RegisteredIconSavesPosition)  { - MockApplicationLauncherIcon::Ptr app_icon(new MockApplicationLauncherIcon(true, "normal-icon.desktop")); + MockApplicationLauncherIcon::Ptr app_icon(new NiceMock<MockApplicationLauncherIcon>(true, "normal-icon.desktop"));  lc.Impl()->RegisterIcon(app_icon);  ASSERT_FALSE(favorite_store.IsFavorite(app_icon->RemoteUri())); @@ -1081,7 +1085,7 @@ TEST_F(TestLauncherController, LauncherAddRequestDeviceAdd)  TEST_F(TestLauncherController, LauncherAddRequestDeviceStick)  {  auto const& model = lc.Impl()->model_; - MockVolumeLauncherIcon::Ptr device_icon(new MockVolumeLauncherIcon()); + MockVolumeLauncherIcon::Ptr device_icon(new NiceMock<MockVolumeLauncherIcon>());  lc.Impl()->RegisterIcon(device_icon, std::numeric_limits<int>::max());  auto app_icons = model->GetSublist<ApplicationLauncherIcon>(); @@ -1105,7 +1109,7 @@ TEST_F(TestLauncherController, LauncherRemoveRequestApplicationUnStickAndQuit)  TEST_F(TestLauncherController, LauncherRemoveRequestDeviceEjects)  { - MockVolumeLauncherIcon::Ptr device_icon(new MockVolumeLauncherIcon()); + MockVolumeLauncherIcon::Ptr device_icon(new NiceMock<MockVolumeLauncherIcon>());  EXPECT_CALL(*(device_icon->volume_), CanBeEjected())  .WillRepeatedly(Return(true)); @@ -1120,7 +1124,7 @@ TEST_F(TestLauncherController, LauncherRemoveRequestDeviceEjects)  TEST_F(TestLauncherController, LauncherRemoveRequestDeviceStops)  { - MockVolumeLauncherIcon::Ptr device_icon(new MockVolumeLauncherIcon()); + MockVolumeLauncherIcon::Ptr device_icon(new NiceMock<MockVolumeLauncherIcon>());  EXPECT_CALL(*(device_icon->volume_), CanBeEjected())  .WillRepeatedly(Return(false)); @@ -1170,15 +1174,15 @@ TEST_F(TestLauncherController, SaveIconsOrder)  lc.DisconnectSignals();  int priority = 0; - MockApplicationLauncherIcon::Ptr sticky_app(new MockApplicationLauncherIcon(true, "sticky-app")); + MockApplicationLauncherIcon::Ptr sticky_app(new NiceMock<MockApplicationLauncherIcon>(true, "sticky-app"));  sticky_app->Stick(false);  lc.Impl()->RegisterIcon(sticky_app, ++priority); - MockApplicationLauncherIcon::Ptr invisible_app(new MockApplicationLauncherIcon(true, "invisible-app")); + MockApplicationLauncherIcon::Ptr invisible_app(new NiceMock<MockApplicationLauncherIcon>(true, "invisible-app"));  invisible_app->SetQuirk(AbstractLauncherIcon::Quirk::VISIBLE, false);  lc.Impl()->RegisterIcon(invisible_app, ++priority); - MockVolumeLauncherIcon::Ptr sticky_device(new MockVolumeLauncherIcon()); + MockVolumeLauncherIcon::Ptr sticky_device(new NiceMock<MockVolumeLauncherIcon>());  sticky_device->Stick(false);  lc.Impl()->RegisterIcon(sticky_device, ++priority); @@ -1205,11 +1209,11 @@ TEST_F(TestLauncherController, SaveIconsOrderWithOnlyStickyIcons)  lc.ClearModel();  int priority = 0; - MockApplicationLauncherIcon::Ptr sticky_app(new MockApplicationLauncherIcon(true, "sticky-app")); + MockApplicationLauncherIcon::Ptr sticky_app(new NiceMock<MockApplicationLauncherIcon>(true, "sticky-app"));  sticky_app->Stick(false);  lc.Impl()->RegisterIcon(sticky_app, ++priority); - MockVolumeLauncherIcon::Ptr sticky_device(new MockVolumeLauncherIcon()); + MockVolumeLauncherIcon::Ptr sticky_device(new NiceMock<MockVolumeLauncherIcon>());  sticky_device->Stick(false);  lc.Impl()->RegisterIcon(sticky_device, ++priority); @@ -1234,11 +1238,11 @@ TEST_F(TestLauncherController, SaveIconsOrderTriesToKeepIconProvidersOrder)  FavoriteStore::URI_PREFIX_APP + "bar.desktop", places::APPS_URI,  FavoriteStore::URI_PREFIX_APP + "foobar.desktop"}); - MockApplicationLauncherIcon::Ptr sticky_app(new MockApplicationLauncherIcon(true, "sticky-app")); + MockApplicationLauncherIcon::Ptr sticky_app(new NiceMock<MockApplicationLauncherIcon>(true, "sticky-app"));  sticky_app->Stick(false);  lc.Impl()->RegisterIcon(sticky_app, ++priority); - MockVolumeLauncherIcon::Ptr sticky_device(new MockVolumeLauncherIcon()); + MockVolumeLauncherIcon::Ptr sticky_device(new NiceMock<MockVolumeLauncherIcon>());  sticky_device->Stick(false);  lc.Impl()->RegisterIcon(sticky_device, ++priority); @@ -1259,11 +1263,11 @@ TEST_F(TestLauncherController, SaveIconsOrderTriesToKeepIconProvidersOrder2)  lc.ClearModel();  int priority = 0; - MockApplicationLauncherIcon::Ptr sticky_app(new MockApplicationLauncherIcon(true, "sticky-app")); + MockApplicationLauncherIcon::Ptr sticky_app(new NiceMock<MockApplicationLauncherIcon>(true, "sticky-app"));  sticky_app->Stick(false);  lc.Impl()->RegisterIcon(sticky_app, ++priority); - MockVolumeLauncherIcon::Ptr sticky_device(new MockVolumeLauncherIcon()); + MockVolumeLauncherIcon::Ptr sticky_device(new NiceMock<MockVolumeLauncherIcon>());  sticky_device->Stick(false);  lc.Impl()->RegisterIcon(sticky_device, ++priority); @@ -1434,7 +1438,7 @@ TEST_F(TestLauncherController, OnFavoriteStoreFavoriteAddedDeviceSection)  TEST_F(TestLauncherController, OnFavoriteStoreFavoriteRemovedApplication)  { - MockApplicationLauncherIcon::Ptr app_icon(new MockApplicationLauncherIcon(true, "sticky-icon")); + MockApplicationLauncherIcon::Ptr app_icon(new NiceMock<MockApplicationLauncherIcon>(true, "sticky-icon"));  lc.Impl()->RegisterIcon(app_icon);  app_icon->Stick(false); @@ -1554,7 +1558,10 @@ TEST_F(TestLauncherController, UpdateSelectionChanged)  ASSERT_EQ(lc.Impl()->model_->Selection()->tooltip_text(), last_selection_change);  } -TEST_F(TestLauncherController, DragAndDrop_MultipleLaunchers) +// thumper: 2012-11-28 disabling the drag and drop tests as they are taking over 20s +// each, and that is not acceptable for unit tests. These sound more like functional +// tests. +TEST_F(TestLauncherController, DISABLED_DragAndDrop_MultipleLaunchers)  {  lc.multiple_launchers = true;  uscreen.SetupFakeMultiMonitor(); @@ -1580,7 +1587,7 @@ TEST_F(TestLauncherController, DragAndDrop_MultipleLaunchers)  Utils::WaitUntil(std::bind(check_fn, i), true);  } -TEST_F(TestLauncherController, DragAndDrop_SingleLauncher) +TEST_F(TestLauncherController, DISABLED_DragAndDrop_SingleLauncher)  {  lc.multiple_launchers = false;  uscreen.SetupFakeMultiMonitor(2); diff --git a/tests/test_main.cpp b/tests/test_main.cpp index c0af3dbd5..9e1289d37 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -4,6 +4,8 @@  #include <NuxCore/Logger.h>  #include <Nux/Nux.h> +#include "logger_helper.h" +  int main(int argc, char** argv)  {  ::testing::InitGoogleTest(&argc, argv); @@ -18,8 +20,7 @@ int main(int argc, char** argv)  // Slightly higher as we're more likely to test things we know will fail  nux::logging::configure_logging("<root>=error"); - // but you can still change it if you're debugging ;) - nux::logging::configure_logging(::getenv("UNITY_LOG_SEVERITY")); + unity::helper::configure_logging("UNITY_TEST_LOG_SEVERITY");  // StandaloneWindowManager brought in at link time.  int ret = RUN_ALL_TESTS(); diff --git a/tests/test_mock_devices.h b/tests/test_mock_devices.h index 427608bbc..e8497a18c 100644 --- a/tests/test_mock_devices.h +++ b/tests/test_mock_devices.h @@ -23,6 +23,8 @@  #ifndef TEST_MOCK_DEVICES_H  #define TEST_MOCK_DEVICES_H +#include <gmock/gmock.h> +  #include "DeviceLauncherSection.h"  #include "AbstractVolumeMonitorWrapper.h"  #include "Volume.h" @@ -63,8 +65,8 @@ struct MockDevicesSettings : DevicesSettings  struct MockDeviceLauncherSection : DeviceLauncherSection  {  MockDeviceLauncherSection(unsigned size = 2) - : DeviceLauncherSection(MockVolumeMonitorWrapper::Ptr(new MockVolumeMonitorWrapper(size)), - DevicesSettings::Ptr(new MockDevicesSettings)) + : DeviceLauncherSection(MockVolumeMonitorWrapper::Ptr(new testing::NiceMock<MockVolumeMonitorWrapper>(size)), + DevicesSettings::Ptr(new testing::NiceMock<MockDevicesSettings>))  {}  }; | 
