Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Destroy TiActivity Presenter properly #35

Merged
merged 18 commits into from
Nov 8, 2016
Merged
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
1 change: 1 addition & 0 deletions plugin-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
9 changes: 9 additions & 0 deletions plugin-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Test module for `plugin` module

This module contains the `androidTests` for the `plugin` module.
The `plugin` module itself cannot run tests because it uses `provided` instead of `compile`.
This reduces problems for users of the library but makes testing harder.

Also, Activities cannot be defined in the `androidTest` package.
A solution would be to put the `TestActivity` into the `debug` buildType.
But shipping an Activity in the `debug` buildType is far from ideal.
37 changes: 37 additions & 0 deletions plugin-test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion COMPILE_SDK_VERSION
buildToolsVersion BUILD_TOOLS_VERSION

defaultConfig {
applicationId "net.grandcentrix.thirtyinch.plugin_test"
minSdkVersion MIN_SDK_VERSION
targetSdkVersion TARGET_SDK_VERSION
versionCode 1
versionName "1.0"

testInstrumentationRunner "net.grandcentrix.thirtyinch.plugin_test.TestRunner"

}
buildTypes {
release {
minifyEnabled false
}
}
lintOptions {
abortOnError false
}
}

dependencies {
compile project(':plugin')

compile "com.android.support:appcompat-v7:$supportLibraryVersion"
compile "com.pascalwelsch.compositeandroid:activity:$supportLibraryVersion"

androidTestCompile "com.android.support:appcompat-v7:$supportLibraryVersion"
androidTestCompile 'com.android.support.test:runner:0.5';
androidTestCompile 'com.android.support.test:rules:0.5';
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2016 grandcentrix GmbH
* 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 net.grandcentrix.thirtyinch.plugin_test;


import android.app.Application;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
import android.support.test.runner.AndroidJUnitRunner;

/**
* own test runner unlocking the screen of the emulator. Helps stability on travis ci, or
* espresso will fail to find views
*
* via http://stackoverflow.com/a/35024346/669294
*/
public class TestRunner extends AndroidJUnitRunner {

private PowerManager.WakeLock mWakeLock;

@Override
public void callApplicationOnCreate(final Application app) {
// Unlock the screen
KeyguardManager keyguard = (KeyguardManager) app.getSystemService(Context.KEYGUARD_SERVICE);
keyguard.newKeyguardLock(getClass().getSimpleName()).disableKeyguard();

// Start a wake lock
PowerManager power = (PowerManager) app.getSystemService(Context.POWER_SERVICE);
mWakeLock = power.newWakeLock(
PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, getClass().getSimpleName());
mWakeLock.acquire();
super.callApplicationOnCreate(app);

}

@Override
public void onDestroy() {
mWakeLock.release();

super.onDestroy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2016 grandcentrix GmbH
* 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 net.grandcentrix.thirtyinch.plugin_test;


import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;

/**
* Useful test methods common to all activities
*/
public class TestUtils {

public static void rotateOrientation(Activity activity) {
int currentOrientation = activity.getResources().getConfiguration().orientation;

switch (currentOrientation) {
case Configuration.ORIENTATION_LANDSCAPE:
rotateToPortrait(activity);
break;
case Configuration.ORIENTATION_PORTRAIT:
rotateToLandscape(activity);
break;
default:
rotateToLandscape(activity);
}
}

private static void rotateToLandscape(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

private static void rotateToPortrait(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2016 grandcentrix GmbH
* 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 net.grandcentrix.thirtyinch.plugin_test;

import org.junit.Test;
import org.junit.runner.RunWith;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.support.test.filters.LargeTest;
import android.support.test.runner.AndroidJUnit4;

import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static net.grandcentrix.thirtyinch.plugin_test.TestUtils.rotateOrientation;
import static org.hamcrest.Matchers.allOf;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class TiPluginTest {

@Test
public void startTestActivity() throws Exception {
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();

// start the activity for the first time
final TestActivity activity = startTestActivity(instrumentation);

// make sure the attached presenter filled the UI
Espresso.onView(withId(R.id.helloworld_text))
.check(matches(allOf(isDisplayed(), withText("Hello World 1"))));

activity.finish();
}

/**
* Tests the full Activity lifecycle. Guarantees every lifecycle method gets called
*/
@Test
public void testFullLifecycleIncludingConfigurationChange() throws Throwable {
final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();

// start the activity for the first time
final TestActivity activity = startTestActivity(instrumentation);

// make sure the attached presenter filled the UI
Espresso.onView(withId(R.id.helloworld_text))
.check(matches(allOf(isDisplayed(), withText("Hello World 1"))));

// restart the activity
rotateOrientation(activity);

// assert the activity was bound to the presenter. The presenter should update the UI
// correctly
Espresso.onView(withId(R.id.helloworld_text))
.check(matches(allOf(isDisplayed(), withText("Hello World 2"))));

activity.finish();
}

private TestActivity startTestActivity(final Instrumentation instrumentation) {
final Intent intent = new Intent(instrumentation.getTargetContext(), TestActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return (TestActivity) instrumentation.startActivitySync(intent);
}
}
13 changes: 13 additions & 0 deletions plugin-test/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<manifest package="net.grandcentrix.thirtyinch.plugin_test"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application android:theme="@style/Theme.AppCompat">
<activity
android:name=".TestActivity"
android:exported="true" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2016 grandcentrix GmbH
* 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 net.grandcentrix.thirtyinch.plugin_test;

import com.pascalwelsch.compositeandroid.activity.CompositeActivity;

import net.grandcentrix.thirtyinch.TiLog;
import net.grandcentrix.thirtyinch.internal.TiPresenterProvider;
import net.grandcentrix.thirtyinch.plugin.TiActivityPlugin;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;

public class TestActivity extends CompositeActivity implements TestView {

private TextView mText;

public TestActivity() {
addPlugin(new TiActivityPlugin<>(new TiPresenterProvider<TestPresenter>() {
@NonNull
@Override
public TestPresenter providePresenter() {
return new TestPresenter();
}
}));
}

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_test);
mText = (TextView) findViewById(R.id.helloworld_text);
}

@Override
public void showText(final String s) {
mText.setText(s);
}

static {
TiLog.setLogger(new TiLog.Logger() {
@Override
public void log(int level, String tag, String msg) {
Log.println(level, tag, msg);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2016 grandcentrix GmbH
* 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 net.grandcentrix.thirtyinch.plugin_test;

import net.grandcentrix.thirtyinch.TiPresenter;

import android.support.annotation.NonNull;

public class TestPresenter extends TiPresenter<TestView> {

private int mViewAttachedCount = 0;

@Override
protected void onAttachView(@NonNull final TestView view) {
super.onAttachView(view);

mViewAttachedCount++;

getView().showText("Hello World " + mViewAttachedCount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2016 grandcentrix GmbH
* 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 net.grandcentrix.thirtyinch.plugin_test;

import net.grandcentrix.thirtyinch.TiView;

public interface TestView extends TiView {

void showText(String s);
}
13 changes: 13 additions & 0 deletions plugin-test/src/main/res/layout/activity_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/helloworld_text"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
Loading