Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ public void close() {
public void onCreate() {
Logger.logVerbose(LOG_TAG, "onCreate");

// Get Termux app SharedProperties without loading from disk since TermuxApplication handles
// load and TermuxActivity handles reloads
mProperties = TermuxAppSharedProperties.getProperties();
mProperties = TermuxAppSharedProperties.init(getApplicationContext());

// Use init() instead of getShellManager() so the singleton is lazily created if the
// OS auto-restarted the service after process death (TermuxApplication.onCreate did not
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.termux.app;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import com.termux.shared.termux.settings.properties.TermuxAppSharedProperties;

import java.lang.reflect.Field;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ServiceController;

@RunWith(RobolectricTestRunner.class)
public class TermuxServicePropertiesNpeTest {

@Before
public void clearPropertiesSingleton() throws Exception {
Field singleton = TermuxAppSharedProperties.class.getDeclaredField("properties");
singleton.setAccessible(true);
singleton.set(null, null);
}

@Test
public void onCreateBeforeTheAppLoaderRan_initializesTheServiceProperties() throws Exception {
assertNull(TermuxAppSharedProperties.getProperties());

ServiceController<TermuxService> controller = Robolectric.buildService(TermuxService.class).create();

Object serviceProperties = servicePropertiesOf(controller.get());
assertNotNull("TermuxService.mProperties must not be null after onCreate()", serviceProperties);
assertSame(TermuxAppSharedProperties.getProperties(), serviceProperties);

controller.destroy();
}

@Test
public void onCreateAfterTheAppLoaderRan_reusesTheLoadedProperties() throws Exception {
TermuxAppSharedProperties loaded = TermuxAppSharedProperties.init(RuntimeEnvironment.getApplication());

ServiceController<TermuxService> controller = Robolectric.buildService(TermuxService.class).create();

assertSame(loaded, servicePropertiesOf(controller.get()));

controller.destroy();
}

private static Object servicePropertiesOf(TermuxService service) throws Exception {
Field field = TermuxService.class.getDeclaredField("mProperties");
field.setAccessible(true);
return field.get(service);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class TermuxAppSharedProperties extends TermuxSharedProperties {

private static TermuxAppSharedProperties properties;
private static volatile TermuxAppSharedProperties properties;


private TermuxAppSharedProperties(@NonNull Context context) {
Expand All @@ -23,7 +23,7 @@ private TermuxAppSharedProperties(@NonNull Context context) {
* @param context The {@link Context} for operations.
* @return Returns the {@link TermuxAppSharedProperties}.
*/
public static TermuxAppSharedProperties init(@NonNull Context context) {
public static synchronized TermuxAppSharedProperties init(@NonNull Context context) {
if (properties == null)
properties = new TermuxAppSharedProperties(context);

Expand Down
Loading