Merge "Add lifecycle observers for future mixin structures."

This commit is contained in:
TreeHugger Robot
2016-08-24 19:12:12 +00:00
committed by Android (Google) Code Review
19 changed files with 583 additions and 53 deletions

View File

@@ -44,19 +44,19 @@ public class VisibilityLoggerMixinTest {
@Before
public void init() {
MockitoAnnotations.initMocks(this);
mMixin = new VisibilityLoggerMixin(new TestInstrumentable(), mLogger);
mMixin = new VisibilityLoggerMixin(TestInstrumentable.TEST_METRIC, mLogger);
}
@Test
public void shouldLogVisibleOnResume() {
mMixin.onResume(null);
mMixin.onResume();
verify(mLogger, times(1))
.visible(any(Context.class), eq(TestInstrumentable.TEST_METRIC));
}
@Test
public void shouldLogHideOnPause() {
mMixin.onPause(null);
mMixin.onPause();
verify(mLogger, times(1))
.hidden(any(Context.class), eq(TestInstrumentable.TEST_METRIC));
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.core.lifecycle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import com.android.settings.TestConfig;
import com.android.settings.core.lifecycle.events.OnDestroy;
import com.android.settings.core.lifecycle.events.OnPause;
import com.android.settings.core.lifecycle.events.OnResume;
import com.android.settings.core.lifecycle.events.OnStart;
import com.android.settings.core.lifecycle.events.OnStop;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.util.ActivityController;
import static org.junit.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class LifecycleTest {
public static class TestActivity extends ObservableActivity {
final Fragment mFragment;
final TestObserver mActObserver;
public TestActivity() {
mFragment = new Fragment();
mActObserver = new TestObserver();
getLifecycle().addObserver(mActObserver);
}
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(mFragment, "tag");
fragmentTransaction.commit();
}
}
public static class TestObserver implements LifecycleObserver, OnStart, OnResume,
OnPause, OnStop, OnDestroy {
boolean mOnStartObserved;
boolean mOnResumeObserved;
boolean mOnPauseObserved;
boolean mOnStopObserved;
boolean mOnDestroyObserved;
@Override
public void onStart() {
mOnStartObserved = true;
}
@Override
public void onPause() {
mOnPauseObserved = true;
}
@Override
public void onResume() {
mOnResumeObserved = true;
}
@Override
public void onStop() {
mOnStopObserved = true;
}
@Override
public void onDestroy() {
mOnDestroyObserved = true;
}
}
@Test
public void runThroughLifecycles_shouldObserveEverything() {
ActivityController<TestActivity> ac = Robolectric.buildActivity(TestActivity.class);
TestActivity activity = ac.get();
ac.create().start();
assertTrue(activity.mActObserver.mOnStartObserved);
ac.resume();
assertTrue(activity.mActObserver.mOnResumeObserved);
ac.pause();
assertTrue(activity.mActObserver.mOnPauseObserved);
ac.stop();
assertTrue(activity.mActObserver.mOnStopObserved);
ac.destroy();
assertTrue(activity.mActObserver.mOnDestroyObserved);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.utils;
import com.android.settings.TestConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ThreadUtilsTest {
@Test
public void testMainThread() throws InterruptedException {
assertTrue(ThreadUtils.isMainThread());
Thread background = new Thread(new Runnable() {
public void run() {
assertFalse(ThreadUtils.isMainThread());
}
});
background.start();
background.join();
}
@Test
public void testEnsureMainThread() throws InterruptedException {
ThreadUtils.ensureMainThread();
Thread background = new Thread(new Runnable() {
public void run() {
try {
ThreadUtils.ensureMainThread();
fail("Should not pass ensureMainThread in a background thread");
} catch (RuntimeException e) {
}
}
});
background.start();
background.join();
}
}