Skip to content

Commit

Permalink
Include simple Unit Test for all classes in the color picker module.
Browse files Browse the repository at this point in the history
  • Loading branch information
slaviboy committed Aug 14, 2020
1 parent d6aa319 commit 7eb6f66
Show file tree
Hide file tree
Showing 67 changed files with 3,830 additions and 1,710 deletions.
2 changes: 1 addition & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/dictionaries/Slaviboy.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 18 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
buildToolsVersion '29.0.3'

defaultConfig {
applicationId "com.slaviboy.colorpickerkotlinexample"
Expand All @@ -22,19 +22,31 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

testOptions {
unitTests.includeAndroidResources = true
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.2"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.3.2"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.3.2"
// unit test
androidTestImplementation 'com.google.truth:truth:1.0.1'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'org.mockito:mockito-core:3.4.6'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'


testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.4.6'

implementation project(":colorpicker")
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package com.slaviboy.colorpickerkotlinexample

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.AndroidJUnit4

import androidx.test.rule.ActivityTestRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http:https://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

@get:Rule
val activityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.slaviboy.colorpickerkotlinexample", appContext.packageName)
fun MainTest() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
}
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http:https://schemas.android.com/apk/res/android"
package="com.slaviboy.colorpickerkotlinexample">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
116 changes: 102 additions & 14 deletions app/src/main/java/com/slaviboy/colorpickerkotlinexample/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
package com.slaviboy.colorpickerkotlinexample

import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.app.Activity
import android.content.ContentValues
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.Bitmap.CompressFormat
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.slaviboy.colorpicker.Updater
import com.slaviboy.colorpicker.Updater.OnUpdateListener
import com.slaviboy.colorpicker.converter.ColorConverter
import com.slaviboy.colorpicker.window.Base
import com.slaviboy.colorpicker.components.Base
import com.slaviboy.colorpicker.main.ColorConverter
import com.slaviboy.colorpicker.main.Updater
import com.slaviboy.colorpicker.main.Updater.OnUpdateListener
import com.slaviboy.colorpicker.module.circular.CircularHS
import com.slaviboy.colorpickerkotlinexample.pickers.CircularHSV
import com.slaviboy.colorpickerkotlinexample.pickers.RectangularHSL
import com.slaviboy.colorpickerkotlinexample.pickers.RectangularHSV
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.sql.Timestamp
import java.util.*


class MainActivity : AppCompatActivity() {

// get color picker views
lateinit var rectangularHSV: RectangularHSV
lateinit var rectangularHSL: RectangularHSL
lateinit var circularHSV: CircularHSV

lateinit var textViewChangeType: EditText
lateinit var labelViewChangeType: TextView
lateinit var updater: Updater
Expand All @@ -29,15 +50,20 @@ class MainActivity : AppCompatActivity() {
initColorPickers()
}

/**
* Initialize the color picker by getting reference to them and connecting(attaching)
* them to a updater that controls the update from onw color window or text view to
* all the other view connected to the same updater.
*/
private fun initColorPickers() {

// get color picker views
val rectangularHSV: RectangularHSV = findViewById(R.id.picker1)
val rectangularHSL: RectangularHSL = findViewById(R.id.picker2)
val circularHSV: CircularHSV = findViewById(R.id.picker3)
rectangularHSV = findViewById(R.id.rectangular_hsv)
rectangularHSL = findViewById(R.id.rectangular_hsl)
circularHSV = findViewById(R.id.circular_hsv)

// create color convert, that will convert from one color model to another
colorConverter = ColorConverter("#1f538cb5") //ColorConverter(160, 73, 184, 50)
// create color convert, that will convert colors from one color model to another
colorConverter = ColorConverter("#1f538cb5")

// create updater object, that will update all color window and text views
updater = Updater(colorConverter)
Expand All @@ -47,8 +73,8 @@ class MainActivity : AppCompatActivity() {
rectangularHSL.attach(updater)
circularHSV.attach(updater)

textViewChangeType = circularHSV.findViewById(R.id.rgba_value)
labelViewChangeType = circularHSV.findViewById(R.id.rgba_label)
textViewChangeType = circularHSV.findViewById(R.id.multiple_value)
labelViewChangeType = circularHSV.findViewById(R.id.multiple_label)

// attach listener to the updater
updater.setOnUpdateListener(object : OnUpdateListener {
Expand Down Expand Up @@ -100,6 +126,7 @@ class MainActivity : AppCompatActivity() {
Log.i("color-picker", "$r, $g, $b")
}


/**
* Enables regular immersive mode.
* For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
Expand All @@ -109,11 +136,10 @@ class MainActivity : AppCompatActivity() {

val decorView = activity.window.decorView
decorView.systemUiVisibility =
(View.SYSTEM_UI_FLAG_IMMERSIVE // Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
(View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // Hide the nav bar and status bar
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN)
}
Expand All @@ -129,4 +155,66 @@ class MainActivity : AppCompatActivity() {
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
}

/**
* Request the permission from the user to allow witting in the external storage
* of the device.
*/
fun requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
val permissions = arrayOf(WRITE_EXTERNAL_STORAGE)
val permissionRequestCode = 1
requestPermissions(permissions, permissionRequestCode)
}
}
}

/**
* Save the bitmap to local storage on the phone with given folder location and file name
* @param bitmap bitmap that will be save to the local storage of the phone
* @param folderName name of the folder where the file will be located
* @param fileName name of the file that will be in PNG file format
* @param fileFormat file format of the image 0=png, 1=jpeg
* @param imageQuality quality of the image only for Jpeg
*/
fun saveBitmapToLocalStorage(bitmap: Bitmap, folderName: String = "Screenshots", fileName: String = "test", fileFormat: Int = 0, imageQuality: Int = 100) {

val fileFormatString = arrayListOf("png", "jpg")

try {
val imageFile: File
val dir = File(Environment.getExternalStorageDirectory(), folderName)
var success = true
if (!dir.exists()) {
success = dir.mkdirs()
}
if (success) {
val date = Date()
imageFile = File(dir.absolutePath + File.separator + Timestamp(date.time).toString() + "$fileName.${fileFormatString[fileFormat]}")
imageFile.createNewFile()
} else {
return
}

val byteArrayOutputStream = ByteArrayOutputStream()
if (fileFormat == 0) {
bitmap.compress(CompressFormat.PNG, imageQuality, byteArrayOutputStream)
} else {
bitmap.compress(CompressFormat.JPEG, imageQuality, byteArrayOutputStream)
}

val fileOutputStream = FileOutputStream(imageFile)
fileOutputStream.write(byteArrayOutputStream.toByteArray())
fileOutputStream.close()

val values = ContentValues()
values.put(MediaStore.Images.Media.MIME_TYPE, "image/${fileFormatString[fileFormat]}")
values.put(MediaStore.MediaColumns.DATA, imageFile.absolutePath)

} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package com.slaviboy.colorpickerkotlinexample.pickers

import android.content.Context
import android.util.AttributeSet
import com.slaviboy.colorpicker.pickers.ColorPicker
import com.slaviboy.colorpicker.main.ColorPicker
import com.slaviboy.colorpickerkotlinexample.R

class CircularHSV : ColorPicker {

constructor(context: Context) : super(context) {
init(context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.slaviboy.colorpickerkotlinexample.pickers

import android.content.Context
import android.util.AttributeSet
import com.slaviboy.colorpicker.pickers.ColorPicker
import com.slaviboy.colorpicker.main.ColorPicker
import com.slaviboy.colorpickerkotlinexample.R

class RectangularHSL : ColorPicker {
Expand All @@ -15,11 +15,7 @@ class RectangularHSL : ColorPicker {
init(context)
}

constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init(context)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package com.slaviboy.colorpickerkotlinexample.pickers

import android.content.Context
import android.util.AttributeSet
import com.slaviboy.colorpicker.pickers.ColorPicker
import com.slaviboy.colorpicker.main.ColorPicker
import com.slaviboy.colorpickerkotlinexample.R


class RectangularHSV : ColorPicker {

constructor(context: Context) : super(context) {
init(context)
}
Expand Down
Loading

0 comments on commit 7eb6f66

Please sign in to comment.