Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CATROID-921 Disable Hints dialog #4083

Merged
merged 1 commit into from
Apr 21, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.catrobat.catroid.uiespresso.ui.dialog;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import org.catrobat.catroid.R;
import org.catrobat.catroid.common.Constants;
import org.catrobat.catroid.content.Project;
import org.catrobat.catroid.io.asynctask.ProjectSaveTask;
import org.catrobat.catroid.testsuites.annotations.Cat;
import org.catrobat.catroid.testsuites.annotations.Level;
import org.catrobat.catroid.ui.MainMenuActivity;
import org.catrobat.catroid.ui.settingsfragments.SettingsFragment;
import org.catrobat.catroid.uiespresso.util.UiTestUtils;
import org.catrobat.catroid.uiespresso.util.rules.BaseActivityTestRule;
import org.catrobat.catroid.utils.SnackbarUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.Set;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.matcher.PreferenceMatchers;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

import static org.catrobat.catroid.R.id.currentProjectLayout;
import static org.catrobat.catroid.common.SharedPreferenceKeys.AGREED_TO_PRIVACY_POLICY_VERSION;
import static org.catrobat.catroid.common.SharedPreferenceKeys.DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY;
import static org.catrobat.catroid.ui.settingsfragments.SettingsFragment.SETTINGS_SHOW_HINTS;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class DisableHintDialogTest {

private boolean hintSetting;
private Set<String> hintList;
int bufferedPreferenceSetting;

@Rule
public BaseActivityTestRule<MainMenuActivity> baseActivityTestRule = new
BaseActivityTestRule<>(MainMenuActivity.class, false, false);

@Before
public void setUp() throws Exception {
createProject("firstProject");

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext());
hintSetting = sharedPreferences.getBoolean(SettingsFragment.SETTINGS_SHOW_HINTS, false);
hintList = new HashSet<>(sharedPreferences.getStringSet(SnackbarUtil.SHOWN_HINT_LIST, new HashSet<String>()));
bufferedPreferenceSetting = sharedPreferences.getInt(AGREED_TO_PRIVACY_POLICY_VERSION, 0);

getDefaultSharedPreferences()
.edit()
.putBoolean(SETTINGS_SHOW_HINTS, true)
.putStringSet(SnackbarUtil.SHOWN_HINT_LIST, new HashSet<String>())
.putBoolean(DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY, false)
.putInt(AGREED_TO_PRIVACY_POLICY_VERSION, Constants.CATROBAT_TERMS_OF_USE_ACCEPTED)
.apply();

baseActivityTestRule.launchActivity(null);
}

@After
public void tearDown() throws Exception {
baseActivityTestRule.deleteAllProjects();

PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext())
.edit()
.putBoolean(SETTINGS_SHOW_HINTS, hintSetting)
.putStringSet(SnackbarUtil.SHOWN_HINT_LIST, hintList)
.putBoolean(DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY, true)
.putInt(AGREED_TO_PRIVACY_POLICY_VERSION, bufferedPreferenceSetting)
.apply();
}

@Category({Cat.AppUi.class, Level.Smoke.class})
@Test
public void disableHintDialogTest() {
onView(withId(currentProjectLayout)).perform(click());

onView(withText(R.string.dialog_disable_hints_title))
.check(matches(isDisplayed()));

onView(withText(R.string.dialog_disable_hints_text))
.check(matches(isDisplayed()));

onView(withText(R.string.dialog_disable_hints_button_hide)).perform(click());

pressBack();

openActionBarOverflowOrOptionsMenu(baseActivityTestRule.getActivity());
onView(withText(R.string.settings)).perform(click());
checkPreferenceHide(R.string.preference_title_enable_hints, SETTINGS_SHOW_HINTS);

pressBack();
}

@Category({Cat.AppUi.class, Level.Smoke.class})
@Test
public void enableHintDialogTest() {
onView(withId(currentProjectLayout)).perform(click());

onView(withText(R.string.dialog_disable_hints_title))
.check(matches(isDisplayed()));

onView(withText(R.string.dialog_disable_hints_text))
.check(matches(isDisplayed()));

onView(withText(R.string.dialog_disable_hints_button_show)).perform(click());

pressBack();

openActionBarOverflowOrOptionsMenu(baseActivityTestRule.getActivity());
onView(withText(R.string.settings)).perform(click());
checkPreferenceShow(R.string.preference_title_enable_hints, SETTINGS_SHOW_HINTS);

pressBack();
}

private void createProject(String projectName) {
Project project = UiTestUtils.createEmptyProject(projectName);
ProjectSaveTask.task(project, ApplicationProvider.getApplicationContext());
}

private SharedPreferences getDefaultSharedPreferences() {
return PreferenceManager.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext());
}

private void checkPreferenceHide(int displayedTitleResourceString, String sharedPreferenceTag) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext());

assertFalse(sharedPreferences.getBoolean(sharedPreferenceTag, false));

onData(PreferenceMatchers.withTitle(displayedTitleResourceString))
.perform(click());

assertTrue(sharedPreferences.getBoolean(sharedPreferenceTag, true));
}

private void checkPreferenceShow(int displayedTitleResourceString, String sharedPreferenceTag) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(ApplicationProvider.getApplicationContext());

assertTrue(sharedPreferences.getBoolean(sharedPreferenceTag, true));

onData(PreferenceMatchers.withTitle(displayedTitleResourceString))
.perform(click());

assertFalse(sharedPreferences.getBoolean(sharedPreferenceTag, false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import static org.catrobat.catroid.common.SharedPreferenceKeys.DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY;
import static org.hamcrest.core.AllOf.allOf;

import static androidx.test.espresso.Espresso.onView;
Expand Down Expand Up @@ -79,6 +80,7 @@ public void setUp() {
sharedPreferences.edit()
.putBoolean(SettingsFragment.SETTINGS_SHOW_HINTS, true)
.putStringSet(SnackbarUtil.SHOWN_HINT_LIST, new HashSet<String>())
.putBoolean(DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY, true)
.commit();

createProject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ private SharedPreferenceKeys() {

public static final String SORT_PROJECTS_PREFERENCE_KEY = "sortProjectsList";

public static final String DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY =
"disableHintsDialogShown";

public static final String SCRATCH_CONVERTER_CLIENT_ID_PREFERENCE_KEY = "scratchconverter.clientID";
public static final String SCRATCH_CONVERTER_DOWNLOAD_STATE_PREFERENCE_KEY = "scratchconverter"
+ ".downloadStatePref";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ boolean shouldShowEmptyView() {
public void onResume() {
initializeAdapter();
super.onResume();
SnackbarUtil.showHintSnackbar(getActivity(), R.string.hint_objects);
Project currentProject = ProjectManager.getInstance().getCurrentProject();
String title;

Expand Down Expand Up @@ -178,7 +179,6 @@ public void onPositiveButtonClick(DialogInterface dialog, String textInput) {

@Override
protected void initializeAdapter() {
SnackbarUtil.showHintSnackbar(getActivity(), R.string.hint_objects);
sharedPreferenceDetailsKey = SHOW_DETAILS_SPRITES_PREFERENCE_KEY;
List<Sprite> items = ProjectManager.getInstance().getCurrentlyEditedScene().getSpriteList();
adapter = new MultiViewSpriteAdapter(items);
Expand Down
90 changes: 70 additions & 20 deletions catroid/src/main/java/org/catrobat/catroid/utils/SnackbarUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2018 The Catrobat Team
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -38,8 +38,11 @@
import java.util.Set;

import androidx.annotation.StringRes;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import static org.catrobat.catroid.common.SharedPreferenceKeys.DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY;

public final class SnackbarUtil {

private static final int MAX_LINES = 5;
Expand All @@ -53,27 +56,35 @@ private SnackbarUtil() {
public static void showHintSnackbar(final Activity activity, @StringRes int resourceId) {
final String messageId = activity.getResources().getResourceName(resourceId);

if (!wasHintAlreadyShown(activity, messageId) && areHintsEnabled(activity)) {
View contentView = activity.findViewById(android.R.id.content);
if (contentView == null) {
return;
}
if (areHintsEnabled(activity) && !wasHintDialogAlreadyShown(activity)) {
showDisableHintsDialog(activity, resourceId);
} else if (!wasHintAlreadyShown(activity, messageId) && areHintsEnabled(activity)) {
hintSnackbar(activity, resourceId);
}
}

public static void hintSnackbar(Activity activity, @StringRes int resourceId) {
final String messageId = activity.getResources().getResourceName(resourceId);

Snackbar snackbar = Snackbar.make(contentView, resourceId, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.got_it, new View.OnClickListener() {
@Override
public void onClick(View v) {
setHintShown(activity, messageId);
}
});
snackbar.setActionTextColor(ContextCompat.getColor(activity, R.color.solid_black));
View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
textView.setMaxLines(MAX_LINES);
textView.setTextColor(ContextCompat.getColor(activity, R.color.solid_white));
snackbarView.setBackgroundColor(ContextCompat.getColor(activity, R.color.snackbar));
snackbar.show();
View contentView = activity.findViewById(android.R.id.content);
if (contentView == null) {
return;
}

Snackbar snackbar = Snackbar.make(contentView, resourceId, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.got_it, new View.OnClickListener() {
@Override
public void onClick(View v) {
setHintShown(activity, messageId);
}
});
snackbar.setActionTextColor(ContextCompat.getColor(activity, R.color.solid_black));
View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(com.google.android.material.R.id.snackbar_text);
textView.setMaxLines(MAX_LINES);
textView.setTextColor(ContextCompat.getColor(activity, R.color.solid_white));
snackbarView.setBackgroundColor(ContextCompat.getColor(activity, R.color.snackbar));
snackbar.show();
}

public static void setHintShown(Activity activity, String messageId) {
Expand All @@ -97,4 +108,43 @@ private static Set<String> getStringSetFromSharedPreferences(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return new HashSet<>(prefs.getStringSet(SnackbarUtil.SHOWN_HINT_LIST, new HashSet<String>()));
}

public static void setHintDialogShown(Activity activity) {
PreferenceManager.getDefaultSharedPreferences(activity)
.edit()
.putBoolean(DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY, true)
.apply();
}

public static boolean wasHintDialogAlreadyShown(Activity activity) {
return PreferenceManager.getDefaultSharedPreferences(activity).getBoolean(DISABLE_HINTS_DIALOG_SHOWN_PREFERENCE_KEY, false);
}

public static boolean showDisableHintsDialog(final Activity activity, @StringRes int resourceId) {
new AlertDialog.Builder(activity)
.setTitle(R.string.dialog_disable_hints_title)
.setMessage(R.string.dialog_disable_hints_text)
.setPositiveButton(R.string.dialog_disable_hints_button_show,
(dialog, id) -> handleShowHints(activity, resourceId))
.setNegativeButton(R.string.dialog_disable_hints_button_hide,
(dialog, id) -> handleHideHints(activity))
.setCancelable(false)
.show();
return true;
}

protected static void handleHideHints(Activity activity) {
SharedPreferences sharedPreferencesSetFalse =
PreferenceManager.getDefaultSharedPreferences(activity);

sharedPreferencesSetFalse.edit()
.putBoolean(SettingsFragment.SETTINGS_SHOW_HINTS, false)
.apply();
setHintDialogShown(activity);
}

protected static void handleShowHints(Activity activity, @StringRes int resourceId) {
setHintDialogShown(activity);
hintSnackbar(activity, resourceId);
}
}
9 changes: 9 additions & 0 deletions catroid/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2096,6 +2096,15 @@ needs read and write access to it. You can always change permissions through you
To add a new actor or object, tap the \"+\" symbol.</string>
<!-- -->

<!-- Dialog Disable hints -->
<string name="dialog_disable_hints_title">Show hints?</string>
<string name="dialog_disable_hints_text">Hints provide you with helpful information
while making your first project.\n\nVisibility of hints can be changed in the
settings.</string>
<string name="dialog_disable_hints_button_hide">HIDE</string>
<string name="dialog_disable_hints_button_show">SHOW</string>
<!-- -->

<!-- Crash report dialog -->
<string name="plus" translatable="false">+</string>
<!-- -->
Expand Down