How to get the viewModel in my Widget class (which extends Broadcast receiver) use Hilt? - widget

I would like create an widget and sent args into. I have Widget class extends Broadcast receiver and I use Hilt. How I can get viewModel in my Widget class? I try use standard way but the viewModel is not found and highlighted by red.
My Widget class
#AndroidEntryPoint
class Widget: AppWidgetProvider() {
private val viewModel: HomeViewModel by viewModels()
...
my ViewModel
#HiltViewModel
class HomeViewModel #Inject constructor(
private val weatherRepo: WeatherRepo,
private val locationTracker: LocationTracker
) : ViewModel() {
...

Related

ActionScript 3.0 class extends

SCENARIO:
I have the class characterObject and n class that extends (cat, dog, bird, n...)
public class characterObject
{
public static var totalCounter:int;
}
public class cat extends characterObject
public class dog extends characterObject
public class bird extends characterObject
Is it possible for the extended classes to listen to changes to static variables (eg totalCounter) in the main class with or without events?
This is possible using a combination of getter/setter functions and events.
Getters and Setters, in actionscript, are functions that can be accessed like variables, giving you the ability to execute functions on variable changes.
public class characterObject
{
private static var _totalCounter:int;
protected static var internalDispatcher:EventDispatcher = new EventDispatcher();
public static function get totalCounter():int
{
return _totalCounter;
}
public static function set totalCounter(value:int):void
{
_totalCounter = value;
var event:Event = new Event("totalCounterChanged");
internalDispatcher.dispatchEvent(event);
}
}
As you can see we have a protected static event dispatcher called internalDispatcher, we can listen to events from this object only if we have extended the characterObject class. By hiding the true totalCounter behind the getter/setter we can dispatch an event whenever something changes it's value.
We can then listen for this event in our extended classes:
public class cat extends characterObject
{
public function cat()
{
super();
internalDispatcher.addEventListener("totalCounterChanged",totalChangedHandler);
}
public function totalChangedHandler(event:Event):void
{
//Your code here;
}
}
Depending on your requirements it maybe better to have your Event Handlers be static also to reduce the same code being executed multiple times without a change in output.

Mocking a class in PowerMock

I am using PowerMocking for JUNIT and Iam new to PowerMock.
I want to mock one class which is non static.
The class scenario goes as follows.
public class Export extends MyUtil implements ExportFormatting<DeptSummaryByDCDTO, LmReportsInputDTO>{
public String createPDF(List<DeptSummaryByDCDTO> summaryDtoList, LmReportsInputDTO inputDto){
}
public String createPDF(Map<String, DeptSummaryByDCDTO> paramMap,
LmReportsInputDTO paramK) {
}
}
The calling class is as follows.
public static Response getMultiplePackSku{
filePath = new Export(inputDto).createPDF(resultList,null);
}
The Question is,
I am trying to test the above class using powermock.
Can anybody tell how to mock the line filePath.....
You need to first mock the constructor and return an Export mock. On the returned mock you need to record the call to createPDF. The tricky part is the constructor mocking. I'll give you an example, hopefully you'll get all of it:
#RunWith(PowerMockRunner.class) // This annotation is for using PowerMock
#PrepareForTest(Export.class) // This annotation is for mocking the Export constructor
public class MyTests {
private mockExport;
#Before
public void setUp () {
// Create the mock
mockExport = PowerMock.createMock(Export.class)
}
#Test
public void testWithConstructor() {
SomeDtoClass inputDto = PowerMock.createMock(SomeDtoClass.class);
PowerMock.expectNew(Export.class, inputDto).andReturn(mockExport);
PowerMock.replay(mockExport, Export.class);
expect(mockExport.createPDF(resultList, null);
// Run the tested method.
}
}
Here is a description of how to mock a constructor call: MockConstructor

Is it ok to reference an abstract class in interface?

I am trying to write a little Entity-Component based game framework for myself. I just encountered an logic problem with my base class system.
The thing is I have an two things, Entities ( that can contain other entities and component ), and Components ( they are attached to certain entity ).
So I made two interfaces :
interface IEntity
interface IComponent
And I made a abstract classes for each
public class Component implements IComponent
public class Entity extends Sprite implements IEntity, IComponent
The problem is that in IEntity interface I have an function:
function addComponent( e:Entity )
The reason the argument type i Entity, is because then in Component I need to reference to the entity functions that it inherits from Sprite ( I cannot do that with IEntity type ).
But it seems that Flash Develop treats it as an error ( implementation of this function in Entity class ). Am I doing something wrong?
EDIT :
This are the interfaces:
public interface IComponent
{
function get parentGameObject() : IEntity;
function set parentGameObject( v:IEntity ) : void;
function init() : void;
function dispose() : void;
}
public interface IEntity
{
function addComponent( c:IComponent ) : IComponent;
function removeComponent( c:IComponent ) : Boolean;
function getComponent( type:Class ) : IComponent;
function hasComponentOfType( type:Class ) : Boolean;
function addGameObject( child:Entity ) : void;
}
Then my abstract Entity class implements both of this interfaces + extends from DisplayObjectContainer because each Entity needs the functionality of rendering itself and its child Entities.
The problem is that :
public function addGameObject( e:Entity ) : void {
m_components.push( v );
this.addChild( v );
v.gameObject = this;
v.init();
}
seems to be invalid, and the error is : interface method addGameObject in interface IEntity is implemented with incompatibile signature in class Entity.
And the reason I want to use e:Entity and not e:IEntity is because I am using this.addChild( v ), which belongs to DisplayObjectContainer.
Hope that clears my question.
I still can't see why this error is thrown, the implementation of addGameObject looks ok so far (I assume the usage of v is a problem that just exist in the example code?), though the parameter name differs from the interface definition where it's child instead of e, however AFAIK this is valid in AS3, nonetheless try using the name as defined in the interface.
And regarding the actual question, of course the answer depends. Generally you can reference whatever classes you like in an interface, the only problem here should be design patterns.
If you want to continue programming against interfaces, then you could simply create an game object interface that forces to implement the addChild method, something like this:
import flash.display.DisplayObject;
public interface IGameObject extends IComponent, IEntity
{
function addChild(child:DisplayObject):DisplayObject;
}
Change your IEntity interface, your addGameObject and Entity implementation accordingly and you should be good to go:
public interface IEntity
{
...
function addGameObject( child:IGameObject ) : void;
}
public function addGameObject( child:IGameObject ) : void {
...
}
public class Entity extends Sprite implements IGameObject
Though you might want to rename Entity to something like GameObject in order to avoid confusion.
This is how I solved this problem for now:
Three basic interfaces for each GameObject functionallity:
public interface IComponent
{
function get gameObject() : IGameObject;
function set gameObject( v:IGameObject ) : void;
function init() : void;
function dispose() : void;
}
public interface IDisplayObjectContainer
{
function get displayContainer() : DisplayObjectContainer;
}
public interface IEntity
{
function addComponent( c:IComponent ) : IComponent;
function removeComponent( c:IComponent ) : Boolean;
function getComponent( type:Class ) : IComponent;
function hasComponentOfType( type:Class ) : Boolean;
}
And my compound GameObject interface for now is extending all of this functionallity:
public interface IGameObject extends IEntity, IComponent, IDisplayObjectContainer
{
function addGameObject( g:IGameObject ) : void;
}

Actionscript 3.0 - Get variable from superclass to new object

How do I do this?
public class SuperClass extends MovieClip {
public static var test:String = 'Hello world';
}
public class SubClass extends SuperClass {
public function SubClass() {
var unit = new Unit();
addChild(Unit);
}
}
public class Unit extends MovieClip {
public function Unit() {
//Get variable:test from SuperClass??
trace(SubClass.test); //Error
}
}
Hope you understand what I want to do?
Want to get the variable 'test' from the SuperClass in my new Unit added in SubClass.
Thanks!
In this case your class named SubClass uses the Unit class through composition, not inheritance. As such the Unit class knows nothing about SubClass or SuperClass or any of their properties like the test property.
You can, however, pass a value into the Unit class through it's constructor (or just set a value directly onto a property of the Unit class):
(showing the last two classes only)
public class SubClass extends SuperClass {
public function SubClass() {
var unit = new Unit(test);
addChild(Unit);
}
}
public class Unit extends MovieClip {
private var test:String;
public function Unit(test:String) {
this.test = test;
}
}
[Edit]
Since your Unit class needs to access many properties of SubClass (and/or SuperClass), you might want to just pass an instance of SubClass to Unit:
public class SubClass extends SuperClass {
public function SubClass() {
var unit = new Unit(this);
addChild(Unit);
}
}
public class Unit extends MovieClip {
private var subClass:SubClass;
public function Unit(subClass:SubClass) {
this.subClass = subClass;
trace(subClass.text);
}
}
How you actually solve this depends on your use case. But there is no fundamental shortcut to do what you are asking.

JMockit mock protected method in superclass and still test method in real child class

I am still learning JMockit and need help understanding it.
I am testing a class that uses superclass methods. My test gets a null pointer when it attempts to use the superclass method due to code inside it that uses struts action context to get the session and pull an object from the session.
The method I want to bypass the struts session stuff inside the protected method.
public class MyExtendingClass extends MySuperClass{
public void methodIamTesting(){///}
}
public abstract class MySuperClass{
//I want to mock this method
protected Object myProtectedSuperClassMethod(){
// struts action context code that returns an object//}
}
Test code
#Test
public void testRunsAndDoesntPass() {
Mockit.setUpMock(MySuperClass.class, new MySuperClass(){
public Object myProtectedSuperClassMethod() {
return object;
}
});
// real class method invocation happens
assertEquals(expected, actual);
}
I keep getting NullPointers just like if I didn't have the mock
Not sure what to try next. All the docs and code samples I have read say to just declare the superclass method as public in the setUpMock and it should work.
I can't mock the entire class because that is the class I am testing.
I discovered that I needed to create the MockClass then reference it using setupmock correctly.
I am really falling in love with JMockit.
#MockClass(realClass = MyExtendingClass.class)
public static class MockSuperClass {
final Object object = new Object();
#Mock
public Object myProtectedSuperClassMethod() {
return object;
}}
#Test
public void testRunsAndNowWillPass() {
Mockit.setUpMock(MySuperClass.class, new MockSuperClass(){
public Object myProtectedSuperClassMethod() {
return object;
}});
// real class method invocation happens where i set expected and actual
assertEquals(expected, actual);
}
you mask the parent class implementation out totally #Mocked final MySuperClass base
abstract class MySuperClass{
protected Object myProtectedSuperClassMethod(){
}
class MyExtendingClass extends MySuperClass{
public void methodIamTesting(){///}
}
#Test
public void testRunsAndDoesntPass(#Mocked final MySuperClass base ) {
//you could mask out all the base class implementation like this
new Expectations(){{
invoke(base, "myProtectedSuperClassMethod");
}};
// real class method invocation happens
// ...
assertEquals(expected, actual);
}