1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "nsWidgetsCID.h"
8 #include "nsIUserIdleService.h"
9 #include "mozilla/StackWalk.h"
10 
11 #ifndef TEST_NAME
12 #  error "Must #define TEST_NAME before including places_test_harness_tail.h"
13 #endif
14 
15 int gTestsIndex = 0;
16 
17 #define TEST_INFO_STR "TEST-INFO | "
18 
19 class RunNextTest : public mozilla::Runnable {
20  public:
RunNextTest()21   RunNextTest() : mozilla::Runnable("RunNextTest") {}
Run()22   NS_IMETHOD Run() override {
23     NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
24     if (gTestsIndex < int(mozilla::ArrayLength(gTests))) {
25       do_test_pending();
26       Test& test = gTests[gTestsIndex++];
27       (void)fprintf(stderr, TEST_INFO_STR "Running %s.\n", test.name);
28       test.func();
29     }
30 
31     do_test_finished();
32     return NS_OK;
33   }
34 };
35 
36 static const bool kDebugRunNextTest = false;
37 
run_next_test()38 void run_next_test() {
39   if (kDebugRunNextTest) {
40     printf_stderr("run_next_test()\n");
41     MozWalkTheStack(stderr);
42   }
43   nsCOMPtr<nsIRunnable> event = new RunNextTest();
44   do_check_success(NS_DispatchToCurrentThread(event));
45 }
46 
47 int gPendingTests = 0;
48 
do_test_pending()49 void do_test_pending() {
50   NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
51   if (kDebugRunNextTest) {
52     printf_stderr("do_test_pending()\n");
53     MozWalkTheStack(stderr);
54   }
55   gPendingTests++;
56 }
57 
do_test_finished()58 void do_test_finished() {
59   NS_ASSERTION(NS_IsMainThread(), "Not running on the main thread?");
60   NS_ASSERTION(gPendingTests > 0, "Invalid pending test count!");
61   gPendingTests--;
62 }
63 
disable_idle_service()64 void disable_idle_service() {
65   (void)fprintf(stderr, TEST_INFO_STR "Disabling Idle Service.\n");
66 
67   nsCOMPtr<nsIUserIdleService> idle =
68       do_GetService("@mozilla.org/widget/useridleservice;1");
69   idle->SetDisabled(true);
70 }
71 
TEST(IHistory,Test)72 TEST(IHistory, Test)
73 {
74   RefPtr<WaitForConnectionClosed> spinClose = new WaitForConnectionClosed();
75 
76   // Tinderboxes are constantly on idle.  Since idle tasks can interact with
77   // tests, causing random failures, disable the idle service.
78   disable_idle_service();
79 
80   do_test_pending();
81   run_next_test();
82 
83   // Spin the event loop until we've run out of tests to run.
84   mozilla::SpinEventLoopUntil([&]() { return !gPendingTests; });
85 
86   // And let any other events finish before we quit.
87   (void)NS_ProcessPendingEvents(nullptr);
88 }
89