Skip to content

Commit c21ea9d

Browse files
authored
Merge pull request #364 from firebase/feature/game_loops
Added example implementation of Firebase Test Lab Game Loop library.
2 parents 65384c0 + 5146286 commit c21ea9d

File tree

13 files changed

+1602
-0
lines changed

13 files changed

+1602
-0
lines changed

testlab/src/android/testlab.cc

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "testlab/src/include/firebase/testlab.h"
16+
17+
#include <jni.h>
18+
19+
#include "app/src/include/firebase/app.h"
20+
#include "app/src/include/firebase/log.h"
21+
#include "app/src/log.h"
22+
#include "app/src/reference_count.h"
23+
#include "app/src/util.h"
24+
#include "app/src/util_android.h"
25+
#include "testlab/src/android/util.h"
26+
#include "testlab/src/common/common.h"
27+
28+
using firebase::internal::ReferenceCount;
29+
using firebase::internal::ReferenceCountLock;
30+
31+
namespace firebase {
32+
namespace test_lab {
33+
namespace game_loop {
34+
35+
static ReferenceCount g_initializer; // NOLINT
36+
37+
namespace internal {
38+
39+
// Determine whether the test lab module is initialized.
40+
bool IsInitialized() { return g_initializer.references() > 0; }
41+
42+
} // namespace internal
43+
44+
// Initialize the API
45+
void Initialize(const firebase::App& app) {
46+
ReferenceCountLock<ReferenceCount> ref_count(&g_initializer);
47+
if (ref_count.references() != 0) {
48+
LogWarning("Test Lab API already initialized");
49+
return;
50+
}
51+
ref_count.AddReference();
52+
LogDebug("Firebase Test Lab API initializing");
53+
internal::Initialize(&app);
54+
}
55+
56+
// Clean up the API
57+
void Terminate() {
58+
ReferenceCountLock<ReferenceCount> ref_count(&g_initializer);
59+
if (ref_count.references() == 0) {
60+
LogWarning("Test Lab API was never initialized");
61+
return;
62+
}
63+
if (ref_count.references() == 1) {
64+
internal::Terminate();
65+
}
66+
ref_count.RemoveReference();
67+
}
68+
69+
// Return the game loop scenario's integer ID, or 0 if no game loop is running
70+
int GetScenario() {
71+
if (!internal::IsInitialized()) return 0;
72+
return internal::GetScenario();
73+
}
74+
75+
// Log progress text to the game loop's custom results and device logs
76+
void LogText(const char* format, ...) {
77+
if (GetScenario() == 0) return;
78+
va_list args;
79+
va_start(args, format);
80+
internal::LogText(format, args);
81+
va_end(args);
82+
}
83+
84+
// Complete the game loop scenario with the specified outcome
85+
void FinishScenario(ScenarioOutcome outcome) {
86+
if (GetScenario() == 0) return;
87+
FILE* result_file = internal::RetrieveCustomResultsFile();
88+
if (result_file == nullptr) {
89+
LogError("Could not obtain the custom results file");
90+
} else {
91+
internal::OutputResult(outcome, result_file);
92+
}
93+
internal::CallFinish();
94+
Terminate();
95+
// TODO(brandonmorris): This works, but isn't the proper way to exit the app.
96+
// Look into either using ANativeActivity_finish or calling finish() on the
97+
// main thread.
98+
exit(0);
99+
}
100+
101+
} // namespace game_loop
102+
} // namespace test_lab
103+
} // namespace firebase

0 commit comments

Comments
 (0)