Skip to content

Commit

Permalink
Merge pull request #572 from ooni/issues/2470
Browse files Browse the repository at this point in the history
 Unify calls to `checkIn` from foreground and automated tests
  • Loading branch information
aanorbel authored Sep 10, 2023
2 parents efeed02 + 9d1f1c6 commit 05caf25
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static void runAsForegroundService(AbstractActivity context,
private static void startRunTestService(AbstractActivity context,
ArrayList<AbstractSuite> testSuites,
OnTestServiceStartedListener onTestServiceStartedListener) {
ServiceUtil.startRunTestService(context, testSuites, true);
ServiceUtil.startRunTestServiceManual(context, testSuites, true);
onTestServiceStartedListener.onTestServiceStarted();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public JobTask(JobService jobService, Application app) {

@Override
protected JobParameters doInBackground(JobParameters... params) {
ServiceUtil.callCheckInAPI(app);
ServiceUtil.startRunTestServiceUnattended(app);
return params[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
if (testSuites == null || testSuites.size() == 0)
return START_STICKY_COMPATIBILITY;
boolean store_db = intent.getBooleanExtra("storeDB", true);
boolean unattended = intent.getBooleanExtra("unattended", false);
Application app = ((Application) getApplication());
NotificationUtility.setChannel(getApplicationContext(), CHANNEL_ID, app.getString(R.string.Settings_AutomatedTesting_Label), false, false, false);
Intent notificationIntent = new Intent(this, RunningActivity.class);
Expand All @@ -79,7 +80,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
.setProgress(100, 0, false)
.build();

task = (TestAsyncTask) new TestAsyncTask(app, testSuites, store_db).execute();
task = (TestAsyncTask) new TestAsyncTask(app, testSuites, store_db, unattended).execute();
//This intent is used to manage the stop test button in the notification
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(RunTestService.ACTION_INTERRUPT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void stopJob(Context context) {
}
}

public static void callCheckInAPI(Application app) {
public static void startRunTestServiceUnattended(Application app) {
app.getServiceComponent().inject(d);

boolean isVPNInUse = ReachabilityManager.isVPNinUse(app);
Expand All @@ -79,27 +79,32 @@ public static void callCheckInAPI(Application app) {
return;
}


AbstractSuite suite = d.generateAutoRunServiceSuite.generate(config);
AbstractSuite suite = d.generateAutoRunServiceSuite.generate();
ArrayList<AbstractSuite> testSuites = new ArrayList<>();
testSuites.add(suite);
testSuites.add(InstantMessagingSuite.initForAutoRun());
testSuites.add(CircumventionSuite.initForAutoRun());
testSuites.add(PerformanceSuite.initForAutoRun());
testSuites.add(ExperimentalSuite.initForAutoRun());
ServiceUtil.startRunTestService(app, testSuites, false);
ServiceUtil.startRunTestServiceCommon(app, testSuites, false, true);
d.generateAutoRunServiceSuite.markAsRan();

}


public static void startRunTestService(Context context, ArrayList<AbstractSuite> iTestSuites, boolean storeDB) {
public static void startRunTestServiceManual(Context context, ArrayList<AbstractSuite> iTestSuites, boolean storeDB) {
startRunTestServiceCommon(context, iTestSuites, storeDB, false);
}

private static void startRunTestServiceCommon(Context context, ArrayList<AbstractSuite> iTestSuites, boolean storeDB, boolean unattended) {
ArrayList<AbstractSuite> testSuites = Lists.newArrayList(
Iterables.filter(Iterables.filter(iTestSuites, item -> item != null), testSuite -> !testSuite.isTestEmpty(d.preferenceManager))
);

Intent serviceIntent = new Intent(context, RunTestService.class);
serviceIntent.putExtra("testSuites", testSuites);
serviceIntent.putExtra("storeDB", storeDB);
serviceIntent.putExtra("unattended", unattended);
ContextCompat.startForegroundService(context, serviceIntent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,17 @@ public class GenerateAutoRunServiceSuite {
this.app = application;
}

public AbstractSuite generate(
OONICheckInConfig config
) {

try {
OONISession session = EngineProvider.get().newSession(
EngineProvider.get().getDefaultSessionConfig(
app,
String.join("-",BuildConfig.SOFTWARE_NAME, AbstractTest.UNATTENDED),
BuildConfig.VERSION_NAME,
new LoggerArray(),
pm.getProxyURL()
)
);
OONIContext ooniContext = session.newContextWithTimeout(30);
OONICheckInResults results = session.checkIn(ooniContext, config);

if (results.getWebConnectivity() != null) {
List<String> inputs = new ArrayList<>();
for (OONIURLInfo url : results.getWebConnectivity().getUrls()) {
inputs.add(url.getUrl());
}

markAsRan();

return AbstractSuite.getSuite(
app,
"web_connectivity",
inputs,
AbstractTest.AUTORUN
);
}

return null;
} catch (Exception e) {
e.printStackTrace();
ThirdPartyServices.logException(e);
return null;
}
public AbstractSuite generate() {

return AbstractSuite.getSuite(
app,
"web_connectivity",
null,
AbstractTest.AUTORUN
);
}


public boolean shouldStart(Boolean isWifi, Boolean isCharging, Boolean isVPNInUse) {
if (pm.testWifiOnly() && !isWifi)
return false;
Expand All @@ -86,7 +56,7 @@ public boolean shouldStart(Boolean isWifi, Boolean isCharging, Boolean isVPNInUs
return true;
}

private void markAsRan() {
public void markAsRan() {
pm.updateAutorunDate();
pm.incrementAutorun();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class TestAsyncTask extends AsyncTask<Void, String, Void> implements Abst
private String proxy;
private boolean store_db = true;

private boolean unattended;
public static List<AbstractSuite> getSuites() {
return Arrays.asList(new WebsitesSuite(),
new InstantMessagingSuite(), new CircumventionSuite(), new PerformanceSuite(), new ExperimentalSuite());
Expand All @@ -75,9 +76,10 @@ public TestAsyncTask(Application app, ArrayList<AbstractSuite> testSuites) {
this.proxy = app.getPreferenceManager().getProxyURL();
}

public TestAsyncTask(Application app, ArrayList<AbstractSuite> testSuites, boolean store_db) {
public TestAsyncTask(Application app, ArrayList<AbstractSuite> testSuites, boolean store_db, boolean unattended) {
this(app, testSuites);
this.store_db = store_db;
this.unattended = unattended;
}

private void registerConnChange() {
Expand Down Expand Up @@ -159,8 +161,15 @@ private void runTest(AbstractTest... tests) {
//This uses the wrapper
private void downloadURLs() {
try {
OONISession session = EngineProvider.get().newSession(EngineProvider.get().getDefaultSessionConfig(
app, BuildConfig.SOFTWARE_NAME, BuildConfig.VERSION_NAME, new LoggerArray(), proxy));
OONISession session = EngineProvider.get().newSession(
EngineProvider.get().getDefaultSessionConfig(
app,
unattended ? String.join("-", BuildConfig.SOFTWARE_NAME, AbstractTest.UNATTENDED) : BuildConfig.SOFTWARE_NAME,
BuildConfig.VERSION_NAME,
new LoggerArray(),
proxy
)
);
OONIContext ooniContext = session.newContextWithTimeout(30);

OONICheckInConfig config = app.getOONICheckInConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@
public class GenerateAutoRunServiceSuiteTest extends RobolectricAbstractTest {

// Mocks
OONICheckInInfoWebConnectivity webConnectivityMock = mock(OONICheckInInfoWebConnectivity.class);
PreferenceManager preferenceManagerMock = mock(PreferenceManager.class);
OONICheckInConfig ooniCheckConfigMock = mock(OONICheckInConfig.class);
OONICheckInResults ooniResultsMock = mock(OONICheckInResults.class);
OONISession ooniSessionMock = mock(OONISession.class);

// Engine && UseCase
Expand All @@ -52,73 +49,19 @@ public void setUp() {
}

@Test
public void shouldNotStartTest() {
public void generateSuite() {
// Act
AbstractSuite suite = generateSuite.generate(ooniCheckConfigMock);

// Assert
Assert.assertNull(suite);
}

@Test
public void generateSuite() throws Exception {
// Arrange
ArrayList<OONIURLInfo> suiteUrls = getTestUrls();

when(preferenceManagerMock.getEnabledCategoryArr()).thenReturn(new ArrayList<String>() {
{
add("ALDR");
add("REL");
add("PORN");
add("PROV");
add("POLR");
add("HUMR");
add("ENV");
}
});

when(webConnectivityMock.getUrls()).thenReturn(suiteUrls);
when(ooniResultsMock.getWebConnectivity()).thenReturn(webConnectivityMock);
when(ooniSessionMock.checkIn(any(), any())).thenReturn(ooniResultsMock);

// Act
AbstractSuite suite = generateSuite.generate(ooniCheckConfigMock);
AbstractSuite suite = generateSuite.generate();

// Assert
Assert.assertNotNull(suite);
verify(preferenceManagerMock, times(1)).updateAutorunDate();
verify(preferenceManagerMock, times(1)).incrementAutorun();
Assert.assertEquals(1, suite.getTestList(preferenceManagerMock).length);

AbstractTest webTest = suite.getTestList(preferenceManagerMock)[0];

Assert.assertEquals("web_connectivity", webTest.getName());
Assert.assertEquals(suiteUrls.size(), webTest.getInputs().size());

for (int i = 0; i < webTest.getInputs().size(); i++) {
Assert.assertEquals( webTest.getInputs().get(i), suiteUrls.get(i).getUrl());
}
}

private ArrayList<OONIURLInfo> getTestUrls() {
Faker faker = new Faker();

OONIURLInfo url1 = mock(OONIURLInfo.class);
when(url1.getUrl()).thenReturn(faker.internet.url());

OONIURLInfo url2 = mock(OONIURLInfo.class);
when(url1.getUrl()).thenReturn(faker.internet.url());

OONIURLInfo url3 = mock(OONIURLInfo.class);
when(url1.getUrl()).thenReturn(faker.internet.url());
Assert.assertNull(webTest.getInputs());

return new ArrayList<OONIURLInfo>(){
{
add(url1);
add(url2);
add(url3);
}
};
}

}

This file was deleted.

Loading

0 comments on commit 05caf25

Please sign in to comment.