~townsend/unity/fix-waiting-to-install

« back to all changes in this revision

Viewing changes to tests/test_utils.h

  • Committer: Chris Townsend
  • Date: 2013-07-17 16:24:40 UTC
  • mfrom: (3379.1.48 trunk)
  • Revision ID: christopher.townsend@canonical.com-20130717162440-zbeyzlbq0kdvnypo
MergeĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
class Utils
17
17
{
18
 
public:  
19
 
  typedef std::function<gchar*()> ErrorStringFunc;
20
 
  static gchar* DefaultErrorString() { return nullptr; }
21
 
 
22
 
  static void WaitUntilMSec(bool& success, unsigned max_wait = 500, ErrorStringFunc const& error_func = &Utils::DefaultErrorString)
23
 
  {
24
 
    WaitUntilMSec([&success] {return success;}, true, max_wait, error_func);
25
 
  }
26
 
 
27
 
  static void WaitUntil(bool& success, unsigned max_wait = 1, ErrorStringFunc const& error_func = &Utils::DefaultErrorString)
28
 
  {
29
 
    WaitUntilMSec(success, max_wait * 1000, error_func);
30
 
  }
31
 
 
32
 
  static void WaitUntilMSec(std::function<bool()> const& check_function, bool expected_result = true, unsigned max_wait = 500, ErrorStringFunc const& error_func = &Utils::DefaultErrorString)
 
18
public:
 
19
  static void WaitUntilMSec(bool& success, unsigned max_wait = 500, std::string const& error_msg = "")
 
20
  {
 
21
    WaitUntilMSec([&success] {return success;}, true, max_wait, error_msg);
 
22
  }
 
23
 
 
24
  static void WaitUntil(bool& success, unsigned max_wait = 1, std::string const& error_msg = "")
 
25
  {
 
26
    WaitUntilMSec(success, max_wait * 1000, error_msg);
 
27
  }
 
28
 
 
29
  static void WaitUntilMSec(std::function<bool()> const& check_function, bool expected_result = true, unsigned max_wait = 500, std::string const& error_msg = "")
33
30
  {
34
31
    ASSERT_NE(check_function, nullptr);
35
32
 
49
46
    if (result == expected_result)
50
47
      g_source_remove(timeout_id);
51
48
 
52
 
    glib::String error(error_func());
53
 
    if (error)
54
 
    {
55
 
      EXPECT_EQ(result, expected_result) << "Error: " << error;
56
 
    }
57
 
    else
58
 
    {
59
 
      EXPECT_EQ(result, expected_result);
60
 
    }
 
49
    EXPECT_EQ(result, expected_result) << (error_msg.empty() ? "" : ("Error: " + error_msg));
61
50
  }
62
51
 
63
 
  static void WaitUntil(std::function<bool()> const& check_function, bool result = true, unsigned max_wait = 1, ErrorStringFunc const& error_func = &Utils::DefaultErrorString)
 
52
  static void WaitUntil(std::function<bool()> const& check_function, bool result = true, unsigned max_wait = 1, std::string const& error_msg = "")
64
53
  {
65
 
    WaitUntilMSec(check_function, result, max_wait * 1000, error_func);
 
54
    WaitUntilMSec(check_function, result, max_wait * 1000, error_msg);
66
55
  }
67
56
 
68
57
  static guint32 ScheduleTimeout(bool* timeout_reached, unsigned timeout_duration = 10)
69
58
  {
70
 
    return g_timeout_add(timeout_duration, TimeoutCallback, timeout_reached);
 
59
    return g_timeout_add_full(G_PRIORITY_DEFAULT+10, timeout_duration, TimeoutCallback, timeout_reached, nullptr);
71
60
  }
72
61
 
73
62
  static void WaitForTimeout(unsigned timeout_duration = 1)
81
70
    ScheduleTimeout(&timeout_reached, timeout_duration);
82
71
 
83
72
    while (!timeout_reached)
84
 
      g_main_context_iteration(g_main_context_get_thread_default(), TRUE);
 
73
      g_main_context_iteration(nullptr, TRUE);
85
74
  }
86
75
 
87
76
  static void init_gsettings_test_environment()
97
86
    g_unsetenv("GSETTINGS_BACKEND");
98
87
  }
99
88
 
 
89
  static void WaitPendingEvents(unsigned max_wait_ms = 5000)
 
90
  {
 
91
    gint64 start_time = g_get_monotonic_time();
 
92
 
 
93
    while (g_main_context_pending(nullptr) &&
 
94
           (g_get_monotonic_time() - start_time) / 1000 < max_wait_ms)
 
95
    {
 
96
      g_main_context_iteration(nullptr, TRUE);
 
97
    }
 
98
  }
 
99
 
100
100
private:
101
101
  static gboolean TimeoutCallback(gpointer data)
102
102
  {